sunlabs.brazil.util.http
Class HttpUtil

java.lang.Object
  extended by sunlabs.brazil.util.http.HttpUtil

public class HttpUtil
extends java.lang.Object

The HttpUtil class contains methods for performing simple HTTP operations.

Version:
2.3
Author:
Colin Stevens (colin.stevens@sun.com)

Method Summary
static void extractQuery(java.lang.String query, java.util.Dictionary table)
          Turns x-www-form-urlencoded form data into a dictionary.
static java.lang.String extractUrlHost(java.lang.String url)
          Get the host portion of a Url String.
static java.lang.String extractUrlPath(java.lang.String url)
          Get the path portion of a Url String.
static java.lang.String extractUrlPort(java.lang.String url)
          Get the port portion of a Url String as a string.
static java.lang.String extractUrlProtocol(java.lang.String url)
          Get the protocol portion of a Url String.
static java.lang.String formatTime()
          Returns a string containing the current time as an HTTP-formatted date.
static java.lang.String formatTime(long time)
          Returns a string containing an HTTP-formatted date.
static java.lang.String getStatusPhrase(int code)
          Returns the HTTP error string associated with the integer error code.
static java.lang.String htmlEncode(java.lang.String src)
          Converts a string into a valid HTML fragment.
static long parseTime(java.lang.String time)
          Convert a last-modified date in "standard" format into a time stamp.
static java.lang.String urlDecode(java.lang.String src)
          Decodes a URL-encoded string by replacing all the "%XX" escape sequences in the string with the corresponding character.
static java.lang.String urlEncode(java.lang.String src)
          Maps a string to be used in a query or post into a form that is acceptable in an URL.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

htmlEncode

public static java.lang.String htmlEncode(java.lang.String src)
Converts a string into a valid HTML fragment. Escapes the characters ", &, <, >, and all non-printables into the form &#xx; (their "decimal reference" form).

Parameters:
src - The string to convert.
Returns:
The string with all the special characters converted to decimal reference form.

urlEncode

public static java.lang.String urlEncode(java.lang.String src)
Maps a string to be used in a query or post into a form that is acceptable in an URL. Typically used when the caller wants to safely generate an HREF containing an arbitrary string that may have special characters.

URL strings may not contain non-alphanumeric characters. All non-alphanumeric characters are converted to the escape sequence "%XX", where XX is the hexadecimal value of that character's code.

Note that the space character " " is NOT converted to "+". That is a common misconception. "+" represents a space only in query strings, not in the URL. "%20" is how an actual space character must be passed in an URL, and is also an acceptable way of passing a space in a query string.

Parameters:
string - The string to convert.
Returns:
The URL-encoded version of the given string.

urlDecode

public static java.lang.String urlDecode(java.lang.String src)
Decodes a URL-encoded string by replacing all the "%XX" escape sequences in the string with the corresponding character.

Malformed "%XX" sequences are silently ignored.

Parameters:
string - The URL-encoded string.
Returns:
The decoded version of the given string.

formatTime

public static java.lang.String formatTime()
Returns a string containing the current time as an HTTP-formatted date.

Returns:
HTTP date string representing the current time.

formatTime

public static java.lang.String formatTime(long time)
Returns a string containing an HTTP-formatted date.

Parameters:
time - The date to format (current time in msec).
Returns:
HTTP date string representing the given time.

parseTime

public static long parseTime(java.lang.String time)
Convert a last-modified date in "standard" format into a time stamp. This "inverses" formatTime.

Parameters:
time - A correctly formatted HTTP date string.
Returns:
milliseconds since the epoch, or 0 if the conversion failed.

extractQuery

public static void extractQuery(java.lang.String query,
                                java.util.Dictionary table)
Turns x-www-form-urlencoded form data into a dictionary.

Parameters:
query - The x-www-form-urlencoded string. May be null
table - The dictionary to insert the form data into.

extractUrlProtocol

public static java.lang.String extractUrlProtocol(java.lang.String url)
Get the protocol portion of a Url String.

Returns:
null if the string is an invalid URL.

extractUrlHost

public static java.lang.String extractUrlHost(java.lang.String url)
Get the host portion of a Url String.

Returns:
null if the string is an invalid URL.

extractUrlPort

public static java.lang.String extractUrlPort(java.lang.String url)
Get the port portion of a Url String as a string.

Returns:
null if the string is an invalid URL, the empy string if no port was specified.

extractUrlPath

public static java.lang.String extractUrlPath(java.lang.String url)
Get the path portion of a Url String.

Returns:
null if the string is an invalid URL.

getStatusPhrase

public static java.lang.String getStatusPhrase(int code)
Returns the HTTP error string associated with the integer error code. This error string can be used in HTTP responses. Unknown codes return the string "Error"

Parameters:
code - The code to look up.
Returns:
The associated error string.