Class HttpUtils

java.lang.Object
com.inductiveautomation.ignition.common.util.HttpUtils

public class HttpUtils extends Object
Filename: HttpUtils Author: Perry Arellano-Jones Created on: 8/14/15

This class provides some simple connectivity utilities which may be common when trying to get information about an HTTP connection.

  • Field Details

    • RESPONSE_OK

      public static final int RESPONSE_OK
      See Also:
    • VALID_URL_PATTERN

      public static final Pattern VALID_URL_PATTERN
  • Constructor Details

    • HttpUtils

      public HttpUtils()
  • Method Details

    • isOkResponse

      public static boolean isOkResponse(URL url) throws IOException
      Makes a test connection to the provided URL. If the response has a 200 "OK" code, the method returns true. If the response is anything other than 200, this method returns false.

      This method is useful when trying to verify that a URL is online and has a valid site available. Omitting the timeout parameter uses 0, or an infinite timeout

      Parameters:
      url - the URL that will be tested for connection/content quality.
      Returns:
      true when the GET request of the url is responded to with a code 200
      Throws:
      IOException
    • isOkResponse

      public static boolean isOkResponse(URL url, int timeout) throws IOException
      Throws:
      IOException
    • isOkResponse

      public static boolean isOkResponse(String address) throws MalformedURLException
      This static method will try to resolve a URL passed in as a string, and if successful, will try to determine if the response given is an "OK" response (aka, code 200). On success it returns true. All other response codes return false.
      Parameters:
      address - which is the url to be tested for connectivity
      Returns:
      true when the given string resolves to an http or https connection that gives a code 200 response
      Throws:
      MalformedURLException
    • isOkResponse

      public static boolean isOkResponse(String address, int timeout) throws MalformedURLException
      This static method will try to resolve a URL passed in as a string, and if successful, will try to determine if the response given is an "OK" response (aka, code 200). On success it returns true. All other response codes return false.
      Parameters:
      address - which is the url to be tested for connectivity
      timeout - connection timeout in millis
      Returns:
      true when the given string resolves to an http or https connection that gives a code 200 response
      Throws:
      MalformedURLException
    • isValidUrl

      public static boolean isValidUrl(String url)
      Tests the url for valid format. Does not attempt to resolve or validate beyond the syntactic structure of the string.
      Parameters:
      url - url to test
      Returns:
      false if invalid or url was null
    • isValidHeaderName

      public static boolean isValidHeaderName(String name)
      Returns:
      true if name is a non-empty, legal HTTP header name (an RFC 7230 token).
    • isValidHeaderValue

      public static boolean isValidHeaderValue(String value)
      Returns:
      true if every character of value is legal in an HTTP header value (printable ASCII or HTAB).
    • sanitizeHeaderValue

      public static String sanitizeHeaderValue(String value)
      Drop characters illegal in an HTTP header value (retaining printable ASCII and HTAB) and trim surrounding whitespace. Use for best-effort, cosmetic values where silently dropping stray characters is preferable to failing the request.