Oracle Fusion Middleware
Java API Reference for Oracle PDK Java
11g Release 1 (11.1.1)
E10691-01

oracle.webdb.utils
Class HTTPUtils

java.lang.Object
  extended by oracle.webdb.utils.HTTPUtils

public class HTTPUtils
extends java.lang.Object

This class contains various utility methods for encoding and decoding URIs String using the set of characters allowed in a URI, as defined in RFC 2396 - "Uniform Resource Identifiers (URI): Generic Syntax". The method does not suffer from the limitations of the java.net.URLEncoder in that it provides control over the character encoding used for character to byte conversions and is capable of encoding characters that are encoded by a sequence of several bytes.

To convert a String, each character is examined in turn:


Field Summary
static int REQUEST_FOR_AUTHENTICATION
           
static int REQUEST_FOR_LOGOUT
           
 
Constructor Summary
HTTPUtils()
           
 
Method Summary
static byte[] base64Decode(java.lang.String string)
          Decodes the supplied base 64 encoded string into its original byte array, using the standard base 64 decoding algorithm.
static java.lang.String base64Encode(byte[] bytes)
          Base64 encodes the supplied bytes array, using the standard base 64 encoding algorithm.
static java.lang.String buildUrlAsString(java.lang.String scheme, java.lang.String host, int port, java.lang.String path)
          Build a fully qualified URL from the pieces provided.
static java.lang.String decode(java.lang.String value, java.lang.String encoding)
          Decodes an encoded String.
static java.lang.String decodeUTF(java.lang.String value)
          Decodes an encoded UTF8 String.
static java.lang.String encode(java.lang.String value, java.lang.String encoding)
          Encodes a String using the set of characters allowed in a URI.
static java.lang.String encode(java.lang.String value, java.lang.String encoding, boolean usePlusEncoding)
          Encodes a String using the set of characters allowed in a URI.
static java.lang.String encodePathInfo(java.lang.String pathInfo)
          Encodes the supplied URL path string into URL safe hex encoded characters, using the passed in charset to perform the char to byte conversion.
static java.lang.String encodePathInfo(java.lang.String pathInfo, java.lang.String charset)
          Encodes the supplied URL path string into URL safe hex encoded characters, using the passed in charset to perform the char to byte conversion.
static java.lang.String encodeScriptCharacters(java.lang.String url, char[] scriptCharacters, java.lang.String charSet)
          Takes a string and URL encodes any invalid URL characters that appear in a defined fixed list using the specified character set.
static java.lang.String encodeUTF(java.lang.String value)
          Encodes a String using the set of characters allowed in a URI.
static java.lang.String getCookie(java.lang.String cookieName, java.lang.String cookies)
          Extracts a cookie value from a list of cookies based on the name.
static java.text.DateFormat getCookieDateFormat()
          Returns the date format used for expiry times in type zero (Netscape) cookies.
static boolean isErrorStatusCode(int statusCode)
          Determines whether the given HTTP status code denotes a client or server error.
static boolean isText(java.lang.String mimeType)
          Return flag to indicate if the supplied mime type is text based.
static boolean isXml(java.lang.String mimeType)
          Return flag to indicate if the supplied mime type corresponds to XML based content.
static boolean needsDecoding(java.lang.String token)
          Determine if a value needs to be decoded using HTTPUtils.decode() This method assumes that a value is encoded.
static java.lang.String setCookie(java.lang.String cookieName, java.lang.String cookieValue, java.lang.String cookies)
          Sets a cookie value in a list of cookies based on the name.
static java.lang.String toHTTPLocale(java.util.Locale locale)
          Converts a Java Locale to a String in the format specified for the Accept-Language and xml:lang attribute, i.e.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REQUEST_FOR_LOGOUT

public static final int REQUEST_FOR_LOGOUT
See Also:
Constant Field Values

REQUEST_FOR_AUTHENTICATION

