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

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

see #12231 - Fix logging of reasonForRequest

File size: 21.3 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 && !"".equalsIgnoreCase(reasonForRequest);
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 public InputStream getContent() throws IOException {
234 InputStream in;
235 try {
236 in = connection.getInputStream();
237 } catch (IOException ioe) {
238 in = connection.getErrorStream();
239 }
240 in = new ProgressInputStream(in, getContentLength(), monitor);
241 in = "gzip".equalsIgnoreCase(getContentEncoding()) ? new GZIPInputStream(in) : in;
242 Compression compression = Compression.NONE;
243 if (uncompress) {
244 final String contentType = getContentType();
245 Main.debug("Uncompressing input stream according to Content-Type header: {0}", contentType);
246 compression = Compression.forContentType(contentType);
247 }
248 if (uncompressAccordingToContentDisposition && Compression.NONE.equals(compression)) {
249 final String contentDisposition = getHeaderField("Content-Disposition");
250 final Matcher matcher = Pattern.compile("filename=\"([^\"]+)\"").matcher(contentDisposition);
251 if (matcher.find()) {
252 Main.debug("Uncompressing input stream according to Content-Disposition header: {0}", contentDisposition);
253 compression = Compression.byExtension(matcher.group(1));
254 }
255 }
256 in = compression.getUncompressedInputStream(in);
257 return in;
258 }
259
260 /**
261 * Returns {@link #getContent()} wrapped in a buffered reader.
262 *
263 * Detects Unicode charset in use utilizing {@link UTFInputStreamReader}.
264 * @return buffered reader
265 * @throws IOException if any I/O error occurs
266 */
267 public BufferedReader getContentReader() throws IOException {
268 return new BufferedReader(
269 UTFInputStreamReader.create(getContent())
270 );
271 }
272
273 /**
274 * Fetches the HTTP response as String.
275 * @return the response
276 * @throws IOException if any I/O error occurs
277 */
278 @SuppressWarnings("resource")
279 public String fetchContent() throws IOException {
280 try (Scanner scanner = new Scanner(getContentReader()).useDelimiter("\\A")) {
281 return scanner.hasNext() ? scanner.next() : "";
282 }
283 }
284
285 /**
286 * Gets the response code from this HTTP connection.
287 * @return HTTP response code
288 *
289 * @see HttpURLConnection#getResponseCode()
290 */
291 public int getResponseCode() {
292 return responseCode;
293 }
294
295 /**
296 * Gets the response message from this HTTP connection.
297 * @return HTTP response message
298 *
299 * @see HttpURLConnection#getResponseMessage()
300 * @since 9172
301 */
302 public String getResponseMessage() {
303 return responseMessage;
304 }
305
306 /**
307 * Returns the {@code Content-Encoding} header.
308 * @return {@code Content-Encoding} HTTP header
309 */
310 public String getContentEncoding() {
311 return connection.getContentEncoding();
312 }
313
314 /**
315 * Returns the {@code Content-Type} header.
316 * @return {@code Content-Type} HTTP header
317 */
318 public String getContentType() {
319 return connection.getHeaderField("Content-Type");
320 }
321
322 /**
323 * Returns the {@code Content-Length} header.
324 * @return {@code Content-Length} HTTP header
325 */
326 public long getContentLength() {
327 return connection.getContentLengthLong();
328 }
329
330 /**
331 * Returns the value of the named header field.
332 * @param name the name of a header field
333 * @return the value of the named header field, or {@code null} if there is no such field in the header
334 * @see HttpURLConnection#getHeaderField(String)
335 * @since 9172
336 */
337 public String getHeaderField(String name) {
338 return connection.getHeaderField(name);
339 }
340
341 /**
342 * Returns the list of Strings that represents the named header field values.
343 * @param name the name of a header field
344 * @return unmodifiable List of Strings that represents the corresponding field values
345 * @see HttpURLConnection#getHeaderFields()
346 * @since 9172
347 */
348 public List<String> getHeaderFields(String name) {
349 return connection.getHeaderFields().get(name);
350 }
351
352 /**
353 * @see HttpURLConnection#disconnect()
354 */
355 public void disconnect() {
356 // TODO is this block necessary for disconnecting?
357 // Fix upload aborts - see #263
358 connection.setConnectTimeout(100);
359 connection.setReadTimeout(100);
360 try {
361 Thread.sleep(100);
362 } catch (InterruptedException ex) {
363 Main.warn("InterruptedException in " + getClass().getSimpleName() + " during cancel");
364 }
365
366 connection.disconnect();
367 }
368 }
369
370 /**
371 * Creates a new instance for the given URL and a {@code GET} request
372 *
373 * @param url the URL
374 * @return a new instance
375 */
376 public static HttpClient create(URL url) {
377 return create(url, "GET");
378 }
379
380 /**
381 * Creates a new instance for the given URL and a {@code GET} request
382 *
383 * @param url the URL
384 * @param requestMethod the HTTP request method to perform when calling
385 * @return a new instance
386 */
387 public static HttpClient create(URL url, String requestMethod) {
388 return new HttpClient(url, requestMethod);
389 }
390
391 /**
392 * Returns the URL set for this connection.
393 * @return the URL
394 * @see #create(URL)
395 * @see #create(URL, String)
396 * @since 9172
397 */
398 public URL getURL() {
399 return url;
400 }
401
402 /**
403 * Returns the request method set for this connection.
404 * @return the HTTP request method
405 * @see #create(URL, String)
406 * @since 9172
407 */
408 public String getRequestMethod() {
409 return requestMethod;
410 }
411
412 /**
413 * Returns the set value for the given {@code header}.
414 * @param header HTTP header name
415 * @return HTTP header value
416 * @since 9172
417 */
418 public String getRequestHeader(String header) {
419 return headers.get(header);
420 }
421
422 /**
423 * Sets whether not to set header {@code Cache-Control=no-cache}
424 *
425 * @param useCache whether not to set header {@code Cache-Control=no-cache}
426 * @return {@code this}
427 * @see HttpURLConnection#setUseCaches(boolean)
428 */
429 public HttpClient useCache(boolean useCache) {
430 this.useCache = useCache;
431 return this;
432 }
433
434 /**
435 * Sets whether not to set header {@code Connection=close}
436 * <p/>
437 * This might fix #7640, see
438 * <a href='https://web.archive.org/web/20140118201501/http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive'>here</a>.
439 *
440 * @param keepAlive whether not to set header {@code Connection=close}
441 * @return {@code this}
442 */
443 public HttpClient keepAlive(boolean keepAlive) {
444 return setHeader("Connection", keepAlive ? null : "close");
445 }
446
447 /**
448 * Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced
449 * by this URLConnection. If the timeout expires before the connection can be established, a
450 * {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite timeout.
451 * @param connectTimeout an {@code int} that specifies the connect timeout value in milliseconds
452 * @return {@code this}
453 * @see HttpURLConnection#setConnectTimeout(int)
454 */
455 public HttpClient setConnectTimeout(int connectTimeout) {
456 this.connectTimeout = connectTimeout;
457 return this;
458 }
459
460 /**
461 * Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from
462 * input stream when a connection is established to a resource. If the timeout expires before there is data available for
463 * read, a {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite timeout.
464 * @param readTimeout an {@code int} that specifies the read timeout value in milliseconds
465 * @return {@code this}
466 * @see HttpURLConnection#setReadTimeout(int)
467 */
468 public HttpClient setReadTimeout(int readTimeout) {
469 this.readTimeout = readTimeout;
470 return this;
471 }
472
473 /**
474 * This method is used to enable streaming of a HTTP request body without internal buffering,
475 * when the content length is known in advance.
476 * <p>
477 * An exception will be thrown if the application attempts to write more data than the indicated content-length,
478 * or if the application closes the OutputStream before writing the indicated amount.
479 * <p>
480 * When output streaming is enabled, authentication and redirection cannot be handled automatically.
481 * A {@linkplain HttpRetryException} will be thrown when reading the response if authentication or redirection
482 * are required. This exception can be queried for the details of the error.
483 *
484 * @param contentLength The number of bytes which will be written to the OutputStream
485 * @return {@code this}
486 * @see HttpURLConnection#setFixedLengthStreamingMode(long)
487 * @since 9178
488 */
489 public HttpClient setFixedLengthStreamingMode(long contentLength) {
490 this.contentLength = contentLength;
491 return this;
492 }
493
494 /**
495 * Sets the {@code Accept} header.
496 * @param accept header value
497 *
498 * @return {@code this}
499 */
500 public HttpClient setAccept(String accept) {
501 return setHeader("Accept", accept);
502 }
503
504 /**
505 * Sets the request body for {@code PUT}/{@code POST} requests.
506 * @param requestBody request body
507 *
508 * @return {@code this}
509 */
510 public HttpClient setRequestBody(byte[] requestBody) {
511 this.requestBody = requestBody;
512 return this;
513 }
514
515 /**
516 * Sets the {@code If-Modified-Since} header.
517 * @param ifModifiedSince header value
518 *
519 * @return {@code this}
520 */
521 public HttpClient setIfModifiedSince(long ifModifiedSince) {
522 this.ifModifiedSince = ifModifiedSince;
523 return this;
524 }
525
526 /**
527 * Sets the maximum number of redirections to follow.
528 *
529 * Set {@code maxRedirects} to {@code -1} in order to ignore redirects, i.e.,
530 * to not throw an {@link IOException} in {@link #connect()}.
531 * @param maxRedirects header value
532 *
533 * @return {@code this}
534 */
535 public HttpClient setMaxRedirects(int maxRedirects) {
536 this.maxRedirects = maxRedirects;
537 return this;
538 }
539
540 /**
541 * Sets an arbitrary HTTP header.
542 * @param key header name
543 * @param value header value
544 *
545 * @return {@code this}
546 */
547 public HttpClient setHeader(String key, String value) {
548 this.headers.put(key, value);
549 return this;
550 }
551
552 /**
553 * Sets arbitrary HTTP headers.
554 * @param headers HTTP headers
555 *
556 * @return {@code this}
557 */
558 public HttpClient setHeaders(Map<String, String> headers) {
559 this.headers.putAll(headers);
560 return this;
561 }
562
563 /**
564 * Sets a reason to show on console. Can be {@code null} if no reason is given.
565 * @param reasonForRequest Reason to show
566 * @return {@code this}
567 * @since 9172
568 */
569 public HttpClient setReasonForRequest(String reasonForRequest) {
570 this.reasonForRequest = reasonForRequest;
571 return this;
572 }
573
574 private static boolean isRedirect(final int statusCode) {
575 switch (statusCode) {
576 case HttpURLConnection.HTTP_MOVED_PERM: // 301
577 case HttpURLConnection.HTTP_MOVED_TEMP: // 302
578 case HttpURLConnection.HTTP_SEE_OTHER: // 303
579 case 307: // TEMPORARY_REDIRECT:
580 case 308: // PERMANENT_REDIRECT:
581 return true;
582 default:
583 return false;
584 }
585 }
586}
Note: See TracBrowser for help on using the repository browser.