Changeset 9172 in josm for trunk/src/org/openstreetmap/josm/io/OsmApi.java
- Timestamp:
- 2015-12-26T23:42:03+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r9078 r9172 5 5 import static org.openstreetmap.josm.tools.I18n.trn; 6 6 7 import java.io.BufferedReader;8 import java.io.BufferedWriter;9 7 import java.io.IOException; 10 import java.io.InputStream;11 import java.io.InputStreamReader;12 import java.io.OutputStream;13 import java.io.OutputStreamWriter;14 8 import java.io.PrintWriter; 15 9 import java.io.StringReader; … … 42 36 import org.openstreetmap.josm.io.Capabilities.CapabilitiesParser; 43 37 import org.openstreetmap.josm.tools.CheckParameterUtil; 38 import org.openstreetmap.josm.tools.HttpClient; 44 39 import org.openstreetmap.josm.tools.Utils; 45 40 import org.openstreetmap.josm.tools.XmlParsingException; … … 617 612 protected final String sendRequest(String requestMethod, String urlSuffix, String requestBody, ProgressMonitor monitor, 618 613 boolean doAuthenticate, boolean fastFail) throws OsmTransferException { 619 StringBuilder responseBody = new StringBuilder();620 614 int retries = fastFail ? 0 : getMaxRetries(); 621 615 … … 623 617 try { 624 618 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); 630 620 if (fastFail) { 631 activeConnection.setReadTimeout(1000);621 client.setReadTimeout(1000); 632 622 } 633 activeConnection.setRequestMethod(requestMethod);634 623 if (doAuthenticate) { 635 addAuth( activeConnection);624 addAuth(client); 636 625 } 637 626 638 627 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)); 655 635 } 656 636 657 activeConnection .connect();637 activeConnection = client.connect(); 658 638 Main.info(activeConnection.getResponseMessage()); 659 639 int retCode = activeConnection.getResponseCode(); … … 667 647 } 668 648 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 685 651 String errorHeader = null; 686 652 // Look for a detailed error message from the server … … 693 659 activeConnection.disconnect(); 694 660 695 if (Main.isDebugEnabled()) {696 Main.debug("RESPONSE: "+ activeConnection.getHeaderFields());697 }698 699 661 errorHeader = errorHeader == null ? null : errorHeader.trim(); 700 String errorBody = responseBody.length() == 0 ? null : responseBody.t oString().trim();662 String errorBody = responseBody.length() == 0 ? null : responseBody.trim(); 701 663 switch(retCode) { 702 664 case HttpURLConnection.HTTP_OK: 703 return responseBody .toString();665 return responseBody; 704 666 case HttpURLConnection.HTTP_GONE: 705 667 throw new OsmApiPrimitiveGoneException(errorHeader, errorBody); … … 729 691 } 730 692 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 740 693 /** 741 694 * Replies the API capabilities.
Note:
See TracChangeset
for help on using the changeset viewer.