Changeset 9309 in josm
- Timestamp:
- 2016-01-04T20:51:45+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/oauth/SignpostAdapters.java
r9254 r9309 40 40 if (response != null) { 41 41 ((HttpResponse) response).response.disconnect(); 42 } else if (request != null) { 43 ((HttpRequest) request).request.disconnect(); 42 44 } 43 45 } -
trunk/src/org/openstreetmap/josm/gui/download/PlaceSelection.java
r9171 r9309 325 325 326 326 private final String searchExpression; 327 private HttpClient .Responseconnection;327 private HttpClient connection; 328 328 private List<SearchResult> data; 329 329 private boolean canceled; … … 368 368 URL url = new URL(urlString); 369 369 synchronized (this) { 370 connection = HttpClient.create(url).connect(); 371 } 372 try (Reader reader = connection.getContentReader()) { 370 connection = HttpClient.create(url); 371 connection.connect(); 372 } 373 try (Reader reader = connection.getResponse().getContentReader()) { 373 374 InputSource inputSource = new InputSource(reader); 374 375 NameFinderResultParser parser = new NameFinderResultParser(); -
trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java
r9296 r9309 69 69 70 70 private boolean canceled; 71 private HttpClient .ResponsedownloadConnection;71 private HttpClient downloadConnection; 72 72 73 73 private synchronized void closeConnectionIfNeeded() { … … 103 103 long size; 104 104 synchronized (this) { 105 downloadConnection = HttpClient.create(url).useCache(false).connect(); 106 size = downloadConnection.getContentLength(); 105 downloadConnection = HttpClient.create(url).useCache(false); 106 downloadConnection.connect(); 107 size = downloadConnection.getResponse().getContentLength(); 107 108 } 108 109 … … 111 112 112 113 try ( 113 InputStream in = downloadConnection.get Content();114 InputStream in = downloadConnection.getResponse().getContent(); 114 115 OutputStream out = new FileOutputStream(file) 115 116 ) { -
trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java
r9301 r9309 44 44 private final OAuthProvider provider; 45 45 private boolean canceled; 46 private HttpClient .Responseconnection;46 private HttpClient connection; 47 47 48 48 private static class SessionId { … … 183 183 184 184 protected String extractToken() { 185 try (BufferedReader r = connection.get ContentReader()) {185 try (BufferedReader r = connection.getResponse().getContentReader()) { 186 186 String c; 187 187 Pattern p = Pattern.compile(".*authenticity_token.*value=\"([^\"]+)\".*"); … … 200 200 201 201 protected SessionId extractOsmSession() { 202 List<String> setCookies = connection.get HeaderFields().get("Set-Cookie");202 List<String> setCookies = connection.getResponse().getHeaderFields().get("Set-Cookie"); 203 203 if (setCookies == null) 204 204 // no cookies set … … 293 293 URL url = new URL(sb.toString()); 294 294 synchronized (this) { 295 connection = HttpClient.create(url).connect(); 295 connection = HttpClient.create(url); 296 connection.connect(); 296 297 } 297 298 SessionId sessionId = extractOsmSession(); … … 322 323 synchronized (this) { 323 324 connection = HttpClient.create(url) 324 .setHeader("Cookie", "_osm_session=" + sessionId.id + "; _osm_username=" + sessionId.userName) 325 325 .setHeader("Cookie", "_osm_session=" + sessionId.id + "; _osm_username=" + sessionId.userName); 326 connection.connect(); 326 327 } 327 328 sessionId.token = extractToken(); … … 357 358 358 359 synchronized (this) { 359 connection = client.connect(); 360 connection = client; 361 connection.connect(); 360 362 } 361 363 … … 364 366 // an error page is sent to back to the user. 365 367 // 366 int retCode = connection.getResponse Code();368 int retCode = connection.getResponse().getResponseCode(); 367 369 if (retCode != HttpURLConnection.HTTP_MOVED_TEMP) 368 370 throw new OsmOAuthAuthorizationException(tr("Failed to authenticate user ''{0}'' with password ''***'' as OAuth user", … … 383 385 URL url = new URL(buildOsmLogoutUrl()); 384 386 synchronized (this) { 385 connection = HttpClient.create(url).setMaxRedirects(-1).connect(); 387 connection = HttpClient.create(url).setMaxRedirects(-1); 388 connection.connect(); 386 389 } 387 390 } catch (IOException e) { … … 432 435 433 436 synchronized (this) { 434 connection = client.connect(); 435 } 436 437 int retCode = connection.getResponseCode(); 437 connection = client; 438 connection.connect(); 439 } 440 441 int retCode = connection.getResponse().getResponseCode(); 438 442 if (retCode != HttpURLConnection.HTTP_OK) 439 443 throw new OsmOAuthAuthorizationException(tr("Failed to authorize OAuth request ''{0}''", requestToken.getKey())); -
trunk/src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java
r9172 r9309 46 46 private final Component parent; 47 47 private final String apiUrl; 48 private HttpClient .Responseconnection;48 private HttpClient connection; 49 49 50 50 /** … … 107 107 sign(client); 108 108 synchronized (this) { 109 connection = client.connect(); 109 connection = client; 110 connection.connect(); 110 111 } 111 112 112 if (connection.getResponse Code() == HttpURLConnection.HTTP_UNAUTHORIZED)113 if (connection.getResponse().getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) 113 114 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, 114 115 tr("Retrieving user details with Access Token Key ''{0}'' was rejected.", token.getKey()), null); 115 116 116 if (connection.getResponse Code() == HttpURLConnection.HTTP_FORBIDDEN)117 if (connection.getResponse().getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) 117 118 throw new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, 118 119 tr("Retrieving user details with Access Token Key ''{0}'' was forbidden.", token.getKey()), null); 119 120 120 if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) 121 throw new OsmApiException(connection.getResponseCode(), connection.getHeaderField("Error"), null); 122 Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(connection.getContent()); 121 if (connection.getResponse().getResponseCode() != HttpURLConnection.HTTP_OK) 122 throw new OsmApiException(connection.getResponse().getResponseCode(), 123 connection.getResponse().getHeaderField("Error"), null); 124 Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(connection.getResponse().getContent()); 123 125 return OsmServerUserInfoReader.buildFromXML(d); 124 126 } catch (SAXException | ParserConfigurationException e) { -
trunk/src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java
r9172 r9309 36 36 private boolean success; 37 37 private final Component parent; 38 private HttpClient .Responseconnection;38 private HttpClient connection; 39 39 40 40 /** … … 177 177 178 178 synchronized (this) { 179 connection = HttpClient.create(capabilitiesUrl).connect(); 180 } 181 182 if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 183 alertInvalidServerResult(connection.getResponseCode()); 179 connection = HttpClient.create(capabilitiesUrl); 180 connection.connect(); 181 } 182 183 if (connection.getResponse().getResponseCode() != HttpURLConnection.HTTP_OK) { 184 alertInvalidServerResult(connection.getResponse().getResponseCode()); 184 185 return; 185 186 } 186 187 187 188 try { 188 Capabilities.CapabilitiesParser.parse(new InputSource(connection.get Content()));189 Capabilities.CapabilitiesParser.parse(new InputSource(connection.getResponse().getContent())); 189 190 } catch (SAXException | ParserConfigurationException e) { 190 191 Main.warn(e.getMessage()); -
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r9182 r9309 618 618 url = new URL(new URL(getBaseUrl()), urlSuffix); 619 619 final HttpClient client = HttpClient.create(url, requestMethod).keepAlive(false); 620 activeConnection = client; 620 621 if (fastFail) { 621 622 client.setConnectTimeout(1000); … … 640 641 } 641 642 642 activeConnection= client.connect();643 Main.info( activeConnection.getResponseMessage());644 int retCode = activeConnection.getResponseCode();643 final HttpClient.Response response = client.connect(); 644 Main.info(response.getResponseMessage()); 645 int retCode = response.getResponseCode(); 645 646 646 647 if (retCode >= 500) { … … 652 653 } 653 654 654 final String responseBody = activeConnection.fetchContent();655 final String responseBody = response.fetchContent(); 655 656 656 657 String errorHeader = null; 657 658 // Look for a detailed error message from the server 658 if ( activeConnection.getHeaderField("Error") != null) {659 errorHeader = activeConnection.getHeaderField("Error");659 if (response.getHeaderField("Error") != null) { 660 errorHeader = response.getHeaderField("Error"); 660 661 Main.error("Error header: " + errorHeader); 661 662 } else if (retCode != HttpURLConnection.HTTP_OK && responseBody.length() > 0) { -
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r9172 r9309 31 31 public class OsmConnection { 32 32 protected boolean cancel; 33 protected HttpClient .ResponseactiveConnection;33 protected HttpClient activeConnection; 34 34 protected OAuthParameters oauthParameters; 35 35 -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r9308 r9309 123 123 124 124 final HttpClient client = HttpClient.create(url); 125 activeConnection = client; 125 126 client.setReasonForRequest(reason); 126 127 adaptRequest(client); … … 131 132 throw new OsmTransferCanceledException("Operation canceled"); 132 133 134 final HttpClient.Response response; 133 135 try { 134 activeConnection= client.connect(progressMonitor);136 response = client.connect(progressMonitor); 135 137 } catch (Exception e) { 136 138 Main.error(e); … … 141 143 } 142 144 try { 143 if ( activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)145 if (response.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) 144 146 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null); 145 147 146 if ( activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)148 if (response.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH) 147 149 throw new OsmTransferCanceledException("Proxy Authentication Required"); 148 150 149 if ( activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {150 String errorHeader = activeConnection.getHeaderField("Error");151 if (response.getResponseCode() != HttpURLConnection.HTTP_OK) { 152 String errorHeader = response.getHeaderField("Error"); 151 153 String errorBody; 152 154 try { 153 errorBody = activeConnection.fetchContent();155 errorBody = response.fetchContent(); 154 156 } catch (Exception e) { 155 157 errorBody = tr("Reading error text failed."); 156 158 } 157 throw new OsmApiException( activeConnection.getResponseCode(), errorHeader, errorBody, url.toString());159 throw new OsmApiException(response.getResponseCode(), errorHeader, errorBody, url.toString()); 158 160 } 159 161 160 activeConnection.uncompressAccordingToContentDisposition(uncompressAccordingToContentDisposition);161 return activeConnection.getContent();162 response.uncompressAccordingToContentDisposition(uncompressAccordingToContentDisposition); 163 return response.getContent(); 162 164 } catch (OsmTransferException e) { 163 165 throw e; -
trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java
r9280 r9309 44 44 private final Collection<PluginInformation> downloaded = new LinkedList<>(); 45 45 private boolean canceled; 46 private HttpClient .ResponsedownloadConnection;46 private HttpClient downloadConnection; 47 47 48 48 /** … … 124 124 synchronized (this) { 125 125 downloadConnection = HttpClient.create(url) 126 .setAccept(PLUGIN_MIME_TYPES) 127 128 } 129 try (InputStream in = downloadConnection.get Content()) {126 .setAccept(PLUGIN_MIME_TYPES); 127 downloadConnection.connect(); 128 } 129 try (InputStream in = downloadConnection.getResponse().getContent()) { 130 130 Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING); 131 131 } -
trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java
r9296 r9309 51 51 private Collection<String> sites; 52 52 private boolean canceled; 53 private HttpClient .Responseconnection;53 private HttpClient connection; 54 54 private List<PluginInformation> availablePlugins; 55 55 private boolean displayErrMsg; … … 157 157 158 158 URL url = new URL(site); 159 connection = HttpClient.create(url).useCache(false).connect(); 160 content = connection.fetchContent(); 161 if (connection.getResponseCode() != 200) { 159 connection = HttpClient.create(url).useCache(false); 160 final HttpClient.Response response = connection.connect(); 161 content = response.fetchContent(); 162 if (response.getResponseCode() != 200) { 162 163 throw new IOException(tr("Unsuccessful HTTP request")); 163 164 } -
trunk/src/org/openstreetmap/josm/tools/HttpClient.java
r9274 r9309 47 47 private boolean useCache; 48 48 private String reasonForRequest; 49 private transient HttpURLConnection connection; // to allow disconnecting before `response` is set 50 private transient Response response; 49 51 50 52 private HttpClient(URL url, String requestMethod) { … … 75 77 } 76 78 final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 79 this.connection = connection; 77 80 connection.setRequestMethod(requestMethod); 78 81 connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString()); … … 148 151 } 149 152 } 150 Responseresponse = new Response(connection, progressMonitor);153 response = new Response(connection, progressMonitor); 151 154 successfulConnection = true; 152 155 return response; … … 156 159 } 157 160 } 161 } 162 163 /** 164 * Returns the HTTP response which is set only after calling {@link #connect()}. 165 * Calling this method again, returns the identical object (unless another {@link #connect()} is performed). 166 * 167 * @return the HTTP response 168 * @since 9309 169 */ 170 public Response getResponse() { 171 return response; 158 172 } 159 173 … … 381 395 */ 382 396 public void disconnect() { 383 // TODO is this block necessary for disconnecting? 384 // Fix upload aborts - see #263 385 connection.setConnectTimeout(100); 386 connection.setReadTimeout(100); 387 try { 388 Thread.sleep(100); 389 } catch (InterruptedException ex) { 390 Main.warn("InterruptedException in " + getClass().getSimpleName() + " during cancel"); 391 } 392 393 connection.disconnect(); 397 HttpClient.disconnect(connection); 394 398 } 395 399 } … … 611 615 } 612 616 } 617 618 /** 619 * @see HttpURLConnection#disconnect() 620 * @since 9309 621 */ 622 public void disconnect() { 623 HttpClient.disconnect(connection); 624 } 625 626 private static void disconnect(final HttpURLConnection connection) { 627 // Fix upload aborts - see #263 628 connection.setConnectTimeout(100); 629 connection.setReadTimeout(100); 630 try { 631 Thread.sleep(100); 632 } catch (InterruptedException ex) { 633 Main.warn("InterruptedException in " + HttpClient.class + " during cancel"); 634 } 635 connection.disconnect(); 636 } 613 637 }
Note:
See TracChangeset
for help on using the changeset viewer.