- Timestamp:
- 2015-12-26T23:41:52+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/help/HelpContentReader.java
r8513 r9168 4 4 import java.io.BufferedReader; 5 5 import java.io.IOException; 6 import java.io.InputStreamReader;7 import java.net.HttpURLConnection;8 6 import java.net.MalformedURLException; 9 7 import java.net.URL; 10 import java.nio.charset.StandardCharsets;11 8 12 import org.openstreetmap.josm.Main; 13 import org.openstreetmap.josm.tools.Utils; 9 import org.openstreetmap.josm.tools.HttpClient; 14 10 import org.openstreetmap.josm.tools.WikiReader; 15 11 … … 46 42 if (helpTopicUrl == null) 47 43 throw new MissingHelpContentException("helpTopicUrl is null"); 48 Http URLConnectioncon = null;44 HttpClient.Response con = null; 49 45 try { 50 46 URL u = new URL(helpTopicUrl); 51 con = Utils.openHttpConnection(u); 52 con.connect(); 53 try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) { 47 con = HttpClient.create(u).connect(); 48 try (BufferedReader in = con.getContentReader()) { 54 49 return prepareHelpContent(in, dotest, u); 55 50 } … … 59 54 HelpContentReaderException ex = new HelpContentReaderException(e); 60 55 if (con != null) { 61 try { 62 ex.setResponseCode(con.getResponseCode()); 63 } catch (IOException e1) { 64 // ignore 65 if (Main.isTraceEnabled()) { 66 Main.trace(e1.getMessage()); 67 } 68 } 56 ex.setResponseCode(con.getResponseCode()); 69 57 } 70 58 throw ex; -
trunk/src/org/openstreetmap/josm/io/CachedFile.java
r9078 r9168 21 21 import java.util.List; 22 22 import java.util.Map; 23 import java.util.Map.Entry;24 23 import java.util.concurrent.ConcurrentHashMap; 25 24 import java.util.zip.ZipEntry; … … 27 26 28 27 import org.openstreetmap.josm.Main; 29 import org.openstreetmap.josm.tools. CheckParameterUtil;28 import org.openstreetmap.josm.tools.HttpClient; 30 29 import org.openstreetmap.josm.tools.Pair; 31 30 import org.openstreetmap.josm.tools.Utils; … … 415 414 destDirFile = new File(destDir, localPath + ".tmp"); 416 415 try { 417 HttpURLConnection con = connectFollowingRedirect(url, httpAccept, ifModifiedSince, httpHeaders); 416 final HttpClient.Response con = HttpClient.create(url) 417 .setAccept(httpAccept) 418 .setIfModifiedSince(ifModifiedSince == null ? 0L : ifModifiedSince) 419 .setHeaders(httpHeaders) 420 .connect(); 418 421 if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { 419 422 if (Main.isDebugEnabled()) { … … 427 430 } 428 431 try ( 429 InputStream bis = new BufferedInputStream(con.get InputStream());432 InputStream bis = new BufferedInputStream(con.getContent()); 430 433 OutputStream fos = new FileOutputStream(destDirFile); 431 434 OutputStream bos = new BufferedOutputStream(fos) … … 462 465 } 463 466 464 /**465 * Opens a connection for downloading a resource.466 * <p>467 * Manually follows redirects because468 * {@link HttpURLConnection#setFollowRedirects(boolean)} fails if the redirect469 * is going from a http to a https URL, see <a href="https://bugs.openjdk.java.net/browse/JDK-4620571">bug report</a>.470 * <p>471 * This can cause problems when downloading from certain GitHub URLs.472 *473 * @param downloadUrl The resource URL to download474 * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Can be {@code null}475 * @param ifModifiedSince The download time of the cache file, optional476 * @return The HTTP connection effectively linked to the resource, after all potential redirections477 * @throws MalformedURLException If a redirected URL is wrong478 * @throws IOException If any I/O operation goes wrong479 * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol480 * @since 6867481 */482 public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince)483 throws MalformedURLException, IOException {484 return connectFollowingRedirect(downloadUrl, httpAccept, ifModifiedSince, null);485 }486 487 /**488 * Opens a connection for downloading a resource.489 * <p>490 * Manually follows redirects because491 * {@link HttpURLConnection#setFollowRedirects(boolean)} fails if the redirect492 * is going from a http to a https URL, see <a href="https://bugs.openjdk.java.net/browse/JDK-4620571">bug report</a>.493 * <p>494 * This can cause problems when downloading from certain GitHub URLs.495 *496 * @param downloadUrl The resource URL to download497 * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Can be {@code null}498 * @param ifModifiedSince The download time of the cache file, optional499 * @param headers http headers to be sent together with http request500 * @return The HTTP connection effectively linked to the resource, after all potential redirections501 * @throws MalformedURLException If a redirected URL is wrong502 * @throws IOException If any I/O operation goes wrong503 * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol504 * @since TODO505 */506 public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince,507 Map<String, String> headers) throws MalformedURLException, IOException {508 CheckParameterUtil.ensureParameterNotNull(downloadUrl, "downloadUrl");509 String downloadString = downloadUrl.toExternalForm();510 511 checkOfflineAccess(downloadString);512 513 int numRedirects = 0;514 while (true) {515 HttpURLConnection con = Utils.openHttpConnection(downloadUrl);516 if (ifModifiedSince != null) {517 con.setIfModifiedSince(ifModifiedSince);518 }519 if (headers != null) {520 for (Entry<String, String> header: headers.entrySet()) {521 con.setRequestProperty(header.getKey(), header.getValue());522 }523 }524 con.setInstanceFollowRedirects(false);525 con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect", 15)*1000);526 con.setReadTimeout(Main.pref.getInteger("socket.timeout.read", 30)*1000);527 if (Main.isDebugEnabled()) {528 Main.debug("GET "+downloadString);529 }530 if (httpAccept != null) {531 if (Main.isTraceEnabled()) {532 Main.trace("Accept: "+httpAccept);533 }534 con.setRequestProperty("Accept", httpAccept);535 }536 try {537 con.connect();538 } catch (IOException e) {539 Main.addNetworkError(downloadUrl, Utils.getRootCause(e));540 throw e;541 }542 switch(con.getResponseCode()) {543 case HttpURLConnection.HTTP_OK:544 return con;545 case HttpURLConnection.HTTP_NOT_MODIFIED:546 if (ifModifiedSince != null)547 return con;548 case HttpURLConnection.HTTP_MOVED_PERM:549 case HttpURLConnection.HTTP_MOVED_TEMP:550 case HttpURLConnection.HTTP_SEE_OTHER:551 String redirectLocation = con.getHeaderField("Location");552 if (redirectLocation == null) {553 /* I18n: argument is HTTP response code */554 String msg = tr("Unexpected response from HTTP server. Got {0} response without ''Location'' header."+555 " Can''t redirect. Aborting.", con.getResponseCode());556 throw new IOException(msg);557 }558 downloadUrl = new URL(redirectLocation);559 downloadString = downloadUrl.toExternalForm();560 // keep track of redirect attempts to break a redirect loops if it happens561 // to occur for whatever reason562 numRedirects++;563 if (numRedirects >= Main.pref.getInteger("socket.maxredirects", 5)) {564 String msg = tr("Too many redirects to the download URL detected. Aborting.");565 throw new IOException(msg);566 }567 Main.info(tr("Download redirected to ''{0}''", downloadString));568 break;569 default:570 String msg = tr("Failed to read from ''{0}''. Server responded with status code {1}.", downloadString, con.getResponseCode());571 throw new IOException(msg);572 }573 }574 }575 467 } -
trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java
r9059 r9168 10 10 import java.io.InputStream; 11 11 import java.io.OutputStream; 12 import java.net.HttpURLConnection;13 12 import java.net.MalformedURLException; 14 13 import java.net.URL; … … 22 21 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 23 22 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 24 import org.openstreetmap.josm.io.CachedFile;25 23 import org.openstreetmap.josm.tools.CheckParameterUtil; 24 import org.openstreetmap.josm.tools.HttpClient; 26 25 import org.xml.sax.SAXException; 27 26 … … 45 44 private final Collection<PluginInformation> downloaded = new LinkedList<>(); 46 45 private boolean canceled; 47 private Http URLConnectiondownloadConnection;46 private HttpClient.Response downloadConnection; 48 47 49 48 /** … … 124 123 URL url = new URL(pi.downloadlink); 125 124 synchronized (this) { 126 downloadConnection = CachedFile.connectFollowingRedirect(url, PLUGIN_MIME_TYPES, null); 125 downloadConnection = HttpClient.create(url) 126 .setAccept(PLUGIN_MIME_TYPES) 127 .connect(); 127 128 } 128 129 try ( 129 InputStream in = downloadConnection.get InputStream();130 InputStream in = downloadConnection.getContent(); 130 131 OutputStream out = new FileOutputStream(file) 131 132 ) { -
trunk/src/org/openstreetmap/josm/tools/WikiReader.java
r8933 r9168 53 53 public String read(String url) throws IOException { 54 54 URL u = new URL(url); 55 try (BufferedReader in = Utils.openURLReader(u)) {55 try (BufferedReader in = HttpClient.create(u).connect().getContentReader()) { 56 56 boolean txt = url.endsWith("?format=txt"); 57 57 if (url.startsWith(getBaseUrlWiki()) && !txt) … … 98 98 99 99 private String readLang(URL url) throws IOException { 100 try (BufferedReader in = Utils.openURLReader(url)) {100 try (BufferedReader in = HttpClient.create(url).connect().getContentReader()) { 101 101 return readFromTrac(in, url); 102 } catch (IOException e) {103 Main.addNetworkError(url, Utils.getRootCause(e));104 throw e;105 102 } 106 103 }
Note:
See TracChangeset
for help on using the changeset viewer.