Class URLUtils
The systemId strings used by Source
s
have similar semantics to the strings which this class converts
to URLs or contexts.
This class assumes that the "file:" protocol is legal for URLs, and will throw AssertionErrors if this turns out not to be the case.
- Author:
- Mark Taylor (Starlink), Norman Gray (Starlink)
-
Method Summary
Modifier and TypeMethodDescriptionstatic URL
Fixes file: URLs which don't have enough slashes in them.static URLConnection
followRedirects
(URLConnection conn, int[] redirCodes) Takes a URLConnection and repeatedly follows 3xx redirects until a non-redirect status is achieved.static void
Attempts to install additional URL protocol handlers suitable for astronomy applications.static URL
makeFileURL
(File file) Constructs a legal URL for a given File.static URL
Obtains a URL from a string.static URL
Obtains a URL from a string in a given context.static boolean
sameResource
(URL url1, URL url2) Attempts to determine whether two URLs refer to the same resource.static boolean
Compares two URLs.static File
Locates the local file, if any, represented by a URL.static URI
Turns a URL into a URI.
-
Method Details
-
makeURL
Obtains a URL from a string. If the String has the form of a URL, it is turned directly into a URL. If it does not, it is treated as a filename, and turned into a file-protocol URL. In the latter case a relative or absolute filename may be used. If it is null or a blank string (or something else equally un-filename like?) then null is returned.- Parameters:
location
- a string representing the location of a resource- Returns:
- a URL representing the location of the resource
-
makeURL
Obtains a URL from a string in a given context. The string context is turned into a URL as per themakeURL(String)
method, unless it is null or the empty string, in which case it is treated as a reference to the current directory. The string location is then turned into a URL in the same way as usingmakeURL(String)
, except that if it represents a relative path it is resolved in the context of context, taking its protocol and/or relative position from it.- Parameters:
context
- a string representing the context within which location is to be resolvedlocation
- a string representing the location of a resource- Returns:
- a URL representing the location of the resource
-
urlToUri
Turns a URL into a URI.Since URIs are syntactically and semantically a superset of URLs, this conversion should not cause any errors. If, however, the input URL is malformed in rather extreme ways, then the URI construction will fail. These ways include (but are not necesssarily limited to) the features discussed in
URI(String,String,String,String,String)
, namely that a scheme is present, but with a relative path, or that it has a registry-based authority part.Because of the way the class does the conversion, the method will itself resolve some malformations of URLs. You should not rely on this, however, firstly because the method might in principle change, but mostly because you should avoid creating such malformed URLs in the first place.
The most common source of malformed URLs is that of
file
URLs which have inadequately escaped (windows) drive letters or spaces in the name: such URLs should be constructed using theFile.toURI()
orFile.toURL()
methods. Such URLs will be escaped by this method.- Parameters:
url
- a URL to be converted. If this is null, then the method returns null- Returns:
- the input URL as a URI, or null if the input was null
- Throws:
MalformedURLException
- if the URI cannot be constructed because the input URL turns out to be malformed
-
makeFileURL
Constructs a legal URL for a given File. Unlike java, this gives you a URL which conforms to RFC1738 and looks like "file://localhost/abs-path
" rather than "file:abs-or-rel-path
".- Parameters:
file
- file- Returns:
- URL
- See Also:
-
fixURL
Fixes file: URLs which don't have enough slashes in them. Java generates invalid URLs of the form "file:abs-or-rel-path
" when it should generate "file://localhost/abs-path
".- Parameters:
url
- input URL- Returns:
- fixed URL
- See Also:
-
sameResource
Attempts to determine whether two URLs refer to the same resource. Not likely to be foolproof, but slightly smarter than usingequals
.- Parameters:
url1
- first URLurl2
- second URL- Returns:
- true if
url1
andurl2
appear to refer to the same resource
-
urlToFile
Locates the local file, if any, represented by a URL. If the URL string uses the "file:" protocol, and has no query or anchor parts, the filename will be extracted and the corresponding file returned. Otherwise, null is returned.- Parameters:
url
- URL string- Returns:
- local file referenced by
url
, or null
-
urlEquals
Compares two URLs. This does approximatly the same job as the URL.equals() method, but it avoids the possible network accesses associated with that implementation, and copes with null values.- Parameters:
url1
- first URLurl2
- second URL- Returns:
- true iff both are the same, or both are null
-
followRedirects
public static URLConnection followRedirects(URLConnection conn, int[] redirCodes) throws IOException Takes a URLConnection and repeatedly follows 3xx redirects until a non-redirect status is achieved. Infinite loops are defended against. The Accept-Encoding header, if present, is propagated to redirect targets.Note that the
HttpURLConnection.setInstanceFollowRedirects(boolean)
method does something like this, but it refuses to redirect between different URL protocols, for security reasons (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4620571). Considering similar arguments, this method will direct HTTP->HTTPS, but not vice versa.- Parameters:
conn
- initial URL connectionredirCodes
- list of HTTP codes for which redirects should be followed; if null all suitable 3xx redirections will be followed (301, 302, 303, 307, 308)- Returns:
- target URL connection
(if no redirects, the same as
hconn
) - Throws:
IOException
- See Also:
-
installCustomHandlers
public static void installCustomHandlers()Attempts to install additional URL protocol handlers suitable for astronomy applications. Currently installs handlers which can supply MySpace connections using either "ivo:
" or "myspace:
" protocols.
-