diff options
author | Jiri Vanek <[email protected]> | 2013-05-02 16:00:17 +0200 |
---|---|---|
committer | Jiri Vanek <[email protected]> | 2013-05-02 16:00:17 +0200 |
commit | c01de86b76ad014e30534ba879478c579e748fa9 (patch) | |
tree | eb83e7fa79f2059ef740ad9243989ad3c1bb5f87 /netx/net/sourceforge/jnlp | |
parent | 5c3d81c1620b814a3883909f4a37d71e9742a602 (diff) |
Fix for portalbank.no (trying get after failed head requests) and tests
Diffstat (limited to 'netx/net/sourceforge/jnlp')
-rw-r--r-- | netx/net/sourceforge/jnlp/Version.java | 38 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/cache/Resource.java | 2 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/cache/ResourceTracker.java | 69 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/util/HttpUtils.java | 76 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/util/StreamUtils.java | 13 |
5 files changed, 113 insertions, 85 deletions
diff --git a/netx/net/sourceforge/jnlp/Version.java b/netx/net/sourceforge/jnlp/Version.java index 99e7cd6..a1732ab 100644 --- a/netx/net/sourceforge/jnlp/Version.java +++ b/netx/net/sourceforge/jnlp/Version.java @@ -308,42 +308,6 @@ public class Version { public String toString() { return versionString; - } - - /** - * Test. - */ - /* - public static void main(String args[]) { - Version jvms[] = { - new Version("1.1* 1.3*"), - new Version("1.2+"), - }; - - Version versions[] = { - new Version("1.1"), - new Version("1.1.8"), - new Version("1.2"), - new Version("1.3"), - new Version("2.0"), - new Version("1.3.1"), - new Version("1.2.1"), - new Version("1.3.1-beta"), - new Version("1.1 1.2"), - new Version("1.2 1.3"), - }; - - for (int j = 0; j < jvms.length; j++) { - for (int v = 0; v < versions.length; v++) { - System.out.print( jvms[j].toString() + " " ); - if (!jvms[j].matches(versions[v])) - System.out.print( "!" ); - System.out.println( "matches " + versions[v].toString() ); - } - } - - System.out.println("Test completed"); - } - */ + } } diff --git a/netx/net/sourceforge/jnlp/cache/Resource.java b/netx/net/sourceforge/jnlp/cache/Resource.java index 00d411b..c4c6bbf 100644 --- a/netx/net/sourceforge/jnlp/cache/Resource.java +++ b/netx/net/sourceforge/jnlp/cache/Resource.java @@ -109,6 +109,8 @@ public class Resource { synchronized (resources) { Resource resource = new Resource(location, requestVersion, updatePolicy); + //FIXME - url ignores port during its comparison + //this may affect test-suites int index = resources.indexOf(resource); if (index >= 0) { // return existing object Resource result = resources.get(index); diff --git a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java index 7b6a772..e8f4235 100644 --- a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java +++ b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java @@ -24,14 +24,10 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; -import java.net.URLDecoder; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; @@ -49,7 +45,7 @@ import net.sourceforge.jnlp.Version; import net.sourceforge.jnlp.event.DownloadEvent; import net.sourceforge.jnlp.event.DownloadListener; import net.sourceforge.jnlp.runtime.JNLPRuntime; -import net.sourceforge.jnlp.util.StreamUtils; +import net.sourceforge.jnlp.util.HttpUtils; import net.sourceforge.jnlp.util.UrlUtils; import net.sourceforge.jnlp.util.WeakList; @@ -118,6 +114,9 @@ public class ResourceTracker { /** max threads */ private static final int maxThreads = 5; + + /** methods used to try individual URLs when choosing best*/ + private static final String[] requestMethods = {"HEAD", "GET"}; /** running threads */ private static int threads = 0; @@ -859,7 +858,7 @@ public class ResourceTracker { * @return the response code if HTTP connection, or HttpURLConnection.HTTP_OK if not. * @throws IOException */ - private static int getUrlResponseCode(URL url, Map<String, String> requestProperties, String requestMethod) throws IOException { + static int getUrlResponseCode(URL url, Map<String, String> requestProperties, String requestMethod) throws IOException { URLConnection connection = url.openConnection(); for (Map.Entry<String, String> property : requestProperties.entrySet()){ @@ -874,7 +873,7 @@ public class ResourceTracker { /* Fully consuming current request helps with connection re-use * See http://docs.oracle.com/javase/1.5.0/docs/guide/net/http-keepalive.html */ - StreamUtils.consumeAndCloseInputStream(httpConnection.getInputStream()); + HttpUtils.consumeAndCloseConnectionSilently(httpConnection); return responseCode; } @@ -891,7 +890,7 @@ public class ResourceTracker { * @param resource the resource * @return the best URL, or null if all failed to resolve */ - private URL findBestUrl(Resource resource) { + URL findBestUrl(Resource resource) { DownloadOptions options = downloadOptions.get(resource); if (options == null) { options = new DownloadOptions(false, false); @@ -903,33 +902,33 @@ public class ResourceTracker { resource.toString() + " : " + urls); } - for (URL url : urls) { - try { - Map<String, String> requestProperties = new HashMap<String, String>(); - requestProperties.put("Accept-Encoding", "pack200-gzip, gzip"); - - int responseCode = getUrlResponseCode(url, requestProperties, "HEAD"); - - if (responseCode == HttpURLConnection.HTTP_NOT_IMPLEMENTED ) { - System.err.println("NOTE: The server does not appear to support HEAD requests, falling back to GET requests."); - /* Fallback: use GET request in the rare case the server does not support HEAD requests */ - responseCode = getUrlResponseCode(url, requestProperties, "GET"); - } - - /* Check if within valid response code range */ - if (responseCode >= 200 && responseCode < 300) { - if (JNLPRuntime.isDebug()) { - System.err.println("best url for " + resource.toString() + " is " + url.toString()); - } - return url; /* This is the best URL */ - } - } catch (IOException e) { - // continue to next candidate - if (JNLPRuntime.isDebug()) { - System.err.println("While processing " + url.toString() + " for resource " + resource.toString() + " got " + e); - } - } - } + for (String requestMethod : requestMethods) { + for (URL url : urls) { + try { + Map<String, String> requestProperties = new HashMap<String, String>(); + requestProperties.put("Accept-Encoding", "pack200-gzip, gzip"); + + int responseCode = getUrlResponseCode(url, requestProperties, requestMethod); + + if (responseCode < 200 || responseCode >= 300) { + if (JNLPRuntime.isDebug()) { + System.err.println("For "+resource.toString()+" the server returned " + responseCode + " code for "+requestMethod+" request for " + url.toExternalForm()); + } + }else { + if (JNLPRuntime.isDebug()) { + System.out.println("best url for " + resource.toString() + " is " + url.toString() + " by " + requestMethod); + } + return url; /* This is the best URL */ + } + } catch (IOException e) { + // continue to next candidate + if (JNLPRuntime.isDebug()) { + System.err.println("While processing " + url.toString() + " by " + requestMethod + " for resource " + resource.toString() + " got " + e + ": "); + e.printStackTrace(); + } + } + } + } /* No valid URL, return null */ return null; diff --git a/netx/net/sourceforge/jnlp/util/HttpUtils.java b/netx/net/sourceforge/jnlp/util/HttpUtils.java new file mode 100644 index 0000000..5bbfa69 --- /dev/null +++ b/netx/net/sourceforge/jnlp/util/HttpUtils.java @@ -0,0 +1,76 @@ +/* + Copyright (C) 2011 Red Hat, Inc. + + This file is part of IcedTea. + + IcedTea is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 2. + + IcedTea is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with IcedTea; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. + */ +package net.sourceforge.jnlp.util; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +public class HttpUtils { + + /** + * Ensure a HttpURLConnection is fully read, required for correct behavior. + * Captured IOException is consumed and printed + */ + public static void consumeAndCloseConnectionSilently(HttpURLConnection c) { + try { + consumeAndCloseConnection(c); + } catch (IOException ex) { + ex.printStackTrace(System.err); + } + } + + /** + * Ensure a HttpURLConnection is fully read, required for correct behavior + * + * @throws IOException + */ + public static void consumeAndCloseConnection(HttpURLConnection c) throws IOException { + InputStream in = null; + try { + in = c.getInputStream(); + byte[] throwAwayBuffer = new byte[256]; + while (in.read(throwAwayBuffer) > 0) { + /* ignore contents */ + } + } finally { + if (in != null) { + in.close(); + } + } + } +} diff --git a/netx/net/sourceforge/jnlp/util/StreamUtils.java b/netx/net/sourceforge/jnlp/util/StreamUtils.java index 6692d1c..c498b7b 100644 --- a/netx/net/sourceforge/jnlp/util/StreamUtils.java +++ b/netx/net/sourceforge/jnlp/util/StreamUtils.java @@ -45,19 +45,6 @@ import java.io.InputStreamReader; public class StreamUtils { - /** - * Ensure a stream is fully read, required for correct behaviour in some - * APIs, namely HttpURLConnection. - * @throws IOException - */ - public static void consumeAndCloseInputStream(InputStream in) throws IOException { - byte[] throwAwayBuffer = new byte[256]; - while (in.read(throwAwayBuffer) > 0) { - /* ignore contents */ - } - in.close(); - } - /*** * Closes a stream, without throwing IOException. * In case of IOException, prints the stack trace to System.err. |