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

see #12231 - Use HttpClient instead of some Utils.openHttpConnection usages

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java

    r9062 r9171  
    66import java.awt.Dimension;
    77import java.awt.GridBagLayout;
    8 import java.io.BufferedReader;
    98import java.io.ByteArrayInputStream;
    109import java.io.File;
     
    1312import java.io.IOException;
    1413import java.io.InputStream;
    15 import java.io.InputStreamReader;
    1614import java.io.OutputStreamWriter;
    1715import java.io.PrintWriter;
    18 import java.net.HttpURLConnection;
    1916import java.net.MalformedURLException;
    2017import java.net.URL;
     
    4239import org.openstreetmap.josm.io.OsmTransferException;
    4340import org.openstreetmap.josm.tools.GBC;
     41import org.openstreetmap.josm.tools.HttpClient;
    4442import org.openstreetmap.josm.tools.Utils;
    4543import org.xml.sax.SAXException;
     
    5351    private Collection<String> sites;
    5452    private boolean canceled;
    55     private HttpURLConnection connection;
     53    private HttpClient.Response connection;
    5654    private List<PluginInformation> availablePlugins;
    5755    private boolean displayErrMsg;
     
    153151        }
    154152
     153        String content = null;
    155154        try {
    156155            monitor.beginTask("");
     
    158157
    159158            URL url = new URL(site);
    160             synchronized (this) {
    161                 connection = Utils.openHttpConnection(url);
    162                 connection.setRequestProperty("Cache-Control", "no-cache");
    163                 connection.setRequestProperty("Accept-Charset", "utf-8");
    164             }
    165             try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
    166                 StringBuilder sb = new StringBuilder();
    167                 String line;
    168                 while ((line = in.readLine()) != null) {
    169                     sb.append(line).append('\n');
    170                 }
    171                 return sb.toString();
    172             }
     159            connection = HttpClient.create(url).useCache(false).connect();
     160            content = connection.fetchContent();
     161            if (connection.getResponseCode() != 200) {
     162                throw new IOException(tr("Unsuccessful HTTP request"));
     163            }
     164            return content;
    173165        } catch (MalformedURLException e) {
    174166            if (canceled) return null;
     
    177169        } catch (IOException e) {
    178170            if (canceled) return null;
    179             Main.addNetworkError(site, e);
    180             handleIOException(monitor, e, tr("Plugin list download error"), tr("JOSM failed to download plugin list:"), displayErrMsg);
     171            handleIOException(monitor, e, content);
    181172            return null;
    182173        } finally {
     
    191182    }
    192183
    193     private void handleIOException(final ProgressMonitor monitor, IOException e, final String title, final String firstMessage,
    194             boolean displayMsg) {
    195         StringBuilder sb = new StringBuilder();
    196         try (InputStream errStream = connection.getErrorStream()) {
    197             if (errStream != null) {
    198                 try (BufferedReader err = new BufferedReader(new InputStreamReader(errStream, StandardCharsets.UTF_8))) {
    199                     String line;
    200                     while ((line = err.readLine()) != null) {
    201                         sb.append(line).append('\n');
    202                     }
    203                 } catch (Exception ex) {
    204                     Main.error(e);
    205                     Main.error(ex);
    206                 }
    207             }
    208         } catch (IOException ex) {
    209             Main.warn(ex);
    210         }
     184    private void handleIOException(final ProgressMonitor monitor, IOException e, String details) {
    211185        final String msg = e.getMessage();
    212         final String details = sb.toString();
    213186        if (details.isEmpty()) {
    214187            Main.error(e.getClass().getSimpleName()+": " + msg);
     
    217190        }
    218191
    219         if (displayMsg) {
    220             displayErrorMessage(monitor, msg, details, title, firstMessage);
     192        if (displayErrMsg) {
     193            displayErrorMessage(monitor, msg, details, tr("Plugin list download error"), tr("JOSM failed to download plugin list:"));
    221194        }
    222195    }
Note: See TracChangeset for help on using the changeset viewer.