Example usage for java.net URI getRawFragment

List of usage examples for java.net URI getRawFragment

Introduction

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

Prototype

public String getRawFragment() 

Source Link

Document

Returns the raw fragment component of this URI.

Usage

From source file:com.groupon.odo.bmp.http.BrowserMobHttpClient.java

private URI makeUri(String url) throws URISyntaxException {
    // MOB-120: check for | character and change to correctly escaped %7C
    url = url.replace(" ", "%20");
    url = url.replace(">", "%3C");
    url = url.replace("<", "%3E");
    url = url.replace("#", "%23");
    url = url.replace("{", "%7B");
    url = url.replace("}", "%7D");
    url = url.replace("|", "%7C");
    url = url.replace("\\", "%5C");
    url = url.replace("^", "%5E");
    url = url.replace("~", "%7E");
    url = url.replace("[", "%5B");
    url = url.replace("]", "%5D");
    url = url.replace("`", "%60");
    url = url.replace("\"", "%22");

    URI uri = new URI(url);

    // are we using the default ports for http/https? if so, let's rewrite the URI to make sure the :80 or :443
    // is NOT included in the string form the URI. The reason we do this is that in HttpClient 4.0 the Host header
    // would include a value such as "yahoo.com:80" rather than "yahoo.com". Not sure why this happens but we don't
    // want it to, and rewriting the URI solves it
    if ((uri.getPort() == 80 && "http".equals(uri.getScheme()))
            || (uri.getPort() == 443 && "https".equals(uri.getScheme()))) {
        // we rewrite the URL with a StringBuilder (vs passing in the components of the URI) because if we were
        // to pass in these components using the URI's 7-arg constructor query parameters get double escaped (bad!)
        StringBuilder sb = new StringBuilder(uri.getScheme()).append("://");
        if (uri.getRawUserInfo() != null) {
            sb.append(uri.getRawUserInfo()).append("@");
        }//from  www.  j  a  v  a2s .  com
        sb.append(uri.getHost());
        if (uri.getRawPath() != null) {
            sb.append(uri.getRawPath());
        }
        if (uri.getRawQuery() != null) {
            sb.append("?").append(uri.getRawQuery());
        }
        if (uri.getRawFragment() != null) {
            sb.append("#").append(uri.getRawFragment());
        }

        uri = new URI(sb.toString());
    }
    return uri;
}

From source file:com.microsoft.azure.storage.core.Utility.java

/**
 * Determines the relative difference between the two specified URIs.
 * /*from www . j av a2  s  .  c om*/
 * @param baseURI
 *            A <code>java.net.URI</code> object that represents the base URI for which <code>toUri</code> will be
 *            made relative.
 * @param toUri
 *            A <code>java.net.URI</code> object that represents the URI to make relative to <code>baseURI</code>.
 * 
 * @return A <code>String</code> that either represents the relative URI of <code>toUri</code> to
 *         <code>baseURI</code>, or the URI of <code>toUri</code> itself, depending on whether the hostname and
 *         scheme are identical for <code>toUri</code> and <code>baseURI</code>. If the hostname and scheme of
 *         <code>baseURI</code> and <code>toUri</code> are identical, this method returns an unencoded relative URI
 *         such that if appended to <code>baseURI</code>, it will yield <code>toUri</code>. If the hostname or
 *         scheme of <code>baseURI</code> and <code>toUri</code> are not identical, this method returns an unencoded
 *         full URI specified by <code>toUri</code>.
 * 
 * @throws URISyntaxException
 *             If <code>baseURI</code> or <code>toUri</code> is invalid.
 */
