Class HttpParser

java.lang.Object
org.glassfish.common.util.HttpParser

public final class HttpParser extends Object
HTTP helper class for parsing HTTP headers, content types, reading responses.
  • Method Details

    • readResponseErrorStream

      public static String readResponseErrorStream(HttpURLConnection connection) throws IOException
      Reads response error input stream from the connection to a String value. Respects charset from content type header, if not set, uses UTF-8.
      Parameters:
      connection -
      Returns:
      String parsed from HttpURLConnection.getErrorStream()
      Throws:
      IOException
    • readResponseInputStream

      public static String readResponseInputStream(URLConnection connection) throws IOException
      Reads response input stream from the connection to a String value. Respects charset from content type header, if not set, uses UTF-8.
      Parameters:
      connection -
      Returns:
      String parsed from URLConnection.getInputStream()
      Throws:
      IOException
    • parseBasicAuthorizationHeader

      public static UserNameAndPassword parseBasicAuthorizationHeader(String authorization) throws CharacterCodingException
      Parameters:
      authorization - Value of the Authorization HTTP header.
      Returns:
      UserNameAndPassword if the header starts with Basic and can be parsed to username and password. Null otherwise.
      Throws:
      CharacterCodingException
    • getCharsetFromHeader

      public static Charset getCharsetFromHeader(String header) throws CharacterCodingException
      Parse the character encoding from the specified content type, authorization header or any header using same rules when they contain ie. header="value"; charset=utf-8 . If the header is null, or there is no explicit character encoding, UTF-8 is returned.
      Parameters:
      header - a content type header
      Returns:
      Charset or UTF-8 if the charset is null, empty string or unknown.
      Throws:
      CharacterCodingException - if the charset is not supported.
    • getCharset

      public static Charset getCharset(String charset) throws CharacterCodingException
      Parses the parameter. If it is null or empty string, returns StandardCharsets.UTF_8.
      Parameters:
      charset -
      Returns:
      specified Charset or UTF-8 if the charset is null, empty string or unknown.
      Throws:
      CharacterCodingException - if the charset is not supported.