org.apache.wicket.request
Class Url

java.lang.Object
  extended by org.apache.wicket.request.Url
All Implemented Interfaces:
Serializable

public class Url
extends Object
implements Serializable

Represents the URL part after Wicket Filter. For example if Wicket Filter is mapped to /app/* then with URL /app/my/url the Url object would represent part my/url. If Wicket Filter is mapped to /* then with URL /my/url the Url object would represent my/url (without leading the slash).

URL consists of segments and query parameters.

Example URLs:

 foo/bar/baz?a=1&b=5    - segments: ["foo","bar,"baz], query parameters: ["a"="1", "b"="5"]
 foo/bar//baz?=4&6      - segments: ["foo", "bar", "", "baz"], query parameters: [""="4", "6"=""]
 /foo/bar/              - segments: ["", "foo", "bar", ""]
 foo/bar//              - segments: ["foo", "bar", "", ""]
 ?a=b                   - segments: [ ], query parameters: ["a"="b"]
 /                      - segments: ["", ""]   (note that Url represents part after Wicket Filter 
                                                - so if Wicket filter is mapped to /* this would be
                                                an additional slash, i.e. //
 
The Url class takes care of encoding and decoding of the segments and parameters.

Author:
Matej Knopp, Igor Vaynberg
See Also:
Serialized Form

Nested Class Summary
static class Url.QueryParameter
          Represents a single query parameter
static class Url.StringMode
          Modes with which urls can be stringized
 
Constructor Summary
Url()
          Construct.
Url(Charset charset)
          Construct.
Url(List<String> segments, Charset charset)
          Construct.
Url(List<String> segments, List<Url.QueryParameter> parameters)
          Construct.
Url(List<String> segments, List<Url.QueryParameter> parameters, Charset charset)
          Construct.
Url(Url url)
          Construct.
 
Method Summary
 void addQueryParameter(String name, Object value)
          Convenience method that removes adds a query parameter with given name
 Url canonical()
          Try to reduce url by eliminating '..' and '.' from the path where appropriate (this is somehow similar to File.getCanonicalPath()).
 void concatSegments(List<String> segments)
          Concatenate the specified segments; The segments can be relative - begin with "." or "..".
 boolean equals(Object obj)
          
 Charset getCharset()
           
 String getHost()
          Gets the host name of this url
 String getPath()
          return path for current url in original encoding
 String getPath(Charset charset)
          return path for current url in given encoding
 Integer getPort()
          Gets the port of this url
 String getProtocol()
          Gets the protocol of this url (http/https/etc)
 Url.QueryParameter getQueryParameter(String name)
          Returns first query parameter with specified name or null if such query parameter doesn't exist.
 List<Url.QueryParameter> getQueryParameters()
          Returns query parameters of the URL.
 StringValue getQueryParameterValue(String name)
          Returns the value of first query parameter with specified name.
 String getQueryString()
          return query string part of url in original encoding
 String getQueryString(Charset charset)
          return query string part of url in given encoding
 List<String> getSegments()
          Returns segments of the URL.
 int hashCode()
          
 boolean isAbsolute()
          Returns whether the Url is absolute.
static Url parse(String url)
          Parses the given URL string.
static Url parse(String url, Charset charset)
          Parses the given URL string.
 void prependLeadingSegments(List<String> newSegments)
          Convenience method that prepends segments to the segments collection
 void removeLeadingSegments(int count)
          Convenience method that removes count leading segments
 void removeQueryParameters(String name)
          Convenience method that removes all query parameters with given name.
 void resolveRelative(Url relative)
          Makes this url the result of resolving the relative url against this url.
 void setHost(String host)
          Sets the host name of this url
 void setPort(Integer port)
          Sets the port of this url
 void setProtocol(String protocol)
          Sets the protocol of this url (http/https/etc)
 void setQueryParameter(String name, Object value)
          Convenience method that removes all query parameters with given name and adds new query parameter with specified name and value
 String toAbsoluteString()
          Deprecated. see toString(StringMode)
 String toAbsoluteString(Charset charset)
          Deprecated. see toString(StringMode, Charset)
 String toString()
          Renders a url with Url.StringMode.LOCAL using the url's charset
 String toString(Charset charset)
          Stringizes this url using Url.StringMode.LOCAL and the specified charset
 String toString(Url.StringMode mode)
          Stringizes this url using the specifid Url.StringMode and url's charset
 String toString(Url.StringMode mode, Charset charset)
          Stringizes this url
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Url

public Url()
Construct.


Url

public Url(Charset charset)
Construct.

Parameters:
charset -

Url

public Url(Url url)
Construct.

Parameters:
url - url being copied

Url

public Url(List<String> segments,
           List<Url.QueryParameter> parameters)
Construct.

Parameters:
segments -
parameters -

Url

public Url(List<String> segments,
           Charset charset)
Construct.

Parameters:
segments -
charset -

Url

public Url(List<String> segments,
           List<Url.QueryParameter> parameters,
           Charset charset)
Construct.

Parameters:
segments -
parameters -
charset -
Method Detail

parse

public static Url parse(String url)
Parses the given URL string.

Parameters:
url - absolute or relative url with query string
Returns:
Url object

parse

public static Url parse(String url,
                        Charset charset)
Parses the given URL string.

Parameters:
url - absolute or relative url with query string
charset -
Returns:
Url object

getCharset

public Charset getCharset()
Returns:
charset

getSegments

public List<String> getSegments()
Returns segments of the URL. Segments form the part before query string.

Returns:
mutable list of segments

getQueryParameters

public List<Url.QueryParameter> getQueryParameters()
Returns query parameters of the URL.

Returns:
mutable list of query parameters

isAbsolute

public boolean isAbsolute()
Returns whether the Url is absolute. Absolute Urls start with a '/'.

Returns:
true if Url is absolute, false otherwise.

removeQueryParameters

public void removeQueryParameters(String name)
Convenience method that removes all query parameters with given name.

Parameters:
name - query parameter name

removeLeadingSegments

public void removeLeadingSegments(int count)
Convenience method that removes count leading segments

Parameters:
count -

prependLeadingSegments

public void prependLeadingSegments(List<String> newSegments)
Convenience method that prepends segments to the segments collection

Parameters:
newSegments -

setQueryParameter

public void setQueryParameter(String name,
                              Object value)
Convenience method that removes all query parameters with given name and adds new query parameter with specified name and value

Parameters:
name -
value -

addQueryParameter

public void addQueryParameter(String name,
                              Object value)
Convenience method that removes adds a query parameter with given name

Parameters:
name -
value -

getQueryParameter

public Url.QueryParameter getQueryParameter(String name)
Returns first query parameter with specified name or null if such query parameter doesn't exist.

Parameters:
name -
Returns:
query parameter or null

getQueryParameterValue

public StringValue getQueryParameterValue(String name)
Returns the value of first query parameter with specified name. Note that this method never returns null. Not even if the parameter does not exist.

Parameters:
name -
Returns:
StringValue instance wrapping the parameter value
See Also:
StringValue.isNull()

equals

public boolean equals(Object obj)

Overrides:
equals in class Object

hashCode

public int hashCode()

Overrides:
hashCode in class Object

toString

public String toString()
Renders a url with Url.StringMode.LOCAL using the url's charset

Overrides:
toString in class Object

toAbsoluteString

@Deprecated
public String toAbsoluteString()
Deprecated. see toString(StringMode)

render full representation of url (including protocol, host and port) into string representation

Returns:
absolute representation of the url

toAbsoluteString

@Deprecated
public String toAbsoluteString(Charset charset)
Deprecated. see toString(StringMode, Charset)

render full representation of url (including protocol, host and port) into string representation

Parameters:
charset -
Returns:
see toStringRepresentation

toString

public String toString(Url.StringMode mode,
                       Charset charset)
Stringizes this url

Parameters:
mode - Url.StringMode that determins how to stringize the url
charset - charset
Returns:
sringized version of this url

toString

public String toString(Url.StringMode mode)
Stringizes this url using the specifid Url.StringMode and url's charset

Parameters:
mode - Url.StringMode that determins how to stringize the url
Returns:
stringized url

toString

public String toString(Charset charset)
Stringizes this url using Url.StringMode.LOCAL and the specified charset

Parameters:
charset -
Returns:
stringized url

concatSegments

public void concatSegments(List<String> segments)
Concatenate the specified segments; The segments can be relative - begin with "." or "..".

Parameters:
segments -

resolveRelative

public void resolveRelative(Url relative)
Makes this url the result of resolving the relative url against this url.

Segments will be properly resolved, handling any .. references, while the query parameters will be completely replaced with relative's query parameters.

For example:

 wicket/page/render?foo=bar
 
resolved with
 ../component/render?a=b
 
will become
 wicket/component/render?a=b
 

Parameters:
relative - relative url

getProtocol

public String getProtocol()
Gets the protocol of this url (http/https/etc)

Returns:
protocol or null if none has been set

setProtocol

public void setProtocol(String protocol)
Sets the protocol of this url (http/https/etc)

Parameters:
protocol -

getPort

public Integer getPort()
Gets the port of this url

Returns:
port or null if none has been set

setPort

public void setPort(Integer port)
Sets the port of this url

Parameters:
port -

getHost

public String getHost()
Gets the host name of this url

Returns:
host name or null if none is seto

setHost

public void setHost(String host)
Sets the host name of this url

Parameters:
host -

getPath

public String getPath(Charset charset)
return path for current url in given encoding

Parameters:
charset - character set for encoding
Returns:
path string

getPath

public String getPath()
return path for current url in original encoding

Returns:
path string

getQueryString

public String getQueryString(Charset charset)
return query string part of url in given encoding

Parameters:
charset - character set for encoding
Returns:
query string

getQueryString

public String getQueryString()
return query string part of url in original encoding

Returns:
query string

canonical

public Url canonical()
Try to reduce url by eliminating '..' and '.' from the path where appropriate (this is somehow similar to File.getCanonicalPath()). Either by different / unexpected browser behavior or by malicious attacks it can happen that these kind of redundant urls are processed by wicket. These urls can cause some trouble when mapping the request.

example: the url

  /example/..;jsessionid=234792?0
will not get normalized by the browser due to the ';jsessionid' string that gets appended by the servlet container. After wicket strips the jsessionid part the resulting internal url will be
  /example/..
instead of
  /

This code correlates to WICKET-4303

Parameters:
url - unprocessed url
Returns:
canonical url


Copyright © 2006-2012 Apache Software Foundation. All Rights Reserved.