java.lang.Object
org.apache.wicket.request.Url
- All Implemented Interfaces:
Serializable
Represents 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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Represents a single query parameterstatic enum
Modes with which urls can be stringized -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addQueryParameter
(String name, Object value) Convenience method that removes adds a query parameter with given nameTry to reduce url by eliminating '..' and '.' from the path where appropriate (this is somehow similar toFile.getCanonicalPath()
).void
concatSegments
(List<String> segments) Concatenate the specified segments; The segments can be relative - begin with "." or "..".boolean
getHost()
Gets the host name of this urlgetPath()
return path for current url in original encodingreturn path for current url in given encodinggetPort()
Gets the port of this urlGets the protocol of this url (http/https/etc)getQueryParameter
(String name) Returns first query parameter with specified name or null if such query parameter doesn't exist.Returns query parameters of the URL.getQueryParameterValue
(String name) Returns the value of first query parameter with specified name.return query string part of url in original encodinggetQueryString
(Charset charset) return query string part of url in given encodingReturns segments of the URL.int
hashCode()
boolean
Returns whether the Url is context absolute.boolean
Tests if the URL is relative to the application context.boolean
Returns whether the Url is a CSS data uri.boolean
isFull()
Returns whether the Url has a host attribute.static Url
parse
(CharSequence url) Parses the given URL string.static Url
parse
(CharSequence _url, Charset charset) Parses the given URL string.static Url
parse
(CharSequence _url, Charset charset, boolean isFullHint) Parses the given URL string.void
prependLeadingSegments
(List<String> newSegments) Convenience method that prependssegments
to the segments collectionvoid
removeLeadingSegments
(int count) Convenience method that removescount
leading segmentsvoid
removeQueryParameters
(String name) Convenience method that removes all query parameters with given name.void
resolveRelative
(Url relative) Makes this url the result of resolving therelative
url against this url.void
setContextRelative
(boolean contextRelative) Flags the URL as relative to the application context.void
setFragment
(String fragment) void
Sets the host name of this urlvoid
Sets the port of this urlvoid
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 valueboolean
toString()
Renders a url withUrl.StringMode.LOCAL
using the url's charsetStringizes this url usingUrl.StringMode.LOCAL
and the specified charsettoString
(Url.StringMode mode) Stringizes this url using the specificUrl.StringMode
and url's charsettoString
(Url.StringMode mode, Charset charset) Stringizes this url
-
Constructor Details
-
Url
public Url()Construct. -
Url
Construct.- Parameters:
charset
-
-
Url
copy constructor- Parameters:
url
- url being copied
-
Url
Construct.- Parameters:
segments
-parameters
-
-
Url
Construct.- Parameters:
segments
-charset
-
-
Url
Construct.- Parameters:
segments
-parameters
-charset
-
-
-
Method Details
-
shouldRenderAsFull
-
parse
Parses the given URL string.- Parameters:
url
- absolute or relative url with query string- Returns:
- Url object
-
parse
Parses the given URL string.- Parameters:
_url
- absolute or relative url with query stringcharset
-- Returns:
- Url object
-
parse
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
- Returns:
- charset
-
getSegments
Returns segments of the URL. Segments form the part before query string.- Returns:
- mutable list of segments
-
getQueryParameters
Returns query parameters of the URL.- Returns:
- mutable list of query parameters
-
getFragment
- Returns:
- fragment
-
setFragment
- Parameters:
fragment
-
-
isContextAbsolute
Returns whether the Url is context absolute. Absolute Urls start with a '/'.- Returns:
true
if Url starts with the context path,false
otherwise.
-
isDataUrl
Returns whether the Url is a CSS data uri. Data uris start with 'data:'.- Returns:
true
if Url starts with 'data:',false
otherwise.
-
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:
true
if Url has a host attribute,false
otherwise.
-
removeQueryParameters
Convenience method that removes all query parameters with given name.- Parameters:
name
- query parameter name
-
removeLeadingSegments
Convenience method that removescount
leading segments- Parameters:
count
-
-
prependLeadingSegments
Convenience method that prependssegments
to the segments collection- Parameters:
newSegments
-
-
setQueryParameter
Convenience method that removes all query parameters with given name and adds new query parameter with specified name and value- Parameters:
name
-value
-
-
addQueryParameter
Convenience method that removes adds a query parameter with given name- Parameters:
name
-value
-
-
getQueryParameter
Returns first query parameter with specified name or null if such query parameter doesn't exist.- Parameters:
name
-- Returns:
- query parameter or
null
-
getQueryParameterValue
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:
StringValue
instance wrapping the parameter value- See Also:
-
equals
-
hashCode
-
toString
Renders a url withUrl.StringMode.LOCAL
using the url's charset -
toString
Stringizes this url- Parameters:
mode
-Url.StringMode
that determins how to stringize the urlcharset
- charset- Returns:
- sringized version of this url
-
toString
Stringizes this url using the specificUrl.StringMode
and url's charset- Parameters:
mode
-Url.StringMode
that determines how to stringize the url- Returns:
- stringized url
-
toString
Stringizes this url usingUrl.StringMode.LOCAL
and the specified charset- Parameters:
charset
-- Returns:
- stringized url
-
concatSegments
Concatenate the specified segments; The segments can be relative - begin with "." or "..".- Parameters:
segments
-
-
resolveRelative
Makes this url the result of resolving therelative
url 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
Gets the protocol of this url (http/https/etc)- Returns:
- protocol or
null
if none has been set
-
setProtocol
Sets the protocol of this url (http/https/etc)- Parameters:
protocol
-
-
setContextRelative
Flags the URL as relative to the application context.- Parameters:
contextRelative
-
-
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
Gets the port of this url- Returns:
- port or
null
if none has been set
-
setPort
Sets the port of this url- Parameters:
port
-
-
getHost
Gets the host name of this url- Returns:
- host name or
null
if none is seto
-
setHost
Sets the host name of this url- Parameters:
host
-
-
getPath
return path for current url in given encoding- Parameters:
charset
- character set for encoding- Returns:
- path string
-
getPath
return path for current url in original encoding- Returns:
- path string
-
getQueryString
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
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
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
-