public class ParsedURL
extends java.lang.Object
URL
-like class that supports custom URI schemes
and GZIP encoding.
This class is used as a replacement for URL
.
This is done for several reasons. First, unlike URL
this class will accept and parse as much of a URL as possible, without
throwing a MalformedURLException
. This makes it useful
for simply parsing a URL string (hence its name).
Second, it allows for extension of the URI schemes supported by the
parser. Batik uses this to support the
data:
URL scheme (RFC2397).
Third, by default it checks the streams that it opens to see if they are GZIP compressed, and if so it automatically uncompresses them (avoiding opening the stream twice in the process).
It is worth noting that most real work is defered to the
ParsedURLData
class to which most methods are forwarded.
This is done because it allows a constructor interface to ParsedURL
(mostly for compatability with core URL
), in spite of the fact that
the real implemenation uses the protocol handlers as factories for protocol
specific instances of the ParsedURLData
class.
Constructor and Description |
---|
ParsedURL(ParsedURL baseURL,
java.lang.String urlStr)
Construct a sub URL from a base ParsedURL and a string for the sub url.
|
ParsedURL(java.lang.String urlStr)
Construct a ParsedURL from the given url string.
|
ParsedURL(java.lang.String baseStr,
java.lang.String urlStr)
Construct a sub URL from two strings.
|
ParsedURL(java.net.URL url)
Construct a ParsedURL from the given java.net.URL instance.
|
ParsedURL(java.net.URL baseURL,
java.lang.String urlStr)
Construct a sub URL from a base URL and a string for the sub url.
|
Modifier and Type | Method and Description |
---|---|
static java.io.InputStream |
checkGZIP(java.io.InputStream is)
This is a utility function others can call that checks if
is is a GZIP stream if so it returns a GZIPInputStream that
will decode the contents, otherwise it returns (or a
buffered version of is) untouched.
|
boolean |
complete()
Returns true if the URL looks well formed and complete.
|
boolean |
equals(java.lang.Object obj)
Implement Object.equals.
|
java.lang.String |
getContentEncoding()
Returns the content encoding if available.
|
java.lang.String |
getContentType()
Returns the content type if available.
|
java.lang.String |
getContentTypeCharset()
Returns the content type's charset parameter, if available.
|
java.lang.String |
getContentTypeMediaType()
Returns the content type's type/subtype, if available.
|
static java.lang.String |
getGlobalUserAgent() |
static ParsedURLProtocolHandler |
getHandler(java.lang.String protocol)
Returns the handler for a particular protocol.
|
java.lang.String |
getHost()
Returns the host for this URL, if any,
null if there isn't
one or it doesn't make sense for the protocol. |
java.lang.String |
getPath()
Returns the path for this URL, if any (where appropriate for
the protocol this also includes the file, not just directory).
|
int |
getPort()
Returns the port on the host to connect to, if it was specified
in the url that was parsed, otherwise returns -1.
|
java.lang.String |
getPortStr()
Returns the URL up to and include the port number on
the host.
|
java.lang.String |
getPostConnectionURL()
Returns the URL that was ultimately used to fetch the resource
represented by this
ParsedURL . |
java.lang.String |
getProtocol()
Returns the protocol for this URL.
|
protected static java.lang.String |
getProtocol(java.lang.String urlStr)
Parse out the protocol from a url string.
|
java.lang.String |
getRef()
Returns the 'fragment' reference in the URL.
|
java.lang.String |
getUserAgent()
Return the user agent current associated with this url (or
null if none).
|
boolean |
hasContentTypeParameter(java.lang.String param)
Returns whether the Content-Type header has the given parameter.
|
int |
hashCode()
Implement Object.hashCode.
|
java.io.InputStream |
openStream()
Attempt to open the stream checking for common compression
types, and automatically decompressing them if found.
|
java.io.InputStream |
openStream(java.util.Iterator mimeTypes)
Attempt to open the stream checking for common compression
types, and automatically decompressing them if found.
|
java.io.InputStream |
openStream(java.lang.String mimeType)
Attempt to open the stream checking for common compression
types, and automatically decompressing them if found.
|
java.io.InputStream |
openStream(java.lang.String[] mimeTypes)
Attempt to open the stream checking for common compression
types, and automatically decompressing them if found.
|
java.io.InputStream |
openStreamRaw()
Attempt to open the stream, does no checking for compression
types.
|
java.io.InputStream |
openStreamRaw(java.util.Iterator mimeTypes)
Attempt to open the stream, does no checking for comression
types.
|
java.io.InputStream |
openStreamRaw(java.lang.String mimeType)
Attempt to open the stream, does no checking for compression
types.
|
java.io.InputStream |
openStreamRaw(java.lang.String[] mimeTypes)
Attempt to open the stream, does no checking for comression
types.
|
static ParsedURLData |
parseURL(ParsedURL baseURL,
java.lang.String urlStr)
Factory method to construct an appropriate subclass of ParsedURLData,
for a sub url.
|
static ParsedURLData |
parseURL(java.lang.String urlStr)
Factory method to construct an appropriate subclass of ParsedURLData
|
static ParsedURLData |
parseURL(java.lang.String baseStr,
java.lang.String urlStr)
Factory method to construct an appropriate subclass of ParsedURLData,
for a sub url.
|
static void |
registerHandler(ParsedURLProtocolHandler handler)
Registers a Protocol handler by adding it to the handlers map.
|
boolean |
sameFile(ParsedURL other) |
static void |
setGlobalUserAgent(java.lang.String userAgent) |
void |
setUserAgent(java.lang.String userAgent)
Sets the user agent associated with this url (null clears
any associated user agent).
|
java.lang.String |
toString()
Return a string rep of the URL (can be passed back into the
constructor if desired).
|
public ParsedURL(java.lang.String urlStr)
urlStr
- The string to try and parse as a URLpublic ParsedURL(java.net.URL url)
url
- The URL to "mimic".public ParsedURL(java.lang.String baseStr, java.lang.String urlStr)
baseStr
- The 'parent' URL. Should be complete.urlStr
- The 'sub' URL may be complete or partial.
the missing pieces will be taken from the baseStr.public ParsedURL(java.net.URL baseURL, java.lang.String urlStr)
baseURL
- The 'parent' URL.urlStr
- The 'sub' URL may be complete or partial.
the missing pieces will be taken from the baseURL.public ParsedURL(ParsedURL baseURL, java.lang.String urlStr)
baseURL
- The 'parent' URL.urlStr
- The 'sub' URL may be complete or partial.
the missing pieces will be taken from the baseURL.public static java.lang.String getGlobalUserAgent()
public static void setGlobalUserAgent(java.lang.String userAgent)
public static ParsedURLProtocolHandler getHandler(java.lang.String protocol)
null
or no match is found in the handlers map it
returns the default protocol handler.protocol
- The protocol to get a handler for.public static void registerHandler(ParsedURLProtocolHandler handler)
null
as it's
supported protocol then it is registered as the default
protocol handler.handler
- the new Protocol Handler to registerpublic static java.io.InputStream checkGZIP(java.io.InputStream is) throws java.io.IOException
is
- Stream that may potentially be a GZIP stream.java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String getPostConnectionURL()
ParsedURL
. For HTTP URLs,
this will result in the post-redirect URL being returned.
If there was no redirect, or if this isn't an HTTP URL, the
original URL is returned (the same string as toString()
).public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public boolean complete()
public java.lang.String getUserAgent()
public void setUserAgent(java.lang.String userAgent)
public java.lang.String getProtocol()
public java.lang.String getHost()
null
if there isn't
one or it doesn't make sense for the protocol.public int getPort()
public java.lang.String getPath()
public java.lang.String getRef()
public java.lang.String getPortStr()
public java.lang.String getContentType()
public java.lang.String getContentTypeMediaType()
public java.lang.String getContentTypeCharset()
public boolean hasContentTypeParameter(java.lang.String param)
public java.lang.String getContentEncoding()
public java.io.InputStream openStream() throws java.io.IOException
java.io.IOException
public java.io.InputStream openStream(java.lang.String mimeType) throws java.io.IOException
mimeType
- The expected mime type of the content
in the returned InputStream (mapped to Http accept
header among other possibilities).java.io.IOException
public java.io.InputStream openStream(java.lang.String[] mimeTypes) throws java.io.IOException
mimeTypes
- The expected mime types of the content
in the returned InputStream (mapped to Http accept
header among other possabilities).java.io.IOException
public java.io.InputStream openStream(java.util.Iterator mimeTypes) throws java.io.IOException
mimeTypes
- The expected mime types of the content
in the returned InputStream (mapped to Http accept
header among other possabilities). The elements of
the iterator must be strings.java.io.IOException
public java.io.InputStream openStreamRaw() throws java.io.IOException
java.io.IOException
public java.io.InputStream openStreamRaw(java.lang.String mimeType) throws java.io.IOException
mimeType
- The expected mime type of the content
in the returned InputStream (mapped to Http accept
header among other possabilities).java.io.IOException
public java.io.InputStream openStreamRaw(java.lang.String[] mimeTypes) throws java.io.IOException
mimeTypes
- The expected mime types of the content
in the returned InputStream (mapped to Http accept
header among other possabilities).java.io.IOException
public java.io.InputStream openStreamRaw(java.util.Iterator mimeTypes) throws java.io.IOException
mimeTypes
- The expected mime types of the content
in the returned InputStream (mapped to Http accept
header among other possabilities). The elements of
the iterator must be strings.java.io.IOException
public boolean sameFile(ParsedURL other)
protected static java.lang.String getProtocol(java.lang.String urlStr)
public static ParsedURLData parseURL(java.lang.String urlStr)
urlStr
- the string to parse.public static ParsedURLData parseURL(java.lang.String baseStr, java.lang.String urlStr)
baseStr
- The base URL string to parse.urlStr
- the sub URL string to parse.public static ParsedURLData parseURL(ParsedURL baseURL, java.lang.String urlStr)
baseURL
- The base ParsedURL to parse.urlStr
- the sub URL string to parse.Copyright © 2022 Apache Software Foundation. All Rights Reserved.