Ignore:
Timestamp:
2015-12-26T23:41:52+01:00 (4 years ago)
Author:
simon04
Message:

see #12231 - Uniform access to HTTP resources

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r9078 r9168  
    2121import java.util.List;
    2222import java.util.Map;
    23 import java.util.Map.Entry;
    2423import java.util.concurrent.ConcurrentHashMap;
    2524import java.util.zip.ZipEntry;
     
    2726
    2827import org.openstreetmap.josm.Main;
    29 import org.openstreetmap.josm.tools.CheckParameterUtil;
     28import org.openstreetmap.josm.tools.HttpClient;
    3029import org.openstreetmap.josm.tools.Pair;
    3130import org.openstreetmap.josm.tools.Utils;
     
    415414        destDirFile = new File(destDir, localPath + ".tmp");
    416415        try {
    417             HttpURLConnection con = connectFollowingRedirect(url, httpAccept, ifModifiedSince, httpHeaders);
     416            final HttpClient.Response con = HttpClient.create(url)
     417                    .setAccept(httpAccept)
     418                    .setIfModifiedSince(ifModifiedSince == null ? 0L : ifModifiedSince)
     419                    .setHeaders(httpHeaders)
     420                    .connect();
    418421            if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
    419422                if (Main.isDebugEnabled()) {
     
    427430            }
    428431            try (
    429                 InputStream bis = new BufferedInputStream(con.getInputStream());
     432                InputStream bis = new BufferedInputStream(con.getContent());
    430433                OutputStream fos = new FileOutputStream(destDirFile);
    431434                OutputStream bos = new BufferedOutputStream(fos)
     
    462465    }
    463466
    464     /**
    465      * Opens a connection for downloading a resource.
    466      * <p>
    467      * Manually follows redirects because
    468      * {@link HttpURLConnection#setFollowRedirects(boolean)} fails if the redirect
    469      * is going from a http to a https URL, see <a href="https://bugs.openjdk.java.net/browse/JDK-4620571">bug report</a>.
    470      * <p>
    471      * This can cause problems when downloading from certain GitHub URLs.
    472      *
    473      * @param downloadUrl The resource URL to download
    474      * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Can be {@code null}
    475      * @param ifModifiedSince The download time of the cache file, optional
    476      * @return The HTTP connection effectively linked to the resource, after all potential redirections
    477      * @throws MalformedURLException If a redirected URL is wrong
    478      * @throws IOException If any I/O operation goes wrong
    479      * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol
    480      * @since 6867
    481      */
    482     public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince)
    483             throws MalformedURLException, IOException {
    484         return connectFollowingRedirect(downloadUrl, httpAccept, ifModifiedSince, null);
    485     }
    486 
    487     /**
    488      * Opens a connection for downloading a resource.
    489      * <p>
    490      * Manually follows redirects because
    491      * {@link HttpURLConnection#setFollowRedirects(boolean)} fails if the redirect
    492      * is going from a http to a https URL, see <a href="https://bugs.openjdk.java.net/browse/JDK-4620571">bug report</a>.
    493      * <p>
    494      * This can cause problems when downloading from certain GitHub URLs.
    495      *
    496      * @param downloadUrl The resource URL to download
    497      * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Can be {@code null}
    498      * @param ifModifiedSince The download time of the cache file, optional
    499      * @param headers http headers to be sent together with http request
    500      * @return The HTTP connection effectively linked to the resource, after all potential redirections
    501      * @throws MalformedURLException If a redirected URL is wrong
    502      * @throws IOException If any I/O operation goes wrong
    503      * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol
    504      * @since TODO
    505      */
    506     public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince,
    507             Map<String, String> headers) throws MalformedURLException, IOException {
    508         CheckParameterUtil.ensureParameterNotNull(downloadUrl, "downloadUrl");
    509         String downloadString = downloadUrl.toExternalForm();
    510 
    511         checkOfflineAccess(downloadString);
    512 
    513         int numRedirects = 0;
    514         while (true) {
    515             HttpURLConnection con = Utils.openHttpConnection(downloadUrl);
    516             if (ifModifiedSince != null) {
    517                 con.setIfModifiedSince(ifModifiedSince);
    518             }
    519             if (headers != null) {
    520                 for (Entry<String, String> header: headers.entrySet()) {
    521                     con.setRequestProperty(header.getKey(), header.getValue());
    522                 }
    523             }
    524             con.setInstanceFollowRedirects(false);
    525             con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect", 15)*1000);
    526             con.setReadTimeout(Main.pref.getInteger("socket.timeout.read", 30)*1000);
    527             if (Main.isDebugEnabled()) {
    528                 Main.debug("GET "+downloadString);
    529             }
    530             if (httpAccept != null) {
    531                 if (Main.isTraceEnabled()) {
    532                     Main.trace("Accept: "+httpAccept);
    533                 }
    534                 con.setRequestProperty("Accept", httpAccept);
    535             }
    536             try {
    537                 con.connect();
    538             } catch (IOException e) {
    539                 Main.addNetworkError(downloadUrl, Utils.getRootCause(e));
    540                 throw e;
    541             }
    542             switch(con.getResponseCode()) {
    543             case HttpURLConnection.HTTP_OK:
    544                 return con;
    545             case HttpURLConnection.HTTP_NOT_MODIFIED:
    546                 if (ifModifiedSince != null)
    547                     return con;
    548             case HttpURLConnection.HTTP_MOVED_PERM:
    549             case HttpURLConnection.HTTP_MOVED_TEMP:
    550             case HttpURLConnection.HTTP_SEE_OTHER:
    551                 String redirectLocation = con.getHeaderField("Location");
    552                 if (redirectLocation == null) {
    553                     /* I18n: argument is HTTP response code */
    554                     String msg = tr("Unexpected response from HTTP server. Got {0} response without ''Location'' header."+
    555                             " Can''t redirect. Aborting.", con.getResponseCode());
    556                     throw new IOException(msg);
    557                 }
    558                 downloadUrl = new URL(redirectLocation);
    559                 downloadString = downloadUrl.toExternalForm();
    560                 // keep track of redirect attempts to break a redirect loops if it happens
    561                 // to occur for whatever reason
    562                 numRedirects++;
    563                 if (numRedirects >= Main.pref.getInteger("socket.maxredirects", 5)) {
    564                     String msg = tr("Too many redirects to the download URL detected. Aborting.");
    565                     throw new IOException(msg);
    566                 }
    567                 Main.info(tr("Download redirected to ''{0}''", downloadString));
    568                 break;
    569             default:
    570                 String msg = tr("Failed to read from ''{0}''. Server responded with status code {1}.", downloadString, con.getResponseCode());
    571                 throw new IOException(msg);
    572             }
    573         }
    574     }
    575467}
Note: See TracChangeset for help on using the changeset viewer.