public class URLUtils
extends java.lang.Object
makeURL
is called on the string "gftp://host/file" and
no gftp handler is installed, it will be interpreted as a file-protocol
URL referring to the (presumably non-existent) file "gftp://host/file".
In this case the eventual upshot will presumably be a file-not-found
type error rather than a MalformedURLException type error getting
presented to the user. Users of this class should be of the opinion
that this is not a particularly bad thing.
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.
Modifier and Type | Method and Description |
---|---|
static java.net.URL |
fixURL(java.net.URL url)
Fixes file: URLs which don't have enough slashes in them.
|
static java.net.URLConnection |
followRedirects(java.net.URLConnection conn,
int[] redirCodes)
Takes a URLConnection and repeatedly follows 3xx redirects
until a non-redirect status is achieved.
|
static void |
installCustomHandlers()
Attempts to install additional URL protocol handlers suitable
for astronomy applications.
|
static java.net.URL |
makeFileURL(java.io.File file)
Constructs a legal URL for a given File.
|
static java.net.URL |
makeURL(java.lang.String location)
Obtains a URL from a string.
|
static java.net.URL |
makeURL(java.lang.String context,
java.lang.String location)
Obtains a URL from a string in a given context.
|
static java.net.URL |
newURL(java.lang.String spec)
Drop-in replacement for the deprecated
URL.URL(String) constructor. |
static boolean |
sameResource(java.net.URL url1,
java.net.URL url2)
Attempts to determine whether two URLs refer to the same resource.
|
static boolean |
urlEquals(java.net.URL url1,
java.net.URL url2)
Compares two URLs.
|
static java.io.File |
urlToFile(java.lang.String url)
Locates the local file, if any, represented by a URL.
|
static java.net.URI |
urlToUri(java.net.URL url)
Turns a URL into a URI.
|
public static java.net.URL newURL(java.lang.String spec) throws java.net.MalformedURLException
URL.URL(String)
constructor.
All URL constructors are deprecated since Java 20 because of issues with parsing and validation. This utility method provides a way for code to avoid deprecation warnings. It may not do much to solve the underlying problems, and might introduce some new ones, but code that is having problems here can be adapted to handle URL creation more carefully; such approaches, according to the JDK documentation, should generally be URI-based. Other utility methods may be added here in future as required.
As far as I can tell, most of the difficulties arising with URL parsing that have led to the deprecation relate to relatively strange URLs, so that "normal" http/https/file-protocol URL strings passed to this method should behave the same as if passed to the deprecated constructor. However, there may be changes of behaviour when it comes to constructions like embedded spaces in paths or special characters in query parts etc.
Note that passing a string to this method which is not a valid URI,
for instance because it contains unescaped illegal characters like "[",
will fail, unlike the call to new URL()
.
In such cases a MalformedURLException
will be thrown
(which is really the result of a URISyntaxException
).
spec
- textual representation of URLjava.net.MalformedURLException
- in case of syntax errorpublic static java.net.URL makeURL(java.lang.String location)
location
- a string representing the location of a resourcepublic static java.net.URL makeURL(java.lang.String context, java.lang.String location)
context
is turned into a URL as per
the makeURL(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 using makeURL(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.context
- a string representing the context within which
location
is to be resolvedlocation
- a string representing the location of a resourcepublic static java.net.URI urlToUri(java.net.URL url) throws java.net.MalformedURLException
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.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 the File.toURI()
or File.toURL()
methods. Such URLs will be escaped by
this method.
url
- a URL to be converted. If this is null, then the
method returns nulljava.net.MalformedURLException
- if the URI cannot be constructed
because the input URL turns out to be malformedpublic static java.net.URL makeFileURL(java.io.File file)
file://localhost/abs-path
" rather than
"file:abs-or-rel-path
".file
- filepublic static java.net.URL fixURL(java.net.URL url)
file:abs-or-rel-path
"
when it should generate "file://localhost/abs-path
".url
- input URLpublic static boolean sameResource(java.net.URL url1, java.net.URL url2)
equals
.url1
- first URLurl2
- second URLurl1
and url2
appear to
refer to the same resourcepublic static java.io.File urlToFile(java.lang.String url)
url
- URL stringurl
, or nullpublic static boolean urlEquals(java.net.URL url1, java.net.URL url2)
url1
- first URLurl2
- second URLpublic static java.net.URLConnection followRedirects(java.net.URLConnection conn, int[] redirCodes) throws java.io.IOException
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.
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)hconn
)java.io.IOException
public static void installCustomHandlers()
ivo:
" or
"myspace:
" protocols.