Java Code Examples for java.net.URI#getFragment()

The following examples show how to use java.net.URI#getFragment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractEdgeGridRequestSigner.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 7 votes vote down vote up
private URI withNewHost(URI uri, String host) {
    // We allow host to contain port only for because mocking OPEN API service requires it
    String[] hostAndPort = host.split(":");
    String hostName = hostAndPort[0];
    int port = (hostAndPort.length == 2)
        ? Integer.parseInt(hostAndPort[1])
        : uri.getPort();

    try {
        return new URI(
            uri.getScheme(),
            uri.getUserInfo(),
            hostName,
            port,
            uri.getPath(),
            uri.getQuery(),
            uri.getFragment());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: ProviderUtils.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a nested URI to decode the underlying path. The translation takes
 * the authority and parses it into the underlying scheme and authority.
 * For example, "myscheme://hdfs@nn/my/path" is converted to
 * "hdfs://nn/my/path".
 * @param nestedUri the URI from the nested URI
 * @return the unnested path
 */
public static Path unnestUri(URI nestedUri) {
  String[] parts = nestedUri.getAuthority().split("@", 2);
  StringBuilder result = new StringBuilder(parts[0]);
  result.append("://");
  if (parts.length == 2) {
    result.append(parts[1]);
  }
  result.append(nestedUri.getPath());
  if (nestedUri.getQuery() != null) {
    result.append("?");
    result.append(nestedUri.getQuery());
  }
  if (nestedUri.getFragment() != null) {
    result.append("#");
    result.append(nestedUri.getFragment());
  }
  return new Path(result.toString());
}
 
Example 3
Source File: UriToFile.java    From caja with Apache License 2.0 5 votes vote down vote up
private File toFileUnderSameDirectory(URI uri) {
  if (uri.isOpaque()) {
    // An opaque URI is not interpretable as a relative path
    return null;
  }

  if (uri.getScheme() != null && !"file".equals(uri.getScheme())) {
    // Not a "file://..." URL so cannot be relative to a directory
    return null;
  }

  if (uri.getAuthority() != null
      || uri.getFragment() != null
      || uri.getQuery() != null) {
    // URI contains stuff that does not apply to filesystems
    return null;
  }

  if (uri.getPath() == null) {
    // Cannot resolve as a file without a path
    return null;
  }

  File f = new File(new File(directory, ".").toURI().resolve(uri));
  // Check that f is a descendant of directory
  for (File tmp = f; tmp != null; tmp = tmp.getParentFile()) {
    if (directory.equals(tmp)) {
      return f;
    }
  }

  return null;
}
 
Example 4
Source File: URIUtils.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static URI trimToLastSlash(URI uri) {
	String path = uri.getPath();
	if ((path == null) || path.indexOf(PATH_SEPARATOR) < 0) {
		return null;
	}
	path = path.substring(0, path.lastIndexOf(PATH_SEPARATOR) + 1);
	try {
		return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), path, uri.getQuery(), uri.getFragment());
	} catch (URISyntaxException e) {
		return null;
	}
}
 
