public class HttpURL extends Object implements Serializable
URL
class. It can also be used as a safer way
to build a URL
or a URI
instance as it will properly escape
appropriate characters before creating those.Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_HTTP_PORT
Default URL HTTP Port.
|
static int |
DEFAULT_HTTPS_PORT
Default Secure URL HTTP Port.
|
static String |
PROTOCOL_HTTP
Constant for "http" protocol.
|
static String |
PROTOCOL_HTTPS
Constant for "https" protocol.
|
Constructor and Description |
---|
HttpURL()
Creates a blank HttpURL using UTF-8 for URL encoding.
|
HttpURL(String url)
Creates a new HttpURL from the URL string using UTF-8 for URL encoding.
|
HttpURL(String url,
String encoding)
Creates a new HttpURL from the URL string using the provided encoding
for URL encoding.
|
HttpURL(URL url)
Creates a new HttpURL from the URL object using UTF-8 for URL encoding.
|
HttpURL(URL url,
String encoding)
Creates a new HttpURL from the URL object using the provided encoding
for URL encoding.
|
Modifier and Type | Method and Description |
---|---|
static String |
encodePath(String path)
URL-Encodes a URL path.
|
static String |
encodeQueryString(String queryString)
URL-Encodes the query string portion of a URL.
|
boolean |
equals(Object obj) |
String |
getEncoding()
Gets the character encoding.
|
String |
getFragment()
Gets the URL fragment.
|
String |
getHost()
Gets the host portion of the URL.
|
String |
getLastPathSegment()
Gets the last URL path segment without the query string.
|
String |
getPath()
Gets the URL path.
|
int |
getPort()
Gets the URL port.
|
String |
getProtocol()
Gets the protocol portion of the URL (e.g.
|
QueryString |
getQueryString()
Gets the URL query string.
|
String |
getRoot()
Gets the root of this HttpUrl.
|
static String |
getRoot(String url)
Gets the root of a URL.
|
int |
hashCode() |
boolean |
isPortDefault()
Whether this URL uses the default port for the protocol.
|
boolean |
isSecure()
Whether this URL is secure (e.g.
|
void |
setFragment(String fragment)
Sets the URL fragment.
|
void |
setHost(String host)
Sets the host portion of the URL.
|
void |
setPath(String path)
Sets the URL path.
|
void |
setPort(int port)
Sets the URL port.
|
void |
setProtocol(String protocol)
Sets the protocol portion of the URL.
|
void |
setQueryString(QueryString queryString)
Sets the URL query string.
|
static String |
toAbsolute(String baseURL,
String relativeURL)
Converts a relative URL to an absolute one, based on the supplied
base URL.
|
String |
toString()
Returns a string representation of this URL, properly encoded.
|
URI |
toURI()
Converts this HttpURL to a
URI , making sure
appropriate characters are escaped properly. |
static URI |
toURI(String url)
Converts the supplied URL to a
URI , making sure
appropriate characters are encoded properly using UTF-8. |
URL |
toURL()
Converts this HttpURL to a regular
URL , making sure
appropriate characters are escaped properly. |
static URL |
toURL(String url)
Converts the supplied URL to a
URL , making sure
appropriate characters are encoded properly using UTF-8. |
public static final int DEFAULT_HTTP_PORT
public static final int DEFAULT_HTTPS_PORT
public static final String PROTOCOL_HTTP
public static final String PROTOCOL_HTTPS
public HttpURL()
public HttpURL(URL url)
url
- a URLpublic HttpURL(String url)
url
- a URLpublic HttpURL(URL url, String encoding)
url
- a URLencoding
- character encodingpublic String getEncoding()
public String getPath()
public void setPath(String path)
path
- url pathpublic QueryString getQueryString()
null
if nonepublic void setQueryString(QueryString queryString)
queryString
- the query stringpublic String getHost()
public void setHost(String host)
host
- the host portion of the URLpublic String getProtocol()
public void setProtocol(String protocol)
protocol
- the protocol portion of the URLpublic boolean isSecure()
true
if protocol is securepublic int getPort()
http
or https
, the port is -1 when
not specified.public void setPort(int port)
port
- the URL portpublic String getFragment()
public void setFragment(String fragment)
fragment
- the fragment to setpublic String getLastPathSegment()
public URL toURL()
URL
, making sure
appropriate characters are escaped properly.URLException
- when URL is malformedpublic String getRoot()
null
or empty string returns
a null
document root.URLException
- when URL is malformedpublic URI toURI()
URI
, making sure
appropriate characters are escaped properly.URLException
- when URL is malformedpublic static URL toURL(String url)
Converts the supplied URL to a URL
, making sure
appropriate characters are encoded properly using UTF-8. This method
is a short form of:
new HttpURL("http://example.com").toURL();
url
- a URL stringURLException
- when URL is malformedpublic static URI toURI(String url)
Converts the supplied URL to a URI
, making sure
appropriate characters are encoded properly using UTF-8. This method
is a short form of:
new HttpURL("http://example.com").toURI();
url
- a URL stringURLException
- when URL is malformedpublic static String getRoot(String url)
Gets the root of a URL. That is the left part of a URL up to and
including the host name. A null
or empty string returns
a null
document root.
This method is a short form of:
new HttpURL("http://example.com/path").getRoot();
url
- a URL stringpublic String toString()
public boolean isPortDefault()
true
if the URL is using the default port.public static String encodeQueryString(String queryString)
URL-Encodes the query string portion of a URL. The entire string supplied is assumed to be a query string.
queryString
- URL query stringpublic static String encodePath(String path)
URL-Encodes a URL path. The entire string supplied is assumed to be a URL path. Unsafe characters are percent-encoded using UTF-8 (as specified by W3C standard).
path
- path portion of a URLpublic static String toAbsolute(String baseURL, String relativeURL)
baseURL
- URL to the reference is relative torelativeURL
- the relative URL portion to transform to absoluteCopyright © 2008–2021 Norconex Inc.. All rights reserved.