public static String safeRelativize(final URI baseURI, final URI toUri) throws URISyntaxException {
    // For compatibility followed
    // http://msdn.microsoft.com/en-us/library/system.uri.makerelativeuri.aspx

    // if host and scheme are not identical return from uri
    if (!baseURI.getHost().equals(toUri.getHost()) || !baseURI.getScheme().equals(toUri.getScheme())) {
        return toUri.toString();
    }

    final String basePath = baseURI.getPath();
    String toPath = toUri.getPath();

    int truncatePtr = 1;

    // Seek to first Difference
    // int maxLength = Math.min(basePath.length(), toPath.length());
    int m = 0;
    int ellipsesCount = 0;
    for (; m < basePath.length(); m++) {
        if (m >= toPath.length()) {
            if (basePath.charAt(m) == '/') {
                ellipsesCount++;
            }
        } else {
            if (basePath.charAt(m) != toPath.charAt(m)) {
                break;
            } else if (basePath.charAt(m) == '/') {
                truncatePtr = m + 1;
            }
        }
    }

    // ../containername and ../containername/{path} should increment the truncatePtr
    // otherwise toPath will incorrectly begin with /containername
    if (m < toPath.length() && toPath.charAt(m) == '/') {
        // this is to handle the empty directory case with the '/' delimiter
        // for example, ../containername/ and ../containername// should not increment the truncatePtr
        if (!(toPath.charAt(m - 1) == '/' && basePath.charAt(m - 1) == '/')) {
            truncatePtr = m + 1;
        }
    }

    if (m == toPath.length()) {
        // No path difference, return query + fragment
        return new URI(null, null, null, toUri.getQuery(), toUri.getFragment()).toString();
    } else {
        toPath = toPath.substring(truncatePtr);
        final StringBuilder sb = new StringBuilder();
        while (ellipsesCount > 0) {
            sb.append("../");
            ellipsesCount--;
        }

        if (!Utility.isNullOrEmpty(toPath)) {
            sb.append(toPath);
        }

        if (!Utility.isNullOrEmpty(toUri.getQuery())) {
            sb.append("?");
            sb.append(toUri.getQuery());
        }
        if (!Utility.isNullOrEmpty(toUri.getFragment())) {
            sb.append("#");
            sb.append(toUri.getRawFragment());
        }

        return sb.toString();
    }
}

From source file:net.sf.taverna.t2.security.credentialmanager.impl.CredentialManagerImpl.java

protected LinkedHashSet<URI> getPossibleServiceURIsToLookup(URI serviceURI, boolean usePathRecursion) {
    try {/*ww  w.  j  av a2s . com*/
        serviceURI = serviceURI.normalize();
        serviceURI = dnParser.setUserInfoForURI(serviceURI, null);
    } catch (URISyntaxException ex) {
        logger.warn("Could not strip userinfo from " + serviceURI, ex);
    }

    /*
     * We'll use a LinkedHashSet to avoid checking for duplicates, like if
     * serviceURI.equals(withoutQuery) Only the first hit should be added to
     * the set.
     */
    LinkedHashSet<URI> possibles = new LinkedHashSet<URI>();

    possibles.add(serviceURI);
    if (!usePathRecursion || !serviceURI.isAbsolute())
        return possibles;

    /*
     * We'll preserve the fragment, as it is used to indicate the realm
     */
    String rawFragment = serviceURI.getRawFragment();
    if (rawFragment == null)
        rawFragment = "";

    URI withoutQuery = serviceURI.resolve(serviceURI.getRawPath());
    addFragmentedURI(possibles, withoutQuery, rawFragment);

    // Immediate parent
    URI parent = withoutQuery.resolve(".");
    addFragmentedURI(possibles, parent, rawFragment);
    URI oldParent = null;
    // Top parent (to be added later)
    URI root = parent.resolve("/");
    while (!parent.equals(oldParent) && !parent.equals(root) && parent.getPath().length() > 0) {
        // Intermediate parents, but not for "http://bla.org" as we would
        // find "http://bla.org.."
        oldParent = parent;
        parent = parent.resolve("..");
        addFragmentedURI(possibles, parent, rawFragment);
    }
    // In case while-loop did not do so, also include root
    addFragmentedURI(possibles, root, rawFragment);
    if (rawFragment.length() > 0)
        // Add the non-fragment versions in the bottom of the list
        for (URI withFragment : new ArrayList<>(possibles))
            try {
                possibles.add(dnParser.setFragmentForURI(withFragment, null));
            } catch (URISyntaxException e) {
                logger.warn("Could not non-fragment URI " + withFragment);
            }
    return possibles;
}