Example usage for java.net URI getFragment

List of usage examples for java.net URI getFragment

Introduction

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

Prototype

public String getFragment() 

Source Link

Document

Returns the decoded fragment component of this URI.

Usage

From source file:org.eclipse.orion.server.servlets.JsonURIUnqualificationStrategy.java

protected static URI unqualifyURI(URI uri, String scheme, String hostname, int port) {
    URI simpleURI = uri;//from  ww  w.j  a  va 2 s. com
    int uriPort = uri.getPort();
    if (uriPort == -1) {
        uriPort = getDefaultPort(uri.getScheme());
    }
    if (scheme.equals(uri.getScheme()) && hostname.equals(uri.getHost()) && port == uriPort) {
        try {
            simpleURI = new URI(null, null, null, -1, uri.getPath(), uri.getQuery(), uri.getFragment());
        } catch (URISyntaxException e) {
            simpleURI = uri;
        }
    }
    return simpleURI;
}

From source file:org.apache.jena.jdbc.remote.RemoteEndpointDriver.java

/**
 * Strips the last component of the given URI if possible
 * //from   w w  w  .jav a  2  s  . c om
 * @param input
 *            URI
 * @return Reduced URI or null if no further reduction is possible
 */
private static String stripLastComponent(String input) {
    try {
        URI uri = new URI(input);
        if (uri.getFragment() != null) {
            return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(),
                    uri.getQuery(), null).toString();
        } else if (uri.getQuery() != null) {
            return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(),
                    null, null).toString();
        } else if (uri.getPath() != null) {
            // Try and strip off last segment of the path
            String currPath = uri.getPath();
            if (currPath.endsWith("/")) {
                currPath = currPath.substring(0, currPath.length() - 1);
                if (currPath.length() == 0)
                    currPath = null;
                return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), currPath, null,
                        null).toString();
            } else if (currPath.contains("/")) {
                currPath = currPath.substring(0, currPath.lastIndexOf('/') + 1);
                if (currPath.length() == 0)
                    currPath = null;
                return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), currPath, null,
                        null).toString();
            } else {
                // If path is non-null it must always contain a /
                // otherwise it would be an invalid path
                // In this case there are no further components to strip
                return null;
            }
        } else {
            // No further components to strip
            return null;
        }
    } catch (URISyntaxException e) {
        // Error stripping component
        return null;
    }
}

From source file:info.rmapproject.webapp.utils.WebappUtils.java

/**
 * Replace the namespace URL with something more readable.
 *
 * @param url the url//from www.  j a v  a  2 s.  c om
 * @return the shortened term that uses the prefix.
 */
public static String replaceNamespace(String url) {
    try {
        URI uri = new URI(url);
        String path = null;
        String term = null;
        String newUrl = url;

        if (url.contains("#")) {
            term = uri.getFragment();
            path = url.substring(0, url.lastIndexOf("#") + 1);
        } else if (url.contains("/") && path == null) {
            term = url.substring(url.lastIndexOf("/") + 1);
            path = url.substring(0, url.lastIndexOf("/") + 1);
        }

        if (term != null && path != null && term.length() > 0 && path.length() > 0) {
            String prefix = null;
            try {
                prefix = prefixes.getMessage(path, null, Locale.ENGLISH);
            } catch (NoSuchMessageException e) {
                // null prefix handled below
            }
            if (prefix != null && prefix.length() > 0) {
                newUrl = prefix + ":" + term;
            } else {
                newUrl = "x" + ":" + term;
            }
        }
        return newUrl;
    } catch (URISyntaxException e) {
        //it's not a uri... that's OK, send it back...
        return url;
    }
}

From source file:mitm.common.util.URIUtils.java

/**
 * Checks whether the given identifier is a valid URI. If type is null, a FULL check will be done.
 * If identifier is null, false will be returned.
 *///from w w  w.  ja v a 2  s.  co  m
public static boolean isValidURI(String identifier, URIType type) {
    if (identifier == null) {
        return false;
    }

    if (type == null) {
        type = URIType.FULL;
    }

    boolean valid = false;

    try {
        URI uri = new URI(identifier);

        if (type == URIType.BASE) {
            /*
             * Only accepts URI of the form [scheme:][//authority][path]
             */
            if (StringUtils.isNotEmpty(uri.getScheme()) && StringUtils.isNotEmpty(uri.getHost())
                    && StringUtils.isEmpty(uri.getQuery()) && StringUtils.isEmpty(uri.getFragment())) {
                valid = true;
            }
        } else if (type == URIType.RELATIVE) {
            /*
             * Only accepts URI of the form [path][?query][#fragment] 
             */
            if (StringUtils.isEmpty(uri.getScheme()) && StringUtils.isEmpty(uri.getAuthority())
                    && StringUtils.isEmpty(uri.getHost())) {
                valid = true;
            }
        } else if (type == URIType.FULL) {
            /*
             * Only accepts URI of the form [scheme:][//authority][path][?query][#fragment] 
             */
            if (StringUtils.isNotEmpty(uri.getScheme()) && StringUtils.isNotEmpty(uri.getHost())) {
                valid = true;
            }
        } else {
            valid = true;
        }

    } catch (URISyntaxException e) {
        // ignore
    }

    return valid;
}

