Example usage for java.net URI getPath

List of usage examples for java.net URI getPath

Introduction

In this page you can find the example usage for java.net URI getPath.

Prototype

public String getPath() 

Source Link

Document

Returns the decoded path component of this URI.

Usage

From source file:org.fcrepo.server.utilities.ServerUtility.java

/**
 * Tell whether the given URL appears to be referring to somewhere within
 * the Fedora webapp.//w w w .  j  a  v  a2  s. c  o  m
 */
public static boolean isURLFedoraServer(String url) {

    // scheme must be http or https
    URI uri = URI.create(url);
    String scheme = uri.getScheme();
    if (!scheme.equals("http") && !scheme.equals("https")) {
        return false;
    }

    // host must be configured hostname or localhost
    String fHost = CONFIG.getParameter(FEDORA_SERVER_HOST, Parameter.class).getValue();
    String host = uri.getHost();
    if (!host.equals(fHost) && !host.equals("localhost")) {
        return false;
    }

    // path must begin with configured webapp context
    String path = uri.getPath();
    String fedoraContext = CONFIG.getParameter(FEDORA_SERVER_CONTEXT, Parameter.class).getValue();
    if (!path.startsWith("/" + fedoraContext + "/")) {
        return false;
    }

    // port specification must match http or https port as appropriate
    String httpPort = CONFIG.getParameter(FEDORA_SERVER_PORT, Parameter.class).getValue();
    String httpsPort = CONFIG.getParameter(FEDORA_REDIRECT_PORT, Parameter.class).getValue();
    if (uri.getPort() == -1) {
        // unspecified, so fedoraPort must be 80 (http), or 443 (https)
        if (scheme.equals("http")) {
            return httpPort.equals("80");
        } else {
            return httpsPort.equals("443");
        }
    } else {
        // specified, so must match appropriate http or https port
        String port = "" + uri.getPort();
        if (scheme.equals("http")) {
            return port.equals(httpPort);
        } else {
            return port.equals(httpsPort);
        }
    }

}

From source file:com.fujitsu.dc.core.model.impl.es.CellEsImpl.java

/**
 * @param uriInfo UriInfo//from w ww .  j  a  v a 2  s  . c o  m
 * @return Cell  ?Cell????????null
 */
public static Cell load(final UriInfo uriInfo) {
    URI reqUri = uriInfo.getRequestUri();
    URI baseUri = uriInfo.getBaseUri();

    String rPath = reqUri.getPath();
    String bPath = baseUri.getPath();
    rPath = rPath.substring(bPath.length());
    String[] paths = StringUtils.split(rPath, "/");

    return findCell("s.Name.untouched", paths[0], uriInfo);
}

From source file:com.sworddance.util.UriFactoryImpl.java

/**
 * TODO need to figure out what parts of WebLocationImpl belong here.
 * @param uri/*from  www  .ja  v a  2 s.  c  om*/
 * @return uri
 */