Example 5
Source File: ClickHouseConnectionImpl.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public void setCatalog(String catalog) throws SQLException {
    properties.setDatabase(catalog);
    URI old = URI.create(url.substring(ClickhouseJdbcUrlParser.JDBC_PREFIX.length()));
    try {
        url = ClickhouseJdbcUrlParser.JDBC_PREFIX +
                new URI(old.getScheme(), old.getUserInfo(), old.getHost(), old.getPort(),
                        "/" + catalog, old.getQuery(), old.getFragment());
    } catch (URISyntaxException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 6
Source File: SFTPFileSystemProvider.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
private URI normalizeWithUsername(URI uri, String username) {
    if (username == null && uri.getUserInfo() == null && uri.getPath() == null && uri.getQuery() == null && uri.getFragment() == null) {
        // nothing to normalize or add, return the URI
        return uri;
    }
    // no path, query or fragment
    return URISupport.create(uri.getScheme(), username, uri.getHost(), uri.getPort(), null, null, null);
}
 
Example 7
Source File: URIs.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean isNonLocalFragment(String uriString) {
    URI uri = makeURISafely(uriString);
    if (uri != null) {
        String s = uri.getScheme();
        String ssp = uri.getSchemeSpecificPart();
        if (((s == null) || (s.length() ==0)) && ((ssp == null) || (ssp.length() ==0)))
            return false;
        return uri.getFragment() != null;
    } else
        return false;
}
 
Example 8
Source File: StratosServerExtension.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
private int startActiveMQServer() throws AutomationFrameworkException {
    try {
        String activemqBindAddress = getParameters().get(Util.ACTIVEMQ_BIND_ADDRESS);
        if (activemqBindAddress == null) {
            throw new AutomationFrameworkException("ActiveMQ bind address not found in automation.xml");
        }
        URI givenURI = new URI(activemqBindAddress);
        int initAMQPort = givenURI.getPort();
        // dynamically pick an open port starting from initial port given in automation.xml
        while (!Util.isPortAvailable(initAMQPort)) {
            initAMQPort++;
        }
        URI dynamicURL = new URI(givenURI.getScheme(), givenURI.getUserInfo(), givenURI.getHost(), initAMQPort,
                givenURI.getPath(), givenURI.getQuery(), givenURI.getFragment());
        long time1 = System.currentTimeMillis();
        log.info("Starting ActiveMQ with dynamic bind address: " + dynamicURL.toString());
        broker.setDataDirectory(StratosServerExtension.class.getResource(File.separator).getPath() +
                File.separator + ".." + File.separator + "activemq-data");
        broker.setBrokerName("testBroker");
        broker.addConnector(dynamicURL.toString());
        broker.start();
        long time2 = System.currentTimeMillis();
        log.info(String.format("ActiveMQ started in %d sec", (time2 - time1) / 1000));
        return initAMQPort;
    }
    catch (Exception e) {
        throw new AutomationFrameworkException("Could not start ActiveMQ", e);
    }
}
 
Example 9
Source File: BaseToCloneConverter.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public static URI getCloneLocation(URI base, BaseToCloneConverter converter) throws URISyntaxException, CoreException {
	IPath filePath = converter.getFilePath(base);
	IPath clonePath = findClonePath(filePath);
	if (clonePath == null)
		return null;
	IPath p = new Path(GitServlet.GIT_URI).append(Clone.RESOURCE).append(clonePath).addTrailingSeparator();
	return new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), p.toString(), base.getQuery(), base.getFragment());
}
 
Example 10
Source File: UnixFileSystemProvider.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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 11
Source File: WindowsFileSystemProvider.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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 12
Source File: TCPTransportConfigurationSchema.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public static List<TransportConfiguration> getTransportConfigurations(URI uri,
                                                                      Map<String, String> query,
                                                                      Set<String> allowableProperties,
                                                                      String name,
                                                                      String factoryName) throws URISyntaxException {
   HashMap<String, Object> props = new HashMap<>();

   Map<String, Object> extraProps = new HashMap<>();
   BeanSupport.setData(uri, props, allowableProperties, query, extraProps);
   List<TransportConfiguration> transportConfigurations = new ArrayList<>();

   TransportConfiguration config = new TransportConfiguration(factoryName, props, name, extraProps);

   transportConfigurations.add(config);
   String connectors = uri.getFragment();

   if (connectors != null && !connectors.trim().isEmpty()) {
      String[] split = connectors.split(",");
      for (String s : split) {
         URI extraUri = new URI(s);
         HashMap<String, Object> newProps = new HashMap<>();
         extraProps = new HashMap<>();
         BeanSupport.setData(extraUri, newProps, allowableProperties, query, extraProps);
         BeanSupport.setData(extraUri, newProps, allowableProperties, parseQuery(extraUri.getQuery(), null), extraProps);
         transportConfigurations.add(new TransportConfiguration(factoryName, newProps, name + ":" + extraUri.toString(), extraProps));
      }
   }
   return transportConfigurations;
}
 
Example 13
Source File: MRApps.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private static void parseDistributedCacheArtifacts(
    Configuration conf,
    Map<String, LocalResource> localResources,
    LocalResourceType type,
    URI[] uris, long[] timestamps, long[] sizes, boolean visibilities[])
