Example usage for java.net URI getQuery

List of usage examples for java.net URI getQuery

Introduction

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

Prototype

public String getQuery() 

Source Link

Document

Returns the decoded query component of this URI.

Usage

From source file:com.jaeksoft.searchlib.util.LinkUtils.java

public final static URL getLink(URL currentURL, String href, UrlFilterItem[] urlFilterList,
        boolean removeFragment) {

    if (href == null)
        return null;
    href = href.trim();//from   w ww.  j a  v  a 2  s  . c om
    if (href.length() == 0)
        return null;

    String fragment = null;
    try {
        URI u = URIUtils.resolve(currentURL.toURI(), href);
        href = u.toString();
        href = UrlFilterList.doReplace(u.getHost(), href, urlFilterList);
        URI uri = URI.create(href);
        uri = uri.normalize();

        String p = uri.getPath();
        if (p != null)
            if (p.contains("/./") || p.contains("/../"))
                return null;

        if (!removeFragment)
            fragment = uri.getRawFragment();

        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(),
                uri.getQuery(), fragment).normalize().toURL();
    } catch (MalformedURLException e) {
        Logging.info(e.getMessage());
        return null;
    } catch (URISyntaxException e) {
        Logging.info(e.getMessage());
        return null;
    } catch (IllegalArgumentException e) {
        Logging.info(e.getMessage());
        return null;
    }
}

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 w  w  w  .  j  av  a  2  s. c om
 *
 * @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.esigate.util.UriUtils.java

/**
 * Interpret the url relatively to the request url (may be relative). Due to a bug in {@link URI} class when using a
 * relUri containing only a query string, we cannot use directly the method provided by {@link URI} class.
 * /*w w  w.  j av  a2  s. c o  m*/
 * @param relUri
 *            the relative URI
 * @param base
 *            the reference {@link URI}
 * @return the resolved {@link URI}
 */
public static URI resolve(String relUri, URI base) {
    URI uri = createURI(relUri);
    if (uri.getScheme() == null && uri.getUserInfo() == null && uri.getHost() == null && uri.getPort() == -1
            && StringUtils.isEmpty(uri.getPath()) && uri.getQuery() != null) {
        try {
            return new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), base.getPath(),
                    uri.getQuery(), uri.getFragment());
        } catch (URISyntaxException e) {
            throw new InvalidUriException(e);
        }
    } else {
        return base.resolve(uri);
    }
}

From source file:controllers.Common.java

@Util
public static String toSafeRedirectURL(String url) {
    String cleanUrl = "";
    try {//ww w.  j a va 2s  .co m
        // Remove Host and port from referrer
        URI uriObject = new URI(url);
        cleanUrl += uriObject.getPath();

        String query = uriObject.getQuery();
        if (!StringUtils.isBlank(query)) {
            cleanUrl += "?" + query;
        }
    } catch (URISyntaxException ignore) {
        Logger.error(ignore.getMessage());
    }

    return cleanUrl;
}

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;
    }//ww w  .ja  v a2  s.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.echocat.jomon.net.http.HttpUtils.java

@Nonnull
public static HttpGet getFor(@Nonnull URI uri, @Nonnull Charset charset,
        @Nullable Map<String, String> parameters) throws UnsupportedEncodingException {
    final HttpGet httpGet;
    if (parameters != null && !parameters.isEmpty()) {
        final StringBuilder sb = new StringBuilder();
        sb.append(uri);//from w w  w  .ja v a  2s  .co m
        boolean questionMarkAdded = uri.getQuery() != null;
        for (final Entry<String, String> keyToValue : parameters.entrySet()) {
            if (questionMarkAdded) {
                sb.append('&');
            } else {
                sb.append('?');
                questionMarkAdded = true;
            }
            sb.append(encode(keyToValue.getKey(), charset.name())).append('=')
                    .append(encode(keyToValue.getValue(), charset.name()));
        }
        httpGet = new HttpGet(create(sb.toString()));
    } else {
        httpGet = new HttpGet(uri);
    }
    return httpGet;
}

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.j a v a 2 s .  co  m*/
 * @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;
}

From source file:org.apache.hadoop.security.ProviderUtils.java

/**
 * Convert a nested URI to decode the underlying path. The translation takes
 * the authority and parses it into the underlying scheme and authority.
 * For example, "myscheme://hdfs@nn/my/path" is converted to
 * "hdfs://nn/my/path".//  ww w  . j  a v a2s  .  c om
 * @param nestedUri the URI from the nested URI
 * @return the unnested path
 */
public static Path unnestUri(URI nestedUri) {
    StringBuilder result = new StringBuilder();
    String authority = nestedUri.getAuthority();
    if (authority != null) {
        String[] parts = nestedUri.getAuthority().split("@", 2);
        result.append(parts[0]);
        result.append("://");
        if (parts.length == 2) {
            result.append(parts[1]);
        }
    }
    result.append(nestedUri.getPath());
    if (nestedUri.getQuery() != null) {
        result.append("?");
        result.append(nestedUri.getQuery());
    }
    if (nestedUri.getFragment() != null) {
        result.append("#");
        result.append(nestedUri.getFragment());
    }
    return new Path(result.toString());
}

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

/**
 * Strips the last component of the given URI if possible
 * /* w w  w. j av a2s .  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:org.soyatec.windowsazure.authenticate.HttpRequestAccessor.java

/**
 * Given the service endpoint in case of path-style URIs, path contains
 * container name and remaining part if they are present, this method
 * constructs the Full Uri.//from   w  ww  .  j  ava 2  s . c  om
 * 
 * @param endPoint
 * @param path
 * @return Full URI
 */
private static URI constructUriFromUriAndString(URI endPoint, String path) {
    // This is where we encode the url path to be valid
    String encodedPath = Utilities.encode(path);
    if (!Utilities.isNullOrEmpty(encodedPath) && encodedPath.charAt(0) != ConstChars.Slash.charAt(0)) {
        encodedPath = ConstChars.Slash + encodedPath;
    }
    try {
        // @Note : rename blob with blank spaces in name
        return new URI(endPoint.getScheme(), null, endPoint.getHost(), endPoint.getPort(),
                encodedPath.replaceAll("%20", " "), endPoint.getQuery(), endPoint.getFragment());
        // return new URI(endPoint.getScheme(), endPoint.getHost(),
        // encodedPath, endPoint.getFragment());
    } catch (URISyntaxException e) {
        Logger.error("Can not new URI", e);
        return null;
    }
}