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

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

OAuth: add robustness, basic unit test, code cleanup

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