org.mortbay.util

Class URI

Implemented Interfaces:
Cloneable

public class URI
extends java.lang.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(String uri)
Construct from a String.
URI(URI uri)
Copy Constructor .

Method Summary

static String
addPaths(String p1, String p2)
Add two URI path segments.
static String
canonicalPath(String path)
Convert a path to a cananonical form.
void
clearParameters()
Clear the URI _parameters.
Object
clone()
Clone URI.
static String
decodePath(String path)
static String
encodePath(String path)
Encode a URI path.
static StringBuffer
encodePath(StringBuffer buf, String path)
Encode a URI path.
static StringBuffer
encodeString(StringBuffer buf, String path, String encode)
Encode a URI path.
String
get(String name)
Get named value
String
getEncodedPath()
Get the encoded uri path.
String
getHost()
Get the uri host.
Set
getParameterNames()
Get the uri query _parameters names.
MultiMap
getParameters()
Get the uri query _parameters.
String
getPath()
Get the uri path.
int
getPort()
Get the uri port.
String
getQuery()
Get the uri query String.
String
getScheme()
Get the uri scheme.
Map
getUnmodifiableParameters()
Get the uri query _parameters.
List
getValues(String name)
Get named multiple values.
static boolean
hasScheme(String uri)
boolean
isAbsolute()
Is the URI an absolute URL?
static String
parentPath(String p)
Return the parent Path.
void
put(Map values)
Add dictionary to the uri query _parameters.
Object
put(Object name, Object value)
Add name value pair to the uri query _parameters.
void
put(String encoded)
Add encoded _parameters.
void
putParametersTo(MultiMap map)
Add the uri query _parameters to a MultiMap
void
remove(String name)
Remove named value
void
setHost(String host)
Set the uri host.
void
setPath(String path)
Set the uri path.
void
setPort(int port)
Set the uri port.
void
setQuery(String query)
Set the uri query String.
void
setScheme(String scheme)
Set the uri scheme.
void
setURI(String uri)
static String
stripPath(String path)
Strip parameters from a path.
String
toString()

Field Details

__CHARSET

public static final String __CHARSET

__CHARSET_IS_DEFAULT

public static final boolean __CHARSET_IS_DEFAULT

Constructor Details

URI

public URI(String uri)
            throws IllegalArgumentException
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]

URI

public URI(URI uri)
            throws IllegalArgumentException
Copy Constructor .
Parameters:
uri -

Method Details

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()
            throws CloneNotSupportedException
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:
buf - StringBuffer to encode path into (or null)
path - The path the encode
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:
buf - StringBuffer to encode path into (or null)
path - The path the encode
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(Map values)
Add dictionary to the uri query _parameters.

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(String encoded)
Add encoded _parameters.
Parameters:
encoded - A HTTP encoded string of _parameters: e.g.. "a=1&b=2"

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)
            throws IllegalArgumentException

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.