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