Class URL
- java.lang.Object
-
- java.net.URL
-
- All Implemented Interfaces:
Serializable
public final class URL extends Object implements Serializable
A stripped-down URL suitable for use in GWT client-side.It could do with a lot more trimming, and use of native js encodeURIComponent.
Any volunteers with some existing unit tests who'd like to improve client side URL performance?
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static byte[]convertFromIPv4MappedAddress(byte[] addr)Convert IPv4-Mapped address to IPv4 address.booleanequals(Object obj)Compares this URL for equality with another object.StringgetAuthority()Gets the authority part of thisURL.intgetDefaultPort()Gets the default port number of the protocol associated with thisURL.StringgetFile()Gets the file name of thisURL.StringgetHost()Gets the host name of thisURL, if applicable.StringgetPath()Gets the path part of thisURL.intgetPort()Gets the port number of thisURL.StringgetProtocol()Gets the protocol name of thisURL.StringgetQuery()Gets the query part of thisURL.StringgetRef()Gets the anchor (also known as the "reference") of thisURL.StringgetUserInfo()Gets the userInfo part of thisURL.inthashCode()Creates an integer suitable for hash table indexing.URLConnectionopenConnection()Returns aURIequivalent to this URL.InputStreamopenStream()protected voidparseURL(URL purl, String pspec, int pstart, int plimit)Parses the string representation of aURLinto aURLobject.protected voidset(String protocol, String host, int port, String file, String ref)Sets the fields of the URL.protected voidset(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref)Sets the specified 8 fields of the URL.static byte[]textToNumericFormatV4(String src)Convert IPv4-Mapped address to IPv4 address.StringtoExternalForm()Constructs a string representation of thisURL.StringtoString()Constructs a string representation of thisURL.
-
-
-
Constructor Detail
-
URL
public URL(String spec) throws MalformedURLException
Creates aURLobject from theStringrepresentation.This constructor is equivalent to a call to the two-argument constructor with a
nullfirst argument.- Parameters:
spec- theStringto parse as a URL.- Throws:
MalformedURLException- If the string specifies an unknown protocol.- See Also:
URL(java.net.URL, java.lang.String)
-
URL
public URL(URL context, String spec) throws MalformedURLException
Creates a URL by parsing the given spec within a specified context. The new URL is created from the given context URL and the spec argument as described in RFC2396 "Uniform Resource Identifiers : Generic * Syntax" :
The reference is parsed into the scheme, authority, path, query and fragment parts. If the path component is empty and the scheme, authority, and query components are undefined, then the new URL is a reference to the current document. Otherwise, the fragment and query parts present in the spec are used in the new URL.<scheme>://<authority><path>?<query>#<fragment>If the scheme component is defined in the given spec and does not match the scheme of the context, then the new URL is created as an absolute URL based on the spec alone. Otherwise the scheme component is inherited from the context URL.
If the authority component is present in the spec then the spec is treated as absolute and the spec authority and path will replace the context authority and path. If the authority component is absent in the spec then the authority of the new URL will be inherited from the context.
If the spec's path component begins with a slash character "/" then the path is treated as absolute and the spec path replaces the context path.
Otherwise, the path is treated as a relative path and is appended to the context path, as described in RFC2396. Also, in this case, the path is canonicalized through the removal of directory changes made by occurences of ".." and ".".
For a more detailed description of URL parsing, refer to RFC2396.
- Parameters:
context- the context in which to parse the specification.spec- theStringto parse as a URL.- Throws:
MalformedURLException- if no protocol is specified, or an unknown protocol is found.- See Also:
java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String),URLStreamHandler,URLStreamHandler.parseURL(java.net.URL, java.lang.String, int, int)
-
-
Method Detail
-
parseURL
protected void parseURL(URL purl, String pspec, int pstart, int plimit)
Parses the string representation of aURLinto aURLobject.If there is any inherited context, then it has already been copied into the
URLargument.The
parseURLmethod ofURLStreamHandlerparses the string representation as if it were anhttpspecification. Most URL protocol families have a similar parsing. A stream protocol handler for a protocol that has a different syntax must override this routine.- Parameters:
purl- theURLto receive the result of parsing the spec.pspec- theStringrepresenting the URL that must be parsed.pstart- the character index at which to begin parsing. This is just past the ':' (if there is one) that specifies the determination of the protocol name.plimit- the character position to stop parsing at. This is the end of the string or the position of the "#" character, if present. All information after the sharp sign indicates an anchor.
-
textToNumericFormatV4
public static byte[] textToNumericFormatV4(String src)
Convert IPv4-Mapped address to IPv4 address. Both input and returned value are in network order binary form.- Parameters:
src- a string representing an IPv4-Mapped address in textual format- Returns:
- a byte array representing the IPv4 numeric address
-
convertFromIPv4MappedAddress
public static byte[] convertFromIPv4MappedAddress(byte[] addr)
Convert IPv4-Mapped address to IPv4 address. Both input and returned value are in network order binary form.- Parameters:
addr- a representing an IPv4-Mapped address in textual format- Returns:
- a byte array representing the IPv4 numeric address
-
set
protected void set(String protocol, String host, int port, String file, String ref)
Sets the fields of the URL. This is not a public method so that only URLStreamHandlers can modify URL fields. URLs are otherwise constant.- Parameters:
protocol- the name of the protocol to usehost- the name of the hostport- the port number on the hostfile- the file on the hostref- the internal reference in the URL
-
set
protected void set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref)
Sets the specified 8 fields of the URL. This is not a public method so that only URLStreamHandlers can modify URL fields. URLs are otherwise constant.- Parameters:
protocol- the name of the protocol to usehost- the name of the hostport- the port number on the hostauthority- the authority part for the urluserInfo- the username and passwordpath- the file on the hostref- the internal reference in the URLquery- the query part of this URL- Since:
- 1.3
-
getQuery
public String getQuery()
Gets the query part of thisURL.- Returns:
- the query part of this
URL, ornullif one does not exist - Since:
- 1.3
-
getPath
public String getPath()
Gets the path part of thisURL.- Returns:
- the path part of this
URL, or an empty string if one does not exist - Since:
- 1.3
-
getUserInfo
public String getUserInfo()
Gets the userInfo part of thisURL.- Returns:
- the userInfo part of this
URL, ornullif one does not exist - Since:
- 1.3
-
getAuthority
public String getAuthority()
Gets the authority part of thisURL.- Returns:
- the authority part of this
URL - Since:
- 1.3
-
getPort
public int getPort()
Gets the port number of thisURL.- Returns:
- the port number, or -1 if the port is not set
-
getDefaultPort
public int getDefaultPort()
Gets the default port number of the protocol associated with thisURL. If the URL scheme or the URLStreamHandler for the URL do not define a default port number, then -1 is returned.- Returns:
- the port number
- Since:
- 1.4
-
getProtocol
public String getProtocol()
Gets the protocol name of thisURL.- Returns:
- the protocol of this
URL.
-
getHost
public String getHost()
Gets the host name of thisURL, if applicable. The format of the host conforms to RFC 2732, i.e. for a literal IPv6 address, this method will return the IPv6 address enclosed in square brackets ( '[' and ']').- Returns:
- the host name of this
URL.
-
getFile
public String getFile()
Gets the file name of thisURL. The returned file portion will be the same asgetPath(), plus the concatenation of the value ofgetQuery(), if any. If there is no query portion, this method andgetPath()will return identical results.- Returns:
- the file name of this
URL, or an empty string if one does not exist
-
getRef
public String getRef()
Gets the anchor (also known as the "reference") of thisURL.- Returns:
- the anchor (also known as the "reference") of this
URL, ornullif one does not exist
-
equals
public boolean equals(Object obj)
Compares this URL for equality with another object.If the given object is not a URL then this method immediately returns
false.Two URL objects are equal if they have the same protocol, reference equivalent hosts, have the same port number on the host, and the same file and fragment of the file.
Two hosts are considered equivalent if both host names can be resolved into the same IP addresses; else if either host name can't be resolved, the host names must be equal without regard to case; or both host names equal to null.
Since hosts comparison requires name resolution, this operation is a blocking operation.
Note: The defined behavior for
equalsis known to be inconsistent with virtual hosting in HTTP.
-
hashCode
public int hashCode()
Creates an integer suitable for hash table indexing.The hash code is based upon all the URL components relevant for URL comparison. As such, this operation is a blocking operation.
-
openStream
public InputStream openStream()
-
toString
public String toString()
Constructs a string representation of thisURL. The string is created by calling thetoExternalFormmethod of the stream protocol handler for this object.- Overrides:
toStringin classObject- Returns:
- a string representation of this object.
- See Also:
java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String),URLStreamHandler.toExternalForm(java.net.URL)
-
toExternalForm
public String toExternalForm()
Constructs a string representation of thisURL. The string is created by calling thetoExternalFormmethod of the stream protocol handler for this object.- Returns:
- a string representation of this object.
- See Also:
java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String),URLStreamHandler.toExternalForm(java.net.URL)
-
openConnection
public URLConnection openConnection() throws IOException
Returns aURIequivalent to this URL. This method functions in the same way asnew URI (this.toString()).Note, any URL instance that complies with RFC 2396 can be converted to a URI. However, some URLs that are not strictly in compliance can not be converted to a URI.
- Returns:
- a URI instance equivalent to this URL.
- Throws:
URISyntaxException- if this URL is not formatted strictly according to to RFC2396 and cannot be converted to a URI.IOException- Since:
- 1.5
-
-