下面列出了java.net.URI#isAbsolute ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void validateURL(final URL url, final File file) {
try {
final URI uri = url.toURI();
if (!uri.isAbsolute()) {
throw new IllegalArgumentException("URI is not absolute: " + uri.toString() + " File: " + file.getAbsolutePath()); //NOI18N
}
if (uri.isOpaque()) {
throw new IllegalArgumentException("URI is not hierarchical: " + uri.toString() + " File: " + file.getAbsolutePath()); //NOI18N
}
if (!"file".equals(uri.getScheme())) {
throw new IllegalArgumentException("URI scheme is not \"file\": " + uri.toString() + " File: " + file.getAbsolutePath()); //NOI18N
}
} catch (URISyntaxException use) {
throw new IllegalArgumentException(use);
}
}
private static URL getDistribution (String distribution, URI base) {
URL retval = null;
if (distribution != null && distribution.length () > 0) {
try {
URI distributionURI = new URI (distribution);
if (! distributionURI.isAbsolute ()) {
if (base != null) {
distributionURI = base.resolve (distributionURI);
}
}
retval = distributionURI.toURL ();
} catch (MalformedURLException | URISyntaxException ex) {
ERR.log (Level.INFO, null, ex);
}
}
return retval;
}
/**
* Creates a new instance of this class with the specified parameters.
*
* @param uri the URI of this description within the {@link ConfigDescriptionRegistry} (must neither be null nor
* empty)
* @param parameters the list of configuration parameters that belong to the given URI
* (could be null or empty)
* @param groups the list of groups associated with the parameters
* @throws IllegalArgumentException if the URI is null or invalid
*/
public ConfigDescription(URI uri, List<ConfigDescriptionParameter> parameters,
List<ConfigDescriptionParameterGroup> groups) {
if (uri == null) {
throw new IllegalArgumentException("The URI must not be null!");
}
if (!uri.isAbsolute()) {
throw new IllegalArgumentException("The scheme is missing!");
}
if (!uri.isOpaque()) {
throw new IllegalArgumentException("The scheme specific part (token) must not start with a slash ('/')!");
}
this.uri = uri;
if (parameters != null) {
this.parameters = Collections.unmodifiableList(parameters);
} else {
this.parameters = Collections.unmodifiableList(new ArrayList<ConfigDescriptionParameter>(0));
}
if (groups != null) {
this.parameterGroups = Collections.unmodifiableList(groups);
} else {
this.parameterGroups = Collections.unmodifiableList(new ArrayList<ConfigDescriptionParameterGroup>(0));
}
}
/**
* Enforces the specification of a "relative" name as used in
* {@linkplain #getFileForInput(Location, String, String)
* getFileForInput}. This method must follow the rules defined in
* that method, do not make any changes without consulting the
* specification.
*/
protected static boolean isRelativeUri(URI uri) {
if (uri.isAbsolute())
return false;
String path = uri.normalize().getPath();
if (path.length() == 0 /* isEmpty() is mustang API */)
return false;
if (!path.equals(uri.getPath())) // implicitly checks for embedded . and ..
return false;
return !path.startsWith("/") && !path.startsWith("./") && !path.startsWith("../");
}
/**
* Build URI starting from the given base and href.
* <br/>
* If href is absolute or base is null then base will be ignored.
*
* @param base URI prefix.
* @param href URI suffix.
* @return built URI.
*/
public static URI getURI(final URI base, final String href) {
if (href == null) {
throw new IllegalArgumentException("Null link provided");
}
URI uri = URI.create(href);
if (!uri.isAbsolute() && base != null) {
uri = URI.create(base.toASCIIString() + "/" + href);
}
return uri.normalize();
}
/**
* Initialize listener
*/
private synchronized void init() {
try {
tenantDataReceiveURLs = ConfigurationFacade.getInstance().getTenantDataEndpointURLs();
if (!tenantDataReceiveURLs.isEmpty()) {
serverURL = IdentityUtil.getServerURL("", true, true);
int index = 0;
for (String tenantDataReceiveUrl : tenantDataReceiveURLs) {
URI tenantDataReceiveURI = new URI(tenantDataReceiveUrl);
if (log.isDebugEnabled()) {
log.debug("Tenant list receiving url added : " + tenantDataReceiveUrl);
}
if (!tenantDataReceiveURI.isAbsolute()) {
// Set the absolute URL for tenant list receiving endpoint
tenantDataReceiveURLs.set(index, serverURL + tenantDataReceiveUrl);
}
index++;
}
initialized = true;
} else {
if (log.isDebugEnabled()) {
log.debug("TenantDataListenerURLs are not set in configuration");
}
}
} catch (URISyntaxException e) {
log.error("Error while getting TenantDataListenerURLs", e);
}
}
private URI createUri(String uri)
{
URI uriToCheck = URI.create(uri);
if (uri.charAt(0) != '/' || uriToCheck.isAbsolute())
{
return uriToCheck;
}
return UriUtils.buildNewUrl(mainApplicationPageURI, uriToCheck.getPath());
}
private static URI handleSeedURL(final MutableString s) {
final URI url = BURL.parse(s);
if (url != null) {
if (url.isAbsolute()) return url;
else LOGGER.error("The seed URL " + s + " is relative");
}
else LOGGER.error("The seed URL " + s + " is malformed");
return null;
}
@Override
public void afterPropertiesSet() {
URI baseUri = configuration.getBaseUri();
if (baseUri.isAbsolute()) {
// HttpServletRequest request = new BasePathAwareHandlerMapping.UriAwareHttpServletRequest(getServletContext(), baseUri);
// this.prefix = URL_PATH_HELPER.getPathWithinApplication(request);
throw new UnsupportedOperationException(format("absolute base URIs not supported %s", baseUri));
} else {
this.prefix = baseUri.toString();
}
super.afterPropertiesSet();
}
/**
* Returns the absolute uri of the Class-Path entry value as specified in
* <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#Main%20Attributes">
* JAR File Specification</a>. Even though the specification only talks about relative urls,
* absolute urls are actually supported too (for example, in Maven surefire plugin).
*/
@VisibleForTesting static URI getClassPathEntry(File jarFile, String path)
throws URISyntaxException {
URI uri = new URI(path);
if (uri.isAbsolute()) {
return uri;
} else {
return new File(jarFile.getParentFile(), path.replace('/', File.separatorChar)).toURI();
}
}
/**
* Checks the syntax of the given URL.
* @param url the URL.
* @return true, if valid.
*/
private boolean checkUrl(String url) {
try {
URI uri = new URI(url);
return uri.isAbsolute();
}
catch (URISyntaxException ex) {
return false;
}
}
public LocalFile( URI uri )
{
this.file = uri.isAbsolute( ) ? new File( uri )
: new File( uri.toString( ) );
}
@Override
public boolean canWriteToLocation(URI location) {
return location.isAbsolute();
}
/**
* Construct the absolute path from the file location and the current
* directory. The current directory is either of the form
* {code}hdfs://<nodename>:<nodeport>/<directory>{code} in Hadoop
* MapReduce mode, or of the form
* {code}file:///<directory>{code} in Hadoop local mode.
*
* @param location the location string specified in the load statement
* @param curDir the current file system directory
* @return the absolute path of file in the file system
* @throws FrontendException if the scheme of the location is incompatible
* with the scheme of the file system
*/
public static String getAbsolutePath(String location, Path curDir)
throws FrontendException {
if (location == null || curDir == null) {
throw new FrontendException(
"location: " + location + " curDir: " + curDir);
}
URI fsUri = curDir.toUri();
String fsScheme = fsUri.getScheme();
if (fsScheme == null) {
throw new FrontendException("curDir: " + curDir);
}
fsScheme = fsScheme.toLowerCase();
String authority = fsUri.getAuthority();
if(authority == null) {
authority = "";
}
Path rootDir = new Path(fsScheme, authority, "/");
ArrayList<String> pathStrings = new ArrayList<String>();
String[] fnames = getPathStrings(location);
for (String fname: fnames) {
// remove leading/trailing whitespace(s)
fname = fname.trim();
Path p = new Path(fname);
URI uri = p.toUri();
// if the supplied location has a scheme (i.e. uri is absolute) or
// an absolute path, just use it.
if(! (uri.isAbsolute() || p.isAbsolute())) {
String scheme = uri.getScheme();
if (scheme != null) {
scheme = scheme.toLowerCase();
}
if (scheme != null && !scheme.equals(fsScheme)) {
throw new FrontendException("Incompatible file URI scheme: "
+ scheme + " : " + fsScheme);
}
String path = uri.getPath();
fname = (p.isAbsolute()) ?
new Path(rootDir, path).toString() :
new Path(curDir, path).toString();
}
fname = fname.replaceFirst("^file:/([^/])", "file:///$1");
// remove the trailing /
fname = fname.replaceFirst("/$", "");
pathStrings.add(fname);
}
return join(pathStrings, ",");
}
protected Message createMessage(Object body,
String httpMethod,
MultivaluedMap<String, String> headers,
URI currentURI,
Exchange exchange,
Map<String, Object> invocationContext,
boolean proxy) {
checkClosed();
Message m = cfg.getConduitSelector().getEndpoint().getBinding().createMessage();
m.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
m.put(Message.INBOUND_MESSAGE, Boolean.FALSE);
setRequestMethod(m, httpMethod);
m.put(Message.PROTOCOL_HEADERS, headers);
if (currentURI.isAbsolute() && currentURI.getScheme().startsWith(HTTP_SCHEME)) {
m.put(Message.ENDPOINT_ADDRESS, currentURI.toString());
} else {
m.put(Message.ENDPOINT_ADDRESS, state.getBaseURI().toString());
}
Object requestURIProperty = cfg.getRequestContext().get(Message.REQUEST_URI);
if (requestURIProperty == null) {
m.put(Message.REQUEST_URI, currentURI.toString());
} else {
m.put(Message.REQUEST_URI, requestURIProperty.toString());
}
String ct = headers.getFirst(HttpHeaders.CONTENT_TYPE);
m.put(Message.CONTENT_TYPE, ct);
body = checkIfBodyEmpty(body, ct);
setEmptyRequestPropertyIfNeeded(m, body);
m.setContent(List.class, getContentsList(body));
m.put(URITemplate.TEMPLATE_PARAMETERS, getState().getTemplates());
PhaseInterceptorChain chain = setupOutInterceptorChain(cfg);
chain.setFaultObserver(setupInFaultObserver(cfg));
m.setInterceptorChain(chain);
exchange = createExchange(m, exchange);
exchange.put(Message.REST_MESSAGE, Boolean.TRUE);
exchange.setOneWay("true".equals(headers.getFirst(Message.ONE_WAY_REQUEST)));
exchange.put(Retryable.class, new RetryableImpl());
// context
setContexts(m, exchange, invocationContext, proxy);
//setup conduit selector
prepareConduitSelector(m, currentURI, proxy);
return m;
}
/**
* Converts given URI to a Path
*/
static WindowsPath fromUri(WindowsFileSystem fs, URI uri) {
if (!uri.isAbsolute())
throw new IllegalArgumentException("URI is not absolute");
if (uri.isOpaque())
throw new IllegalArgumentException("URI is not hierarchical");
String scheme = uri.getScheme();
if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
throw new IllegalArgumentException("URI scheme is not \"file\"");
if (uri.getFragment() != null)
throw new IllegalArgumentException("URI has a fragment component");
if (uri.getQuery() != null)
throw new IllegalArgumentException("URI has a query component");
String path = uri.getPath();
if (path.equals(""))
throw new IllegalArgumentException("URI path component is empty");
// UNC
String auth = uri.getAuthority();
if (auth != null && !auth.equals("")) {
String host = uri.getHost();
if (host == null)
throw new IllegalArgumentException("URI authority component has undefined host");
if (uri.getUserInfo() != null)
throw new IllegalArgumentException("URI authority component has user-info");
if (uri.getPort() != -1)
throw new IllegalArgumentException("URI authority component has port number");
// IPv6 literal
// 1. drop enclosing brackets
// 2. replace ":" with "-"
// 3. replace "%" with "s" (zone/scopeID delimiter)
// 4. Append .ivp6-literal.net
if (host.startsWith("[")) {
host = host.substring(1, host.length()-1)
.replace(':', '-')
.replace('%', 's');
host += IPV6_LITERAL_SUFFIX;
}
// reconstitute the UNC
path = "\\\\" + host + path;
} else {
if ((path.length() > 2) && (path.charAt(2) == ':')) {
// "/c:/foo" --> "c:/foo"
path = path.substring(1);
}
}
return WindowsPath.parse(fs, path);
}
/**
* Converts given URI to a Path
*/
static WindowsPath fromUri(WindowsFileSystem fs, URI uri) {
if (!uri.isAbsolute())
throw new IllegalArgumentException("URI is not absolute");
if (uri.isOpaque())
throw new IllegalArgumentException("URI is not hierarchical");
String scheme = uri.getScheme();
if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
throw new IllegalArgumentException("URI scheme is not \"file\"");
if (uri.getFragment() != null)
throw new IllegalArgumentException("URI has a fragment component");
if (uri.getQuery() != null)
throw new IllegalArgumentException("URI has a query component");
String path = uri.getPath();
if (path.equals(""))
throw new IllegalArgumentException("URI path component is empty");
// UNC
String auth = uri.getAuthority();
if (auth != null && !auth.equals("")) {
String host = uri.getHost();
if (host == null)
throw new IllegalArgumentException("URI authority component has undefined host");
if (uri.getUserInfo() != null)
throw new IllegalArgumentException("URI authority component has user-info");
if (uri.getPort() != -1)
throw new IllegalArgumentException("URI authority component has port number");
// IPv6 literal
// 1. drop enclosing brackets
// 2. replace ":" with "-"
// 3. replace "%" with "s" (zone/scopeID delimiter)
// 4. Append .ivp6-literal.net
if (host.startsWith("[")) {
host = host.substring(1, host.length()-1)
.replace(':', '-')
.replace('%', 's');
host += IPV6_LITERAL_SUFFIX;
}
// reconstitute the UNC
path = "\\\\" + host + path;
} else {
if ((path.length() > 2) && (path.charAt(2) == ':')) {
// "/c:/foo" --> "c:/foo"
path = path.substring(1);
}
}
return WindowsPath.parse(fs, path);
}
/**
* Creates a new <tt>File</tt> instance by converting the given
* <tt>file:</tt> URI into an abstract pathname.
*
* <p> The exact form of a <tt>file:</tt> URI is system-dependent, hence
* the transformation performed by this constructor is also
* system-dependent.
*
* <p> For a given abstract pathname <i>f</i> it is guaranteed that
*
* <blockquote><tt>
* new File(</tt><i> f</i><tt>.{@link #toURI() toURI}()).equals(</tt><i> f</i><tt>.{@link #getAbsoluteFile() getAbsoluteFile}())
* </tt></blockquote>
*
* so long as the original abstract pathname, the URI, and the new abstract
* pathname are all created in (possibly different invocations of) the same
* Java virtual machine. This relationship typically does not hold,
* however, when a <tt>file:</tt> URI that is created in a virtual machine
* on one operating system is converted into an abstract pathname in a
* virtual machine on a different operating system.
*
* @param uri
* An absolute, hierarchical URI with a scheme equal to
* <tt>"file"</tt>, a non-empty path component, and undefined
* authority, query, and fragment components
*
* @throws NullPointerException
* If <tt>uri</tt> is <tt>null</tt>
*
* @throws IllegalArgumentException
* If the preconditions on the parameter do not hold
*
* @see #toURI()
* @see java.net.URI
* @since 1.4
*/
public File(URI uri) {
// Check our many preconditions
if (!uri.isAbsolute())
throw new IllegalArgumentException("URI is not absolute");
if (uri.isOpaque())
throw new IllegalArgumentException("URI is not hierarchical");
String scheme = uri.getScheme();
if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
throw new IllegalArgumentException("URI scheme is not \"file\"");
if (uri.getAuthority() != null)
throw new IllegalArgumentException("URI has an authority component");
if (uri.getFragment() != null)
throw new IllegalArgumentException("URI has a fragment component");
if (uri.getQuery() != null)
throw new IllegalArgumentException("URI has a query component");
String p = uri.getPath();
if (p.equals(""))
throw new IllegalArgumentException("URI path component is empty");
// Okay, now initialize
p = fs.fromURIPath(p);
if (File.separatorChar != '/')
p = p.replace('/', File.separatorChar);
this.path = fs.normalize(p);
this.prefixLength = fs.prefixLength(this.path);
}
/**
* Creates a new <tt>File</tt> instance by converting the given
* <tt>file:</tt> URI into an abstract pathname.
*
* <p> The exact form of a <tt>file:</tt> URI is system-dependent, hence
* the transformation performed by this constructor is also
* system-dependent.
*
* <p> For a given abstract pathname <i>f</i> it is guaranteed that
*
* <blockquote><tt>
* new File(</tt><i> f</i><tt>.{@link #toURI() toURI}()).equals(</tt><i> f</i><tt>.{@link #getAbsoluteFile() getAbsoluteFile}())
* </tt></blockquote>
*
* so long as the original abstract pathname, the URI, and the new abstract
* pathname are all created in (possibly different invocations of) the same
* Java virtual machine. This relationship typically does not hold,
* however, when a <tt>file:</tt> URI that is created in a virtual machine
* on one operating system is converted into an abstract pathname in a
* virtual machine on a different operating system.
*
* @param uri
* An absolute, hierarchical URI with a scheme equal to
* <tt>"file"</tt>, a non-empty path component, and undefined
* authority, query, and fragment components
*
* @throws NullPointerException
* If <tt>uri</tt> is <tt>null</tt>
*
* @throws IllegalArgumentException
* If the preconditions on the parameter do not hold
*
* @see #toURI()
* @see java.net.URI
* @since 1.4
*/
public File(URI uri) {
// Check our many preconditions
if (!uri.isAbsolute())
throw new IllegalArgumentException("URI is not absolute");
if (uri.isOpaque())
throw new IllegalArgumentException("URI is not hierarchical");
String scheme = uri.getScheme();
if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
throw new IllegalArgumentException("URI scheme is not \"file\"");
if (uri.getAuthority() != null)
throw new IllegalArgumentException("URI has an authority component");
if (uri.getFragment() != null)
throw new IllegalArgumentException("URI has a fragment component");
if (uri.getQuery() != null)
throw new IllegalArgumentException("URI has a query component");
String p = uri.getPath();
if (p.equals(""))
throw new IllegalArgumentException("URI path component is empty");
// Okay, now initialize
p = fs.fromURIPath(p);
if (File.separatorChar != '/')
p = p.replace('/', File.separatorChar);
this.path = fs.normalize(p);
this.prefixLength = fs.prefixLength(this.path);
}
/**
* Creates a new <tt>File</tt> instance by converting the given
* <tt>file:</tt> URI into an abstract pathname.
*
* <p> The exact form of a <tt>file:</tt> URI is system-dependent, hence
* the transformation performed by this constructor is also
* system-dependent.
*
* <p> For a given abstract pathname <i>f</i> it is guaranteed that
*
* <blockquote><tt>
* new File(</tt><i> f</i><tt>.{@link #toURI() toURI}()).equals(</tt><i> f</i><tt>.{@link #getAbsoluteFile() getAbsoluteFile}())
* </tt></blockquote>
*
* so long as the original abstract pathname, the URI, and the new abstract
* pathname are all created in (possibly different invocations of) the same
* Java virtual machine. This relationship typically does not hold,
* however, when a <tt>file:</tt> URI that is created in a virtual machine
* on one operating system is converted into an abstract pathname in a
* virtual machine on a different operating system.
*
* @param uri
* An absolute, hierarchical URI with a scheme equal to
* <tt>"file"</tt>, a non-empty path component, and undefined
* authority, query, and fragment components
*
* @throws NullPointerException
* If <tt>uri</tt> is <tt>null</tt>
*
* @throws IllegalArgumentException
* If the preconditions on the parameter do not hold
*
* @see #toURI()
* @see java.net.URI
* @since 1.4
*/
public File(URI uri) {
// Check our many preconditions
if (!uri.isAbsolute())
throw new IllegalArgumentException("URI is not absolute");
if (uri.isOpaque())
throw new IllegalArgumentException("URI is not hierarchical");
String scheme = uri.getScheme();
if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
throw new IllegalArgumentException("URI scheme is not \"file\"");
if (uri.getAuthority() != null)
throw new IllegalArgumentException("URI has an authority component");
if (uri.getFragment() != null)
throw new IllegalArgumentException("URI has a fragment component");
if (uri.getQuery() != null)
throw new IllegalArgumentException("URI has a query component");
String p = uri.getPath();
if (p.equals(""))
throw new IllegalArgumentException("URI path component is empty");
// Okay, now initialize
p = fs.fromURIPath(p);
if (File.separatorChar != '/')
p = p.replace('/', File.separatorChar);
this.path = fs.normalize(p);
this.prefixLength = fs.prefixLength(this.path);
}