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/OsmApi.java

    r9078 r9172  
    55import static org.openstreetmap.josm.tools.I18n.trn;
    66
    7 import java.io.BufferedReader;
    8 import java.io.BufferedWriter;
    97import java.io.IOException;
    10 import java.io.InputStream;
    11 import java.io.InputStreamReader;
    12 import java.io.OutputStream;
    13 import java.io.OutputStreamWriter;
    148import java.io.PrintWriter;
    159import java.io.StringReader;
     
    4236import org.openstreetmap.josm.io.Capabilities.CapabilitiesParser;
    4337import org.openstreetmap.josm.tools.CheckParameterUtil;
     38import org.openstreetmap.josm.tools.HttpClient;
    4439import org.openstreetmap.josm.tools.Utils;
    4540import org.openstreetmap.josm.tools.XmlParsingException;
     
    617612    protected final String sendRequest(String requestMethod, String urlSuffix, String requestBody, ProgressMonitor monitor,
    618613            boolean doAuthenticate, boolean fastFail) throws OsmTransferException {
    619         StringBuilder responseBody = new StringBuilder();
    620614        int retries = fastFail ? 0 : getMaxRetries();
    621615
     
    623617            try {
    624618                url = new URL(new URL(getBaseUrl()), urlSuffix);
    625                 Main.info(requestMethod + ' ' + url + "... ");
    626                 Main.debug(requestBody);
    627                 // fix #5369, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive
    628                 activeConnection = Utils.openHttpConnection(url, false);
    629                 activeConnection.setConnectTimeout(fastFail ? 1000 : Main.pref.getInteger("socket.timeout.connect", 15)*1000);
     619                final HttpClient client = HttpClient.create(url, requestMethod).keepAlive(false);
    630620                if (fastFail) {
    631                     activeConnection.setReadTimeout(1000);
     621                    client.setReadTimeout(1000);
    632622                }
    633                 activeConnection.setRequestMethod(requestMethod);
    634623                if (doAuthenticate) {
    635                     addAuth(activeConnection);
     624                    addAuth(client);
    636625                }
    637626
    638627                if ("PUT".equals(requestMethod) || "POST".equals(requestMethod) || "DELETE".equals(requestMethod)) {
    639                     activeConnection.setDoOutput(true);
    640                     activeConnection.setRequestProperty("Content-type", "text/xml");
    641                     try (OutputStream out = activeConnection.getOutputStream()) {
    642                         // It seems that certain bits of the Ruby API are very unhappy upon
    643                         // receipt of a PUT/POST message without a Content-length header,
    644                         // even if the request has no payload.
    645                         // Since Java will not generate a Content-length header unless
    646                         // we use the output stream, we create an output stream for PUT/POST
    647                         // even if there is no payload.
    648                         if (requestBody != null) {
    649                             try (BufferedWriter bwr = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))) {
    650                                 bwr.write(requestBody);
    651                                 bwr.flush();
    652                             }
    653                         }
    654                     }
     628                    // It seems that certain bits of the Ruby API are very unhappy upon
     629                    // receipt of a PUT/POST message without a Content-length header,
     630                    // even if the request has no payload.
     631                    // Since Java will not generate a Content-length header unless
     632                    // we use the output stream, we create an output stream for PUT/POST
     633                    // even if there is no payload.
     634                    client.setRequestBody(requestBody.getBytes(StandardCharsets.UTF_8));
    655635                }
    656636
    657                 activeConnection.connect();
     637                activeConnection = client.connect();
    658638                Main.info(activeConnection.getResponseMessage());
    659639                int retCode = activeConnection.getResponseCode();
     
    667647                }
    668648
    669                 // populate return fields.
    670                 responseBody.setLength(0);
    671 
    672                 // If the API returned an error code like 403 forbidden, getInputStream will fail with an IOException.
    673                 InputStream i = getConnectionStream();
    674                 if (i != null) {
    675                     // the input stream can be null if both the input and the error stream
    676                     // are null. Seems to be the case if the OSM server replies a 401 Unauthorized, see #3887.
    677                     String s;
    678                     try (BufferedReader in = new BufferedReader(new InputStreamReader(i, StandardCharsets.UTF_8))) {
    679                         while ((s = in.readLine()) != null) {
    680                             responseBody.append(s);
    681                             responseBody.append('\n');
    682                         }
    683                     }
    684                 }
     649                final String responseBody = activeConnection.fetchContent();
     650
    685651                String errorHeader = null;
    686652                // Look for a detailed error message from the server
     
    693659                activeConnection.disconnect();
    694660
    695                 if (Main.isDebugEnabled()) {
    696                     Main.debug("RESPONSE: "+ activeConnection.getHeaderFields());
    697                 }
    698 
    699661                errorHeader = errorHeader == null ? null : errorHeader.trim();
    700                 String errorBody = responseBody.length() == 0 ? null : responseBody.toString().trim();
     662                String errorBody = responseBody.length() == 0 ? null : responseBody.trim();
    701663                switch(retCode) {
    702664                case HttpURLConnection.HTTP_OK:
    703                     return responseBody.toString();
     665                    return responseBody;
    704666                case HttpURLConnection.HTTP_GONE:
    705667                    throw new OsmApiPrimitiveGoneException(errorHeader, errorBody);
     
    729691    }
    730692
    731     private InputStream getConnectionStream() {
    732         try {
    733             return activeConnection.getInputStream();
    734         } catch (IOException ioe) {
    735             Main.warn(ioe);
    736             return activeConnection.getErrorStream();
    737         }
    738     }
    739 
    740693    /**
    741694     * Replies the API capabilities.
Note: See TracChangeset for help on using the changeset viewer.