Changeset 9232 in josm


Ignore:
Timestamp:
2016-01-01T12:07:51+01:00 (8 years ago)
Author:
simon04
Message:

fix #12265 - Use HttpClient for imagery requests

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java

    r9228 r9232  
    44import java.io.FileNotFoundException;
    55import java.io.IOException;
    6 import java.net.HttpURLConnection;
    76import java.net.URL;
    8 import java.net.URLConnection;
    97import java.util.HashSet;
    108import java.util.List;
    119import java.util.Map;
    12 import java.util.Map.Entry;
    1310import java.util.Random;
    1411import java.util.Set;
     
    2724import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult;
    2825import org.openstreetmap.josm.data.preferences.IntegerProperty;
     26import org.openstreetmap.josm.tools.HttpClient;
    2927import org.openstreetmap.josm.tools.Utils;
    3028
     
    311309            }
    312310
    313             HttpURLConnection urlConn = getURLConnection(getUrl(), true);
     311            final HttpClient request = getRequest("GET", true);
    314312
    315313            if (isObjectLoadable()  &&
    316314                    (now - attributes.getLastModification()) <= ABSOLUTE_EXPIRE_TIME_LIMIT) {
    317                 urlConn.setIfModifiedSince(attributes.getLastModification());
     315                request.setIfModifiedSince(attributes.getLastModification());
    318316            }
    319317            if (isObjectLoadable() && attributes.getEtag() != null) {
    320                 urlConn.addRequestProperty("If-None-Match", attributes.getEtag());
    321             }
    322 
    323             log.log(Level.INFO, "GET {0} -> {1}", new Object[]{getUrl(), urlConn.getResponseCode()});
    324 
    325             // follow redirects
    326             for (int i = 0; i < 5; i++) {
    327                 if (urlConn.getResponseCode() == 302) {
    328                     urlConn = getURLConnection(new URL(urlConn.getHeaderField("Location")), true);
    329                 } else {
    330                     break;
    331                 }
    332             }
     318                request.setHeader("If-None-Match", attributes.getEtag());
     319            }
     320
     321            final HttpClient.Response urlConn = request.connect();
     322
    333323            if (urlConn.getResponseCode() == 304) {
    334324                // If isModifiedSince or If-None-Match has been set
    335325                // and the server answers with a HTTP 304 = "Not Modified"
    336                 log.log(Level.FINE, "JCS - IfModifiedSince/Etag test: local version is up to date: {0}", getUrl());
     326                log.log(Level.FINE, "JCS - If-Modified-Since/ETag test: local version is up to date: {0}", getUrl());
    337327                return true;
    338             } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 resposne code
     328            } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 response code
    339329                    && (
    340                             (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||
     330                            (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) ||
    341331                            attributes.getLastModification() == urlConn.getLastModified())
    342332                    ) {
     
    344334                // for further requests - use HEAD
    345335                String serverKey = getServerKey();
    346                 log.log(Level.INFO, "JCS - Host: {0} found not to return 304 codes for If-Modifed-Since or If-None-Match headers",
     336                log.log(Level.INFO, "JCS - Host: {0} found not to return 304 codes for If-Modified-Since or If-None-Match headers",
    347337                        serverKey);
    348338                useHead.put(serverKey, Boolean.TRUE);
     
    361351                byte[] raw;
    362352                if (urlConn.getResponseCode() == 200) {
    363                     raw = Utils.readBytesFromStream(urlConn.getInputStream());
     353                    raw = Utils.readBytesFromStream(urlConn.getContent());
    364354                } else {
    365355                    raw = new byte[]{};
     
    434424    protected abstract V createCacheEntry(byte[] content);
    435425
    436     protected CacheEntryAttributes parseHeaders(URLConnection urlConn) {
     426    protected CacheEntryAttributes parseHeaders(HttpClient.Response urlConn) {
    437427        CacheEntryAttributes ret = new CacheEntryAttributes();
    438428
     
    461451        ret.setEtag(urlConn.getHeaderField("ETag"));
    462452
    463         if (Main.isDebugEnabled()) {
    464             for (Entry<String, List<String>> header: urlConn.getHeaderFields().entrySet()) {
    465                 log.log(Level.FINE, "Response header - {0}: {1}", new Object[]{header.getKey(), header.getValue()});
    466             }
    467         }
    468 
    469453        return ret;
    470454    }
    471455
    472     private HttpURLConnection getURLConnection(URL url, boolean noCache) throws IOException {
    473         HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    474         urlConn.setRequestProperty("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
     456    private HttpClient getRequest(String requestMethod, boolean noCache) throws IOException {
     457        final HttpClient urlConn = HttpClient.create(getUrl(), requestMethod);
     458        urlConn.setAccept("text/html, image/png, image/jpeg, image/gif, */*");
    475459        urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout
    476460        urlConn.setConnectTimeout(connectTimeout);
    477461        if (headers != null) {
    478             for (Map.Entry<String, String> e: headers.entrySet()) {
    479                 urlConn.setRequestProperty(e.getKey(), e.getValue());
    480             }
     462            urlConn.setHeaders(headers);
    481463        }
    482464
    483465        if (force || noCache) {
    484             urlConn.setUseCaches(false);
     466            urlConn.useCache(false);
    485467        }
    486468        return urlConn;
     
    488470
    489471    private boolean isCacheValidUsingHead() throws IOException {
    490         HttpURLConnection urlConn = getURLConnection(getUrl(), false);
    491         urlConn.setRequestMethod("HEAD");
    492         for (int i = 0; i < 5; i++) {
    493             if (urlConn.getResponseCode() == 302) {
    494                 urlConn = getURLConnection(new URL(urlConn.getHeaderField("Location")), false);
    495             } else {
    496                 break;
    497             }
    498         }
     472        final HttpClient.Response urlConn = getRequest("HEAD", false).connect();
    499473        long lastModified = urlConn.getLastModified();
    500         return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||
     474        return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) ||
    501475                (lastModified != 0 && lastModified <= attributes.getLastModification());
    502476    }
  • trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJob.java

    r9067 r9232  
    77import java.io.IOException;
    88import java.net.URL;
    9 import java.net.URLConnection;
    109import java.util.HashSet;
    1110import java.util.List;
     
    3130import org.openstreetmap.josm.data.cache.ICachedLoaderListener;
    3231import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob;
     32import org.openstreetmap.josm.tools.HttpClient;
    3333
    3434/**
     
    240240
    241241    @Override
    242     protected CacheEntryAttributes parseHeaders(URLConnection urlConn) {
     242    protected CacheEntryAttributes parseHeaders(HttpClient.Response urlConn) {
    243243        CacheEntryAttributes ret = super.parseHeaders(urlConn);
    244244        // keep the expiration time between MINIMUM_EXPIRES and MAXIMUM_EXPIRES, so we will cache the tiles
  • trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java

    r9227 r9232  
    200200
    201201    protected SessionId extractOsmSession() {
    202         List<String> setCookies = connection.getHeaderFields("Set-Cookie");
     202        List<String> setCookies = connection.getHeaderFields().get("Set-Cookie");
    203203        if (setCookies == null)
    204204            // no cookies set
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r9230 r9232  
    310310         * Returns the {@code Content-Encoding} header.
    311311         * @return {@code Content-Encoding} HTTP header
     312         * @see HttpURLConnection#getContentEncoding()
    312313         */
    313314        public String getContentEncoding() {
     
    324325
    325326        /**
     327         * Returns the {@code Expire} header.
     328         * @return {@code Expire} HTTP header
     329         * @see HttpURLConnection#getExpiration()
     330         * @since
     331         */
     332        public long getExpiration() {
     333            return connection.getExpiration();
     334        }
     335
     336        /**
     337         * Returns the {@code Last-Modified} header.
     338         * @return {@code Last-Modified} HTTP header
     339         * @see HttpURLConnection#getLastModified()
     340         * @since 9232
     341         */
     342        public long getLastModified() {
     343            return connection.getLastModified();
     344        }
     345
     346        /**
    326347         * Returns the {@code Content-Length} header.
    327348         * @return {@code Content-Length} HTTP header
     349         * @see HttpURLConnection#getContentLengthLong()
    328350         */
    329351        public long getContentLength() {
     
    343365
    344366        /**
    345          * Returns the list of Strings that represents the named header field values.
    346          * @param name the name of a header field
    347          * @return unmodifiable List of Strings that represents the corresponding field values
     367         * Returns an unmodifiable Map mapping header keys to a List of header values.
     368         * @return unmodifiable Map mapping header keys to a List of header values
    348369         * @see HttpURLConnection#getHeaderFields()
    349          * @since 9172
    350          */
    351         public List<String> getHeaderFields(String name) {
    352             return connection.getHeaderFields().get(name);
     370         * @since 9232
     371         */
     372        public Map<String, List<String>> getHeaderFields() {
     373            return connection.getHeaderFields();
    353374        }
    354375
Note: See TracChangeset for help on using the changeset viewer.