throws IOException {

  if (uris != null) {
    // Sanity check
    if ((uris.length != timestamps.length) || (uris.length != sizes.length) ||
        (uris.length != visibilities.length)) {
      throw new IllegalArgumentException("Invalid specification for " +
          "distributed-cache artifacts of type " + type + " :" +
          " #uris=" + uris.length +
          " #timestamps=" + timestamps.length +
          " #visibilities=" + visibilities.length
          );
    }
    
    for (int i = 0; i < uris.length; ++i) {
      URI u = uris[i];
      Path p = new Path(u);
      FileSystem remoteFS = p.getFileSystem(conf);
      p = remoteFS.resolvePath(p.makeQualified(remoteFS.getUri(),
          remoteFS.getWorkingDirectory()));
      // Add URI fragment or just the filename
      Path name = new Path((null == u.getFragment())
        ? p.getName()
        : u.getFragment());
      if (name.isAbsolute()) {
        throw new IllegalArgumentException("Resource name must be relative");
      }
      String linkName = name.toUri().getPath();
      LocalResource orig = localResources.get(linkName);
      org.apache.hadoop.yarn.api.records.URL url = 
        ConverterUtils.getYarnUrlFromURI(p.toUri());
      if(orig != null && !orig.getResource().equals(url)) {
        LOG.warn(
            getResourceDescription(orig.getType()) + 
            toString(orig.getResource()) + " conflicts with " + 
            getResourceDescription(type) + toString(url) + 
            " This will be an error in Hadoop 2.0");
        continue;
      }
      localResources.put(linkName, LocalResource.newInstance(ConverterUtils
        .getYarnUrlFromURI(p.toUri()), type, visibilities[i]
          ? LocalResourceVisibility.PUBLIC : LocalResourceVisibility.PRIVATE,
        sizes[i], timestamps[i]));
    }
  }
}
 
Example 14
Source File: BaseToRemoteConverter.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public URI baseToRemoteLocation(URI base, String remote, String branch) throws URISyntaxException {
	IPath p = new Path(base.getPath());
	p = p.uptoSegment(1).append(Remote.RESOURCE).append(remote).append(branch).addTrailingSeparator().append(p.removeFirstSegments(2));
	return new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), p.toString(), base.getQuery(), base.getFragment());
}
 
Example 15
Source File: WindowsUriSupport.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 16
Source File: DefaultExecutableFactory.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
private Executable lookupByUri ( final String name, final Execute execute, final RunnerContext ctx )
{
    logger.debug ( "Looking up by uri - name: {}, execute: {}", name, execute );

    try
    {
        final URI uri = new URI ( name );
        if ( !"bundle-class".equals ( uri.getScheme () ) )
        {
            logger.debug ( "Wrong URI scheme: {}", uri.getScheme () );
            return null;
        }

        final String host = uri.getHost ();

        String clazzName = uri.getPath ();
        if ( clazzName.startsWith ( "/" ) )
        {
            // cut of first slash
            clazzName = clazzName.substring ( 1 );
        }

        if ( clazzName.startsWith ( "." ) )
        {
            clazzName = host + clazzName;
        }

        final Class<?> clazz = findBundle ( host, clazzName );

        if ( Executable.class.isAssignableFrom ( clazz ) )
        {
            logger.debug ( "Return by Executable interface" );
            if ( clazz.isAnnotationPresent ( Singleton.class ) )
            {
                if ( !ctx.getSingletons ().containsKey ( clazz ) )
                {
                    ctx.getSingletons ().put ( clazz, clazz.newInstance () );
                }
                return (Executable)ctx.getSingletons ().get ( clazz );
            }
            else
            {
                return (Executable)clazz.newInstance ();
            }
        }
        else
        {
            String fragment = uri.getFragment ();
            if ( fragment == null )
            {
                fragment = "execute";
            }

            logger.debug ( "Return by call wrapper: #{}", fragment );
            return createCallWrapper ( name, clazz, fragment, execute, ctx );
        }
    }
    catch ( final Exception e )
    {
        logger.info ( "Failed to lookup", e );
        return null;
    }
}
 
Example 17
Source File: FileCache.java    From Cubert with Apache License 2.0 4 votes vote down vote up
public static String get(String path)
{
    // first try to load the file using the symbolic link
    // (note that symbolic link does not work with hadoop 1 in local mode)
    try
    {
        URI uri = new URI(path);
        String fragment = uri.getFragment();
        if (fragment != null)
        {
            File file = new File(fragment);
            if (file.exists())
                return file.toString();
        }

        // remove the fragment
        path = uri.getPath();
    }
    catch (URISyntaxException e)
    {
        // do nothing... fall through to the remaining code
    }

    // otherwise, try to load from full path

    if (cachedFiles == null)
        return null;

    for (Path cachedFile : cachedFiles)
    {
        if (cachedFile.toString().endsWith(path))
        {
            return cachedFile.toString();
        }
        if (cachedFile.getParent().toString().endsWith(path))
        {
            return cachedFile.toString();
        }
    }

    return null;
}
 
