Java Code Examples for java.net.URI.getAuthority()

The following are Jave code examples for showing how to use getAuthority() of the java.net.URI class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: hadoop-oss   File: NuCypherExtUtilClient.java   View Source Code Vote up 6 votes
/**
 * @return address of file system
 */
public static InetSocketAddress getNNAddress(URI filesystemURI) {
  String authority = filesystemURI.getAuthority();
  if (authority == null) {
    throw new IllegalArgumentException(String.format(
        "Invalid URI for NameNode address (check %s): %s has no authority.",
        FileSystem.FS_DEFAULT_NAME_KEY, filesystemURI.toString()));
  }
  if (!HdfsConstants.HDFS_URI_SCHEME.equalsIgnoreCase(
      filesystemURI.getScheme())) {
    throw new IllegalArgumentException(String.format(
        "Invalid URI for NameNode address (check %s): " +
            "%s is not of scheme '%s'.", FileSystem.FS_DEFAULT_NAME_KEY,
        filesystemURI.toString(), HdfsConstants.HDFS_URI_SCHEME));
  }
  return getNNAddress(authority);
}
 
Example 2
Project: hadoop   File: TestHarFileSystemWithHA.java   View Source Code Vote up 6 votes
/**
 * Test that the HarFileSystem works with underlying HDFS URIs that have no
 * port specified, as is often the case with an HA setup.
 */
@Test
public void testHarUriWithHaUriWithNoPort() throws Exception {
  Configuration conf = new HdfsConfiguration();
  MiniDFSCluster cluster = null;
  try {
    cluster = new MiniDFSCluster.Builder(conf)
        .numDataNodes(1)
        .nnTopology(MiniDFSNNTopology.simpleHATopology())
        .build();
    cluster.transitionToActive(0);
    HATestUtil.setFailoverConfigurations(cluster, conf);
    
    createEmptyHarArchive(HATestUtil.configureFailoverFs(cluster, conf),
        TEST_HAR_PATH);
    
    URI failoverUri = FileSystem.getDefaultUri(conf);
    Path p = new Path("har://hdfs-" + failoverUri.getAuthority() + TEST_HAR_PATH);
    p.getFileSystem(conf);
  } finally {
    cluster.shutdown();
  }
}
 
Example 3
Project: openjdk-jdk10   File: JrtFileSystemProvider.java   View Source Code Vote up 6 votes
@Override
public Path getPath(URI uri) {
    checkPermission();
    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.getQuery() != null) {
        throw new IllegalArgumentException("Query component present");
    }
    if (uri.getFragment() != null) {
        throw new IllegalArgumentException("Fragment component present");
    }
    String path = uri.getPath();
    if (path == null || path.charAt(0) != '/') {
        throw new IllegalArgumentException("Invalid path component");
    }
    return getTheFileSystem().getPath(path);
}
 
Example 4
Project: hadoop   File: ViewFs.java   View Source Code Vote up 5 votes
/**
 * 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);
    }
  };
}
 
Example 5
Project: Selenium-Foundation   File: PageSourceUtils.java   View Source Code Vote up 5 votes
/**
 * Insert "base" node so that style sheets, images, and other relative resources load properly.
 * 
 * @param sourceBuilder {@link StringBuilder} object used to build page source
 * @param driver web driver object
 * @return the [sourceBuilder] object
 */
private static StringBuilder insertBaseElement(StringBuilder sourceBuilder, WebDriver driver) {
    int offset = sourceBuilder.indexOf("<head>") + 6;
    
    // if no head found
    if (offset < 6) {
        return sourceBuilder;
    }
    
    int closing = sourceBuilder.indexOf("</head>", offset);
    String substr = sourceBuilder.substring(offset, closing);
    
    // if base already exists
    if (substr.contains("<base ")) {
        return sourceBuilder;
    }
    
    URI uri = URI.create(driver.getCurrentUrl());
    
    String path = uri.getPath();
    int endIndex = path.lastIndexOf('/') + 1;
    String root = path.substring(0, endIndex);
    
    String authority = uri.getAuthority();
    if (authority != null) {
        root = authority + root;
    }
    
    sourceBuilder.insert(offset, "\">\n")
            .insert(offset, root)
            .insert(offset, "://")
            .insert(offset, uri.getScheme())
            .insert(offset, "<base href=\"")
            .insert(offset, "\n<!-- Inserted by Selenium Foundation -->\n");
    
    return sourceBuilder;
}
 
