sunlabs.brazil.util.http
Class HttpUtil

java.lang.Object
  extended by sunlabs.brazil.util.http.HttpUtil (view source)

public class HttpUtil
extends Object

The HttpUtil class contains methods for performing simple HTTP operations.


Method Summary
static void extractQuery(String query, Dictionary table)
          Turns x-www-form-urlencoded form data into a dictionary.
static String extractUrlHost(String url)
          Get the host portion of a Url String.
static String extractUrlPath(String url)
          Get the path portion of a Url String.
static String extractUrlPort(String url)
          Get the port portion of a Url String as a string.
static String extractUrlProtocol(String url)
          Get the protocol portion of a Url String.
static String formatTime()
          Returns a string containing the current time as an HTTP-formatted date.
static String formatTime(long time)
          Returns a string containing an HTTP-formatted date.
static String getStatusPhrase(int code)
          Returns the HTTP error string associated with the integer error code.
static String htmlEncode(String src)
          Converts a string into a valid HTML fragment.
static long parseTime(String time)
          Convert a last-modified date in "standard" format into a time stamp.
static String urlDecode(String src)
          Decodes a URL-encoded string by replacing all the "%XX" escape sequences in the string with the corresponding character.
static String urlEncode(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
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

htmlEncode

public static String htmlEncode(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 String urlEncode(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:
src - The string to convert.
Returns:
The URL-encoded version of the given string.

urlDecode

public static String urlDecode(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:
src - The URL-encoded string.
Returns:
The decoded version of the given string.

formatTime

public static 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 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(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(String query,
                                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 String extractUrlProtocol(String url)
Get the protocol portion of a Url String.

Returns:
null if the string is an invalid URL.

extractUrlHost

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

Returns:
null if the string is an invalid URL.

extractUrlPort

public static String extractUrlPort(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 String extractUrlPath(String url)
Get the path portion of a Url String.

Returns:
null if the string is an invalid URL.

getStatusPhrase

public static 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.

Version Kenai-svn-r24, Generated 08/18/09
Copyright (c) 2001-2009, Sun Microsystems.