de.innovationgate.utils
public class URLBuilder extends java.lang.Object implements java.lang.Cloneable
URLBuilder
constructor.
All setter methods are prepared to use "method chaining", as they all return the current URLBuilder instance.Constructor and Description |
---|
URLBuilder(javax.servlet.http.HttpServletRequest request)
Builds a URL builder taking the request URL of the given request, decoding it using UTF-8
|
URLBuilder(javax.servlet.http.HttpServletRequest request,
java.lang.String parameterEncoding)
Builds a URL builder taking the request URL of the given request
|
URLBuilder(java.lang.String protocol,
int port,
java.lang.String host,
java.lang.String path,
java.lang.String query)
Builds a URL builder for the given URL parts
|
URLBuilder(java.lang.String protocol,
int port,
java.lang.String host,
java.lang.String path,
java.lang.String query,
java.lang.String decode)
Creates a URL Builder that takes various URL parts as parameters
|
URLBuilder(org.apache.commons.httpclient.URI uri)
Creates a URLBuilder that parses an existing URI
|
URLBuilder(org.apache.commons.httpclient.URI uri,
java.lang.String encoding)
Creates a URLBuilder that parses an existing URI
|
URLBuilder(java.net.URL url)
Creates a URLBuilder that parses an existing URL
|
URLBuilder(java.net.URL url,
java.lang.String encoding)
Creates a URLBuilder that parses an existing URL
|
Modifier and Type | Method and Description |
---|---|
URLBuilder |
addQueryString(java.lang.String queryString,
java.lang.String decode)
Adds a complete query string to the parameters of this URLBuilder
|
URLBuilder |
appendPath(java.lang.String pathElement)
Appends a new path element to the current path
This method automatically ensures that a single slash is between the current path and the new path element.
|
java.lang.String |
build()
Rebuilds the URL in relative form to a string.
|
java.lang.String |
build(boolean absolute)
Rebuilds the URL to a string.
|
URLBuilder |
clearParameters()
Clears all url parameters
|
static java.lang.Integer |
getDefaultPortForProtocol(java.lang.String protocol)
Returns the default port for the given URL protocol
|
java.lang.String |
getEncoding()
Returns the encoding by which the URL will be encoded when building it using
build() and variants. |
java.lang.String |
getHost()
Returns the host part
|
java.lang.String |
getParameter(java.lang.String param)
Returns a URL parameter value
|
java.lang.String |
getParameterEncoding()
Deprecated.
Use
getEncoding() instead. Encoding is now also used for other URL components. |
java.util.Set<java.lang.String> |
getParameterNames()
Returns the names of URL parameters
|
java.lang.String |
getPath()
Returns the path part
|
int |
getPort()
Returns the TCP port for this URL
|
java.lang.String |
getProtocol()
Gets the protocol
|
java.lang.String |
getQuery()
Returns the query part
|
boolean |
hasParameter(java.lang.String param)
Tests for existence of a URL parameter
|
static boolean |
isDefaultPortForProtocol(int port,
java.lang.String protocol)
Tests if the given port is the default port for the given protocol.
|
java.net.URL |
rebuild()
Deprecated.
Because of inconsistent return type. Use
build(boolean) instead or use implicit string conversion |
java.lang.String |
rebuild(boolean absolute)
Deprecated.
Because of inconsistent return type. Use
build(boolean) instead or use implicit string conversion |
URLBuilder |
removeParameter(java.lang.String param)
Removes a parameter
|
URLBuilder |
setEncoding(java.lang.String encoding)
Sets the encoding used to encode the URL on building it using
build() and variants. |
URLBuilder |
setHost(java.lang.String host)
Sets the host
|
URLBuilder |
setParameter(java.lang.String param,
java.lang.String value)
Createds/modifies a parameter
|
URLBuilder |
setParameterEncoding(java.lang.String parameterEncoding)
Deprecated.
Use
setEncoding(String) instead. Encoding is now also used for other URL components. |
URLBuilder |
setPath(java.lang.String file)
Sets the path part
|
URLBuilder |
setPort(int port)
Sets the TCP port for this URL
|
URLBuilder |
setProtocol(java.lang.String protocol)
Sets the protocol
|
URLBuilder |
setQuery(java.lang.String query)
Deprecated.
Modify the query via methods
setParameter(String, String) and removeParameter(String) |
java.lang.String |
toString() |
public URLBuilder(java.net.URL url)
url
- The URL to parsepublic URLBuilder(java.net.URL url, java.lang.String encoding)
url
- The URL to parseencoding
- The encoding of URL parameters. The URLBuilder will decode them.public URLBuilder(org.apache.commons.httpclient.URI uri) throws org.apache.commons.httpclient.URIException
uri
- The URI to parseorg.apache.commons.httpclient.URIException
public URLBuilder(org.apache.commons.httpclient.URI uri, java.lang.String encoding) throws org.apache.commons.httpclient.URIException
uri
- The URI to parseencoding
- The encoding of URL parameters. The URLBuilder will decode them.org.apache.commons.httpclient.URIException
public URLBuilder(java.lang.String protocol, int port, java.lang.String host, java.lang.String path, java.lang.String query, java.lang.String decode)
protocol
- The protocol URL partport
- The port URL parthost
- The host URL partpath
- The path URL part. May be left null.query
- The query string URL part. May be left null.decode
- The encoding by which the URL parts will be decoded. Specify null for no decoding.public URLBuilder(java.lang.String protocol, int port, java.lang.String host, java.lang.String path, java.lang.String query)
protocol
- port
- host
- path
- query
- public URLBuilder(javax.servlet.http.HttpServletRequest request, java.lang.String parameterEncoding) throws java.net.MalformedURLException
request
- The requestparameterEncoding
- The encoding to use in decoding the URLjava.net.MalformedURLException
public URLBuilder(javax.servlet.http.HttpServletRequest request) throws java.net.MalformedURLException
request
- The requestjava.net.MalformedURLException
public static boolean isDefaultPortForProtocol(int port, java.lang.String protocol)
port
- Tested portprotocol
- Tested protocolpublic static java.lang.Integer getDefaultPortForProtocol(java.lang.String protocol)
protocol
- The URL protocolpublic URLBuilder addQueryString(java.lang.String queryString, java.lang.String decode)
queryString
- The query stringdecode
- The encoding by which parameters should get decoded. Use null for no decoding.public java.lang.String getQuery()
public URLBuilder setQuery(java.lang.String query)
query
- The query to setpublic java.lang.String getPath()
public URLBuilder setPath(java.lang.String file)
file
- The file to setorg.apache.commons.httpclient.URIException
public URLBuilder appendPath(java.lang.String pathElement)
pathElement
- public java.lang.String getHost()
public URLBuilder setHost(java.lang.String host)
host
- The host to setpublic java.lang.String getProtocol()
public URLBuilder setProtocol(java.lang.String protocol)
protocol
- The protocol to setpublic java.lang.String rebuild(boolean absolute)
build(boolean)
instead or use implicit string conversionabsolute
- Specify true for an absolute URL. false will rebuild it only from the path part on, omitting protocol, port and hos.public java.lang.String build(boolean absolute)
absolute
- Specify true for an absolute URL. false will rebuild it only from the path part on, omitting protocol, port and host.public java.lang.String build()
public java.net.URL rebuild() throws java.net.MalformedURLException
build(boolean)
instead or use implicit string conversionjava.net.MalformedURLException
public java.lang.String getParameter(java.lang.String param)
param
- The parameter namepublic boolean hasParameter(java.lang.String param)
param
- The parameter namepublic URLBuilder removeParameter(java.lang.String param)
param
- The parameter namepublic URLBuilder setParameter(java.lang.String param, java.lang.String value)
param
- The parameter namevalue
- The parameter valuepublic java.lang.String getParameterEncoding()
getEncoding()
instead. Encoding is now also used for other URL components.public URLBuilder setParameterEncoding(java.lang.String parameterEncoding)
setEncoding(String)
instead. Encoding is now also used for other URL components.parameterEncoding
- The parameterEncoding to set.public URLBuilder clearParameters()
public java.lang.String toString()
toString
in class java.lang.Object
public int getPort()
public URLBuilder setPort(int port)
public java.util.Set<java.lang.String> getParameterNames()
public java.lang.String getEncoding()
build()
and variants.
Is initialized with the encoding by which the given URL was decoded, if one was given to the URLBuilder constructor that was used.public URLBuilder setEncoding(java.lang.String encoding)
build()
and variants.encoding
- The encoding to set.