Class HttpClient

java.lang.Object
org.openstreetmap.josm.tools.HttpClient
Direct Known Subclasses:
Http1Client

public abstract class HttpClient extends Object
Provides uniform access for a HTTP/HTTPS server. This class should be used in favour of HttpURLConnection.
Since:
9168
  • Field Details

  • Constructor Details

    • HttpClient

      protected HttpClient(URL url, String requestMethod)
      Constructs a new HttpClient.
      Parameters:
      url - URL to access
      requestMethod - HTTP request method (GET, POST, PUT, DELETE...)
  • Method Details

    • setFactory

      public static void setFactory(HttpClient.HttpClientFactory newFactory)
      Registers a new HTTP client factory.
      Parameters:
      newFactory - new HTTP client factory
      Since:
      15229
    • connect

      public final HttpClient.Response connect() throws IOException
      Opens the HTTP connection.
      Returns:
      HTTP response
      Throws:
      IOException - if any I/O error occurs
    • connect

      public final HttpClient.Response connect(ProgressMonitor progressMonitor) throws IOException
      Opens the HTTP connection.
      Parameters:
      progressMonitor - progress monitor
      Returns:
      HTTP response
      Throws:
      IOException - if any I/O error occurs
      Since:
      9179
    • connect

      public final HttpClient.Response connect(ProgressMonitor progressMonitor, String authRedirectLocation, String authRequestProperty) throws IOException
      Opens the HTTP connection.
      Parameters:
      progressMonitor - progress monitor
      authRedirectLocation - The location where we will be redirected for authentication
      authRequestProperty - The authorization header to set when being redirected to the auth location
      Returns:
      HTTP response
      Throws:
      IOException - if any I/O error occurs
      Since:
      18913
    • setupConnection

      protected abstract void setupConnection(ProgressMonitor progressMonitor) throws IOException
      Throws:
      IOException
    • performConnection

      Throws:
      IOException
    • performDisconnection

      protected abstract void performDisconnection() throws IOException
      Throws:
      IOException
    • buildResponse

      protected abstract HttpClient.Response buildResponse(ProgressMonitor progressMonitor) throws IOException
      Throws:
      IOException
    • notifyConnect

      protected final void notifyConnect(ProgressMonitor progressMonitor)
    • logRequest

      protected final void logRequest(String pattern, Object... args)
    • logRequestBody

      protected final void logRequestBody()
    • getResponse

      Returns the HTTP response which is set only after calling connect(). Calling this method again, returns the identical object (unless another connect() is performed).
      Returns:
      the HTTP response
      Since:
      9309
    • create

      public static HttpClient create(URL url)
      Creates a new instance for the given URL and a GET request
      Parameters:
      url - the URL
      Returns:
      a new instance
    • create

      public static HttpClient create(URL url, String requestMethod)
      Creates a new instance for the given URL and a GET request
      Parameters:
      url - the URL
      requestMethod - the HTTP request method to perform when calling
      Returns:
      a new instance
    • getURL

      public final URL getURL()
      Returns the URL set for this connection.
      Returns:
      the URL
      Since:
      9172
      See Also:
    • getRequestBody

      public final byte[] getRequestBody()
      Returns the request body set for this connection.
      Returns:
      the HTTP request body, or null
      Since:
      15229
    • hasRequestBody

      public final boolean hasRequestBody()
      Determines if a non-empty request body has been set for this connection.
      Returns:
      true if the request body is set and non-empty
      Since:
      15229
    • requiresBody

      public final boolean requiresBody()
      Determines if the underlying HTTP method requires a body.
      Returns:
      true if the underlying HTTP method requires a body
      Since:
      15229
    • getRequestMethod

      public final String getRequestMethod()
      Returns the request method set for this connection.
      Returns:
      the HTTP request method
      Since:
      9172
      See Also:
    • getRequestHeader

      public final String getRequestHeader(String header)
      Returns the set value for the given header.
      Parameters:
      header - HTTP header name
      Returns:
      HTTP header value
      Since:
      9172
    • getConnectTimeout

      public final int getConnectTimeout()
      Returns the connect timeout.
      Returns:
      the connect timeout, in milliseconds
      Since:
      15229
    • getReadTimeout

      public final int getReadTimeout()
      Returns the read timeout.
      Returns:
      the read timeout, in milliseconds
      Since:
      15229
    • getIfModifiedSince

      public final long getIfModifiedSince()
      Returns the If-Modified-Since header value.
      Returns:
      the If-Modified-Since header value
      Since:
      15229
    • isUseCache

      public final boolean isUseCache()
      Determines whether not to set header Cache-Control=no-cache. By default, useCache is true, i.e., the header Cache-Control=no-cache is not sent.
      Returns:
      whether not to set header Cache-Control=no-cache
      Since:
      15229
    • getHeaders

      public final Map<String,String> getHeaders()
      Returns the headers.
      Returns:
      the headers
      Since:
      15229
    • getReasonForRequest

      public final String getReasonForRequest()
      Returns the reason for request.
      Returns:
      the reason for request
      Since:
      15229
    • getOutputMessage

      protected final String getOutputMessage()
      Returns the output message.
      Returns:
      the output message
    • isFinishOnCloseOutput

      protected final boolean isFinishOnCloseOutput()
      Determines whether the progress monitor task will be finished when the output stream is closed. true by default.
      Returns:
      the finishOnCloseOutput
    • useCache

      public final HttpClient useCache(boolean useCache)
      Sets whether not to set header Cache-Control=no-cache. By default, useCache is true, i.e., the header Cache-Control=no-cache is not sent.
      Parameters:
      useCache - whether not to set header Cache-Control=no-cache
      Returns:
      this
      See Also:
    • keepAlive

      public final HttpClient keepAlive(boolean keepAlive)
      Sets whether not to set header Connection=close

      This might fix #7640, see here.

      Parameters:
      keepAlive - whether not to set header Connection=close
      Returns:
      this
    • setConnectTimeout

      public final HttpClient setConnectTimeout(int connectTimeout)
      Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
      Parameters:
      connectTimeout - an int that specifies the connect timeout value in milliseconds
      Returns:
      this
      See Also:
    • setReadTimeout

      public final HttpClient setReadTimeout(int readTimeout)
      Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
      Parameters:
      readTimeout - an int that specifies the read timeout value in milliseconds
      Returns:
      this
      See Also:
    • setAccept

      public final HttpClient setAccept(String accept)
      Sets the Accept header.
      Parameters:
      accept - header value
      Returns:
      this
    • setRequestBody

      public final HttpClient setRequestBody(byte[] requestBody)
      Sets the request body for PUT/POST requests.
      Parameters:
      requestBody - request body
      Returns:
      this
    • setIfModifiedSince

      public final HttpClient setIfModifiedSince(long ifModifiedSince)
      Sets the If-Modified-Since header.
      Parameters:
      ifModifiedSince - header value
      Returns:
      this
    • setMaxRedirects

      public final HttpClient setMaxRedirects(int maxRedirects)
      Sets the maximum number of redirections to follow. Set maxRedirects to -1 in order to ignore redirects, i.e., to not throw an IOException in connect().
      Parameters:
      maxRedirects - header value
      Returns:
      this
    • setHeader

      public final HttpClient setHeader(String key, String value)
      Sets an arbitrary HTTP header.
      Parameters:
      key - header name
      value - header value
      Returns:
      this
    • setHeaders

      public final HttpClient setHeaders(Map<String,String> headers)
      Sets arbitrary HTTP headers.
      Parameters:
      headers - HTTP headers
      Returns:
      this
    • setReasonForRequest

      public final HttpClient setReasonForRequest(String reasonForRequest)
      Sets a reason to show on console. Can be null if no reason is given.
      Parameters:
      reasonForRequest - Reason to show
      Returns:
      this
      Since:
      9172
    • setOutputMessage

      public final HttpClient setOutputMessage(String outputMessage)
      Sets the output message to be displayed in progress monitor for PUT, POST and DELETE methods. Defaults to "Uploading data ..." (translated). Has no effect for GET or any other method.
      Parameters:
      outputMessage - message to be displayed in progress monitor
      Returns:
      this
      Since:
      12711
    • setFinishOnCloseOutput

      public final HttpClient setFinishOnCloseOutput(boolean finishOnCloseOutput)
      Sets whether the progress monitor task will be finished when the output stream is closed. This is true by default.
      Parameters:
      finishOnCloseOutput - whether the progress monitor task will be finished when the output stream is closed
      Returns:
      this
      Since:
      10302
    • setLogAtDebug

      public final HttpClient setLogAtDebug(boolean debug)
      Sets the connect log at DEBUG level instead of the default INFO level.
      Parameters:
      debug - true to set the connect log at DEBUG level
      Returns:
      this
      Since:
      15389
    • isRedirect

      public static boolean isRedirect(int statusCode)
      Determines if the given status code is an HTTP redirection.
      Parameters:
      statusCode - HTTP status code
      Returns:
      true if the given status code is an HTTP redirection
      Since:
      15423
    • disconnect

      public abstract void disconnect()
      Disconnect client.
      Since:
      9309
      See Also:
    • getTomcatErrorMatcher

      public static Matcher getTomcatErrorMatcher(String data)
      Returns a Matcher against predefined Tomcat error messages. If it matches, error message can be extracted from group(1).
      Parameters:
      data - HTML contents to check
      Returns:
      a Matcher against predefined Tomcat error messages
      Since:
      13358