Example 18
Source File: BaseToRemoteConverter.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public URI baseToRemoteLocation(URI base, String remote, String branch) throws URISyntaxException {
	IPath p = new Path(base.getPath());
	p = new Path(GitServlet.GIT_URI).append(Remote.RESOURCE).append(remote).append(branch).addTrailingSeparator().append(p);
	return new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), p.toString(), base.getQuery(), base.getFragment());
}
 
Example 19
Source File: JobDirectoryServerServiceImpl.java    From genie with Apache License 2.0 4 votes vote down vote up
private void handleRequest(
    final URI baseUri,
    final String relativePath,
    final HttpServletRequest request,
    final HttpServletResponse response,
    final DirectoryManifest manifest,
    final URI jobDirectoryRoot
) throws IOException, GenieNotFoundException, GenieServerException {
    log.debug(
        "Handle request, baseUri: '{}', relpath: '{}', jobRootUri: '{}'",
        baseUri,
        relativePath,
        jobDirectoryRoot
    );
    final DirectoryManifest.ManifestEntry entry = manifest.getEntry(relativePath).orElseThrow(
        () -> new GenieNotFoundException("No such entry in job manifest: " + relativePath)
    );

    if (entry.isDirectory()) {
        // For now maintain the V3 structure
        // TODO: Once we determine what we want for V4 use v3/v4 flags or some way to differentiate
        // TODO: there's no unit test covering this section
        final DefaultDirectoryWriter.Directory directory = new DefaultDirectoryWriter.Directory();
        final List<DefaultDirectoryWriter.Entry> files = Lists.newArrayList();
        final List<DefaultDirectoryWriter.Entry> directories = Lists.newArrayList();
        try {
            entry.getParent().ifPresent(
                parentPath -> {
                    final DirectoryManifest.ManifestEntry parentEntry = manifest
                        .getEntry(parentPath)
                        .orElseThrow(IllegalArgumentException::new);
                    directory.setParent(createEntry(parentEntry, baseUri));
                }
            );

            for (final String childPath : entry.getChildren()) {
                final DirectoryManifest.ManifestEntry childEntry = manifest
                    .getEntry(childPath)
                    .orElseThrow(IllegalArgumentException::new);

                if (childEntry.isDirectory()) {
                    directories.add(this.createEntry(childEntry, baseUri));
                } else {
                    files.add(this.createEntry(childEntry, baseUri));
                }
            }
        } catch (final IllegalArgumentException iae) {
            throw new GenieServerException("Error while traversing files manifest: " + iae.getMessage(), iae);
        }

        directories.sort(Comparator.comparing(DefaultDirectoryWriter.Entry::getName));
        files.sort(Comparator.comparing(DefaultDirectoryWriter.Entry::getName));

        directory.setDirectories(directories);
        directory.setFiles(files);

        final String accept = request.getHeader(HttpHeaders.ACCEPT);
        if (accept != null && accept.contains(MediaType.TEXT_HTML_VALUE)) {
            response.setContentType(MediaType.TEXT_HTML_VALUE);
            response
                .getOutputStream()
                .write(
                    DefaultDirectoryWriter
                        .directoryToHTML(entry.getName(), directory)
                        .getBytes(StandardCharsets.UTF_8)
                );
        } else {
            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
            GenieObjectMapper.getMapper().writeValue(response.getOutputStream(), directory);
        }
    } else {
        final URI location = jobDirectoryRoot.resolve(entry.getPath());
        final String locationString = location.toString()
            + (jobDirectoryRoot.getFragment() != null ? ("#" + jobDirectoryRoot.getFragment()) : "");
        log.debug("Get resource: {}", locationString);
        final Resource jobResource = this.resourceLoader.getResource(locationString);
        // Every file really should have a media type but if not use text/plain
        final String mediaType = entry.getMimeType().orElse(MediaType.TEXT_PLAIN_VALUE);
        final ResourceHttpRequestHandler handler = this.genieResourceHandlerFactory.get(mediaType, jobResource);
        try {
            handler.handleRequest(request, response);
        } catch (ServletException e) {
            throw new GenieServerException("Servlet exception: " + e.getMessage(), e);
        }
    }
}
 
Example 20
Source File: SimpleLocalIvyConfigStoreFactory.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
/**
 * Base URI for a config store should be root of the zip file, so change path part of URI to be null
 */
URI getBaseURI(URI configKey) throws URISyntaxException {
  return new URI(configKey.getScheme(), configKey.getAuthority(), "/", configKey.getQuery(), configKey.getFragment());
}