下面列出了java.net.URI#getAuthority ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void parseURL(URL location, String spec, int start, int limit) {
_scheme = spec.substring(0, spec.indexOf("://"));
// start needs to be adjusted for schemes that include a ':' such as
// java:wse, etc.
// int index = spec.indexOf(":/");
// start = index + 1;
URI specURI = _getSpecURI(spec);
String host = specURI.getHost();
int port = specURI.getPort();
String authority = specURI.getAuthority();
String userInfo = specURI.getUserInfo();
String path = specURI.getPath();
String query = specURI.getQuery();
setURL(location, _scheme, host, port, authority, userInfo, path, query, null);
// super.parseURL(location, spec, start, limit);
}
/**
* Puts in the authority of the default file system if it is a WASB file
* system and the given URI's authority is null.
*
* @return The URI with reconstructed authority if necessary and possible.
*/
private static URI reconstructAuthorityIfNeeded(URI uri, Configuration conf) {
if (null == uri.getAuthority()) {
// If WASB is the default file system, get the authority from there
URI defaultUri = FileSystem.getDefaultUri(conf);
if (defaultUri != null && isWasbScheme(defaultUri.getScheme())) {
try {
// Reconstruct the URI with the authority from the default URI.
return new URI(uri.getScheme(), defaultUri.getAuthority(),
uri.getPath(), uri.getQuery(), uri.getFragment());
} catch (URISyntaxException e) {
// This should never happen.
throw new Error("Bad URI construction", e);
}
}
}
return uri;
}
public boolean isKeyForDirectorySet(String key, Set<String> dirSet) {
String defaultFS = FileSystem.getDefaultUri(sessionConfiguration).toString();
for (String dir : dirSet) {
if (dir.isEmpty() ||
key.startsWith(dir + "/")) {
return true;
}
// Allow for blob directories with paths relative to the default file
// system.
//
try {
URI uriPageBlobDir = new URI (dir);
if (null == uriPageBlobDir.getAuthority()) {
// Concatenate the default file system prefix with the relative
// page blob directory path.
//
if (key.startsWith(trim(defaultFS, "/") + "/" + dir + "/")){
return true;
}
}
} catch (URISyntaxException e) {
LOG.info(String.format(
"URI syntax error creating URI for %s", dir));
}
}
return false;
}
public static URI replacePath(URI uri, String path) {
try {
return new URI(uri.getScheme(), uri.getAuthority(), path, uri.getQuery(), uri.getFragment());
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid URI/Scheme: replacing path with '"+path+"' for "+uri);
}
}
private static boolean statementTargetMatches(URI frameUrl, URI assetUrl) {
if (assetUrl.getScheme() == null || assetUrl.getAuthority() == null) {
return false;
}
return assetUrl.getScheme().equals(frameUrl.getScheme())
&& assetUrl.getAuthority().equals(frameUrl.getAuthority());
}
/**
* Get the URI for the file system based on the given URI. The path, query
* part of the given URI is stripped out and default file system port is used
* to form the URI.
*
* @param uri FileSystem URI.
* @param authorityNeeded if true authority cannot be null in the URI. If
* false authority must be null.
* @param defaultPort default port to use if port is not specified in the URI.
*
* @return URI of the file system
*
* @throws URISyntaxException <code>uri</code> has syntax error
*/
private URI getUri(URI uri, String supportedScheme,
boolean authorityNeeded, int defaultPort) throws URISyntaxException {
checkScheme(uri, supportedScheme);
// A file system implementation that requires authority must always
// specify default port
if (defaultPort < 0 && authorityNeeded) {
throw new HadoopIllegalArgumentException(
"FileSystem implementation error - default port " + defaultPort
+ " is not valid");
}
String authority = uri.getAuthority();
if (authority == null) {
if (authorityNeeded) {
throw new HadoopIllegalArgumentException("Uri without authority: " + uri);
} else {
return new URI(supportedScheme + ":///");
}
}
// authority is non null - AuthorityNeeded may be true or false.
int port = uri.getPort();
port = (port == -1 ? defaultPort : port);
if (port == -1) { // no port supplied and default port is not specified
return new URI(supportedScheme, authority, "/", null);
}
return new URI(supportedScheme + "://" + uri.getHost() + ":" + port);
}
/**
* Construct a KafkaDestination by parsing URI to extract the relevant fields
* @param uri Kafka destination URI
* @return KafkaDestination created by parsing the URI string
*/
public static KafkaDestination parse(String uri) {
Validate.isTrue(uri.startsWith(SCHEME_KAFKA) || uri.startsWith(SCHEME_SECURE_KAFKA),
"Invalid scheme in URI: " + uri);
try {
// Decode URI in case it's escaped
uri = URIUtil.decode(uri);
} catch (Exception e) {
throw new DatastreamRuntimeException("Failed to decode Kafka destination URI: " + uri, e);
}
URI u = URI.create(uri);
String scheme = u.getScheme();
String zkAddress = u.getAuthority();
String path = u.getPath();
String topicName;
int lastSlash = path.lastIndexOf("/");
if (lastSlash > 0) {
// intermediate paths are part of ZK address
zkAddress += path.substring(0, lastSlash);
topicName = path.substring(lastSlash + 1);
} else {
topicName = path.substring(1);
}
for (String hostInfo : zkAddress.split(",")) {
long portNum = URI.create(SCHEME_KAFKA + "://" + hostInfo).getPort();
Validate.isTrue(portNum != -1, "Missing port number in URI: " + uri);
}
Validate.notBlank(zkAddress, "Missing zkAddress in URI: " + uri);
Validate.notBlank(topicName, "Missing topic name in URI: " + uri);
boolean isSecure = scheme.equals(SCHEME_SECURE_KAFKA);
return new KafkaDestination(zkAddress, topicName, isSecure);
}
public ServiceConnector(URI uri) {
String scheme = uri.getScheme();
String authority = uri.getAuthority();
serverHostPort = scheme + "://" + authority;
internalServerHostPort = "http://" + authority;
services = parseServicesJson();
}
private void checkUri(URI uri) {
if (!uri.getScheme().equalsIgnoreCase(getScheme()))
throw new IllegalArgumentException("URI does not match this provider");
if (uri.getAuthority() != null)
throw new IllegalArgumentException("Authority component present");
if (uri.getPath() == null)
throw new IllegalArgumentException("Path component is undefined");
if (!uri.getPath().equals("/"))
throw new IllegalArgumentException("Path component should be '/'");
if (uri.getQuery() != null)
throw new IllegalArgumentException("Query component present");
if (uri.getFragment() != null)
throw new IllegalArgumentException("Fragment component present");
}
/**
* This constructor has the signature needed by
* {@link AbstractFileSystem#createFileSystem(URI, Configuration)}.
*
* @param theUri which must be that of ViewFs
* @param conf
* @throws IOException
* @throws URISyntaxException
*/
ViewFs(final URI theUri, final Configuration conf) throws IOException,
URISyntaxException {
super(theUri, FsConstants.VIEWFS_SCHEME, false, -1);
creationTime = Time.now();
ugi = UserGroupInformation.getCurrentUser();
config = conf;
// Now build client side view (i.e. client side mount table) from config.
String authority = theUri.getAuthority();
fsState = new InodeTree<AbstractFileSystem>(conf, authority) {
@Override
protected
AbstractFileSystem getTargetFileSystem(final URI uri)
throws URISyntaxException, UnsupportedFileSystemException {
String pathString = uri.getPath();
if (pathString.isEmpty()) {
pathString = "/";
}
return new ChRootedFs(
AbstractFileSystem.createFileSystem(uri, config),
new Path(pathString));
}
@Override
protected
AbstractFileSystem getTargetFileSystem(
final INodeDir<AbstractFileSystem> dir) throws URISyntaxException {
return new InternalDirOfViewFs(dir, creationTime, ugi, getUri());
}
@Override
protected
AbstractFileSystem getTargetFileSystem(URI[] mergeFsURIList)
throws URISyntaxException, UnsupportedFileSystemException {
throw new UnsupportedFileSystemException("mergefs not implemented yet");
// return MergeFs.createMergeFs(mergeFsURIList, config);
}
};
}
private static String anonymize(StatePool statePool, Configuration conf,
FileNameState fState, String fileName) {
String ret = null;
try {
URI uri = new URI(fileName);
// anonymize the path i.e without the authority & scheme
ret =
anonymizePath(uri.getPath(), fState.getDirectoryState(),
fState.getFileNameState());
// anonymize the authority and scheme
String authority = uri.getAuthority();
String scheme = uri.getScheme();
if (scheme != null) {
String anonymizedAuthority = "";
if (authority != null) {
// anonymize the authority
NodeName hostName = new NodeName(null, uri.getHost());
anonymizedAuthority = hostName.getAnonymizedValue(statePool, conf);
}
ret = scheme + "://" + anonymizedAuthority + ret;
}
} catch (URISyntaxException use) {
throw new RuntimeException (use);
}
return ret;
}
/**
* Set the namenode address that will be used by clients to access this
* namenode or name service. This needs to be called before the config
* is overriden.
*/
public void setClientNamenodeAddress(Configuration conf) {
String nnAddr = conf.get(FS_DEFAULT_NAME_KEY);
if (nnAddr == null) {
// default fs is not set.
clientNamenodeAddress = null;
return;
}
LOG.info("{} is {}", FS_DEFAULT_NAME_KEY, nnAddr);
URI nnUri = URI.create(nnAddr);
String nnHost = nnUri.getHost();
if (nnHost == null) {
clientNamenodeAddress = null;
return;
}
if (DFSUtil.getNameServiceIds(conf).contains(nnHost)) {
// host name is logical
clientNamenodeAddress = nnHost;
} else if (nnUri.getPort() > 0) {
// physical address with a valid port
clientNamenodeAddress = nnUri.getAuthority();
} else {
// the port is missing or 0. Figure out real bind address later.
clientNamenodeAddress = null;
return;
}
LOG.info("Clients are to use {} to access"
+ " this namenode/service.", clientNamenodeAddress );
}
/**
* Synthesizes a Hadoop path for the given GCS path by casting straight into the scheme indicated
* by the ghfsFileSystemDescriptor instance; if the URI contains an 'authority', the authority is
* re-interpreted as the topmost path component of a URI sitting inside the fileSystemRoot
* indicated by the ghfsFileSystemDescriptor.
*
* <p>Examples:
*
* <ul>
* <li>{@code gs:/// -> gsg:/}
* <li>{@code gs://foo/bar -> gs://root-bucket/foo/bar}
* <li>{@code gs://foo/bar -> hdfs:/foo/bar}
* </ul>
*
* <p>Note that it cannot be generally assumed that GCS-based filesystems will "invert" this path
* back into the same GCS path internally; for example, if a bucket-rooted filesystem is based in
* 'my-system-bucket', then this method will convert:
*
* <p>{@code gs://foo/bar -> gs:/foo/bar}
*
* <p>which will then be converted internally:
*
* <p>{@code gs:/foo/bar -> gs://my-system-bucket/foo/bar}
*
* <p>when the bucket-rooted FileSystem creates actual data in the underlying GcsFs.
*/
protected Path castAsHadoopPath(URI gcsPath) {
String childPath = gcsPath.getRawPath();
if (childPath != null && childPath.startsWith("/")) {
childPath = childPath.substring(1);
}
String authority = gcsPath.getAuthority();
if (Strings.isNullOrEmpty(authority)) {
if (Strings.isNullOrEmpty(childPath)) {
return ghfsFileSystemDescriptor.getFileSystemRoot();
} else {
return new Path(ghfsFileSystemDescriptor.getFileSystemRoot(), childPath);
}
} else {
if (Strings.isNullOrEmpty(childPath)) {
return new Path(ghfsFileSystemDescriptor.getFileSystemRoot(), authority);
} else {
return new Path(ghfsFileSystemDescriptor.getFileSystemRoot(), new Path(
authority, childPath));
}
}
}
/**
* 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, ",");
}
/**
* 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);
}
private static String joinURI(String baseURI, String relativeURI) throws URISyntaxException {
String bscheme = null;
String bauthority = null;
String bpath = "";
String bquery = null;
// pre-parse the baseURI
if (baseURI != null) {
if (baseURI.endsWith("..")) {
baseURI = baseURI + "/";
}
URI base = new URI(baseURI);
bscheme = base.getScheme();
bauthority = base.getAuthority();
bpath = base.getPath();
bquery = base.getQuery();
}
URI r = new URI(relativeURI);
String rscheme = r.getScheme();
String rauthority = r.getAuthority();
String rpath = r.getPath();
String rquery = r.getQuery();
String tscheme, tauthority, tpath, tquery;
if (rscheme != null && rscheme.equals(bscheme)) {
rscheme = null;
}
if (rscheme != null) {
tscheme = rscheme;
tauthority = rauthority;
tpath = removeDotSegments(rpath);
tquery = rquery;
} else {
if (rauthority != null) {
tauthority = rauthority;
tpath = removeDotSegments(rpath);
tquery = rquery;
} else {
if (rpath.length() == 0) {
tpath = bpath;
if (rquery != null) {
tquery = rquery;
} else {
tquery = bquery;
}
} else {
if (rpath.startsWith("/")) {
tpath = removeDotSegments(rpath);
} else {
if (bauthority != null && bpath.length() == 0) {
tpath = "/" + rpath;
} else {
int last = bpath.lastIndexOf('/');
if (last == -1) {
tpath = rpath;
} else {
tpath = bpath.substring(0, last+1) + rpath;
}
}
tpath = removeDotSegments(tpath);
}
tquery = rquery;
}
tauthority = bauthority;
}
tscheme = bscheme;
}
return new URI(tscheme, tauthority, tpath, tquery, null).toString();
}
private Path constructRenamedPath(Path defaultNewPath, Path currentPath) {
URI currentUri = currentPath.toUri();
return new Path(currentUri.getScheme(), currentUri.getAuthority(),
defaultNewPath.toUri().getPath());
}
static URIRef create(URI ref) {
return new URIRef(ref.getScheme(),ref.getAuthority(),ref.getPath(),ref.getQuery(),ref.getFragment());
}
/**
* Construct the URI for a Config-Store node given a path.
* The implementation will be based on scheme, while the signature of this method will not be subject to
* different implementation.
*
* The implementation will be different since Fs-based config-store simply append dataNode's path in the end,
* while ivy-based config-store will require query to store those information.
*
* @param path The relative path of a node inside Config-Store.
* @param configStoreUri The config store URI.
* @return The URI to inspect a data node represented by path inside Config Store.
* @throws URISyntaxException
*/
private static URI getUriFromPath(Path path, String configStoreUri) throws URISyntaxException {
URI storeUri = new URI(configStoreUri);
return new URI(storeUri.getScheme(), storeUri.getAuthority(),
PathUtils.mergePaths(new Path(storeUri.getPath()), path).toString(), storeUri.getQuery(), storeUri.getFragment());
}
/**
* 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);
}