Package sunlabs.brazil.util.http
Class HttpUtil
java.lang.Object
sunlabs.brazil.util.http.HttpUtil
The
HttpUtil
class contains methods for performing simple
HTTP operations.- Version:
- 2.3
- Author:
- Colin Stevens (colin.stevens@sun.com)
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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
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
Convert a last-modified date in "standard" format into a time stamp.static String
Decodes a URL-encoded string by replacing all the "%XX" escape sequences in the string with the corresponding character.static String
Maps a string to be used in a query or post into a form that is acceptable in an URL.
-
Method Details
-
htmlEncode
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
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
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
Returns a string containing the current time as an HTTP-formatted date.- Returns:
- HTTP date string representing the current time.
-
formatTime
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
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
Turns x-www-form-urlencoded form data into a dictionary.- Parameters:
query
- The x-www-form-urlencoded string. May benull
table
- The dictionary to insert the form data into.
-
extractUrlProtocol
Get the protocol portion of a Url String.- Returns:
- null if the string is an invalid URL.
-
extractUrlHost
Get the host portion of a Url String.- Returns:
- null if the string is an invalid URL.
-
extractUrlPort
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
Get the path portion of a Url String.- Returns:
- null if the string is an invalid URL.
-
getStatusPhrase
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.
-