java.lang.Object
|
+--stec.net.Utils
public final class Utils
Includes methods for encoding/decoding URLs, encoding/parsing/extracting
cookies, parsing character set and getting arguments.
Methods
|
Method
|
Description
|
|
concatParameters
|
Concatenates two parameter hashtables.
|
|
concatPaths
|
Concatenates two path strings.
|
|
concatURIs
|
Concatenates two URI strings.
|
|
decodeParameters
|
Converts the specified & delimited string into key, value pairs.
|
|
decodeURL
|
Converts the given x-www-form-urlencoded MIME encoded string into a string.
|
|
encodeCookie
|
Converts the given Cookie into a string.
|
|
encodeParameters
|
Converts the specified key, value pairs into an & delimited string.
|
|
encodeURL
|
Converts the given string into a x-www-form-urlencoded MIME encoded string.
|
|
extractCookie
|
Returns the named Cookie from the given array of Cookies.
|
|
getArgs
|
Returns any arguments sent with the request.
|
|
getItem
|
Used to read the specified item.
|
|
getParameters
|
Returns any parameters sent with the request.
|
|
getRootHandler
|
Returns an IOHandler to the document root directory of the specified virtual host.
|
|
getConfig
|
Used to read and parse the specified configuration file.
|
|
getTemplate
|
Used to read the specified template file.
|
|
mergeParameters
|
Merges a Hashtable of parameters into another Hashtable of parameters..
|
|
parseCharacterEncoding
|
Returns the character set using the given content type.
|
|
parseCookieHeader
|
Returns an array of Cookies using the given cookie header.
|
|
readLine
|
Reads a line from the specified input stream to the given byte buffer
starting at the specified offset, up to the given given number of bytes.
|
concatParameters
Concatenates two parameter hashtables.
Syntax
public final static Hashtable concatParameters(Hashtable ht1,
Hashtable ht2)
Parameters
|
ht1
|
the first Hashtable.
|
|
ht2
|
the second hashtable.
|
Returns
|
Hashtable
|
the resultant Hashtable.
|
Throws
Example
Hashtable params = Utils.concatParameters(init_params, parsed_params);
concatPaths
Concatenates two path strings.
Syntax
public final static String concatPaths(String path1,
String path2)
Parameters
|
path1
|
the first path.
|
|
path2
|
the second path.
|
Returns
|
String
|
the resultant path.
|
Throws
Example
String path = Utils.concatPaths(basedir, filepath);
concatURIs
Concatenates two URI strings.
Syntax
public final static String concatURIs(String uri1,
String uri2)
Parameters
|
uri1
|
the first URI.
|
|
uri2
|
the second URI.
|
Returns
|
String
|
the resultant URI.
|
Throws
Example
String uri = Utils.concatURIs(baseuri, uri);
decodeParameters
Converts the specified & delimited string into key, value pairs.
Syntax
public final static Hashtable decodeParameters(String parameters)
Parameters
|
parameters
|
an & delimited String of key=value pairs.
|
Returns
|
Hashtable
|
containing the key, value pairs.
|
Throws
|
IllegalArgumentException
|
Thrown when an invalid parameter String is specified.
|
Example
Hashtable values = Utils.decodeParameters(parameters);
decodeURL
Converts the given x-www-form-urlencoded MIME encoded string into a string.
Syntax
public final static String decodeURL(String url)
throws IllegalArgumentException
Parameters
Returns
Throws
|
IllegalArgumentException
|
Thrown when parsing an invalid hexadecimal value.
|
Example
String url = Utils.decodeURL(queryString);
encodeCookie
Converts the given Cookie into a string.
Syntax
public final static String encodeCookie(Cookie cookie)
Parameters
|
cookie
|
the Cookie to encode.
|
Returns
|
String
|
the encoded Cookie.
|
Throws
Example
String sCookie = Utils.encodeCookie(cookie);
encodeParameters
Converts the specified key, value pairs into an & delimited string.
Syntax
public final static String encodeParameters(Hashtable values)
Parameters
|
values
|
a Hashtable containing key, value pairs.
|
Returns
|
String
|
an & delimited String of key=value pairs.
|
Throws
Example
String parameters = Utils.encodeParameters(values);
encodeURL
Converts the given string into a x-www-form-urlencoded MIME encoded string.
Syntax
public final static String encodeURL(String url)
Parameters
Returns
Throws
Example
String eurl = Utils.encodeURL(url);
extractCookie
Returns the named Cookie from the given array of Cookies.
Syntax
public final static Cookie extractCookie(Cookie[] cookies,
String name)
public final static Cookie extractCookie(Vector cookies,
String name)
Parameters
|
cookies
|
the array or Vector of cookies to use.
|
|
name
|
the name of the cookie to extract.
|
Returns
|
Cookie
|
the associated Cookie, null if not found.
|
Throws
Example
Cookie cookie = Utils.extractCookie(cookies, key);
getArgs
Returns any arguments sent with request. Arguments are found before any
parameters.
Syntax
public final static String getArgs(HttpServletRequest request)
throws IOException
Parameters
|
request
|
the client's request.
|
Returns
|
String
|
any arguments, null if none found.
|
Throws
Example
String args = Utils.getArgs(request);
getItem
Used to read the specified item.
Syntax
public final static String getItem(File file)
throws IOException
public final static String getItem(String filename)
throws IOException
public final static String getItem(IOHandler handler)
throws IOException
public final static String getItem(InputStream inputstream)
throws IOException
public final static String getItem(Reader reader)
throws IOException
Parameters
|
file
|
a file handle to the item to read.
|
|
filename
|
the name of the item to read.
|
|
handler
|
an IOHandler for the item to read.
|
|
inputstream
|
a java.io.InputStream to the item to read.
|
|
reader
|
a java.io.Reader to the item to read.
|
Returns
Throws
|
IOException
|
Thrown when any IO error occurs.
|
Notes
Reads items relative to the server's base directory when a filename is specified.
Example
String item = Utils.getItem(filename);
getParameters
Returns any parameters sent with request. Parameters are found after any
arguments.
Syntax
public final static String getParameters(
HttpServletRequest request)
Parameters
|
request
|
the client's request.
|
Returns
|
String
|
any parameters, null if none found.
|
Throws
Example
String params = Utils.getParameters(request);
getRootHandler
Returns an IOHandler to the document root directory of the specified virtual host.
Syntax
public final static IOHandler getParameters(String server_name)
throws IOException
Parameters
|
server_name
|
the virtual host name.
|
Returns
|
IOHandler
|
an IOHandler to the document root directory of the specified virtual host.
|
Throws
|
IOException
|
Thrown when any IO error occurs.
|
Example
IOHandler handler = Utils.getRootHandler(hostname);
getConfig
Used to read and parse the specified configuration file.
Syntax
public final static Hashtable getConfig(String filename)
throws IOException
Parameters
|
filename
|
the name of the configuration file to read.
|
Returns
|
Hashtable
|
contains key,value pairs.
|
Throws
|
IOException
|
Thrown when any IO error occurs.
|
Notes
Reads configuration files relative to the server's configuration directory.
Example
Hashtable servlets = Utils.getConfig("./servlets.ini");
getTemplate
Used to read the specified template file.
Syntax
public final static String getTemplate(String filename)
throws IOException
Parameters
|
filename
|
the name of the template file to read.
|
Returns
Throws
|
IOException
|
Thrown when any IO error occurs.
|
Notes
Reads templates files relative to the server's templates directory.
Example
String template = Utils.getTemplate("./messages.html");
mergeParameters
Merges a Hashtable of parameters into another Hashtable of parameters..
Syntax
public final static Hashtable mergeParameters(Hashtable ht1,
Hashtable ht2)
Parameters
|
ht1
|
the hashtable to merge into.
|
|
ht2
|
the hashtable to merge.
|
Returns
|
Hashtable
|
the resultant Hashtable.
|
Throws
Example
params = Utils.mergeParameters(params, tparams);
parseCharacterEncoding
Returns the character set using the given content type.
Syntax
public final static String parseCharacterEncoding(
String contenttype,
String default)
Parameters
|
contenttype
|
the content type.
|
|
default
|
a default value to use.
|
Returns
|
String
|
the character set, if charset is not found then ISO-8859-1 is
used.
|
Throws
Example
String charset = Utils.parseCharacterEncoding(contentType);
parseCookieHeader
Returns an array of Cookies using the given cookie header.
Syntax
public final static Cookie[] parseCookieHeader(String header)
Parameters
|
header
|
the cookie header.
|
Returns
|
Cookie[]
|
an array of Cookies.
|
Throws
Example
Cookie[] cookies = Utils.parseCookieHeader(header);
readLine
Reads a line from the specified input stream to the given byte buffer
starting at the specified offset, up to the given given number of bytes.
Syntax
public final static int readLine(InputStream inputstream,
byte[] buffer,
int offset,
int length)
throws IOException
Parameters
|
inputstream
|
the InputStream to read from.
|
|
buffer
|
the byte array that the line will be read into.
|
|
offset
|
the starting index.
|
|
length
|
the maximum number of bytes to read.
|
Returns
|
int
|
the number of bytes read, -1 if end of stream.
|
Throws
|
IOException.
|
Thrown if an input exception occurred.
|
Example
int bytesRead = Utils.readLine(buffer, 0, buffer.length);
|