Class HttpTools

java.lang.Object
de.aristaflow.adept2.util.net.HttpTools

public class HttpTools extends Object
Tool methods for simplifying life with HTTP.
  • Field Details

    • HEADER_FORWARDED

      public static final String HEADER_FORWARDED
      The header name for forward information for HTTP requests through a proxy.
      See Also:
    • HEADER_X_FORWARDED_HOST

      public static final String HEADER_X_FORWARDED_HOST
      The header name for the forward host for HTTP requests through a proxy.
      See Also:
    • HEADER_X_FORWARDED_PORT

      public static final String HEADER_X_FORWARDED_PORT
      The header name for the forward port for HTTP requests through a proxy.
      See Also:
    • HEADER_X_FORWARDED_PROTO

      public static final String HEADER_X_FORWARDED_PROTO
      The header name for the forward protocol for HTTP requests through a proxy.
      See Also:
    • hostPattern

      protected static final Pattern hostPattern
      The pattern determining the host and port from the Forwarded header.
    • protoPattern

      protected static final Pattern protoPattern
      The pattern determining the protocol from the Forwarded header.
    • charsetPattern

      protected static final Pattern charsetPattern
      The pattern determining the character set from the Content-Type.
  • Constructor Details

    • HttpTools

      public HttpTools()
  • Method Details

    • adaptUrl

      public static String adaptUrl(String url, Function<String,String> headers, Logger logger)
      Adapts the designated URL by transforming the string to a URL, applying adaptUrl(URL, Function, Logger) and converting the URL back to String again.
      Parameters:
      url - The URL which may need to be adapted to a different protocol, host and/or port. This must not be null.
      headers - The function providing HTTP request headers. This must not be null but it may return null.
      logger - The logger for logging problems, e. g. the port cannot be converted to int. This must not be null.
      Returns:
      The URL with protocol, host and/or port changed to the values provided by the designated headers function for the corresponding HTTP headers.
      See Also:
    • adaptUrl

      public static URL adaptUrl(URL url, Function<String,String> headers, Logger logger)
      Adapts the designated URL by setting protocol, host and port from the headers provided by the designated function. This allows to change the known values to the ones provided by the HTTP request which may be routed through a proxy. That is, the URL working for the requestor may need to be different from the URL known by our service.

      This supports (in this order, i. e. if a value is provided, the corresponding alternative header will be ignored):

      Parameters:
      url - The URL which may need to be adapted to a different protocol, host and/or port. This must not be null.
      headers - The function providing HTTP request headers. This must not be null but it may return null.
      logger - The logger for logging problems, e. g. the port cannot be converted to int. This must not be null.
      Returns:
      The URL with protocol, host and/or port changed to the values provided by the designated headers function for the corresponding HTTP headers.
    • getCharset

      public static Charset getCharset(URLConnection conn)
      Gets the character set used for text content of the designated URLConnection. If the content is no text content or the character set is not specified, null will be returned.
      Parameters:
      conn - The URL connection for which to determine the character set of the text content.
      Returns:
      The character set used for text content of the designated URLConnection or null in case of no text content or no specified character set.