Example 6
Project: hadoop   File: AbstractFileSystem.java   View Source Code Vote up 5 votes
private static URI getBaseUri(URI uri) {
  String scheme = uri.getScheme();
  String authority = uri.getAuthority();
  String baseUriString = scheme + "://";
  if (authority != null) {
    baseUriString = baseUriString + authority;
  } else {
    baseUriString = baseUriString + "/";
  }
  return URI.create(baseUriString);
}
 
Example 7
Project: jdk8u-jdk   File: WindowsFileSystemProvider.java   View Source Code Vote up 5 votes
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");
}
 
Example 8
Project: xlight_android_native   File: SseURLStreamHandlerImpl.java   View Source Code Vote up 5 votes
@Override
protected void parseURL(URL location, String spec, int start, int limit) {
    _scheme = spec.substring(0, spec.indexOf("://"));

    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);
}
 
Example 9
Project: grpc-java-contrib   File: StaticResolver.java   View Source Code Vote up 5 votes
@Nullable
@Override
public NameResolver newNameResolver(URI targetUri, Attributes params) {
    if (scheme.equals(targetUri.getScheme())) {
        final String authority = targetUri.getAuthority();

        return new NameResolver() {
            @Override
            public String getServiceAuthority() {
                return authority;
            }

            @Override
            public void start(NameResolver.Listener listener) {
                try {
                    listener.onAddresses(
                            Collections.singletonList(new EquivalentAddressGroup(staticAddress)),
                            Attributes.EMPTY
                    );
                } catch (Throwable e) {
                    listener.onError(Status.UNKNOWN);
                }
            }

            @Override
            public void shutdown() {

            }
        };
    } else {
        return null;
    }
}
 
Example 10
Project: hadoop-oss   File: FileSystem.java   View Source Code Vote up 5 votes
Key(URI uri, Configuration conf, long unique) throws IOException {
  scheme = uri.getScheme()==null ?
      "" : StringUtils.toLowerCase(uri.getScheme());
  authority = uri.getAuthority()==null ?
      "" : StringUtils.toLowerCase(uri.getAuthority());
  this.unique = unique;
  
  this.ugi = UserGroupInformation.getCurrentUser();
}
 
Example 11
Project: hadoop-oss   File: PathData.java   View Source Code Vote up 5 votes
/**
 * Expand the given path as a glob pattern.  Non-existent paths do not
 * throw an exception because creation commands like touch and mkdir need
 * to create them.  The "stat" field will be null if the path does not
 * exist.
 * @param pattern the pattern to expand as a glob
 * @param conf the hadoop configuration
 * @return list of {@link PathData} objects.  if the pattern is not a glob,
 * and does not exist, the list will contain a single PathData with a null
 * stat 
 * @throws IOException anything else goes wrong...
 */
public static PathData[] expandAsGlob(String pattern, Configuration conf)
throws IOException {
  Path globPath = new Path(pattern);
  FileSystem fs = globPath.getFileSystem(conf);    
  FileStatus[] stats = fs.globStatus(globPath);
  PathData[] items = null;
  
  if (stats == null) {
    // remove any quoting in the glob pattern
    pattern = pattern.replaceAll("\\\\(.)", "$1");
    // not a glob & file not found, so add the path with a null stat
    items = new PathData[]{ new PathData(fs, pattern, null) };
  } else {
    // figure out what type of glob path was given, will convert globbed
    // paths to match the type to preserve relativity
    PathType globType;
    URI globUri = globPath.toUri();
    if (globUri.getScheme() != null) {
      globType = PathType.HAS_SCHEME;
    } else if (!globUri.getPath().isEmpty() &&
               new Path(globUri.getPath()).isAbsolute()) {
      globType = PathType.SCHEMELESS_ABSOLUTE;
    } else {
      globType = PathType.RELATIVE;
    }

    // convert stats to PathData
    items = new PathData[stats.length];
    int i=0;
    for (FileStatus stat : stats) {
      URI matchUri = stat.getPath().toUri();
      String globMatch = null;
      switch (globType) {
        case HAS_SCHEME: // use as-is, but remove authority if necessary
          if (globUri.getAuthority() == null) {
            matchUri = removeAuthority(matchUri);
          }
          globMatch = uriToString(matchUri, false);
          break;
        case SCHEMELESS_ABSOLUTE: // take just the uri's path
          globMatch = matchUri.getPath();
          break;
        case RELATIVE: // make it relative to the current working dir
          URI cwdUri = fs.getWorkingDirectory().toUri();
          globMatch = relativize(cwdUri, matchUri, stat.isDirectory());
          break;
      }
      items[i++] = new PathData(fs, globMatch, stat);
    }
  }
  Arrays.sort(items);
  return items;
}
 
