Package org.apache.wicket.request
Class Url
- java.lang.Object
-
- org.apache.wicket.request.Url
-
- All Implemented Interfaces:
java.io.Serializable
public class Url extends java.lang.Object implements java.io.SerializableRepresents the URL to an external resource or internal resource/component.A url could be:
- full - consists of an optional protocol/scheme, a host name, an optional port, optional segments and and optional query parameters.
- non-full:
- absolute - a url relative to the host name. Such url may escape from the application by using
different context path and/or different filter path. For example:
/foo/bar - relative - a url relative to the current base url. The base url is the url of the currently rendered page.
For example:
foo/bar,../foo/bar
- absolute - a url relative to the host name. Such url may escape from the application by using
different context path and/or different filter path. For example:
- http://hostname:1234/foo/bar?a=b#baz - protocol: http, host: hostname, port: 1234, segments: ["foo","bar"], fragment: baz
- http://hostname:1234/foo/bar?a=b - protocol: http, host: hostname, port: 1234, segments: ["foo","bar"]
- //hostname:1234/foo/bar?a=b - protocol: null, host: hostname, port: 1234, segments: ["foo","bar"]
- 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"]
- Author:
- Matej Knopp, Igor Vaynberg
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classUrl.QueryParameterRepresents a single query parameterstatic classUrl.StringModeModes with which urls can be stringized
-
Constructor Summary
Constructors Constructor Description Url()Construct.Url(java.nio.charset.Charset charset)Construct.Url(java.util.List<java.lang.String> segments, java.nio.charset.Charset charset)Construct.Url(java.util.List<java.lang.String> segments, java.util.List<Url.QueryParameter> parameters)Construct.Url(java.util.List<java.lang.String> segments, java.util.List<Url.QueryParameter> parameters, java.nio.charset.Charset charset)Construct.Url(Url url)copy constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddQueryParameter(java.lang.String name, java.lang.Object value)Convenience method that removes adds a query parameter with given nameUrlcanonical()Try to reduce url by eliminating '..' and '.' from the path where appropriate (this is somehow similar toFile.getCanonicalPath()).voidconcatSegments(java.util.List<java.lang.String> segments)Concatenate the specified segments; The segments can be relative - begin with "." or "..".booleanequals(java.lang.Object obj)java.nio.charset.CharsetgetCharset()java.lang.StringgetFragment()java.lang.StringgetHost()Gets the host name of this urljava.lang.StringgetPath()return path for current url in original encodingjava.lang.StringgetPath(java.nio.charset.Charset charset)return path for current url in given encodingjava.lang.IntegergetPort()Gets the port of this urljava.lang.StringgetProtocol()Gets the protocol of this url (http/https/etc)Url.QueryParametergetQueryParameter(java.lang.String name)Returns first query parameter with specified name or null if such query parameter doesn't exist.java.util.List<Url.QueryParameter>getQueryParameters()Returns query parameters of the URL.org.apache.wicket.util.string.StringValuegetQueryParameterValue(java.lang.String name)Returns the value of first query parameter with specified name.java.lang.StringgetQueryString()return query string part of url in original encodingjava.lang.StringgetQueryString(java.nio.charset.Charset charset)return query string part of url in given encodingjava.util.List<java.lang.String>getSegments()Returns segments of the URL.inthashCode()booleanisContextAbsolute()Returns whether the Url is context absolute.booleanisContextRelative()Tests if the URL is relative to the application context.booleanisDataUrl()Returns whether the Url is a CSS data uri.booleanisFull()Returns whether the Url has a host attribute.static Urlparse(java.lang.CharSequence url)Parses the given URL string.static Urlparse(java.lang.CharSequence _url, java.nio.charset.Charset charset)Parses the given URL string.static Urlparse(java.lang.CharSequence _url, java.nio.charset.Charset charset, boolean isFullHint)Parses the given URL string.voidprependLeadingSegments(java.util.List<java.lang.String> newSegments)Convenience method that prependssegmentsto the segments collectionvoidremoveLeadingSegments(int count)Convenience method that removescountleading segmentsvoidremoveQueryParameters(java.lang.String name)Convenience method that removes all query parameters with given name.voidresolveRelative(Url relative)Makes this url the result of resolving therelativeurl against this url.voidsetContextRelative(boolean contextRelative)Flags the URL as relative to the application context.voidsetFragment(java.lang.String fragment)voidsetHost(java.lang.String host)Sets the host name of this urlvoidsetPort(java.lang.Integer port)Sets the port of this urlvoidsetProtocol(java.lang.String protocol)Sets the protocol of this url (http/https/etc)voidsetQueryParameter(java.lang.String name, java.lang.Object value)Convenience method that removes all query parameters with given name and adds new query parameter with specified name and valuejava.lang.StringtoString()Renders a url withUrl.StringMode.LOCALusing the url's charsetjava.lang.StringtoString(java.nio.charset.Charset charset)Stringizes this url usingUrl.StringMode.LOCALand the specified charsetjava.lang.StringtoString(Url.StringMode mode)Stringizes this url using the specificUrl.StringModeand url's charsetjava.lang.StringtoString(Url.StringMode mode, java.nio.charset.Charset charset)Stringizes this url
-
-
-
Constructor Detail
-
Url
public Url()
Construct.
-
Url
public Url(java.nio.charset.Charset charset)
Construct.- Parameters:
charset-
-
Url
public Url(Url url)
copy constructor- Parameters:
url- url being copied
-
Url
public Url(java.util.List<java.lang.String> segments, java.util.List<Url.QueryParameter> parameters)Construct.- Parameters:
segments-parameters-
-
Url
public Url(java.util.List<java.lang.String> segments, java.nio.charset.Charset charset)Construct.- Parameters:
segments-charset-
-
Url
public Url(java.util.List<java.lang.String> segments, java.util.List<Url.QueryParameter> parameters, java.nio.charset.Charset charset)Construct.- Parameters:
segments-parameters-charset-
-
-
Method Detail
-
parse
public static Url parse(java.lang.CharSequence url)
Parses the given URL string.- Parameters:
url- absolute or relative url with query string- Returns:
- Url object
-
parse
public static Url parse(java.lang.CharSequence _url, java.nio.charset.Charset charset)
Parses the given URL string.- Parameters:
_url- absolute or relative url with query stringcharset-- Returns:
- Url object
-
parse
public static Url parse(java.lang.CharSequence _url, java.nio.charset.Charset charset, boolean isFullHint)
Parses the given URL string.- Parameters:
_url- absolute or relative url with query stringcharset-isFullHint- a hint whether to try to parse the protocol, host and port part of the url- Returns:
- Url object
-
getCharset
public java.nio.charset.Charset getCharset()
- Returns:
- charset
-
getSegments
public java.util.List<java.lang.String> getSegments()
Returns segments of the URL. Segments form the part before query string.- Returns:
- mutable list of segments
-
getQueryParameters
public java.util.List<Url.QueryParameter> getQueryParameters()
Returns query parameters of the URL.- Returns:
- mutable list of query parameters
-
getFragment
public java.lang.String getFragment()
- Returns:
- fragment
-
setFragment
public void setFragment(java.lang.String fragment)
- Parameters:
fragment-
-
isContextAbsolute
public boolean isContextAbsolute()
Returns whether the Url is context absolute. Absolute Urls start with a '/'.- Returns:
trueif Url starts with the context path,falseotherwise.
-
isDataUrl
public boolean isDataUrl()
Returns whether the Url is a CSS data uri. Data uris start with 'data:'.- Returns:
trueif Url starts with 'data:',falseotherwise.
-
isFull
public boolean isFull()
Returns whether the Url has a host attribute. The scheme is optional because the url may be//host/path. The port is also optional because there are defaults for the different protocols.- Returns:
trueif Url has a host attribute,falseotherwise.
-
removeQueryParameters
public void removeQueryParameters(java.lang.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 removescountleading segments- Parameters:
count-
-
prependLeadingSegments
public void prependLeadingSegments(java.util.List<java.lang.String> newSegments)
Convenience method that prependssegmentsto the segments collection- Parameters:
newSegments-
-
setQueryParameter
public void setQueryParameter(java.lang.String name, java.lang.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(java.lang.String name, java.lang.Object value)Convenience method that removes adds a query parameter with given name- Parameters:
name-value-
-
getQueryParameter
public Url.QueryParameter getQueryParameter(java.lang.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 org.apache.wicket.util.string.StringValue getQueryParameterValue(java.lang.String name)
Returns the value of first query parameter with specified name. Note that this method never returnsnull. Not even if the parameter does not exist.- Parameters:
name-- Returns:
StringValueinstance wrapping the parameter value- See Also:
StringValue.isNull()
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
Renders a url withUrl.StringMode.LOCALusing the url's charset- Overrides:
toStringin classjava.lang.Object
-
toString
public java.lang.String toString(Url.StringMode mode, java.nio.charset.Charset charset)
Stringizes this url- Parameters:
mode-Url.StringModethat determins how to stringize the urlcharset- charset- Returns:
- sringized version of this url
-
toString
public java.lang.String toString(Url.StringMode mode)
Stringizes this url using the specificUrl.StringModeand url's charset- Parameters:
mode-Url.StringModethat determines how to stringize the url- Returns:
- stringized url
-
toString
public java.lang.String toString(java.nio.charset.Charset charset)
Stringizes this url usingUrl.StringMode.LOCALand the specified charset- Parameters:
charset-- Returns:
- stringized url
-
concatSegments
public void concatSegments(java.util.List<java.lang.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 therelativeurl against this url.Segments will be properly resolved, handling any
..references, while the query parameters will be completely replaced withrelative's query parameters.For example:
wicket/page/render?foo=bar
resolved with../component/render?a=b
will becomewicket/component/render?a=b
- Parameters:
relative- relative url
-
getProtocol
public java.lang.String getProtocol()
Gets the protocol of this url (http/https/etc)- Returns:
- protocol or
nullif none has been set
-
setProtocol
public void setProtocol(java.lang.String protocol)
Sets the protocol of this url (http/https/etc)- Parameters:
protocol-
-
setContextRelative
public void setContextRelative(boolean contextRelative)
Flags the URL as relative to the application context.- Parameters:
contextRelative-
-
isContextRelative
public boolean isContextRelative()
Tests if the URL is relative to the application context. If so, it holds all the information an absolute URL would have, minus the context and filter mapping segments- Returns:
- contextRelative
-
getPort
public java.lang.Integer getPort()
Gets the port of this url- Returns:
- port or
nullif none has been set
-
setPort
public void setPort(java.lang.Integer port)
Sets the port of this url- Parameters:
port-
-
getHost
public java.lang.String getHost()
Gets the host name of this url- Returns:
- host name or
nullif none is seto
-
setHost
public void setHost(java.lang.String host)
Sets the host name of this url- Parameters:
host-
-
getPath
public java.lang.String getPath(java.nio.charset.Charset charset)
return path for current url in given encoding- Parameters:
charset- character set for encoding- Returns:
- path string
-
getPath
public java.lang.String getPath()
return path for current url in original encoding- Returns:
- path string
-
getQueryString
public java.lang.String getQueryString(java.nio.charset.Charset charset)
return query string part of url in given encoding- Parameters:
charset- character set for encoding- Returns:
- query string (null if empty)
- Since:
- Wicket 7 the return value does not contain any "?" and could be null
-
getQueryString
public java.lang.String getQueryString()
return query string part of url in original encoding- Returns:
- query string (null if empty)
- Since:
- Wicket 7 the return value does not contain any "?" and could be null
-
canonical
public Url canonical()
Try to reduce url by eliminating '..' and '.' from the path where appropriate (this is somehow similar toFile.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- Returns:
- canonical url
-
-