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

Last change on this file since 9232 was 9232, checked in by simon04, 8 years ago

fix #12265 - Use HttpClient for imagery requests

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