Changeset 9168 in josm


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

see #12231 - Uniform access to HTTP resources

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/help/HelpContentReader.java

    r8513 r9168  
    44import java.io.BufferedReader;
    55import java.io.IOException;
    6 import java.io.InputStreamReader;
    7 import java.net.HttpURLConnection;
    86import java.net.MalformedURLException;
    97import java.net.URL;
    10 import java.nio.charset.StandardCharsets;
    118
    12 import org.openstreetmap.josm.Main;
    13 import org.openstreetmap.josm.tools.Utils;
     9import org.openstreetmap.josm.tools.HttpClient;
    1410import org.openstreetmap.josm.tools.WikiReader;
    1511
     
    4642        if (helpTopicUrl == null)
    4743            throw new MissingHelpContentException("helpTopicUrl is null");
    48         HttpURLConnection con = null;
     44        HttpClient.Response con = null;
    4945        try {
    5046            URL u = new URL(helpTopicUrl);
    51             con = Utils.openHttpConnection(u);
    52             con.connect();
    53             try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
     47            con = HttpClient.create(u).connect();
     48            try (BufferedReader in = con.getContentReader()) {
    5449                return prepareHelpContent(in, dotest, u);
    5550            }
     
    5954            HelpContentReaderException ex = new HelpContentReaderException(e);
    6055            if (con != null) {
    61                 try {
    62                     ex.setResponseCode(con.getResponseCode());
    63                 } catch (IOException e1) {
    64                     // ignore
    65                     if (Main.isTraceEnabled()) {
    66                         Main.trace(e1.getMessage());
    67                     }
    68                 }
     56                ex.setResponseCode(con.getResponseCode());
    6957            }
    7058            throw ex;
  • 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}
  • trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java

    r9059 r9168  
    1010import java.io.InputStream;
    1111import java.io.OutputStream;
    12 import java.net.HttpURLConnection;
    1312import java.net.MalformedURLException;
    1413import java.net.URL;
     
    2221import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    2322import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    24 import org.openstreetmap.josm.io.CachedFile;
    2523import org.openstreetmap.josm.tools.CheckParameterUtil;
     24import org.openstreetmap.josm.tools.HttpClient;
    2625import org.xml.sax.SAXException;
    2726
     
    4544    private final Collection<PluginInformation> downloaded = new LinkedList<>();
    4645    private boolean canceled;
    47     private HttpURLConnection downloadConnection;
     46    private HttpClient.Response downloadConnection;
    4847
    4948    /**
     
    124123            URL url = new URL(pi.downloadlink);
    125124            synchronized (this) {
    126                 downloadConnection = CachedFile.connectFollowingRedirect(url, PLUGIN_MIME_TYPES, null);
     125                downloadConnection = HttpClient.create(url)
     126                        .setAccept(PLUGIN_MIME_TYPES)
     127                        .connect();
    127128            }
    128129            try (
    129                 InputStream in = downloadConnection.getInputStream();
     130                InputStream in = downloadConnection.getContent();
    130131                OutputStream out = new FileOutputStream(file)
    131132            ) {
  • trunk/src/org/openstreetmap/josm/tools/WikiReader.java

    r8933 r9168  
    5353    public String read(String url) throws IOException {
    5454        URL u = new URL(url);
    55         try (BufferedReader in = Utils.openURLReader(u)) {
     55        try (BufferedReader in = HttpClient.create(u).connect().getContentReader()) {
    5656            boolean txt = url.endsWith("?format=txt");
    5757            if (url.startsWith(getBaseUrlWiki()) && !txt)
     
    9898
    9999    private String readLang(URL url) throws IOException {
    100         try (BufferedReader in = Utils.openURLReader(url)) {
     100        try (BufferedReader in = HttpClient.create(url).connect().getContentReader()) {
    101101            return readFromTrac(in, url);
    102         } catch (IOException e) {
    103             Main.addNetworkError(url, Utils.getRootCause(e));
    104             throw e;
    105102        }
    106103    }
Note: See TracChangeset for help on using the changeset viewer.