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

Last change on this file since 9870 was 9810, checked in by Don-vip, 8 years ago

checkstyle

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