Ticket #11548: HttpURL.2.patch

File HttpURL.2.patch, 3.7 KB (added by akks, 9 years ago)
  • src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java

     
    318318                return true;
    319319            }
    320320
    321             HttpURLConnection urlConn = getURLConnection();
     321            URLConnection urlConn = getURLConnection();
    322322
    323323            if (isObjectLoadable()  &&
    324324                    (now - attributes.getLastModification()) <= ABSOLUTE_EXPIRE_TIME_LIMIT) {
     
    327327            if (isObjectLoadable() && attributes.getEtag() != null) {
    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"
    333333                log.log(Level.FINE, "JCS - IfModifiedSince/Etag test: local version is up to date: {0}", getUrl());
     
    348348            attributes = parseHeaders(urlConn);
    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
    362362                    cacheData = createCacheEntry(raw);
     
    447447        return ret;
    448448    }
    449449
    450     private HttpURLConnection getURLConnection() throws IOException {
    451         HttpURLConnection urlConn = (HttpURLConnection) getUrl().openConnection();
     450    private URLConnection getURLConnection() throws IOException {
     451        URLConnection urlConn = getUrl().openConnection();
    452452        urlConn.setRequestProperty("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
    453453        urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout
    454454        urlConn.setConnectTimeout(connectTimeout);
     
    462462    }
    463463
    464464    private boolean isCacheValidUsingHead() throws IOException {
    465         HttpURLConnection urlConn = (HttpURLConnection) getUrl().openConnection();
    466         urlConn.setRequestMethod("HEAD");
     465        URLConnection urlConn = getUrl().openConnection();
     466        if (urlConn instanceof HttpURLConnection) {
     467            ((HttpURLConnection)urlConn).setRequestMethod("HEAD");
     468        }
    467469        long lastModified = urlConn.getLastModified();
    468470        return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||
    469471               (lastModified != 0 && lastModified <= attributes.getLastModification());
     
    521523    public void handleJobCancellation() {
    522524        finishLoading(LoadResult.CANCELED);
    523525    }
     526
     527    private int responseCode(URLConnection urlConn) throws IOException {
     528        if (urlConn instanceof HttpURLConnection) {
     529            return ((HttpURLConnection) urlConn).getResponseCode();
     530        } else {
     531            return 500;
     532        }
     533    }
    524534}