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

Last change on this file since 11283 was 11277, checked in by Don-vip, 7 years ago

sonar - fix recently added warnings

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