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:Main.java

/**
 * Parse a filename from the given URI./*from  w  ww  .  j av a 2 s . c  o  m*/
 *
 * @param uri a uri
 * @return a filename if the uri ends with just a resource, null otherwise
 */
public static final String getFilenameFromURI(URI uri) {
    String path = uri.getPath();
    int lastSlash = path.lastIndexOf('/');

    // Is the last slash at the end?
    if (lastSlash == path.length() - 1) {
        return null;
    }
    return path.substring(lastSlash + 1);
}

From source file:Main.java

public static String urlFromRequest(URI paramURI) {
    return paramURI.getScheme() + "://" + paramURI.getHost() + paramURI.getPath();
}

From source file:com.splunk.shuttl.archiver.util.UtilsURI.java

/**
 * Trim eventual ending {@link File#separator}.<br/>
 * Ex:<br/>/*from   w w  w  . j a v a  2  s .com*/
 * 
 * <pre>
 * "file:/a/b/c" -> "/a/b/c"
 * "file:/a/b/c/" -> "/a/b/c"
 * "file:/a/b/c.txt" -> "/a/b/c.txt"
 * </pre>
 */
public static String getPathByTrimmingEndingFileSeparator(URI uri) {
    String path = uri.getPath();
    if (path.endsWith(File.separator))
        return path.substring(0, path.length() - 1);
    else
        return path;
}

From source file:Main.java

/**
 * Removes query parameters from url (only for logging, as query parameters may contain sensible informations)
 *
 * @param uri//  w w w .ja  v  a 2  s.  com
 * @return URI without parameters
 */
public static String shortenUrl(URI uri) {
    return uri.getScheme() + "://" + uri.getHost() + uri.getPath();
}

From source file:at.bitfire.davdroid.URIUtils.java

public static URI ensureTrailingSlash(URI href) {
    if (!href.getPath().endsWith("/")) {
        try {//from w  w w  .  j av a  2 s.  c om
            URI newURI = new URI(href.getScheme(), href.getAuthority(), href.getPath() + "/", null, null);
            Log.d(TAG, "Appended trailing slash to collection " + href + " -> " + newURI);
            href = newURI;
        } catch (URISyntaxException e) {
        }
    }
    return href;
}

From source file:Main.java

public static String resolvePath(final String path) {
    try {//ww  w. j a  v a2 s. c o  m
        final URI uri = new URI(path);
        return removeExtraSlashes(URLDecoder.decode(uri.getPath(), "UTF-8"));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Removes dot segments according to RFC 3986, section 5.2.4
 *
 * @param uri the original URI/*from www.  j  av a  2 s . c  o m*/
 * @return the URI without dot segments
 */
private static URI removeDotSegments(URI uri) {
    String path = uri.getPath();
    if ((path == null) || (path.indexOf("/.") == -1)) {
        // No dot segments to remove
        return uri;
    }
    String[] inputSegments = path.split("/");
    Stack<String> outputSegments = new Stack<String>();
    for (int i = 0; i < inputSegments.length; i++) {
        if ((inputSegments[i].length() == 0) || (".".equals(inputSegments[i]))) {
            // Do nothing
        } else if ("..".equals(inputSegments[i])) {
            if (!outputSegments.isEmpty()) {
                outputSegments.pop();
            }
        } else {
            outputSegments.push(inputSegments[i]);
        }
    }
    StringBuilder outputBuffer = new StringBuilder();
    for (String outputSegment : outputSegments) {
        outputBuffer.append('/').append(outputSegment);
    }
    try {
        return new URI(uri.getScheme(), uri.getAuthority(), outputBuffer.toString(), uri.getQuery(),
                uri.getFragment());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:URIUtil.java

/**
 * Get the parent URI of the supplied URI
 * @param uri The input URI for which the parent URI is being requrested.
 * @return The parent URI.  Returns a URI instance equivalent to "../" if
 * the supplied URI path has no parent./*w  ww .  ja v a 2s  . c om*/
 * @throws URISyntaxException Failed to reconstruct the parent URI.
 */
public static URI getParent(URI uri) throws URISyntaxException {

    String parentPath = new File(uri.getPath()).getParent();

    if (parentPath == null) {
        return new URI("../");
    }

    return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
            parentPath.replace('\\', '/'), uri.getQuery(), uri.getFragment());
}

From source file:com.intel.podm.rest.odataid.ODataId.java

public static ODataId oDataId(URI uri) {
    URI uriWithoutTrailingSlash = create(removeEnd(uri.getPath(), "/"));
    return new ODataId(uriWithoutTrailingSlash);
}

From source file:com.omnigon.aem.common.utils.url.SlingUrlBuilder.java

public static SlingUrlBuilder create(final String uri) {
    if (StringUtils.isNotBlank(uri)) {
        try {/*w  w  w . j  a v  a2s . c o m*/
            URI u = URLUtils.createURI(uri);
            String path = u.getPath();

            if (u.isAbsolute() || StringUtils.isBlank(path)) {
                return new AbsoluteUrlBuilder(u.toASCIIString());
            } else {
                if (!StringUtils.startsWith(path, "/")) {
                    path = "/" + path;
                }

                return create(new PathInfo(path));
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }

    return new NullUrlBuilder();
}