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

see #12231 - Use HttpClient for OSM API calls

This requires adaptors to the OAuth library: SignpostAdapters

File:
1 edited

Legend:

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

    r9078 r9172  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.BufferedReader;
    7 import java.io.IOException;
    86import java.io.InputStream;
    9 import java.io.InputStreamReader;
    107import java.net.HttpURLConnection;
    118import java.net.MalformedURLException;
    129import java.net.URL;
    13 import java.nio.charset.StandardCharsets;
    1410import java.util.List;
    15 import java.util.Map;
    16 import java.util.zip.GZIPInputStream;
    17 import java.util.zip.Inflater;
    18 import java.util.zip.InflaterInputStream;
    1911
    2012import org.openstreetmap.josm.Main;
     
    2315import org.openstreetmap.josm.data.osm.DataSet;
    2416import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    25 import org.openstreetmap.josm.tools.Utils;
     17import org.openstreetmap.josm.tools.HttpClient;
    2618
    2719/**
     
    129121                throw new OsmTransferException(e);
    130122            }
    131             try {
    132                 // fix #7640, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive
    133                 activeConnection = Utils.openHttpConnection(url, false);
    134             } catch (Exception e) {
    135                 throw new OsmTransferException(tr("Failed to open connection to API {0}.", url.toExternalForm()), e);
    136             }
    137             Utils.setupURLConnection(activeConnection);
    138             if (cancel) {
    139                 activeConnection.disconnect();
    140                 return null;
    141             }
    142 
     123
     124            final HttpClient client = HttpClient.create(url);
     125            client.setReasonForRequest(reason);
    143126            if (doAuthenticate) {
    144                 addAuth(activeConnection);
     127                addAuth(client);
    145128            }
    146129            if (cancel)
    147130                throw new OsmTransferCanceledException("Operation canceled");
    148             if (Main.pref.getBoolean("osm-server.use-compression", true)) {
    149                 activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
    150             }
    151131
    152132            try {
    153                 if (reason != null && !reason.isEmpty()) {
    154                     Main.info("GET " + url + " (" + reason + ')');
    155                 } else {
    156                     Main.info("GET " + url);
    157                 }
    158                 activeConnection.connect();
     133                activeConnection = client.connect();
    159134            } catch (Exception e) {
    160135                Main.error(e);
     
    165140            }
    166141            try {
    167                 if (Main.isDebugEnabled()) {
    168                     Main.debug("RESPONSE: "+activeConnection.getHeaderFields());
    169                 }
    170142                if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
    171143                    throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null);
     
    174146                    throw new OsmTransferCanceledException("Proxy Authentication Required");
    175147
    176                 String encoding = activeConnection.getContentEncoding();
    177148                if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
    178149                    String errorHeader = activeConnection.getHeaderField("Error");
    179                     StringBuilder errorBody = new StringBuilder();
    180                     try {
    181                         InputStream i = fixEncoding(activeConnection.getErrorStream(), encoding);
    182                         if (i != null) {
    183                             BufferedReader in = new BufferedReader(new InputStreamReader(i, StandardCharsets.UTF_8));
    184                             String s;
    185                             while ((s = in.readLine()) != null) {
    186                                 errorBody.append(s);
    187                                 errorBody.append('\n');
    188                             }
    189                         }
    190                     } catch (Exception e) {
    191                         errorBody.append(tr("Reading error text failed."));
    192                     }
    193 
    194                     throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString(), url.toString());
     150                    final String errorBody = activeConnection.fetchContent();
     151                    throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody, url.toString());
    195152                }
    196153
    197154                InputStream in = new ProgressInputStream(activeConnection, progressMonitor);
    198155                if (uncompressAccordingToContentDisposition) {
    199                     in = uncompressAccordingToContentDisposition(in, activeConnection.getHeaderFields());
     156                    activeConnection.uncompressAccordingToContentDisposition(true);
    200157                }
    201                 return fixEncoding(in, encoding);
     158                return in;
    202159            } catch (OsmTransferException e) {
    203160                throw e;
     
    210167    }
    211168
    212     private static InputStream fixEncoding(InputStream stream, String encoding) throws IOException {
    213         if ("gzip".equalsIgnoreCase(encoding)) {
    214             stream = new GZIPInputStream(stream);
    215         } else if ("deflate".equalsIgnoreCase(encoding)) {
    216             stream = new InflaterInputStream(stream, new Inflater(true));
    217         }
    218         return stream;
    219     }
    220 
    221     private InputStream uncompressAccordingToContentDisposition(InputStream stream, Map<String, List<String>> headerFields) throws IOException {
    222         List<String> field = headerFields.get("Content-Disposition");
    223         if (field != null && field.toString().contains(".gz\"")) {
    224             return Compression.GZIP.getUncompressedInputStream(stream);
    225         } else if (field != null && field.toString().contains(".bz2\"")) {
    226             return Compression.BZIP2.getUncompressedInputStream(stream);
    227         } else {
    228             return stream;
    229         }
    230     }
    231 
    232169    /**
    233170     * Download OSM files from somewhere
Note: See TracChangeset for help on using the changeset viewer.