org.mortbay.util

Class URI

public class URI extends Object implements Cloneable

URI Holder. This class assists with the decoding and encoding or HTTP URI's. It differs from the java.net.URL class as it does not provide communications ability, but it does assist with query string formatting.

ISO_8859_1 encoding is used by default for % encoded characters. This may be overridden with the org.mortbay.util.URI.charset system property.

Version: $Id: URI.java,v 1.39 2006/01/04 13:55:31 gregwilkins Exp $

Author: Greg Wilkins (gregw)

See Also: UrlEncoded

Field Summary
static String__CHARSET
static boolean__CHARSET_IS_DEFAULT
Constructor Summary
URI(URI uri)
Copy Constructor .
URI(String uri)
Construct from a String.
Method Summary
static StringaddPaths(String p1, String p2)
Add two URI path segments.
static StringcanonicalPath(String path)
Convert a path to a cananonical form.
voidclearParameters()
Clear the URI _parameters.
Objectclone()
Clone URI.
static StringdecodePath(String path)
static StringencodePath(String path)
Encode a URI path.
static StringBufferencodePath(StringBuffer buf, String path)
Encode a URI path.
static StringBufferencodeString(StringBuffer buf, String path, String encode)
Encode a URI path.
Stringget(String name)
Get named value
StringgetEncodedPath()
Get the encoded uri path.
StringgetHost()
Get the uri host.
SetgetParameterNames()
Get the uri query _parameters names.
MultiMapgetParameters()
Get the uri query _parameters.
StringgetPath()
Get the uri path.
intgetPort()
Get the uri port.
StringgetQuery()
Get the uri query String.
StringgetScheme()
Get the uri scheme.
MapgetUnmodifiableParameters()
Get the uri query _parameters.
ListgetValues(String name)
Get named multiple values.
static booleanhasScheme(String uri)
booleanisAbsolute()
Is the URI an absolute URL?
static StringparentPath(String p)
Return the parent Path.
voidput(String encoded)
Add encoded _parameters.
Objectput(Object name, Object value)
Add name value pair to the uri query _parameters.
voidput(Map values)
Add dictionary to the uri query _parameters.
voidputParametersTo(MultiMap map)
Add the uri query _parameters to a MultiMap
voidremove(String name)
Remove named value
voidsetHost(String host)
Set the uri host.
voidsetPath(String path)
Set the uri path.
voidsetPort(int port)
Set the uri port.
voidsetQuery(String query)
Set the uri query String.
voidsetScheme(String scheme)
Set the uri scheme.
voidsetURI(String uri)
static StringstripPath(String path)
Strip parameters from a path.
StringtoString()

Field Detail

__CHARSET

public static final String __CHARSET

__CHARSET_IS_DEFAULT

public static final boolean __CHARSET_IS_DEFAULT

Constructor Detail

URI

public URI(URI uri)
Copy Constructor .

Parameters: uri

URI

public URI(String uri)
Construct from a String. The string must contain a URI path, but optionaly may contain a scheme, host, port and query string.

Parameters: uri [scheme://host[:port]]/path[?query]

Method Detail

addPaths

public static String addPaths(String p1, String p2)
Add two URI path segments. Handles null and empty paths, path and query params (eg ?a=b or ;JSESSIONID=xxx) and avoids duplicate '/'

Parameters: p1 URI path segment p2 URI path segment

Returns: Legally combined path segments.

canonicalPath

public static String canonicalPath(String path)
Convert a path to a cananonical form. All instances of "." and ".." are factored out. Null is returned if the path tries to .. above it's root.

Parameters: path

Returns: path or null.

clearParameters

public void clearParameters()
Clear the URI _parameters.

clone

public Object clone()
Clone URI.

Returns: cloned URI

decodePath

public static String decodePath(String path)

encodePath

public static String encodePath(String path)
Encode a URI path. This is the same encoding offered by URLEncoder, except that the '/' character is not encoded.

Parameters: path The path the encode

Returns: The encoded path

encodePath

public static StringBuffer encodePath(StringBuffer buf, String path)
Encode a URI path.

Parameters: path The path the encode buf StringBuffer to encode path into (or null)

Returns: The StringBuffer or null if no substitutions required.

encodeString

public static StringBuffer encodeString(StringBuffer buf, String path, String encode)
Encode a URI path.

Parameters: path The path the encode buf StringBuffer to encode path into (or null) encode String of characters to encode. % is always encoded.

Returns: The StringBuffer or null if no substitutions required.

get

public String get(String name)
Get named value

getEncodedPath

public String getEncodedPath()
Get the encoded uri path.

Returns: the URI path

getHost

public String getHost()
Get the uri host.

Returns: the URI host

getParameterNames

public Set getParameterNames()
Get the uri query _parameters names.

Returns: Unmodifiable set of URI query _parameters names

getParameters

public MultiMap getParameters()
Get the uri query _parameters.

Returns: the URI query _parameters

getPath

public String getPath()
Get the uri path.

Returns: the URI path

getPort

public int getPort()
Get the uri port.

Returns: the URI port

getQuery

public String getQuery()
Get the uri query String.

Returns: the URI query string

getScheme

public String getScheme()
Get the uri scheme.

Returns: the URI scheme

getUnmodifiableParameters

public Map getUnmodifiableParameters()
Get the uri query _parameters.

Returns: the URI query _parameters in an unmodifiable map.

getValues

public List getValues(String name)
Get named multiple values.

Parameters: name The parameter name

Returns: Umodifiable list of values or null

hasScheme

public static boolean hasScheme(String uri)

Parameters: uri URI

Returns: True if the uri has a scheme

isAbsolute

public boolean isAbsolute()
Is the URI an absolute URL?

Returns: True if the URI has a scheme or host

parentPath

public static String parentPath(String p)
Return the parent Path. Treat a URI like a directory path and return the parent directory.

put

public void put(String encoded)
Add encoded _parameters.

Parameters: encoded A HTTP encoded string of _parameters: e.g.. "a=1&b=2"

put

public Object put(Object name, Object value)
Add name value pair to the uri query _parameters.

Parameters: name name of value value The value, which may be a multi valued list or String array.

put

public void put(Map values)
Add dictionary to the uri query _parameters.

putParametersTo

public void putParametersTo(MultiMap map)
Add the uri query _parameters to a MultiMap

remove

public void remove(String name)
Remove named value

setHost

public void setHost(String host)
Set the uri host.

Parameters: host the uri host

setPath

public void setPath(String path)
Set the uri path.

Parameters: path the URI path

setPort

public void setPort(int port)
Set the uri port. A port of 0 implies use the default port.

Parameters: port the uri port

setQuery

public void setQuery(String query)
Set the uri query String.

Parameters: query the URI query string

setScheme

public void setScheme(String scheme)
Set the uri scheme.

Parameters: scheme the uri scheme

setURI

public void setURI(String uri)

stripPath

public static String stripPath(String path)
Strip parameters from a path. Return path upto any semicolon parameters.

toString

public String toString()

Returns: the URI string encoded.

Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.