Class HttpURL
- java.lang.Object
-
- com.networknt.url.HttpURL
-
- All Implemented Interfaces:
Serializable
public class HttpURL extends Object implements Serializable
This class act as a mutable URL, which could be a replacement or "wrapper" to theURLclass. It can also be used as a safer way to build aURLor aURIinstance as it will properly escape appropriate characters before creating those.- Author:
- Pascal Essiembre
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_HTTP_PORTDefault URL HTTP Port.static intDEFAULT_HTTPS_PORTDefault Secure URL HTTP Port.static StringPROTOCOL_HTTPConstant for "http" protocol.static StringPROTOCOL_HTTPSConstant for "https" protocol.
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static StringencodePath(String path)URL-Encodes a URL path.static StringencodeQueryString(String queryString)URL-Encodes the query string portion of a URL.booleanequals(Object o)StringgetEncoding()Gets the character encoding.StringgetFragment()Gets the URL fragment.StringgetHost()Gets the host portion of the URL.StringgetLastPathSegment()Gets the last URL path segment without the query string.StringgetPath()Gets the URL path.intgetPort()Gets the URL port.StringgetProtocol()Gets the protocol portion of the URL (e.g.QueryStringgetQueryString()Gets the URL query string.StringgetRoot()Gets the root of this HttpUrl.static StringgetRoot(String url)Gets the root of a URL.inthashCode()booleanisPortDefault()Whether this URL uses the default port for the protocol.booleanisSecure()Whether this URL is secure (e.g.voidsetFragment(String fragment)Sets the URL fragment.voidsetHost(String host)Sets the host portion of the URL.voidsetPath(String path)Sets the URL path.voidsetPort(int port)Sets the URL port.voidsetProtocol(String protocol)Sets the protocol portion of the URL.voidsetQueryString(QueryString queryString)Sets the URL query string.static StringtoAbsolute(String baseURL, String relativeURL)Converts a relative URL to an absolute one, based on the supplied base URL.StringtoString()Returns a string representation of this URL, properly encoded.URItoURI()Converts this HttpURL to aURI, making sure appropriate characters are escaped properly.static URItoURI(String url)Converts the supplied URL to aURI, making sure appropriate characters are encoded properly using UTF-8.URLtoURL()Converts this HttpURL to a regularURL, making sure appropriate characters are escaped properly.static URLtoURL(String url)Converts the supplied URL to aURL, making sure appropriate characters are encoded properly using UTF-8.
-
-
-
Field Detail
-
DEFAULT_HTTP_PORT
public static final int DEFAULT_HTTP_PORT
Default URL HTTP Port.- See Also:
- Constant Field Values
-
DEFAULT_HTTPS_PORT
public static final int DEFAULT_HTTPS_PORT
Default Secure URL HTTP Port.- See Also:
- Constant Field Values
-
PROTOCOL_HTTP
public static final String PROTOCOL_HTTP
Constant for "http" protocol.- See Also:
- Constant Field Values
-
PROTOCOL_HTTPS
public static final String PROTOCOL_HTTPS
Constant for "https" protocol.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
HttpURL
public HttpURL()
Creates a blank HttpURL using UTF-8 for URL encoding.
-
HttpURL
public HttpURL(URL url)
Creates a new HttpURL from the URL object using UTF-8 for URL encoding.- Parameters:
url- a URL
-
HttpURL
public HttpURL(String url)
Creates a new HttpURL from the URL string using UTF-8 for URL encoding.- Parameters:
url- a URL
-
HttpURL
public HttpURL(URL url, String encoding)
Creates a new HttpURL from the URL object using the provided encoding for URL encoding.- Parameters:
url- a URLencoding- character encoding- Since:
- 1.7.0
-
-
Method Detail
-
getEncoding
public String getEncoding()
Gets the character encoding. Default is UTF-8.- Returns:
- character encoding
- Since:
- 1.7.0
-
getPath
public String getPath()
Gets the URL path.- Returns:
- URL path
-
setPath
public void setPath(String path)
Sets the URL path.- Parameters:
path- url path
-
getQueryString
public QueryString getQueryString()
Gets the URL query string.- Returns:
- URL query string, or
nullif none
-
setQueryString
public void setQueryString(QueryString queryString)
Sets the URL query string.- Parameters:
queryString- the query string
-
getHost
public String getHost()
Gets the host portion of the URL.- Returns:
- the host portion of the URL
-
setHost
public void setHost(String host)
Sets the host portion of the URL.- Parameters:
host- the host portion of the URL
-
getProtocol
public String getProtocol()
Gets the protocol portion of the URL (e.g. http, https);- Returns:
- the protocol portion of the URL
-
setProtocol
public void setProtocol(String protocol)
Sets the protocol portion of the URL.- Parameters:
protocol- the protocol portion of the URL
-
isSecure
public boolean isSecure()
Whether this URL is secure (e.g. https).- Returns:
trueif protocol is secure
-
getPort
public int getPort()
Gets the URL port. If the protocol is other thanhttporhttps, the port is -1 when not specified.- Returns:
- the URL port
-
setPort
public void setPort(int port)
Sets the URL port.- Parameters:
port- the URL port
-
getFragment
public String getFragment()
Gets the URL fragment.- Returns:
- the fragment
- Since:
- 1.8.0
-
setFragment
public void setFragment(String fragment)
Sets the URL fragment.- Parameters:
fragment- the fragment to set- Since:
- 1.8.0
-
getLastPathSegment
public String getLastPathSegment()
Gets the last URL path segment without the query string. If there are segment to return, an empty string will be returned instead.- Returns:
- the last URL path segment
-
toURL
public URL toURL()
Converts this HttpURL to a regularURL, making sure appropriate characters are escaped properly.- Returns:
- a URL
- Throws:
RuntimeException- when URL is malformed
-
getRoot
public String getRoot()
Gets the root of this HttpUrl. That is the left part of a URL up to and including the host name. Anullor empty string returns anulldocument root.- Returns:
- left part of a URL up to (and including the host name
- Throws:
RuntimeException- when URL is malformed- Since:
- 1.8.0
-
toURI
public URI toURI()
Converts this HttpURL to aURI, making sure appropriate characters are escaped properly.- Returns:
- a URI
- Throws:
RuntimeException- when URL is malformed- Since:
- 1.7.0
-
toURL
public 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();- Parameters:
url- a URL string- Returns:
- a URL object
- Throws:
RuntimeException- when URL is malformed- Since:
- 1.7.0
-
toURI
public 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();- Parameters:
url- a URL string- Returns:
- a URI object
- Throws:
RuntimeException- when URL is malformed- Since:
- 1.7.0
-
getRoot
public 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
nullor empty string returns anulldocument root. This method is a short form of:
new HttpURL("http://example.com/path").getRoot();- Parameters:
url- a URL string- Returns:
- left part of a URL up to (and including the host name
- Since:
- 1.8.0
-
toString
public String toString()
Returns a string representation of this URL, properly encoded.
-
isPortDefault
public boolean isPortDefault()
Whether this URL uses the default port for the protocol. The default port is 80 for "http" protocol, and 443 for "https". Other protocols are not supported and this method will always return false for them.- Returns:
trueif the URL is using the default port.- Since:
- 1.8.0
-
encodeQueryString
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.
- Parameters:
queryString- URL query string- Returns:
- encoded path
- Since:
- 1.8.0
-
encodePath
public 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).
- Parameters:
path- path portion of a URL- Returns:
- encoded path
- Since:
- 1.7.0
-
toAbsolute
public static String toAbsolute(String baseURL, String relativeURL)
Converts a relative URL to an absolute one, based on the supplied base URL. The base URL is assumed to be a valid URL. Behavior is unexpected when base URL is invalid.- Parameters:
baseURL- URL to the reference is relative torelativeURL- the relative URL portion to transform to absolute- Returns:
- absolute URL
- Since:
- 1.8.0
-
-