public static final int REQUEST_FOR_AUTHENTICATION
See Also:
Constant Field Values
Constructor Detail

HTTPUtils

public HTTPUtils()
Method Detail

getCookieDateFormat

public static java.text.DateFormat getCookieDateFormat()
Returns the date format used for expiry times in type zero (Netscape) cookies. Also used as the date format for portlet session expiries. Creates a new instance, so thread safe.

Returns:
the date format used for expiry times in type zero (Netscape) cookies

buildUrlAsString

public static java.lang.String buildUrlAsString(java.lang.String scheme,
                                                java.lang.String host,
                                                int port,
                                                java.lang.String path)
Build a fully qualified URL from the pieces provided. This method does not call any encoding methods, so the path argument has to be properly encoded.

Parameters:
scheme - server protocol.
host - server name.
port - server port.
path - properly encoded relative URL.
Throws:
java.lang.IllegalArgumentException - if scheme, host or port is null.

encode

public static java.lang.String encode(java.lang.String value,
                                      java.lang.String encoding,
                                      boolean usePlusEncoding)
                               throws java.io.UnsupportedEncodingException
Encodes a String using the set of characters allowed in a URI.

Parameters:
value - String to be translated.
encoding - the Java alias for the character encoding to be used to convert non-ASCII characters into bytes (e.g. "UTF8").
usePlusEncoding - whether or not to encode spaces with plus signs
Returns:
the translated String.
Throws:
java.io.UnsupportedEncodingException - if the given encoding is not a recognised character encoding.

encode

public static java.lang.String encode(java.lang.String value,
                                      java.lang.String encoding)
                               throws java.io.UnsupportedEncodingException
Encodes a String using the set of characters allowed in a URI. It will also encode spaces with plus signs.

Parameters:
value - String to be translated.
encoding - the Java alias for the character encoding to be used to convert non-ASCII characters into bytes (e.g. "UTF8").
Returns:
the translated String.
Throws:
java.io.UnsupportedEncodingException - if the given encoding is not a recognised character encoding.

encodeUTF

public static java.lang.String encodeUTF(java.lang.String value)
Encodes a String using the set of characters allowed in a URI. This method encodes a multibyte string in UTF8

Parameters:
value - String to be translated.
Returns:
the translated String.

decode

public static java.lang.String decode(java.lang.String value,
                                      java.lang.String encoding)
                               throws java.io.UnsupportedEncodingException
Decodes an encoded String.

Parameters:
value - String to be translated.
encoding - the Java alias for the character encoding to be used to convert byte sequences into characters(e.g. "UTF8").
Returns:
the translated String.
Throws:
java.io.UnsupportedEncodingException - if the given encoding is not a recognised character encoding.

decodeUTF

public static java.lang.String decodeUTF(java.lang.String value)
Decodes an encoded UTF8 String.

Parameters:
value - String to be translated.
Returns:
the translated String.

isErrorStatusCode

public static boolean isErrorStatusCode(int statusCode)
Determines whether the given HTTP status code denotes a client or server error. According to the HTTP 1.1 spec, the codes have the following categories: Note: Status code 499 and 470 are an exception to the above list. They are used for authentication and logout purpose respectively.

Parameters:
statusCode - an HTTP response status code
Returns:
true if the given status code denotes a client or server error

needsDecoding

