source: josm/trunk/src/org/openstreetmap/josm/tools/HttpClient.java@ 12841

Last change on this file since 12841 was 12841, checked in by bastiK, 7 years ago

see #15229 - fix deprecations caused by [12840]

File size: 25.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedOutputStream;
7import java.io.BufferedReader;
8import java.io.ByteArrayInputStream;
9import java.io.IOException;
10import java.io.InputStream;
11import java.io.OutputStream;
12import java.net.CookieHandler;
13import java.net.CookieManager;
14import java.net.HttpURLConnection;
15import java.net.URL;
16import java.util.Collections;
17import java.util.List;
18import java.util.Locale;
19import java.util.Map;
20import java.util.Map.Entry;
21import java.util.Optional;
22import java.util.Scanner;
23import java.util.TreeMap;
24import java.util.concurrent.TimeUnit;
25import java.util.regex.Matcher;
26import java.util.regex.Pattern;
27import java.util.zip.GZIPInputStream;
28
29import org.openstreetmap.josm.Main;
30import org.openstreetmap.josm.data.Version;
31import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
32import org.openstreetmap.josm.gui.progress.ProgressMonitor;
33import org.openstreetmap.josm.io.Compression;
34import org.openstreetmap.josm.io.ProgressInputStream;
35import org.openstreetmap.josm.io.ProgressOutputStream;
36import org.openstreetmap.josm.io.UTFInputStreamReader;
37import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
38
39/**
40 * Provides a uniform access for a HTTP/HTTPS server. This class should be used in favour of {@link HttpURLConnection}.
41 * @since 9168
42 */
43public final class HttpClient {
44
45 private URL url;
46 private final String requestMethod;
47 private int connectTimeout = (int) TimeUnit.SECONDS.toMillis(Main.pref.getInt("socket.timeout.connect", 15));
48 private int readTimeout = (int) TimeUnit.SECONDS.toMillis(Main.pref.getInt("socket.timeout.read", 30));
49 private byte[] requestBody;
50 private long ifModifiedSince;
51 private final Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
52 private int maxRedirects = Main.pref.getInt("socket.maxredirects", 5);
53 private boolean useCache;
54 private String reasonForRequest;
55 private String outputMessage = tr("Uploading data ...");
56 private HttpURLConnection connection; // to allow disconnecting before `response` is set
57 private Response response;
58 private boolean finishOnCloseOutput = true;
59
60 static {
61 CookieHandler.setDefault(new CookieManager());
62 }
63
64 private HttpClient(URL url, String requestMethod) {
65 this.url = url;
66 this.requestMethod = requestMethod;
67 this.headers.put("Accept-Encoding", "gzip");
68 }
69
70 /**
71 * Opens the HTTP connection.
72 * @return HTTP response
73 * @throws IOException if any I/O error occurs
74 */
75 public Response connect() throws IOException {
76 return connect(null);
77 }
78
79 /**
80 * Opens the HTTP connection.
81 * @param progressMonitor progress monitor
82 * @return HTTP response
83 * @throws IOException if any I/O error occurs
84 * @since 9179
85 */
86 public Response connect(ProgressMonitor progressMonitor) throws IOException {
87 if (progressMonitor == null) {
88 progressMonitor = NullProgressMonitor.INSTANCE;
89 }
90 final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
91 this.connection = connection;
92 connection.setRequestMethod(requestMethod);
93 connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());
94 connection.setConnectTimeout(connectTimeout);
95 connection.setReadTimeout(readTimeout);
96 connection.setInstanceFollowRedirects(false); // we do that ourselves
97 if (ifModifiedSince > 0) {
98 connection.setIfModifiedSince(ifModifiedSince);
99 }
100 connection.setUseCaches(useCache);
101 if (!useCache) {
102 connection.setRequestProperty("Cache-Control", "no-cache");
103 }
104 for (Map.Entry<String, String> header : headers.entrySet()) {
105 if (header.getValue() != null) {
106 connection.setRequestProperty(header.getKey(), header.getValue());
107 }
108 }
109
110 progressMonitor.beginTask(tr("Contacting Server..."), 1);
111 progressMonitor.indeterminateSubTask(null);
112
113 if ("PUT".equals(requestMethod) || "POST".equals(requestMethod) || "DELETE".equals(requestMethod)) {
114 Logging.info("{0} {1} ({2}) ...", requestMethod, url, Utils.getSizeString(requestBody.length, Locale.getDefault()));
115 connection.setFixedLengthStreamingMode(requestBody.length);
116 connection.setDoOutput(true);
117 try (OutputStream out = new BufferedOutputStream(
118 new ProgressOutputStream(connection.getOutputStream(), requestBody.length,
119 progressMonitor, outputMessage, finishOnCloseOutput))) {
120 out.write(requestBody);
121 }
122 }
123
124 boolean successfulConnection = false;
125 try {
126 try {
127 connection.connect();
128 final boolean hasReason = reasonForRequest != null && !reasonForRequest.isEmpty();
129 Logging.info("{0} {1}{2} -> {3}{4}",
130 requestMethod, url, hasReason ? (" (" + reasonForRequest + ')') : "",
131 connection.getResponseCode(),
132 connection.getContentLengthLong() > 0
133 ? (" (" + Utils.getSizeString(connection.getContentLengthLong(), Locale.getDefault()) + ')')
134 : ""
135 );
136 if (Logging.isDebugEnabled()) {
137 Logging.debug("RESPONSE: {0}", connection.getHeaderFields());
138 }
139 if (DefaultAuthenticator.getInstance().isEnabled() && connection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
140 DefaultAuthenticator.getInstance().addFailedCredentialHost(url.getHost());
141 }
142 } catch (IOException e) {
143 Logging.info("{0} {1} -> !!!", requestMethod, url);
144 Logging.warn(e);
145 //noinspection ThrowableResultOfMethodCallIgnored
146 Main.addNetworkError(url, Utils.getRootCause(e));
147 throw e;
148 }
149 if (isRedirect(connection.getResponseCode())) {
150 final String redirectLocation = connection.getHeaderField("Location");
151 if (redirectLocation == null) {
152 /* I18n: argument is HTTP response code */
153 throw new IOException(tr("Unexpected response from HTTP server. Got {0} response without ''Location'' header." +
154 " Can''t redirect. Aborting.", connection.getResponseCode()));
155 } else if (maxRedirects > 0) {
156 url = new URL(url, redirectLocation);
157 maxRedirects--;
158 Logging.info(tr("Download redirected to ''{0}''", redirectLocation));
159 return connect();
160 } else if (maxRedirects == 0) {
161 String msg = tr("Too many redirects to the download URL detected. Aborting.");
162 throw new IOException(msg);
163 }
164 }
165 response = new Response(connection, progressMonitor);
166 successfulConnection = true;
167 return response;
168 } finally {
169 if (!successfulConnection) {
170 connection.disconnect();
171 }
172 }
173 }
174
175 /**
176 * Returns the HTTP response which is set only after calling {@link #connect()}.
177 * Calling this method again, returns the identical object (unless another {@link #connect()} is performed).
178 *
179 * @return the HTTP response
180 * @since 9309
181 */
182 public Response getResponse() {
183 return response;
184 }
185
186 /**
187 * A wrapper for the HTTP response.
188 */
189 public static final class Response {
190 private final HttpURLConnection connection;
191 private final ProgressMonitor monitor;
192 private final int responseCode;
193 private final String responseMessage;
194 private boolean uncompress;
195 private boolean uncompressAccordingToContentDisposition;
196 private String responseData;
197
198 private Response(HttpURLConnection connection, ProgressMonitor monitor) throws IOException {
199 CheckParameterUtil.ensureParameterNotNull(connection, "connection");
200 CheckParameterUtil.ensureParameterNotNull(monitor, "monitor");
201 this.connection = connection;
202 this.monitor = monitor;
203 this.responseCode = connection.getResponseCode();
204 this.responseMessage = connection.getResponseMessage();
205 if (this.responseCode >= 300) {
206 String contentType = getContentType();
207 if (contentType == null || (
208 contentType.contains("text") ||
209 contentType.contains("html") ||
210 contentType.contains("xml"))
211 ) {
212 String content = this.fetchContent();
213 if (content.isEmpty()) {
214 Logging.debug("Server did not return any body");
215 } else {
216 Logging.debug("Response body: ");
217 Logging.debug(this.fetchContent());
218 }
219 } else {
220 Logging.debug("Server returned content: {0} of length: {1}. Not printing.", contentType, this.getContentLength());
221 }
222 }
223 }
224
225 /**
226 * Sets whether {@link #getContent()} should uncompress the input stream if necessary.
227 *
228 * @param uncompress whether the input stream should be uncompressed if necessary
229 * @return {@code this}
230 */
231 public Response uncompress(boolean uncompress) {
232 this.uncompress = uncompress;
233 return this;
234 }
235
236 /**
237 * Sets whether {@link #getContent()} should uncompress the input stream according to {@code Content-Disposition}
238 * HTTP header.
239 * @param uncompressAccordingToContentDisposition whether the input stream should be uncompressed according to
240 * {@code Content-Disposition}
241 * @return {@code this}
242 * @since 9172
243 */
244 public Response uncompressAccordingToContentDisposition(boolean uncompressAccordingToContentDisposition) {
245 this.uncompressAccordingToContentDisposition = uncompressAccordingToContentDisposition;
246 return this;
247 }
248
249 /**
250 * Returns the URL.
251 * @return the URL
252 * @see HttpURLConnection#getURL()
253 * @since 9172
254 */
255 public URL getURL() {
256 return connection.getURL();
257 }
258
259 /**
260 * Returns the request method.
261 * @return the HTTP request method
262 * @see HttpURLConnection#getRequestMethod()
263 * @since 9172
264 */
265 public String getRequestMethod() {
266 return connection.getRequestMethod();
267 }
268
269 /**
270 * Returns an input stream that reads from this HTTP connection, or,
271 * error stream if the connection failed but the server sent useful data.
272 * <p>
273 * Note: the return value can be null, if both the input and the error stream are null.
274 * Seems to be the case if the OSM server replies a 401 Unauthorized, see #3887
275 * @return input or error stream
276 * @throws IOException if any I/O error occurs
277 *
278 * @see HttpURLConnection#getInputStream()
279 * @see HttpURLConnection#getErrorStream()
280 */
281 @SuppressWarnings("resource")
282 public InputStream getContent() throws IOException {
283 InputStream in;
284 try {
285 in = connection.getInputStream();
286 } catch (IOException ioe) {
287 Logging.debug(ioe);
288 in = Optional.ofNullable(connection.getErrorStream()).orElseGet(() -> new ByteArrayInputStream(new byte[]{}));
289 }
290 in = new ProgressInputStream(in, getContentLength(), monitor);
291 in = "gzip".equalsIgnoreCase(getContentEncoding()) ? new GZIPInputStream(in) : in;
292 Compression compression = Compression.NONE;
293 if (uncompress) {
294 final String contentType = getContentType();
295 Logging.debug("Uncompressing input stream according to Content-Type header: {0}", contentType);
296 compression = Compression.forContentType(contentType);
297 }
298 if (uncompressAccordingToContentDisposition && Compression.NONE.equals(compression)) {
299 final String contentDisposition = getHeaderField("Content-Disposition");
300 final Matcher matcher = Pattern.compile("filename=\"([^\"]+)\"").matcher(
301 contentDisposition != null ? contentDisposition : "");
302 if (matcher.find()) {
303 Logging.debug("Uncompressing input stream according to Content-Disposition header: {0}", contentDisposition);
304 compression = Compression.byExtension(matcher.group(1));
305 }
306 }
307 in = compression.getUncompressedInputStream(in);
308 return in;
309 }
310
311 /**
312 * Returns {@link #getContent()} wrapped in a buffered reader.
313 *
314 * Detects Unicode charset in use utilizing {@link UTFInputStreamReader}.
315 * @return buffered reader
316 * @throws IOException if any I/O error occurs
317 */
318 public BufferedReader getContentReader() throws IOException {
319 return new BufferedReader(
320 UTFInputStreamReader.create(getContent())
321 );
322 }
323
324 /**
325 * Fetches the HTTP response as String.
326 * @return the response
327 * @throws IOException if any I/O error occurs
328 */
329 public synchronized String fetchContent() throws IOException {
330 if (responseData == null) {
331 try (Scanner scanner = new Scanner(getContentReader()).useDelimiter("\\A")) { // \A - beginning of input
332 responseData = scanner.hasNext() ? scanner.next() : "";
333 }
334 }
335 return responseData;
336 }
337
338 /**
339 * Gets the response code from this HTTP connection.
340 * @return HTTP response code
341 *
342 * @see HttpURLConnection#getResponseCode()
343 */
344 public int getResponseCode() {
345 return responseCode;
346 }
347
348 /**
349 * Gets the response message from this HTTP connection.
350 * @return HTTP response message
351 *
352 * @see HttpURLConnection#getResponseMessage()
353 * @since 9172
354 */
355 public String getResponseMessage() {
356 return responseMessage;
357 }
358
359 /**
360 * Returns the {@code Content-Encoding} header.
361 * @return {@code Content-Encoding} HTTP header
362 * @see HttpURLConnection#getContentEncoding()
363 */
364 public String getContentEncoding() {
365 return connection.getContentEncoding();
366 }
367
368 /**
369 * Returns the {@code Content-Type} header.
370 * @return {@code Content-Type} HTTP header
371 */
372 public String getContentType() {
373 return connection.getHeaderField("Content-Type");
374 }
375
376 /**
377 * Returns the {@code Expire} header.
378 * @return {@code Expire} HTTP header
379 * @see HttpURLConnection#getExpiration()
380 * @since 9232
381 */
382 public long getExpiration() {
383 return connection.getExpiration();
384 }
385
386 /**
387 * Returns the {@code Last-Modified} header.
388 * @return {@code Last-Modified} HTTP header
389 * @see HttpURLConnection#getLastModified()
390 * @since 9232
391 */
392 public long getLastModified() {
393 return connection.getLastModified();
394 }
395
396 /**
397 * Returns the {@code Content-Length} header.
398 * @return {@code Content-Length} HTTP header
399 * @see HttpURLConnection#getContentLengthLong()
400 */
401 public long getContentLength() {
402 return connection.getContentLengthLong();
403 }
404
405 /**
406 * Returns the value of the named header field.
407 * @param name the name of a header field
408 * @return the value of the named header field, or {@code null} if there is no such field in the header
409 * @see HttpURLConnection#getHeaderField(String)
410 * @since 9172
411 */
412 public String getHeaderField(String name) {
413 return connection.getHeaderField(name);
414 }
415
416 /**
417 * Returns an unmodifiable Map mapping header keys to a List of header values.
418 * As per RFC 2616, section 4.2 header names are case insensitive, so returned map is also case insensitive
419 * @return unmodifiable Map mapping header keys to a List of header values
420 * @see HttpURLConnection#getHeaderFields()
421 * @since 9232
422 */
423 public Map<String, List<String>> getHeaderFields() {
424 // returned map from HttpUrlConnection is case sensitive, use case insensitive TreeMap to conform to RFC 2616
425 Map<String, List<String>> ret = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
426 for (Entry<String, List<String>> e: connection.getHeaderFields().entrySet()) {
427 if (e.getKey() != null) {
428 ret.put(e.getKey(), e.getValue());
429 }
430 }
431 return Collections.unmodifiableMap(ret);
432 }
433
434 /**
435 * @see HttpURLConnection#disconnect()
436 */
437 public void disconnect() {
438 HttpClient.disconnect(connection);
439 }
440 }
441
442 /**
443 * Creates a new instance for the given URL and a {@code GET} request
444 *
445 * @param url the URL
446 * @return a new instance
447 */
448 public static HttpClient create(URL url) {
449 return create(url, "GET");
450 }
451
452 /**
453 * Creates a new instance for the given URL and a {@code GET} request
454 *
455 * @param url the URL
456 * @param requestMethod the HTTP request method to perform when calling
457 * @return a new instance
458 */
459 public static HttpClient create(URL url, String requestMethod) {
460 return new HttpClient(url, requestMethod);
461 }
462
463 /**
464 * Returns the URL set for this connection.
465 * @return the URL
466 * @see #create(URL)
467 * @see #create(URL, String)
468 * @since 9172
469 */
470 public URL getURL() {
471 return url;
472 }
473
474 /**
475 * Returns the request method set for this connection.
476 * @return the HTTP request method
477 * @see #create(URL, String)
478 * @since 9172
479 */
480 public String getRequestMethod() {
481 return requestMethod;
482 }
483
484 /**
485 * Returns the set value for the given {@code header}.
486 * @param header HTTP header name
487 * @return HTTP header value
488 * @since 9172
489 */
490 public String getRequestHeader(String header) {
491 return headers.get(header);
492 }
493
494 /**
495 * Sets whether not to set header {@code Cache-Control=no-cache}
496 *
497 * @param useCache whether not to set header {@code Cache-Control=no-cache}
498 * @return {@code this}
499 * @see HttpURLConnection#setUseCaches(boolean)
500 */
501 public HttpClient useCache(boolean useCache) {
502 this.useCache = useCache;
503 return this;
504 }
505
506 /**
507 * Sets whether not to set header {@code Connection=close}
508 * <p>
509 * This might fix #7640, see
510 * <a href='https://web.archive.org/web/20140118201501/http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive'>here</a>.
511 *
512 * @param keepAlive whether not to set header {@code Connection=close}
513 * @return {@code this}
514 */
515 public HttpClient keepAlive(boolean keepAlive) {
516 return setHeader("Connection", keepAlive ? null : "close");
517 }
518
519 /**
520 * Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced
521 * by this URLConnection. If the timeout expires before the connection can be established, a
522 * {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite timeout.
523 * @param connectTimeout an {@code int} that specifies the connect timeout value in milliseconds
524 * @return {@code this}
525 * @see HttpURLConnection#setConnectTimeout(int)
526 */
527 public HttpClient setConnectTimeout(int connectTimeout) {
528 this.connectTimeout = connectTimeout;
529 return this;
530 }
531
532 /**
533 * Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from
534 * input stream when a connection is established to a resource. If the timeout expires before there is data available for
535 * read, a {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite timeout.
536 * @param readTimeout an {@code int} that specifies the read timeout value in milliseconds
537 * @return {@code this}
538 * @see HttpURLConnection#setReadTimeout(int)
539 */
540 public HttpClient setReadTimeout(int readTimeout) {
541 this.readTimeout = readTimeout;
542 return this;
543 }
544
545 /**
546 * Sets the {@code Accept} header.
547 * @param accept header value
548 *
549 * @return {@code this}
550 */
551 public HttpClient setAccept(String accept) {
552 return setHeader("Accept", accept);
553 }
554
555 /**
556 * Sets the request body for {@code PUT}/{@code POST} requests.
557 * @param requestBody request body
558 *
559 * @return {@code this}
560 */
561 public HttpClient setRequestBody(byte[] requestBody) {
562 this.requestBody = Utils.copyArray(requestBody);
563 return this;
564 }
565
566 /**
567 * Sets the {@code If-Modified-Since} header.
568 * @param ifModifiedSince header value
569 *
570 * @return {@code this}
571 */
572 public HttpClient setIfModifiedSince(long ifModifiedSince) {
573 this.ifModifiedSince = ifModifiedSince;
574 return this;
575 }
576
577 /**
578 * Sets the maximum number of redirections to follow.
579 *
580 * Set {@code maxRedirects} to {@code -1} in order to ignore redirects, i.e.,
581 * to not throw an {@link IOException} in {@link #connect()}.
582 * @param maxRedirects header value
583 *
584 * @return {@code this}
585 */
586 public HttpClient setMaxRedirects(int maxRedirects) {
587 this.maxRedirects = maxRedirects;
588 return this;
589 }
590
591 /**
592 * Sets an arbitrary HTTP header.
593 * @param key header name
594 * @param value header value
595 *
596 * @return {@code this}
597 */
598 public HttpClient setHeader(String key, String value) {
599 this.headers.put(key, value);
600 return this;
601 }
602
603 /**
604 * Sets arbitrary HTTP headers.
605 * @param headers HTTP headers
606 *
607 * @return {@code this}
608 */
609 public HttpClient setHeaders(Map<String, String> headers) {
610 this.headers.putAll(headers);
611 return this;
612 }
613
614 /**
615 * Sets a reason to show on console. Can be {@code null} if no reason is given.
616 * @param reasonForRequest Reason to show
617 * @return {@code this}
618 * @since 9172
619 */
620 public HttpClient setReasonForRequest(String reasonForRequest) {
621 this.reasonForRequest = reasonForRequest;
622 return this;
623 }
624
625 /**
626 * Sets the output message to be displayed in progress monitor for {@code PUT}, {@code POST} and {@code DELETE} methods.
627 * Defaults to "Uploading data ..." (translated). Has no effect for {@code GET} or any other method.
628 * @param outputMessage message to be displayed in progress monitor
629 * @return {@code this}
630 * @since 12711
631 */
632 public HttpClient setOutputMessage(String outputMessage) {
633 this.outputMessage = outputMessage;
634 return this;
635 }
636
637 /**
638 * Sets whether the progress monitor task will be finished when the output stream is closed. This is {@code true} by default.
639 * @param finishOnCloseOutput whether the progress monitor task will be finished when the output stream is closed
640 * @return {@code this}
641 * @since 10302
642 */
643 public HttpClient setFinishOnCloseOutput(boolean finishOnCloseOutput) {
644 this.finishOnCloseOutput = finishOnCloseOutput;
645 return this;
646 }
647
648 private static boolean isRedirect(final int statusCode) {
649 switch (statusCode) {
650 case HttpURLConnection.HTTP_MOVED_PERM: // 301
651 case HttpURLConnection.HTTP_MOVED_TEMP: // 302
652 case HttpURLConnection.HTTP_SEE_OTHER: // 303
653 case 307: // TEMPORARY_REDIRECT:
654 case 308: // PERMANENT_REDIRECT:
655 return true;
656 default:
657 return false;
658 }
659 }
660
661 /**
662 * @see HttpURLConnection#disconnect()
663 * @since 9309
664 */
665 public void disconnect() {
666 HttpClient.disconnect(connection);
667 }
668
669 private static void disconnect(final HttpURLConnection connection) {
670 if (connection != null) {
671 // Fix upload aborts - see #263
672 connection.setConnectTimeout(100);
673 connection.setReadTimeout(100);
674 try {
675 Thread.sleep(100);
676 } catch (InterruptedException ex) {
677 Logging.warn("InterruptedException in " + HttpClient.class + " during cancel");
678 Thread.currentThread().interrupt();
679 }
680 connection.disconnect();
681 }
682 }
683}
Note: See TracBrowser for help on using the repository browser.