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