Class Url
java.lang.Object
org.refcodes.web.Url
- All Implemented Interfaces:
CredentialsAccessor, IdentityAccessor, PathAccessor<String>, PortAccessor, SecretAccessor, IpAddressAccessor, FragmentAccessor, HostAccessor, QueryFieldsAccessor, SchemeAccessor
- Direct Known Subclasses:
UrlBuilder
public class Url
extends Object
implements SchemeAccessor, HostAccessor, IpAddressAccessor, PortAccessor, PathAccessor<String>, QueryFieldsAccessor, FragmentAccessor, CredentialsAccessor
The Url class represents an immutable URL: An URL looks something
like this:
"scheme://[identity[:secret]@]host[:port][/path][?query][#fragment] In
contrast to the java.net URL, this URL also supports
"relative" locators with neither a scheme nor a host declaration. If the
relative locator starts with a "/" slash, then we assume not having a host
being provided: "/path?query#fragment" When it does *not* start with a "/"
slash, then we assume that the first element being the host:
"[identity[:secret]@]host[:port]/path[?query][#fragment]"
-
Nested Class Summary
Nested classes/interfaces inherited from interface CredentialsAccessor
CredentialsAccessor.CredentialsBuilder<B>, CredentialsAccessor.CredentialsMutator, CredentialsAccessor.CredentialsPropertyNested classes/interfaces inherited from interface FragmentAccessor
FragmentAccessor.FragmentBuilder<B>, FragmentAccessor.FragmentMutator, FragmentAccessor.FragmentPropertyNested classes/interfaces inherited from interface HostAccessor
HostAccessor.HostBuilder<B>, HostAccessor.HostMutator, HostAccessor.HostPropertyNested classes/interfaces inherited from interface IdentityAccessor
IdentityAccessor.IdentityBuilder<B>, IdentityAccessor.IdentityMutator, IdentityAccessor.IdentityPropertyNested classes/interfaces inherited from interface IpAddressAccessor
IpAddressAccessor.IpAddressBuilder<B>, IpAddressAccessor.IpAddressMutator, IpAddressAccessor.IpAddressPropertyNested classes/interfaces inherited from interface PathAccessor
PathAccessor.PathBuilder<T,B>, PathAccessor.PathMutator<T>, PathAccessor.PathProperty<T> Nested classes/interfaces inherited from interface PortAccessor
PortAccessor.PortBuilder<B>, PortAccessor.PortMutator, PortAccessor.PortPropertyNested classes/interfaces inherited from interface QueryFieldsAccessor
QueryFieldsAccessor.QueryFieldsBuilder<B>, QueryFieldsAccessor.QueryFieldsMutator, QueryFieldsAccessor.QueryFieldsPropertyNested classes/interfaces inherited from interface SchemeAccessor
SchemeAccessor.SchemeBuilder<B>, SchemeAccessor.SchemeMutator, SchemeAccessor.SchemePropertyNested classes/interfaces inherited from interface SecretAccessor
SecretAccessor.SecretBuilder<B>, SecretAccessor.SecretMutator, SecretAccessor.SecretProperty -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedUrl()Default constructor.Constructs anUrlwith the common attributes.Constructs anUrlwith the common attributes.Constructs anUrlwith the common attributes.Url(String aProtocol, String aHost, int aPort, String aPath, FormFields aQueryFields) Constructs anUrlwith the common attributes.Url(String aProtocol, String aHost, int aPort, String aPath, FormFields aQueryFields, String aFragment) Constructs anUrlwith the common attributes.Constructs anUrlwith the common attributes.Url(String aProtocol, String aHost, String aPath, FormFields aQueryFields) Constructs anUrlwith the common attributes.Constructs anUrlwith the common attributes.Url(String aUrl, FormFields aQueryFields) Url(String aUrl, FormFields aQueryFields, String aFragment) Constructs anUrlwith the common attributes.Constructs anUrlwith the common attributes.Constructs anUrlwith the common attributes.Url(Scheme aScheme, String aHost, int aPort, String aPath, FormFields aQueryFields) Constructs anUrlwith the common attributes.Url(Scheme aScheme, String aHost, int aPort, String aPath, FormFields aQueryFields, String aFragment) Constructs anUrlwith the common attributes.Constructs anUrlwith the common attributes.Url(Scheme aScheme, String aHost, String aPath, FormFields aQueryFields) Constructs anUrlwith the common attributes.Constructs anUrlwith the common attributes. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidprotected voidRetrieves the fragment from the fragment property.getHost()Retrieves the host from the host property.int[]getPath()intgetPort()Retrieves the request Query-Fields from the request Query-Fields property.Retrieves the UrlScheme from the URL scheme.protected voidprotected voidsetIpAddress(int[] aIpAddress) protected voidprotected voidsetPort(int aPort) protected voidsetProtocol(String aProtocol) protected voidtoHost()Returns the "host" depending on whether an IP-Address has been provided or a host name.Creates the locator part from theUrlinstance's state, excluding the fragment or the query fields.Retrieves the protocol representation from theScheme.toString()protected static StringtoTruncatePath(String aPath) Removes any leading and trailing path delimiters (as ofDelimiter.PATH).protected static StringtoTruncatePathPrefix(String aPath) Removes any leading path delimiters (as ofDelimiter.PATH).protected static StringtoTruncatePathSuffix(String aPath) Removes any trailing path delimiters (as ofDelimiter.PATH).toURL()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface IpAddressAccessor
toCidrNotation
-
Field Details
-
_scheme
-
_protocol
-
_host
-
_ipAddress
protected int[] _ipAddress -
_path
-
_port
protected int _port -
_queryFields
-
_identity
-
_secret
-
_fragment
-
-
Constructor Details
-
Url
protected Url()Default constructor. Make sure to set required attributes for a valid URL. -
Url
-
Url
- Parameters:
aUrl- The URLStringto be parsed. The URL consists of the scheme (protocol), the identify and the secret (optional), the host as well as an optional port and the (optional) path.- Throws:
MalformedURLException- in case the provided URL is considered being malformed.
-
Url
- Parameters:
aUrl- The URLStringto be parsed. The URL consists of the scheme (protocol), the identify and the secret (optional), the host as well as an optional port and the (optional) path.aQueryFields- The Query-Fields to be used for the HTTP Query-String.- Throws:
MalformedURLException- in case the provided URL is considered being malformed.
-
Url
- Parameters:
aUrl- The URLStringto be parsed. The URL consists of the scheme (protocol), the identify and the secret (optional), the host as well as an optional port and the (optional) path.aQueryFields- The Query-Fields to be used for the HTTP Query-String.aFragment- The fragment to be set.- Throws:
MalformedURLException- in case the provided URL is considered being malformed.
-
Url
-
Url
-
Url
-
Url
Constructs anUrlwith the common attributes.- Parameters:
aScheme- TheScheme(e.g. HTTP or HTTPS) to be used for the destination URL.aHost- The host to which the destination URL is to point to.aPort- The port to be used when connecting to the host.aPath- The path on the host to which the base destination URL is to point to.
-
Url
Constructs anUrlwith the common attributes.- Parameters:
aScheme- TheScheme(e.g. HTTP or HTTPS) to be used for the destination URL.aHost- The host to which the destination URL is to point to.aPort- The port to be used when connecting to the host.aPath- The path on the host to which the base destination URL is to point to.aQueryFields- The Query-Fields to be used for the HTTP Query-String.
-
Url
public Url(Scheme aScheme, String aHost, int aPort, String aPath, FormFields aQueryFields, String aFragment) Constructs anUrlwith the common attributes.- Parameters:
aScheme- TheScheme(e.g. HTTP or HTTPS) to be used for the destination URL.aHost- The host to which the destination URL is to point to.aPort- The port to be used when connecting to the host.aPath- The path on the host to which the base destination URL is to point to.aQueryFields- The Query-Fields to be used for the HTTP Query-String.aFragment- The fragment to be set.
-
Url
-
Url
-
Url
Constructs anUrlwith the common attributes.- Parameters:
aProtocol- The protocolString(e.g. "http://" or "https://") to be used for the destination URL.aHost- The host to which the destination URL is to point to.aPort- The port to be used when connecting to the host.aPath- The path on the host to which the base destination URL is to point to.
-
Url
Constructs anUrlwith the common attributes.- Parameters:
aProtocol- The protocolString(e.g. "http://" or "https://") to be used for the destination URL.aHost- The host to which the destination URL is to point to.aPort- The port to be used when connecting to the host.aPath- The path on the host to which the base destination URL is to point to.aQueryFields- The Query-Fields to be used for the HTTP Query-String.
-
Url
public Url(String aProtocol, String aHost, int aPort, String aPath, FormFields aQueryFields, String aFragment) Constructs anUrlwith the common attributes.- Parameters:
aProtocol- The protocolString(e.g. "http://" or "https://") to be used for the destination URL.aHost- The host to which the destination URL is to point to.aPort- The port to be used when connecting to the host.aPath- The path on the host to which the base destination URL is to point to.aQueryFields- The Query-Fields to be used for the HTTP Query-String.aFragment- The fragment to be set.
-
Url
-
Url
Constructs anUrlwith the common attributes.- Parameters:
aScheme- TheScheme(e.g. HTTP or HTTPS) to be used for the destination URL.aHost- The host to which the destination URL is to point to.aPath- The path on the host to which the base destination URL is to point to.aQueryFields- The Query-Fields to be used for the HTTP Query-String.
-
Url
Constructs anUrlwith the common attributes.- Parameters:
aScheme- TheScheme(e.g. HTTP or HTTPS) to be used for the destination URL.aHost- The host to which the destination URL is to point to.aPath- The path on the host to which the base destination URL is to point to.aQueryFields- The Query-Fields to be used for the HTTP Query-String.aFragment- The fragment to be set.
-
Url
-
Url
Constructs anUrlwith the common attributes.- Parameters:
aProtocol- The protocolString(e.g. "http://" or "https://") to be used for the destination URL.aHost- The host to which the destination URL is to point to.aPath- The path on the host to which the base destination URL is to point to.aQueryFields- The Query-Fields to be used for the HTTP Query-String.
-
Url
-
Url
Constructs anUrlwith the common attributes.- Parameters:
aProtocol- The protocolString(e.g. "http://" or "https://") to be used for the destination URL.aHost- The host to which the destination URL is to point to.aPath- The path on the host to which the base destination URL is to point to.aQueryFields- The Query-Fields to be used for the HTTP Query-String.aFragment- The fragment to be set.
-
Url
-
-
Method Details
-
getFragment
Retrieves the fragment from the fragment property.- Specified by:
getFragmentin interfaceFragmentAccessor- Returns:
- The fragment stored by the fragment property.
-
getHost
Retrieves the host from the host property.- Specified by:
getHostin interfaceHostAccessor- Returns:
- The host stored by the host property.
-
getIdentity
- Specified by:
getIdentityin interfaceIdentityAccessor
-
getIpAddress
public int[] getIpAddress()- Specified by:
getIpAddressin interfaceIpAddressAccessor
-
getPath
- Specified by:
getPathin interfacePathAccessor<String>
-
getPort
public int getPort()- Specified by:
getPortin interfacePortAccessor
-
getQueryFields
Retrieves the request Query-Fields from the request Query-Fields property.- Specified by:
getQueryFieldsin interfaceQueryFieldsAccessor- Returns:
- The request Query-Fields stored by the request Query-Fields property.
-
getScheme
Retrieves the UrlScheme from the URL scheme.- Specified by:
getSchemein interfaceSchemeAccessor- Returns:
- The UrlScheme stored by the URL scheme.
-
getSecret
- Specified by:
getSecretin interfaceSecretAccessor
-
toProtocol
Retrieves the protocol representation from theScheme. In case of a scheme unknown by theSchemeenumeration, thenSchemeAccessor.getScheme()might return null whilstSchemeAccessor.toProtocol()still retrieves the unknown scheme's protocol representation (as ofSchemeAccessor.SchemeMutator.setProtocol(String)).- Specified by:
toProtocolin interfaceSchemeAccessor- Returns:
- The protocol representation for the URL.
-
toHost
Returns the "host" depending on whether an IP-Address has been provided or a host name.- Returns:
- The determined host.
-
toHttpUrl
-
toLocator
-
toURL
- Returns:
- The according
URLinstance. - Throws:
MalformedURLException- thrown in case the state of yourUrlinstance cannot be used to construct a validURL, you may be missing some properties.
-
toString
-
fromUrl
- Parameters:
aUrl- The URL from which to determine the state.- Throws:
MalformedURLException- in case the provided URL is considered being malformed.
-
fromURL
-
setScheme
-
setProtocol
-
setHost
-
setIpAddress
protected void setIpAddress(int[] aIpAddress) -
setPort
protected void setPort(int aPort) -
setPath
-
toTruncatePathPrefix
Removes any leading path delimiters (as ofDelimiter.PATH).- Parameters:
aPath- The path to be processed.- Returns:
- The path without any leading path delimiters.
-
toTruncatePathSuffix
Removes any trailing path delimiters (as ofDelimiter.PATH).- Parameters:
aPath- The path to be processed.- Returns:
- The path without any trailing path delimiters.
-
toTruncatePath
Removes any leading and trailing path delimiters (as ofDelimiter.PATH).- Parameters:
aPath- The path to be processed.- Returns:
- The path with neither any leading nor any trailing path delimiters.
-