public class Url extends Object implements Serializable
A url could be:
/foo/bar
foo/bar
, ../foo/bar
Modifier and Type | Class and Description |
---|---|
static class |
Url.QueryParameter
Represents a single query parameter
|
static class |
Url.StringMode
Modes with which urls can be stringized
|
Constructor and Description |
---|
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)
copy constructor
|
Modifier and Type | Method and Description |
---|---|
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 |
getFragment() |
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 |
isContextAbsolute()
Returns whether the Url is context absolute.
|
boolean |
isContextRelative()
Tests if the URL is relative to the application context.
|
boolean |
isDataUrl()
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 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 |
setContextRelative(boolean contextRelative)
Flags the URL as relative to the application context.
|
void |
setFragment(String fragment) |
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
|
boolean |
shouldRenderAsFull() |
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 specific
Url.StringMode and url's charset |
String |
toString(Url.StringMode mode,
Charset charset)
Stringizes this url
|
public Url()
public Url(Charset charset)
charset
- public Url(Url url)
url
- url being copiedpublic Url(List<String> segments, List<Url.QueryParameter> parameters)
segments
- parameters
- public Url(List<String> segments, List<Url.QueryParameter> parameters, Charset charset)
segments
- parameters
- charset
- public boolean shouldRenderAsFull()
public static Url parse(CharSequence url)
url
- absolute or relative url with query stringpublic static Url parse(CharSequence _url, Charset charset)
_url
- absolute or relative url with query stringcharset
- public static Url parse(CharSequence _url, Charset charset, boolean isFullHint)
_url
- absolute or relative url with query stringcharset
- isFullHint
- a hint whether to try to parse the protocol, host and port part of the urlpublic Charset getCharset()
public List<String> getSegments()
public List<Url.QueryParameter> getQueryParameters()
public String getFragment()
public void setFragment(String fragment)
fragment
- public boolean isContextAbsolute()
true
if Url starts with the context path, false
otherwise.public boolean isDataUrl()
true
if Url starts with 'data:', false
otherwise.public boolean isFull()
//host/path
.
The port is also optional because there are defaults for the different protocols.true
if Url has a host attribute, false
otherwise.public void removeQueryParameters(String name)
name
- query parameter namepublic void removeLeadingSegments(int count)
count
leading segmentscount
- public void prependLeadingSegments(List<String> newSegments)
segments
to the segments collectionnewSegments
- public void setQueryParameter(String name, Object value)
name
- value
- public void addQueryParameter(String name, Object value)
name
- value
- public Url.QueryParameter getQueryParameter(String name)
name
- null
public StringValue getQueryParameterValue(String name)
null
. Not even if the parameter does not exist.name
- StringValue
instance wrapping the parameter valueStringValue.isNull()
public String toString()
Url.StringMode.LOCAL
using the url's charsetpublic String toString(Url.StringMode mode, Charset charset)
mode
- Url.StringMode
that determins how to stringize the urlcharset
- charsetpublic String toString(Url.StringMode mode)
Url.StringMode
and url's charsetmode
- Url.StringMode
that determines how to stringize the urlpublic String toString(Charset charset)
Url.StringMode.LOCAL
and the specified charsetcharset
- public void concatSegments(List<String> segments)
segments
- public void resolveRelative(Url relative)
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=barresolved with
../component/render?a=bwill become
wicket/component/render?a=b
relative
- relative urlpublic String getProtocol()
null
if none has been setpublic void setProtocol(String protocol)
protocol
- public void setContextRelative(boolean contextRelative)
contextRelative
- public boolean isContextRelative()
public Integer getPort()
null
if none has been setpublic void setPort(Integer port)
port
- public String getHost()
null
if none is setopublic void setHost(String host)
host
- public String getPath(Charset charset)
charset
- character set for encodingpublic String getPath()
public String getQueryString(Charset charset)
charset
- character set for encodingpublic String getQueryString()
public Url canonical()
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?0will 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
Copyright © 2006–2022 Apache Software Foundation. All rights reserved.