public static URI getNormalizedUri(URI uri) {
    try {
        String path = uri.getPath();

        // see WebLocationImpl ( this implementation here may not be correct )
        return new URI(uri.getScheme(), uri.getHost(), path == null ? PATH_SEPARATOR : PATH_SEPARATOR + path,
                uri.getQuery());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.ibm.jaggr.core.test.TestUtils.java

private static IResource mockAggregatorNewResource(URI uri, File workDir) throws Throwable {
    final String aggrResPath = "/com.ibm.jaggr.core/"; // path for bundle resource in the aggregator
    String scheme = uri.getScheme();
    if ("file".equals(scheme)) {
        return new FileResource(uri);
    } else if ("namedbundleresource".equals(scheme)) {
        if (uri.getPath().startsWith(aggrResPath)) {
            String path = uri.getPath().substring(aggrResPath.length());
            return new FileResource(IAggregator.class.getClassLoader().getResource(path).toURI());
        }/*from   w  w w .  j  a  v  a  2s  .c o  m*/
        return new FileResource(new File(workDir, uri.getPath()).toURI());
    }
    throw new UnsupportedOperationException();
}

From source file:com.microsoft.tfs.core.util.URIUtils.java

/**
 * <p>//from   w  w w.j  a  va 2 s. co  m
 * Ensures that the specified {@link URI}'s path component ends with a slash
 * character (<code>/</code>).
 * </p>
 *
 * <p>
 * {@link URI}s that will be resolved against should always have a trailing
 * slash in their path component. For more information, see Sun Java bug
 * 4666701 (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4666701).
 * </p>
 *
 * <p>
 * If the specified {@link URI} is opaque, it is returned. If the specified
 * {@link URI} is hierarchical and its path component already ends in a
 * slash, it is returned. Otherwise, a new {@link URI} is returned that is
 * identical to the specified {@link URI} except in its path component,
 * which will have a slash appended on.
 * </p>
 *
 * @param uri
 *        a {@link URI} to check (must not be <code>null</code>)
 * @return a {@link URI} as described above (never <code>null</code>)
 */
public static URI ensurePathHasTrailingSlash(final URI uri) {
    Check.notNull(uri, "uri"); //$NON-NLS-1$

    if (uri.isOpaque()) {
        return uri;
    }

    String path = uri.getPath();
    if (path != null && path.endsWith("/")) //$NON-NLS-1$
    {
        return uri;
    }

    if (path == null) {
        path = "/"; //$NON-NLS-1$
    } else {
        path = path + "/"; //$NON-NLS-1$
    }

    return newURI(uri.getScheme(), uri.getAuthority(), path, uri.getQuery(), uri.getFragment());
}

From source file:com.microsoft.tfs.core.util.URIUtils.java

/**
 * Ensures that the specified {@link URI} has any trailing slashes REMOVED.
 * VisualStudio uses server URIs that lack trailing slashes, this is for
 * compatibility. However, a path of only / will be maintained.
 *
 * @param uri/* ww w.  ja  v  a 2s.c  o m*/
 *        a {@link URI} to check (must not be <code>null</code>)
 * @return a {@link URI} as described above (never <code>null</code>)
 */
public static URI removeTrailingSlash(final URI uri) {
    Check.notNull(uri, "uri"); //$NON-NLS-1$

    if (uri.isOpaque()) {
        return uri;
    }

    String path = uri.getPath();

    if (path == null) {
        path = "/"; //$NON-NLS-1$
    } else if (!path.equals("/")) //$NON-NLS-1$
    {
        while (path.endsWith("/")) //$NON-NLS-1$
        {
            path = path.substring(0, path.length() - 1);
        }
    }

    return newURI(uri.getScheme(), uri.getAuthority(), path, uri.getQuery(), uri.getFragment());
}

From source file:com.microsoft.tfs.core.util.URIUtils.java

/**
 * Returns a new {@link URI} containing only the scheme, user info, host,
 * port, and path of the given {@link URI}.
 *
 * @param uri/*from  w  ww . ja  v  a  2 s. co  m*/
 *        the {@link URI} to remove the query parts from (must not be
 *        <code>null</code>)
 * @return a new {@link URI} containing only the scheme, user info, host,
 *         port, and path of the given {@link URI}
 */
public static URI removeQueryParts(final URI uri) {
    Check.notNull(uri, "uri"); //$NON-NLS-1$

    try {
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null,
                null);
    } catch (final URISyntaxException e) {
        final IllegalArgumentException e2 = new IllegalArgumentException(
                MessageFormat.format(Messages.getString("URIUtils.IllegalURIFormat"), uri)); //$NON-NLS-1$
        e2.initCause(e);
        throw e2;
    }
}

From source file:org.mule.tools.rhinodo.impl.NodeModuleImplBuilder.java

private static NodeModuleImpl extractFromPackageJson(URI root) {
    if (root == null) {
        throw new IllegalArgumentException("Error validating rootDirectory");
    }/*w  w w  .j  a v  a2  s .  c  om*/

    URI packageJson;
    try {
        packageJson = new URI(root.toString() + "/" + "package.json");
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

    boolean exists;
    if ("file".equals(packageJson.getScheme())) {
        exists = new File(packageJson).exists();
    } else {
        throw new IllegalStateException(
                String.format("Error: scheme [%s] not supported.", packageJson.getScheme()));
    }

    if (!exists) {
        throw new IllegalStateException(String.format("Error: package.json not found at [%s].", packageJson));
    }

    return NodeModuleImpl.create(root,
            NodeModuleImplBuilder.<String, String>getPackageJSONMap(new File(packageJson.getPath())));
}

From source file:com.sworddance.util.UriFactoryImpl.java

public static String getFilename(URI uri, String defaultFileName) {
    String fileName = null;//from w w  w . j  a v  a 2s .  com
    if (uri != null) {
        fileName = substringAfterLast(uri.getPath(), PATH_SEPARATOR);
    }
    if (isNotBlank(fileName)) {
        return fileName;
    } else {
        return defaultFileName;
    }
}

From source file:com.sworddance.util.UriFactoryImpl.java

/**
 * Purpose of this method is to process redirect case. A lot of sites are NOT compatible to HTTP RFCs. It means
 * that they may specify redirect in wrong way. For correct way refer to http://www.ietf.org/rfc/rfc2616.txt 14.30.
 *
 * Let's say we received redirect directive from external server after requested http://www.w3.org/Whois page.
 * Below are the variants we may've received and the ways we will treat them:
 * <code>/*from   w  w  w .  j av a  2  s.c  o  m*/
 * <ol>
 * <li> "http://www.w3.org/pub/WWW/People.html" - correct redirect, no questions
 * <li> "/pub/WWW/People.html" - resolve to http://www.w3.org/pub/WWW/People.html
 * <li> "pub/WWW/People.html" - resolve to http://www.w3.org/pub/WWW/People.html
 * <li> "" - resolve to http://www.w3.org/
 * </ol>
 * </code>
 *
 * Please add the cases if you found anything else we may suffer from on redirect matter.
 *
 * One more note. Why this couldn't be done in a regular method {@link #createUriWithSchemaAndPath(Object)} ,
 * why we need separate one. Regular one deals with user input with no URI context built in. Redirect case
 * ALWAYS has context - the page it was redirected from. User meaning of something.page is (try it in browser!)
 * http://something.page. Redirect meaning of the same is most likely http://theHostRedirectCameFrom/something.page.
 *
 * @param redirectTo string saying where we should be redirected
 * @param hostRedirectedFrom host we received redirect from
 * @return resolved URI
 */
public static URI createUriForRedirect(String redirectTo, String hostRedirectedFrom) {
    // empty redirect. Redirect to the main page.
    if (isEmpty(redirectTo)) {
        return createUriWithPath(hostRedirectedFrom);
    }

    URI idealCaseURI = createUriWithPath(redirectTo);
    if (idealCaseURI.getScheme() != null && idealCaseURI.getHost() != null && idealCaseURI.getPath() != null) {
        // that's it. Thanks our remote server for following RFC!
        return idealCaseURI;
    }

    // if we're failed with ideal case - let's try other ways
    notNull(hostRedirectedFrom,
            "Host we came from shouldn't be null because redirectTo doesn't have host information. redirectTo=",
            redirectTo);
    if (!redirectTo.startsWith(PATH_SEPARATOR)) {
        redirectTo = PATH_SEPARATOR + redirectTo;
    }
    return createUriWithSchema(hostRedirectedFrom + redirectTo);
}