Changeset 8485 in josm


Ignore:
Timestamp:
2015-06-11T00:31:44+02:00 (9 years ago)
Author:
wiktorn
Message:

Addresses: #11548 - do not assume, that we work with HTTP based connection for fetching from remote resources

File:
1 edited

Legend:

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

    r8462 r8485  
    9090                t.setName(name);
    9191                return t;
    92                 }
     92            }
    9393        };
    9494    }
     
    319319            }
    320320
    321             HttpURLConnection urlConn = getURLConnection();
     321            URLConnection urlConn = getURLConnection();
    322322
    323323            if (isObjectLoadable()  &&
     
    328328                urlConn.addRequestProperty("If-None-Match", attributes.getEtag());
    329329            }
    330             if (urlConn.getResponseCode() == 304) {
     330            if (responseCode(urlConn) == 304) {
    331331                // If isModifiedSince or If-None-Match has been set
    332332                // and the server answers with a HTTP 304 = "Not Modified"
     
    349349
    350350            for (int i = 0; i < 5; ++i) {
    351                 if (urlConn.getResponseCode() == 503) {
     351                if (responseCode(urlConn) == 503) {
    352352                    Thread.sleep(5000+(new Random()).nextInt(5000));
    353353                    continue;
    354354                }
    355355
    356                 attributes.setResponseCode(urlConn.getResponseCode());
     356                attributes.setResponseCode(responseCode(urlConn));
    357357                byte[] raw = read(urlConn);
    358358
    359                 if (isResponseLoadable(urlConn.getHeaderFields(), urlConn.getResponseCode(), raw)) {
     359                if (isResponseLoadable(urlConn.getHeaderFields(), responseCode(urlConn), raw)) {
    360360                    // we need to check cacheEmpty, so for cases, when data is returned, but we want to store
    361361                    // as empty (eg. empty tile images) to save some space
     
    386386        } catch (IOException e) {
    387387            log.log(Level.FINE, "JCS - IOExecption during communication with server for: {0}", getUrl());
     388
    388389            attributes.setResponseCode(499); // set dummy error code
    389390            boolean doCache = isResponseLoadable(null, 499, null) || cacheAsEmpty(); //generic 499 error code returned
     
    448449    }
    449450
    450     private HttpURLConnection getURLConnection() throws IOException {
    451         HttpURLConnection urlConn = (HttpURLConnection) getUrl().openConnection();
     451    private URLConnection getURLConnection() throws IOException {
     452        URLConnection urlConn = getUrl().openConnection();
    452453        urlConn.setRequestProperty("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
    453454        urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout
     
    463464
    464465    private boolean isCacheValidUsingHead() throws IOException {
    465         HttpURLConnection urlConn = (HttpURLConnection) getUrl().openConnection();
    466         urlConn.setRequestMethod("HEAD");
    467         long lastModified = urlConn.getLastModified();
    468         return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||
    469                (lastModified != 0 && lastModified <= attributes.getLastModification());
     466        URLConnection urlConn = getUrl().openConnection();
     467        if(urlConn instanceof HttpURLConnection) {
     468            ((HttpURLConnection)urlConn).setRequestMethod("HEAD");
     469            long lastModified = urlConn.getLastModified();
     470            return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||
     471                    (lastModified != 0 && lastModified <= attributes.getLastModification());
     472        }
     473        // for other URL connections, do not use HEAD requests for cache validation
     474        return false;
    470475    }
    471476
     
    522527        finishLoading(LoadResult.CANCELED);
    523528    }
     529
     530    /*
     531     * Temporary fix for file URLs. Returns response code for HttpURLConnections or 200 for all other
     532     */
     533    private int responseCode(URLConnection urlConn) throws IOException {
     534        if (urlConn instanceof HttpURLConnection) {
     535            return ((HttpURLConnection) urlConn).getResponseCode();
     536        } else {
     537            return 200;
     538        }
     539    }
    524540}
Note: See TracChangeset for help on using the changeset viewer.