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:com.microsoft.tfs.core.util.URIUtils.java

/**
 * <p>/*www .j ava2  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.jaeksoft.searchlib.web.AbstractServlet.java

protected static URI buildUri(URI uri, String additionalPath, String indexName, String login, String apiKey,
        String additionnalQuery) throws URISyntaxException {
    StringBuilder path = new StringBuilder(uri.getPath());
    if (additionalPath != null)
        path.append(additionalPath);//from   www.ja  v  a2 s . c  o m
    StringBuilder query = new StringBuilder();
    if (indexName != null) {
        query.append("index=");
        query.append(indexName);
    }
    if (login != null) {
        query.append("&login=");
        query.append(login);
    }
    if (apiKey != null) {
        query.append("&key=");
        query.append(apiKey);
    }
    if (additionnalQuery != null) {
        if (query.length() > 0)
            query.append("&");
        query.append(additionnalQuery);
    }
    return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), path.toString(),
            query.toString(), uri.getFragment());

}

From source file:org.mcxiaoke.commons.http.util.URIUtilsEx.java

/**
 * A convenience method that creates a new {@link URI} whose scheme, host,
 * port, path, query are taken from the existing URI, dropping any fragment
 * or user-information. The path is set to "/" if not explicitly specified.
 * The existing URI is returned unmodified if it has no fragment or
 * user-information and has a path.//  w w  w .  jav  a2 s  . c  om
 * 
 * @param uri
 *            original URI.
 * @throws URISyntaxException
 *             If the resulting URI is invalid.
 */
public static URI rewriteURI(final URI uri) throws URISyntaxException {
    if (uri == null) {
        throw new IllegalArgumentException("URI may not be null");
    }
    if (uri.getFragment() != null || uri.getUserInfo() != null
            || (uri.getPath() == null || uri.getPath().length() == 0)) {
        URIBuilderEx uribuilder = new URIBuilderEx(uri);
        uribuilder.setFragment(null).setUserInfo(null);
        if (uribuilder.getPath() == null || uribuilder.getPath().length() == 0) {
            uribuilder.setPath("/");
        }
        return uribuilder.build();
    } else {
        return uri;
    }
}

From source file:org.mulgara.scon.Connection.java

/**
 * Encodes a URI if it looks like it needs it.
 * @param u The URI to encode, if needed.
 * @return a minimally encoded URI.//  www. j  a v a2s .  co m
 */
private static final String enc(URI u) {
    try {
        // if there is no query, then just return the unencoded URI
        String query = u.getRawQuery();
        if (query == null)
            return u.toString();
        // encode the query, and add it to the end of the URI
        String encQuery = encode(query);
        String encU = new URI(u.getScheme(), u.getUserInfo(), u.getHost(), u.getPort(), u.getPath(), encQuery,
                u.getFragment()).toString();
        // if the partial encoding works, then return it
        if (decode(encU).equals(u.toString()))
            return encU;
        // nothing else worked, so encode it fully
        return encode(u.toString());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Unable to encode a URI", e);
    }
}

From source file:uk.q3c.krail.core.navigate.StrictURIFragmentHandler.java

@Override
public NavigationState navigationState(URI uri) {
    return navigationState(uri.getFragment());
}

From source file:com.github.fge.jackson.jsonpointer.JsonPointerTest.java

@Test(dataProvider = "uriPointers")
public void uriPointerResolvingWorks(final String input, final JsonNode expected)
        throws URISyntaxException, JsonPointerException {
    final URI uri = new URI(input);
    final JsonPointer pointer = new JsonPointer(uri.getFragment());

    assertEquals(pointer.get(document), expected);
}

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  2  s . 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:jenkins.scm.impl.subversion.SubversionSampleRepoRule.java

public String rootUrl() throws URISyntaxException {
    URI u = repo.toURI();
    // TODO SVN rejects File.toUri syntax (requires blank authority field)
    return new URI(u.getScheme(), "", u.getPath(), u.getFragment()).toString();
}

From source file:org.orbeon.oxf.xforms.XFormsUtils.java

private static String uriToStringNoFragment(XFormsContainingDocument containingDocument, URI resolvedURI) {
    if (containingDocument.isPortletContainer() && resolvedURI.getFragment() != null) {
        // XForms page was loaded from a portlet and there is a fragment, remove it
        try {//from  w  w  w.java 2 s  . c  o m
            return new URI(resolvedURI.getScheme(), resolvedURI.getRawAuthority(), resolvedURI.getRawPath(),
                    resolvedURI.getRawQuery(), null).toString();
        } catch (URISyntaxException e) {
            throw new OXFException(e);
        }
    } else {
        return resolvedURI.toString();
    }
}

From source file:com.collective.celos.ci.deploy.JScpWorker.java

URI getURIRespectingUsername(URI uri) throws URISyntaxException {

    if (userName != null && uri.getUserInfo() == null) {
        uri = new URI(uri.getScheme(), userName, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(),
                uri.getFragment());
    }//from w  w w  . ja v  a2s.com
    return uri;
}