From source file:utils.Config.java

/**
 * Make a full URI from the given {@code uri} and the configuration.
 *
 * The new url is created by copying the given url and fill scheme and
 * authority if they are empty./*from ww  w .  jav a  2s.  co m*/
 *
 * @param uri
 * @return
 * @throws URISyntaxException
 */
public static URI createFullURI(URI uri) throws URISyntaxException {
    String scheme = uri.getScheme();
    String authority = uri.getAuthority();

    scheme = (scheme != null) ? scheme : getScheme();
    authority = (authority != null) ? authority : getHostport();

    return new URI(scheme, authority, uri.getPath(), uri.getQuery(), uri.getFragment());
}

From source file:org.sakaiproject.mediasite.tool.MediasiteContentLaunch.java

public static URI getRootPath(URI baseUri) throws URISyntaxException {
    URI uri = new URI(baseUri.getScheme(), null, baseUri.getHost(), baseUri.getPort(),
            baseUri.getPath() + WebApiConstants.SONICFOUNDRY_WEB_API_PATH.value(), baseUri.getQuery(),
            baseUri.getFragment());
    return uri;//w  ww .j  a v a2s.  com
}

From source file:URISupport.java

public static CompositeData parseComposite(URI uri) throws URISyntaxException {

    CompositeData rc = new CompositeData();
    rc.scheme = uri.getScheme();/*from w  ww . ja  v  a 2  s.  c om*/
    String ssp = stripPrefix(uri.getSchemeSpecificPart().trim(), "//").trim();

    parseComposite(uri, rc, ssp);

    rc.fragment = uri.getFragment();
    return rc;
}

From source file:com.collective.celos.Util.java

public static String augmentHdfsPath(String hdfsPrefix, String path) throws URISyntaxException {

    if (hdfsPrefix.isEmpty() || hdfsPrefix.equals("/")) {
        return path;
    }/*w w w .  ja  v  a 2s .c  o  m*/

    for (String ch : conversions.keySet()) {
        path = path.replace(ch.toString(), conversions.get(ch).toString());
    }
    URI oldUri = URI.create(path);

    String host = oldUri.getHost();
    if (oldUri.getRawSchemeSpecificPart().startsWith("///") && host == null) {
        host = "";
    }

    URI newUri = new URI(oldUri.getScheme(), oldUri.getUserInfo(), host, oldUri.getPort(),
            hdfsPrefix + oldUri.getPath(), oldUri.getQuery(), oldUri.getFragment());
    path = newUri.toString();
    for (String ch : backConversions.keySet()) {
        path = path.replace(ch.toString(), backConversions.get(ch).toString());
    }
    return path;
}

From source file:org.moe.cli.utils.GrabUtils.java

private static @Nullable String[] parseGitURI(@NonNull URI git) throws URISyntaxException {
    String path = git.getPath();/* www.  jav  a2 s  .c  o m*/
    if (path.endsWith(".git")) {
        String[] toRet = new String[3]; //{git,tag,path}
        toRet[0] = new URI(git.getScheme(), git.getUserInfo(), git.getHost(), git.getPort(), path, null, null)
                .toString(); //git
        String fragment = git.getFragment();
        if (fragment != null) {
            int colonIdx = fragment.indexOf(':');
            if (colonIdx > 0) { //tag exists
                toRet[1] = fragment.substring(0, colonIdx);
                toRet[2] = fragment.substring(colonIdx + 1);
            } else {
                toRet[2] = fragment;
            }
        }
        return toRet;
    } else {
        return null;
    }
}

From source file:org.dhatim.resource.URIResourceLocator.java

/**
 * Extract the base URI from the supplied resource URI.
 * @param resourceURI The resource URI.//from  w  w w . ja  va 2s  .c om
 * @return The base URI for the supplied resource URI.
 */
public static URI extractBaseURI(URI resourceURI) {
    File resFile = new File(resourceURI.getPath());

    try {
        File configFolder = resFile.getParentFile();
        if (configFolder != null) {
            return new URI(resourceURI.getScheme(), resourceURI.getUserInfo(), resourceURI.getHost(),
                    resourceURI.getPort(), configFolder.getPath().replace('\\', '/'), resourceURI.getQuery(),
                    resourceURI.getFragment());
        }
    } catch (URISyntaxException e) {
        logger.debug("Error extracting base URI.", e);
    }

    return DEFAULT_BASE_URI;
}