Example 12
Project: hadoop-oss   File: ViewFs.java   View Source Code Vote up 5 votes
/**
 * 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);
    }
  };
}
 
Example 13
Project: hadoop   File: FileContext.java   View Source Code Vote up 5 votes
/**
 * Are qualSrc and qualDst of the same file system?
 * @param qualPath1 - fully qualified path
 * @param qualPath2 - fully qualified path
 * @return
 */
private static boolean isSameFS(Path qualPath1, Path qualPath2) {
  URI srcUri = qualPath1.toUri();
  URI dstUri = qualPath2.toUri();
  return (srcUri.getScheme().equals(dstUri.getScheme()) && 
      !(srcUri.getAuthority() != null && dstUri.getAuthority() != null && srcUri
      .getAuthority().equals(dstUri.getAuthority())));
}
 
Example 14
Project: truevfs   File: TUriHelper.java   View Source Code Vote up 5 votes
static int pathPrefixLength(final URI uri) {
    final String ssp = uri.getSchemeSpecificPart();
    final String a = uri.getAuthority();
    final int al = null == a ? 0 : 2 + a.length();
    final int pl = Paths.prefixLength(ssp, SEPARATOR_CHAR, true) - al;
    return pl >= 0 ? pl : Paths.prefixLength(uri.getPath(), SEPARATOR_CHAR, false);
}
 
Example 15
Project: hadoop   File: FileSystem.java   View Source Code Vote up 5 votes
Key(URI uri, Configuration conf, long unique) throws IOException {
  scheme = uri.getScheme()==null ?
      "" : StringUtils.toLowerCase(uri.getScheme());
  authority = uri.getAuthority()==null ?
      "" : StringUtils.toLowerCase(uri.getAuthority());
  this.unique = unique;
  
  this.ugi = UserGroupInformation.getCurrentUser();
}
 
Example 16
Project: hadoop-oss   File: FileContext.java   View Source Code Vote up 5 votes
/**
 * Are qualSrc and qualDst of the same file system?
 * @param qualPath1 - fully qualified path
 * @param qualPath2 - fully qualified path
 * @return
 */
private static boolean isSameFS(Path qualPath1, Path qualPath2) {
  URI srcUri = qualPath1.toUri();
  URI dstUri = qualPath2.toUri();
  return (srcUri.getScheme().equals(dstUri.getScheme()) && 
      !(srcUri.getAuthority() != null && dstUri.getAuthority() != null && srcUri
      .getAuthority().equals(dstUri.getAuthority())));
}
 
Example 17
Project: OpenJSharp   File: UnixFileSystemProvider.java   View Source Code Vote up 5 votes
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");
}
 
Example 18
Project: OpenJSharp   File: WindowsUriSupport.java   View Source Code Vote up 4 votes
/**
 * 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);
}
 
Example 19
Project: openjdk-jdk10   File: Canonicalizer11.java   View Source Code Vote up 4 votes
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();
}
 
Example 20
Project: OpenJSharp   File: File.java   View Source Code Vote up 3 votes
/**
 * 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>&nbsp;f</i><tt>.{@link #toURI() toURI}()).equals(</tt><i>&nbsp;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);
}