public static final boolean needsDecoding(java.lang.String token)
Determine if a value needs to be decoded using HTTPUtils.decode() This method assumes that a value is encoded. As such, the existence of '%' (leading character of an encoded sequence) or '+' (sometimes used to replace <space> characters indicates that decoding IS required.


getCookie

public static java.lang.String getCookie(java.lang.String cookieName,
                                         java.lang.String cookies)
Extracts a cookie value from a list of cookies based on the name.

Parameters:
cookieName - Name of the cookie to search for.
cookies - List of cookies of the form "name1=value1; name2=value2; ...; nameN=valueN".
Returns:
The cookie value stored as a String object. null if cookie not found.

toHTTPLocale

public static java.lang.String toHTTPLocale(java.util.Locale locale)
Converts a Java Locale to a String in the format specified for the Accept-Language and xml:lang attribute, i.e. RFC 1766.


setCookie

public static java.lang.String setCookie(java.lang.String cookieName,
                                         java.lang.String cookieValue,
                                         java.lang.String cookies)
Sets a cookie value in a list of cookies based on the name. If the cookie value is null, the cookie is removed from the list.

Parameters:
cookieName - Name of the cookie to set.
cookieValue - Value of the cookie to set.
cookies - List of cookies of the form "name1=value1; name2=value2; ...; nameN=valueN".
Returns:
The new cookie string.

base64Encode

public static java.lang.String base64Encode(byte[] bytes)
Base64 encodes the supplied bytes array, using the standard base 64 encoding algorithm.

Parameters:
bytes - The byte array to encode
Returns:
The base 64 encoded string representing the byte array

base64Decode

public static byte[] base64Decode(java.lang.String string)
Decodes the supplied base 64 encoded string into its original byte array, using the standard base 64 decoding algorithm.

Parameters:
string - The base 64 encoded string to decode
Returns:
The decoded byte array

encodePathInfo

public static java.lang.String encodePathInfo(java.lang.String pathInfo,
                                              java.lang.String charset)
                                       throws java.io.UnsupportedEncodingException
Encodes the supplied URL path string into URL safe hex encoded characters, using the passed in charset to perform the char to byte conversion. This function is different from encode() in that it parses the path and only encodes the pieces between each path separator '/'.

Parameters:
pathInfo - The path to be encoded
charset - The charset to use for encoding
Returns:
The encoded path in the given charset
Throws:
java.io.UnsupportedEncodingException - if the given encoding is not a recognised character encoding.

encodePathInfo

public static java.lang.String encodePathInfo(java.lang.String pathInfo)
                                       throws java.io.UnsupportedEncodingException
Encodes the supplied URL path string into URL safe hex encoded characters, using the passed in charset to perform the char to byte conversion. This function is different from encode() in that it parses the path and only encodes the pieces between each path separator '/'. encodePathInfo used to accept another argument: "charset". But with the integration of WebLogic Server in 11g, the default charset is UTF-8. Also by specifying -Dfile.encoding=<charset string> in the Server Startup arguments, the charset used for decoding getPathInfo could be different. So now HTTPUtils.encodePathInfo determines the JVM encoding by getting file.encoding value and uses that encoding to encode Path Info. Hence the call below does not need to specify any charset at all.

Parameters:
pathInfo - The path to be encoded
charset - The charset to use for encoding
Returns:
The encoded path in the given charset
Throws:
java.io.UnsupportedEncodingException - if the given encoding is not a recognised character encoding.

encodeScriptCharacters

public static java.lang.String encodeScriptCharacters(java.lang.String url,
                                                      char[] scriptCharacters,
                                                      java.lang.String charSet)
                                               throws java.io.UnsupportedEncodingException
Takes a string and URL encodes any invalid URL characters that appear in a defined fixed list using the specified character set.

Parameters:
url - the url string to be encoded
scriptCharacters - array of characters that should be escaped. If null, no encoding will be carried out.
charSet - the character set to be used for encoding
Throws:
java.io.UnsupportedEncodingException - if the character set is not recognised

isText

public static boolean isText(java.lang.String mimeType)
Return flag to indicate if the supplied mime type is text based.

Parameters:
mimeType - mime type string to check
Returns:
true if it's a text-based type, false otherwise.

isXml

public static boolean isXml(java.lang.String mimeType)
Return flag to indicate if the supplied mime type corresponds to XML based content.

Parameters:
mimeType - mime type string to check
Returns:
true if mimeType is a xml-based type, false otherwise.

Oracle Fusion Middleware
Java API Reference for Oracle PDK Java
11g Release 1 (11.1.1)
E10691-01

Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.