org.mortbay.util
Class URI

java.lang.Object
  extended by org.mortbay.util.URI
All Implemented Interfaces:
java.lang.Cloneable

public class URI
extends java.lang.Object
implements java.lang.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.40 2009/05/16 02:02:00 gregwilkins Exp $
Author:
Greg Wilkins (gregw)
See Also:
UrlEncoded

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

Field Detail

__CHARSET

public static final java.lang.String __CHARSET

__CHARSET_IS_DEFAULT

public static final boolean __CHARSET_IS_DEFAULT
Constructor Detail

URI

public URI(URI uri)
    throws java.lang.IllegalArgumentException
Copy Constructor .

Parameters:
uri -
Throws:
java.lang.IllegalArgumentException

URI

public URI(java.lang.String uri)
    throws java.lang.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]
Throws:
java.lang.IllegalArgumentException
Method Detail

setURI

public void setURI(java.lang.String uri)
            throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

isAbsolute

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

Returns:
True if the URI has a scheme or host

getScheme

public java.lang.String getScheme()
Get the uri scheme.

Returns:
the URI scheme

setScheme

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

Parameters:
scheme - the uri scheme

getHost

public java.lang.String getHost()
Get the uri host.

Returns:
the URI host

setHost

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

Parameters:
host - the uri host

getPort

public int getPort()
Get the uri port.

Returns:
the URI port

setPort

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

Parameters:
port - the uri port

getPath

public java.lang.String getPath()
Get the uri path.

Returns:
the URI path

getEncodedPath

public java.lang.String getEncodedPath()
Get the encoded uri path.

Returns:
the URI path

setPath

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

Parameters:
path - the URI path

getQuery

public java.lang.String getQuery()
Get the uri query String.

Returns:
the URI query string

setQuery

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

Parameters:
query - the URI query string

getParameterNames

public java.util.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

getUnmodifiableParameters

public java.util.Map getUnmodifiableParameters()
Get the uri query _parameters.

Returns:
the URI query _parameters in an unmodifiable map.

putParametersTo

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


clearParameters

public void clearParameters()
Clear the URI _parameters.


put

public void put(java.lang.String encoded)
Add encoded _parameters.

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

put

public java.lang.Object put(java.lang.Object name,
                            java.lang.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(java.util.Map values)
Add dictionary to the uri query _parameters.


get

public java.lang.String get(java.lang.String name)
Get named value


getValues

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

Parameters:
name - The parameter name
Returns:
Umodifiable list of values or null

remove

public void remove(java.lang.String name)
Remove named value


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
the URI string encoded.

encodePath

public static java.lang.String encodePath(java.lang.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 java.lang.StringBuffer encodePath(java.lang.StringBuffer buf,
                                                java.lang.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 java.lang.StringBuffer encodeString(java.lang.StringBuffer buf,
                                                  java.lang.String path,
                                                  java.lang.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.

decodePath

public static java.lang.String decodePath(java.lang.String path)

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Clone URI.

Overrides:
clone in class java.lang.Object
Returns:
cloned URI
Throws:
java.lang.CloneNotSupportedException

addPaths

public static java.lang.String addPaths(java.lang.String p1,
                                        java.lang.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.

parentPath

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


stripPath

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


canonicalPath

public static java.lang.String canonicalPath(java.lang.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.

hasScheme

public static boolean hasScheme(java.lang.String uri)
Parameters:
uri - URI
Returns:
True if the uri has a scheme


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