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

Last change on this file since 17076 was 17076, checked in by Don-vip, 4 years ago

fix #19860 - prevent illegal state to raise an NPE at JOSM startup on macOS

File size: 31.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.BufferedReader;
7import java.io.IOException;
8import java.io.InputStream;
9import java.net.CookieHandler;
10import java.net.CookieManager;
11import java.net.HttpURLConnection;
12import java.net.MalformedURLException;
13import java.net.URL;
14import java.nio.charset.StandardCharsets;
15import java.util.List;
16import java.util.Locale;
17import java.util.Map;
18import java.util.Objects;
19import java.util.Scanner;
20import java.util.TreeMap;
21import java.util.concurrent.TimeUnit;
22import java.util.regex.Matcher;
23import java.util.regex.Pattern;
24import java.util.zip.GZIPInputStream;
25import java.util.zip.InflaterInputStream;
26
27import org.openstreetmap.josm.data.validation.routines.DomainValidator;
28import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
29import org.openstreetmap.josm.gui.progress.ProgressMonitor;
30import org.openstreetmap.josm.io.Compression;
31import org.openstreetmap.josm.io.NetworkManager;
32import org.openstreetmap.josm.io.ProgressInputStream;
33import org.openstreetmap.josm.io.UTFInputStreamReader;
34import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
35import org.openstreetmap.josm.spi.preferences.Config;
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 abstract class HttpClient {
42
43 /**
44 * HTTP client factory.
45 * @since 15229
46 */
47 @FunctionalInterface
48 public interface HttpClientFactory {
49 /**
50 * Creates a new instance for the given URL and a {@code GET} request
51 *
52 * @param url the URL
53 * @param requestMethod the HTTP request method to perform when calling
54 * @return a new instance
55 */
56 HttpClient create(URL url, String requestMethod);
57 }
58
59 private URL url;
60 private final String requestMethod;
61 private int connectTimeout = (int) TimeUnit.SECONDS.toMillis(Config.getPref().getInt("socket.timeout.connect", 15));
62 private int readTimeout = (int) TimeUnit.SECONDS.toMillis(Config.getPref().getInt("socket.timeout.read", 30));
63 private byte[] requestBody;
64 private long ifModifiedSince;
65 private final Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
66 private int maxRedirects = Config.getPref().getInt("socket.maxredirects", 5);
67 private boolean useCache = true;
68 private String reasonForRequest;
69 private String outputMessage = tr("Uploading data ...");
70 private Response response;
71 private boolean finishOnCloseOutput = true;
72 private boolean debug;
73
74 // Pattern to detect Tomcat error message. Be careful with change of format:
75 // CHECKSTYLE.OFF: LineLength
76 // https://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/ErrorReportValve.java?r1=1740707&r2=1779641&pathrev=1779641&diff_format=h
77 // CHECKSTYLE.ON: LineLength
78 private static final Pattern TOMCAT_ERR_MESSAGE = Pattern.compile(
79 ".*<p><b>[^<]+</b>[^<]+</p><p><b>[^<]+</b> (?:<u>)?([^<]*)(?:</u>)?</p><p><b>[^<]+</b> (?:<u>)?[^<]*(?:</u>)?</p>.*",
80 Pattern.CASE_INSENSITIVE);
81
82 private static HttpClientFactory factory;
83
84 static {
85 try {
86 CookieHandler.setDefault(new CookieManager());
87 } catch (SecurityException e) {
88 Logging.log(Logging.LEVEL_ERROR, "Unable to set default cookie handler", e);
89 }
90 }
91
92 /**
93 * Registers a new HTTP client factory.
94 * @param newFactory new HTTP client factory
95 * @since 15229
96 */
97 public static void setFactory(HttpClientFactory newFactory) {
98 factory = Objects.requireNonNull(newFactory);
99 }
100
101 /**
102 * Constructs a new {@code HttpClient}.
103 * @param url URL to access
104 * @param requestMethod HTTP request method (GET, POST, PUT, DELETE...)
105 */
106 protected HttpClient(URL url, String requestMethod) {
107 try {
108 String host = url.getHost();
109 String asciiHost = DomainValidator.unicodeToASCII(host);
110 this.url = asciiHost.equals(host) ? url : new URL(url.getProtocol(), asciiHost, url.getPort(), url.getFile());
111 } catch (MalformedURLException e) {
112 throw new JosmRuntimeException(e);
113 }
114 this.requestMethod = requestMethod;
115 this.headers.put("Accept-Encoding", "gzip, deflate");
116 }
117
118 /**
119 * Opens the HTTP connection.
120 * @return HTTP response
121 * @throws IOException if any I/O error occurs
122 */
123 public final Response connect() throws IOException {
124 return connect(null);
125 }
126
127 /**
128 * Opens the HTTP connection.
129 * @param progressMonitor progress monitor
130 * @return HTTP response
131 * @throws IOException if any I/O error occurs
132 * @since 9179
133 */
134 public final Response connect(ProgressMonitor progressMonitor) throws IOException {
135 if (progressMonitor == null) {
136 progressMonitor = NullProgressMonitor.INSTANCE;
137 }
138 setupConnection(progressMonitor);
139
140 boolean successfulConnection = false;
141 try {
142 Stopwatch stopwatch = Stopwatch.createStarted();
143 ConnectionResponse cr;
144 try {
145 if (Logging.isDebugEnabled()) {
146 Logging.debug("REQUEST HEADERS: {0}", headers);
147 }
148 cr = performConnection();
149 final boolean hasReason = reasonForRequest != null && !reasonForRequest.isEmpty();
150 logRequest("{0} {1}{2} -> {3} {4} ({5}{6})",
151 getRequestMethod(), getURL(), hasReason ? (" (" + reasonForRequest + ')') : "",
152 cr.getResponseVersion(), cr.getResponseCode(),
153 stopwatch,
154 cr.getContentLengthLong() > 0
155 ? ("; " + Utils.getSizeString(cr.getContentLengthLong(), Locale.getDefault()))
156 : ""
157 );
158 if (Logging.isDebugEnabled()) {
159 try {
160 Logging.debug("RESPONSE HEADERS: {0}", cr.getHeaderFields());
161 } catch (IllegalArgumentException e) {
162 Logging.warn(e);
163 }
164 }
165 if (DefaultAuthenticator.getInstance().isEnabled() && cr.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
166 DefaultAuthenticator.getInstance().addFailedCredentialHost(url.getHost());
167 }
168 } catch (IOException | RuntimeException e) {
169 logRequest("{0} {1} -> !!! ({2})", requestMethod, url, stopwatch);
170 Logging.warn(e);
171 //noinspection ThrowableResultOfMethodCallIgnored
172 NetworkManager.addNetworkError(url, Utils.getRootCause(e));
173 throw e;
174 }
175 if (isRedirect(cr.getResponseCode())) {
176 final String redirectLocation = cr.getHeaderField("Location");
177 if (redirectLocation == null) {
178 /* I18n: argument is HTTP response code */
179 throw new IOException(tr("Unexpected response from HTTP server. Got {0} response without ''Location'' header." +
180 " Can''t redirect. Aborting.", cr.getResponseCode()));
181 } else if (maxRedirects > 0) {
182 url = new URL(url, redirectLocation);
183 maxRedirects--;
184 logRequest(tr("Download redirected to ''{0}''", redirectLocation));
185 response = connect();
186 successfulConnection = true;
187 return response;
188 } else if (maxRedirects == 0) {
189 String msg = tr("Too many redirects to the download URL detected. Aborting.");
190 throw new IOException(msg);
191 }
192 }
193 response = buildResponse(progressMonitor);
194 successfulConnection = true;
195 return response;
196 } finally {
197 if (!successfulConnection) {
198 performDisconnection();
199 progressMonitor.finishTask();
200 }
201 }
202 }
203
204 protected abstract void setupConnection(ProgressMonitor progressMonitor) throws IOException;
205
206 protected abstract ConnectionResponse performConnection() throws IOException;
207
208 protected abstract void performDisconnection() throws IOException;
209
210 protected abstract Response buildResponse(ProgressMonitor progressMonitor) throws IOException;
211
212 protected final void notifyConnect(ProgressMonitor progressMonitor) {
213 progressMonitor.beginTask(tr("Contacting Server..."), 1);
214 progressMonitor.indeterminateSubTask(null);
215 }
216
217 protected final void logRequest(String pattern, Object... args) {
218 if (debug) {
219 Logging.debug(pattern, args);
220 } else {
221 Logging.info(pattern, args);
222 }
223 }
224
225 protected final void logRequestBody() {
226 logRequest("{0} {1} ({2}) ...", requestMethod, url, Utils.getSizeString(requestBody.length, Locale.getDefault()));
227 if (Logging.isTraceEnabled() && hasRequestBody()) {
228 Logging.trace("BODY: {0}", new String(requestBody, StandardCharsets.UTF_8));
229 }
230 }
231
232 /**
233 * Returns the HTTP response which is set only after calling {@link #connect()}.
234 * Calling this method again, returns the identical object (unless another {@link #connect()} is performed).
235 *
236 * @return the HTTP response
237 * @since 9309
238 */
239 public final Response getResponse() {
240 return response;
241 }
242
243 /**
244 * A wrapper for the HTTP connection response.
245 * @since 15229
246 */
247 public interface ConnectionResponse {
248 /**
249 * Gets the HTTP version from the HTTP response.
250 * @return the HTTP version from the HTTP response
251 */
252 String getResponseVersion();
253
254 /**
255 * Gets the status code from an HTTP response message.
256 * For example, in the case of the following status lines:
257 * <PRE>
258 * HTTP/1.0 200 OK
259 * HTTP/1.0 401 Unauthorized
260 * </PRE>
261 * It will return 200 and 401 respectively.
262 * Returns -1 if no code can be discerned
263 * from the response (i.e., the response is not valid HTTP).
264 * @return the HTTP Status-Code, or -1
265 * @throws IOException if an error occurred connecting to the server.
266 */
267 int getResponseCode() throws IOException;
268
269 /**
270 * Returns the value of the {@code content-length} header field as a long.
271 *
272 * @return the content length of the resource that this connection's URL
273 * references, or {@code -1} if the content length is not known.
274 */
275 long getContentLengthLong();
276
277 /**
278 * Returns an unmodifiable Map of the header fields.
279 * The Map keys are Strings that represent the response-header field names.
280 * Each Map value is an unmodifiable List of Strings that represents
281 * the corresponding field values.
282 *
283 * @return a Map of header fields
284 */
285 Map<String, List<String>> getHeaderFields();
286
287 /**
288 * Returns the value of the named header field.
289 * @param name the name of a header field.
290 * @return the value of the named header field, or {@code null}
291 * if there is no such field in the header.
292 */
293 String getHeaderField(String name);
294 }
295
296 /**
297 * A wrapper for the HTTP response.
298 */
299 public abstract static class Response {
300 private final ProgressMonitor monitor;
301 private final int responseCode;
302 private final String responseMessage;
303 private boolean uncompress;
304 private boolean uncompressAccordingToContentDisposition;
305 private String responseData;
306
307 protected Response(ProgressMonitor monitor, int responseCode, String responseMessage) {
308 this.monitor = Objects.requireNonNull(monitor, "monitor");
309 this.responseCode = responseCode;
310 this.responseMessage = responseMessage;
311 }
312
313 protected final void debugRedirect() throws IOException {
314 if (responseCode >= 300) {
315 String contentType = getContentType();
316 if (contentType == null ||
317 contentType.contains("text") ||
318 contentType.contains("html") ||
319 contentType.contains("xml")
320 ) {
321 String content = fetchContent();
322 Logging.debug(content.isEmpty() ? "Server did not return any body" : "Response body: \n" + content);
323 } else {
324 Logging.debug("Server returned content: {0} of length: {1}. Not printing.", contentType, getContentLength());
325 }
326 }
327 }
328
329 /**
330 * Sets whether {@link #getContent()} should uncompress the input stream if necessary.
331 *
332 * @param uncompress whether the input stream should be uncompressed if necessary
333 * @return {@code this}
334 */
335 public final Response uncompress(boolean uncompress) {
336 this.uncompress = uncompress;
337 return this;
338 }
339
340 /**
341 * Sets whether {@link #getContent()} should uncompress the input stream according to {@code Content-Disposition}
342 * HTTP header.
343 * @param uncompressAccordingToContentDisposition whether the input stream should be uncompressed according to
344 * {@code Content-Disposition}
345 * @return {@code this}
346 * @since 9172
347 */
348 public final Response uncompressAccordingToContentDisposition(boolean uncompressAccordingToContentDisposition) {
349 this.uncompressAccordingToContentDisposition = uncompressAccordingToContentDisposition;
350 return this;
351 }
352
353 /**
354 * Returns the URL.
355 * @return the URL
356 * @see HttpURLConnection#getURL()
357 * @since 9172
358 */
359 public abstract URL getURL();
360
361 /**
362 * Returns the request method.
363 * @return the HTTP request method
364 * @see HttpURLConnection#getRequestMethod()
365 * @since 9172
366 */
367 public abstract String getRequestMethod();
368
369 /**
370 * Returns an input stream that reads from this HTTP connection, or,
371 * error stream if the connection failed but the server sent useful data.
372 * <p>
373 * Note: the return value can be null, if both the input and the error stream are null.
374 * Seems to be the case if the OSM server replies a 401 Unauthorized, see #3887
375 * @return input or error stream
376 * @throws IOException if any I/O error occurs
377 *
378 * @see HttpURLConnection#getInputStream()
379 * @see HttpURLConnection#getErrorStream()
380 */
381 @SuppressWarnings("resource")
382 public final InputStream getContent() throws IOException {
383 InputStream in = getInputStream();
384 in = new ProgressInputStream(in, getContentLength(), monitor);
385 in = "gzip".equalsIgnoreCase(getContentEncoding())
386 ? new GZIPInputStream(in)
387 : "deflate".equalsIgnoreCase(getContentEncoding())
388 ? new InflaterInputStream(in)
389 : in;
390 Compression compression = Compression.NONE;
391 if (uncompress) {
392 final String contentType = getContentType();
393 Logging.debug("Uncompressing input stream according to Content-Type header: {0}", contentType);
394 compression = Compression.forContentType(contentType);
395 }
396 if (uncompressAccordingToContentDisposition && Compression.NONE == compression) {
397 final String contentDisposition = getHeaderField("Content-Disposition");
398 final Matcher matcher = Pattern.compile("filename=\"([^\"]+)\"").matcher(
399 contentDisposition != null ? contentDisposition : "");
400 if (matcher.find()) {
401 Logging.debug("Uncompressing input stream according to Content-Disposition header: {0}", contentDisposition);
402 compression = Compression.byExtension(matcher.group(1));
403 }
404 }
405 in = compression.getUncompressedInputStream(in);
406 return in;
407 }
408
409 protected abstract InputStream getInputStream() throws IOException;
410
411 /**
412 * Returns {@link #getContent()} wrapped in a buffered reader.
413 *
414 * Detects Unicode charset in use utilizing {@link UTFInputStreamReader}.
415 * @return buffered reader
416 * @throws IOException if any I/O error occurs
417 */
418 public final BufferedReader getContentReader() throws IOException {
419 return new BufferedReader(
420 UTFInputStreamReader.create(getContent())
421 );
422 }
423
424 /**
425 * Fetches the HTTP response as String.
426 * @return the response
427 * @throws IOException if any I/O error occurs
428 */
429 public final synchronized String fetchContent() throws IOException {
430 if (responseData == null) {
431 try (Scanner scanner = new Scanner(getContentReader()).useDelimiter("\\A")) { // \A - beginning of input
432 responseData = scanner.hasNext() ? scanner.next() : "";
433 }
434 }
435 return responseData;
436 }
437
438 /**
439 * Gets the response code from this HTTP connection.
440 * @return HTTP response code
441 *
442 * @see HttpURLConnection#getResponseCode()
443 */
444 public final int getResponseCode() {
445 return responseCode;
446 }
447
448 /**
449 * Gets the response message from this HTTP connection.
450 * @return HTTP response message
451 *
452 * @see HttpURLConnection#getResponseMessage()
453 * @since 9172
454 */
455 public final String getResponseMessage() {
456 return responseMessage;
457 }
458
459 /**
460 * Returns the {@code Content-Encoding} header.
461 * @return {@code Content-Encoding} HTTP header
462 * @see HttpURLConnection#getContentEncoding()
463 */
464 public abstract String getContentEncoding();
465
466 /**
467 * Returns the {@code Content-Type} header.
468 * @return {@code Content-Type} HTTP header
469 * @see HttpURLConnection#getContentType()
470 */
471 public abstract String getContentType();
472
473 /**
474 * Returns the {@code Expire} header.
475 * @return {@code Expire} HTTP header
476 * @see HttpURLConnection#getExpiration()
477 * @since 9232
478 */
479 public abstract long getExpiration();
480
481 /**
482 * Returns the {@code Last-Modified} header.
483 * @return {@code Last-Modified} HTTP header
484 * @see HttpURLConnection#getLastModified()
485 * @since 9232
486 */
487 public abstract long getLastModified();
488
489 /**
490 * Returns the {@code Content-Length} header.
491 * @return {@code Content-Length} HTTP header
492 * @see HttpURLConnection#getContentLengthLong()
493 */
494 public abstract long getContentLength();
495
496 /**
497 * Returns the value of the named header field.
498 * @param name the name of a header field
499 * @return the value of the named header field, or {@code null} if there is no such field in the header
500 * @see HttpURLConnection#getHeaderField(String)
501 * @since 9172
502 */
503 public abstract String getHeaderField(String name);
504
505 /**
506 * Returns an unmodifiable Map mapping header keys to a List of header values.
507 * As per RFC 2616, section 4.2 header names are case insensitive, so returned map is also case insensitive
508 * @return unmodifiable Map mapping header keys to a List of header values
509 * @see HttpURLConnection#getHeaderFields()
510 * @since 9232
511 */
512 public abstract Map<String, List<String>> getHeaderFields();
513
514 /**
515 * Indicates that other requests to the server are unlikely in the near future.
516 * @see HttpURLConnection#disconnect()
517 */
518 public abstract void disconnect();
519 }
520
521 /**
522 * Creates a new instance for the given URL and a {@code GET} request
523 *
524 * @param url the URL
525 * @return a new instance
526 */
527 public static HttpClient create(URL url) {
528 return create(url, "GET");
529 }
530
531 /**
532 * Creates a new instance for the given URL and a {@code GET} request
533 *
534 * @param url the URL
535 * @param requestMethod the HTTP request method to perform when calling
536 * @return a new instance
537 */
538 public static HttpClient create(URL url, String requestMethod) {
539 if (factory == null) {
540 throw new IllegalStateException("HTTP factory has not been set");
541 }
542 return factory.create(url, requestMethod)
543 // #18812: specify `Accept=*/*` to prevent Java from adding `Accept=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2`
544 .setAccept("*/*");
545 }
546
547 /**
548 * Returns the URL set for this connection.
549 * @return the URL
550 * @see #create(URL)
551 * @see #create(URL, String)
552 * @since 9172
553 */
554 public final URL getURL() {
555 return url;
556 }
557
558 /**
559 * Returns the request body set for this connection.
560 * @return the HTTP request body, or null
561 * @since 15229
562 */
563 public final byte[] getRequestBody() {
564 return Utils.copyArray(requestBody);
565 }
566
567 /**
568 * Determines if a non-empty request body has been set for this connection.
569 * @return {@code true} if the request body is set and non-empty
570 * @since 15229
571 */
572 public final boolean hasRequestBody() {
573 return requestBody != null && requestBody.length > 0;
574 }
575
576 /**
577 * Determines if the underlying HTTP method requires a body.
578 * @return {@code true} if the underlying HTTP method requires a body
579 * @since 15229
580 */
581 public final boolean requiresBody() {
582 return "PUT".equals(requestMethod) || "POST".equals(requestMethod) || "DELETE".equals(requestMethod);
583 }
584
585 /**
586 * Returns the request method set for this connection.
587 * @return the HTTP request method
588 * @see #create(URL, String)
589 * @since 9172
590 */
591 public final String getRequestMethod() {
592 return requestMethod;
593 }
594
595 /**
596 * Returns the set value for the given {@code header}.
597 * @param header HTTP header name
598 * @return HTTP header value
599 * @since 9172
600 */
601 public final String getRequestHeader(String header) {
602 return headers.get(header);
603 }
604
605 /**
606 * Returns the connect timeout.
607 * @return the connect timeout, in milliseconds
608 * @since 15229
609 */
610 public final int getConnectTimeout() {
611 return connectTimeout;
612 }
613
614 /**
615 * Returns the read timeout.
616 * @return the read timeout, in milliseconds
617 * @since 15229
618 */
619 public final int getReadTimeout() {
620 return readTimeout;
621 }
622
623 /**
624 * Returns the {@code If-Modified-Since} header value.
625 * @return the {@code If-Modified-Since} header value
626 * @since 15229
627 */
628 public final long getIfModifiedSince() {
629 return ifModifiedSince;
630 }
631
632 /**
633 * Determines whether not to set header {@code Cache-Control=no-cache}.
634 * By default, {@code useCache} is true, i.e., the header {@code Cache-Control=no-cache} is not sent.
635 *
636 * @return whether not to set header {@code Cache-Control=no-cache}
637 * @since 15229
638 */
639 public final boolean isUseCache() {
640 return useCache;
641 }
642
643 /**
644 * Returns the headers.
645 * @return the headers
646 * @since 15229
647 */
648 public final Map<String, String> getHeaders() {
649 return headers;
650 }
651
652 /**
653 * Returns the reason for request.
654 * @return the reason for request
655 * @since 15229
656 */
657 public final String getReasonForRequest() {
658 return reasonForRequest;
659 }
660
661 /**
662 * Returns the output message.
663 * @return the output message
664 */
665 protected final String getOutputMessage() {
666 return outputMessage;
667 }
668
669 /**
670 * Determines whether the progress monitor task will be finished when the output stream is closed. {@code true} by default.
671 * @return the finishOnCloseOutput
672 */
673 protected final boolean isFinishOnCloseOutput() {
674 return finishOnCloseOutput;
675 }
676
677 /**
678 * Sets whether not to set header {@code Cache-Control=no-cache}.
679 * By default, {@code useCache} is true, i.e., the header {@code Cache-Control=no-cache} is not sent.
680 *
681 * @param useCache whether not to set header {@code Cache-Control=no-cache}
682 * @return {@code this}
683 * @see HttpURLConnection#setUseCaches(boolean)
684 */
685 public final HttpClient useCache(boolean useCache) {
686 this.useCache = useCache;
687 return this;
688 }
689
690 /**
691 * Sets whether not to set header {@code Connection=close}
692 * <p>
693 * This might fix #7640, see
694 * <a href='https://web.archive.org/web/20140118201501/http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive'>here</a>.
695 *
696 * @param keepAlive whether not to set header {@code Connection=close}
697 * @return {@code this}
698 */
699 public final HttpClient keepAlive(boolean keepAlive) {
700 return setHeader("Connection", keepAlive ? null : "close");
701 }
702
703 /**
704 * Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced
705 * by this URLConnection. If the timeout expires before the connection can be established, a
706 * {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite timeout.
707 * @param connectTimeout an {@code int} that specifies the connect timeout value in milliseconds
708 * @return {@code this}
709 * @see HttpURLConnection#setConnectTimeout(int)
710 */
711 public final HttpClient setConnectTimeout(int connectTimeout) {
712 this.connectTimeout = connectTimeout;
713 return this;
714 }
715
716 /**
717 * Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from
718 * input stream when a connection is established to a resource. If the timeout expires before there is data available for
719 * read, a {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite timeout.
720 * @param readTimeout an {@code int} that specifies the read timeout value in milliseconds
721 * @return {@code this}
722 * @see HttpURLConnection#setReadTimeout(int)
723 */
724 public final HttpClient setReadTimeout(int readTimeout) {
725 this.readTimeout = readTimeout;
726 return this;
727 }
728
729 /**
730 * Sets the {@code Accept} header.
731 * @param accept header value
732 *
733 * @return {@code this}
734 */
735 public final HttpClient setAccept(String accept) {
736 return setHeader("Accept", accept);
737 }
738
739 /**
740 * Sets the request body for {@code PUT}/{@code POST} requests.
741 * @param requestBody request body
742 *
743 * @return {@code this}
744 */
745 public final HttpClient setRequestBody(byte[] requestBody) {
746 this.requestBody = Utils.copyArray(requestBody);
747 return this;
748 }
749
750 /**
751 * Sets the {@code If-Modified-Since} header.
752 * @param ifModifiedSince header value
753 *
754 * @return {@code this}
755 */
756 public final HttpClient setIfModifiedSince(long ifModifiedSince) {
757 this.ifModifiedSince = ifModifiedSince;
758 return this;
759 }
760
761 /**
762 * Sets the maximum number of redirections to follow.
763 *
764 * Set {@code maxRedirects} to {@code -1} in order to ignore redirects, i.e.,
765 * to not throw an {@link IOException} in {@link #connect()}.
766 * @param maxRedirects header value
767 *
768 * @return {@code this}
769 */
770 public final HttpClient setMaxRedirects(int maxRedirects) {
771 this.maxRedirects = maxRedirects;
772 return this;
773 }
774
775 /**
776 * Sets an arbitrary HTTP header.
777 * @param key header name
778 * @param value header value
779 *
780 * @return {@code this}
781 */
782 public final HttpClient setHeader(String key, String value) {
783 this.headers.put(key, value);
784 return this;
785 }
786
787 /**
788 * Sets arbitrary HTTP headers.
789 * @param headers HTTP headers
790 *
791 * @return {@code this}
792 */
793 public final HttpClient setHeaders(Map<String, String> headers) {
794 this.headers.putAll(headers);
795 return this;
796 }
797
798 /**
799 * Sets a reason to show on console. Can be {@code null} if no reason is given.
800 * @param reasonForRequest Reason to show
801 * @return {@code this}
802 * @since 9172
803 */
804 public final HttpClient setReasonForRequest(String reasonForRequest) {
805 this.reasonForRequest = reasonForRequest;
806 return this;
807 }
808
809 /**
810 * Sets the output message to be displayed in progress monitor for {@code PUT}, {@code POST} and {@code DELETE} methods.
811 * Defaults to "Uploading data ..." (translated). Has no effect for {@code GET} or any other method.
812 * @param outputMessage message to be displayed in progress monitor
813 * @return {@code this}
814 * @since 12711
815 */
816 public final HttpClient setOutputMessage(String outputMessage) {
817 this.outputMessage = outputMessage;
818 return this;
819 }
820
821 /**
822 * Sets whether the progress monitor task will be finished when the output stream is closed. This is {@code true} by default.
823 * @param finishOnCloseOutput whether the progress monitor task will be finished when the output stream is closed
824 * @return {@code this}
825 * @since 10302
826 */
827 public final HttpClient setFinishOnCloseOutput(boolean finishOnCloseOutput) {
828 this.finishOnCloseOutput = finishOnCloseOutput;
829 return this;
830 }
831
832 /**
833 * Sets the connect log at DEBUG level instead of the default INFO level.
834 * @param debug {@code true} to set the connect log at DEBUG level
835 * @return {@code this}
836 * @since 15389
837 */
838 public final HttpClient setLogAtDebug(boolean debug) {
839 this.debug = debug;
840 return this;
841 }
842
843 /**
844 * Determines if the given status code is an HTTP redirection.
845 * @param statusCode HTTP status code
846 * @return {@code true} if the given status code is an HTTP redirection
847 * @since 15423
848 */
849 public static boolean isRedirect(final int statusCode) {
850 switch (statusCode) {
851 case HttpURLConnection.HTTP_MOVED_PERM: // 301
852 case HttpURLConnection.HTTP_MOVED_TEMP: // 302
853 case HttpURLConnection.HTTP_SEE_OTHER: // 303
854 case 307: // TEMPORARY_REDIRECT:
855 case 308: // PERMANENT_REDIRECT:
856 return true;
857 default:
858 return false;
859 }
860 }
861
862 /**
863 * Disconnect client.
864 * @see HttpURLConnection#disconnect()
865 * @since 9309
866 */
867 public abstract void disconnect();
868
869 /**
870 * Returns a {@link Matcher} against predefined Tomcat error messages.
871 * If it matches, error message can be extracted from {@code group(1)}.
872 * @param data HTML contents to check
873 * @return a {@link Matcher} against predefined Tomcat error messages
874 * @since 13358
875 */
876 public static Matcher getTomcatErrorMatcher(String data) {
877 return data != null ? TOMCAT_ERR_MESSAGE.matcher(data) : null;
878 }
879}
Note: See TracBrowser for help on using the repository browser.