Example usage for java.net URL getQuery

List of usage examples for java.net URL getQuery

Introduction

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

Prototype

public String getQuery() 

Source Link

Document

Gets the query part of this URL .

Usage

From source file:fr.eolya.utils.http.HttpUtils.java

public static String urlRemoveParameters(String url, String paramsToRemove) {
    if (paramsToRemove == null || "".equals(paramsToRemove))
        return url;

    try {/*  w w w .  j ava2  s .  c  o m*/
        URL u = new URL(url);
        if (u.getQuery() == null && u.getPath().indexOf(";jsessionid=") == -1)
            return url;
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
        return null;
    }

    try {

        url = url.replace("?&", "?");

        if ("*".equals(paramsToRemove)) {
            int offset = url.lastIndexOf("?");
            if (offset != -1)
                return url.substring(0, offset);
        }

        paramsToRemove = paramsToRemove.replaceAll(" ", "").replaceAll(";", ",");
        String[] aToRemove = paramsToRemove.split(",");
        String tempUrl = url;
        for (int i = 0; i < aToRemove.length; i++) {
            boolean found = true;
            while (found) {
                found = false;
                String re = "[?&;]" + aToRemove[i].toLowerCase() + "[=&]";
                Pattern p = Pattern.compile(re);
                Matcher m = p.matcher(tempUrl.toLowerCase());
                if (m.find()) {
                    found = true;
                    int start = m.start();
                    int stop = start;
                    if ("jsessionid".equals(aToRemove[i].toLowerCase())) {
                        stop = tempUrl.indexOf("?", start + 1);
                        if (stop == -1)
                            stop = tempUrl.indexOf("&", start + 1);
                    } else {
                        stop = tempUrl.indexOf("&", start + 1);
                    }
                    if (stop == -1) {
                        tempUrl = tempUrl.substring(0, start);
                    } else {
                        String ope = tempUrl.substring(start, start + 1);
                        if (";".equals(ope))
                            ope = "?";
                        tempUrl = tempUrl.substring(0, start) + ope + tempUrl.substring(stop + 1);
                    }
                }
                re = "[?&;]" + aToRemove[i].toLowerCase() + "$";
                p = Pattern.compile(re);
                m = p.matcher(tempUrl.toLowerCase());
                if (m.find()) {
                    found = true;
                    int start = m.start();
                    int stop = start;
                    if ("jsessionid".equals(aToRemove[i].toLowerCase())) {
                        stop = tempUrl.indexOf("?", start + 1);
                        if (stop == -1)
                            stop = tempUrl.indexOf("&", start + 1);
                    } else {
                        stop = tempUrl.indexOf("&", start + 1);
                    }
                    if (stop == -1) {
                        tempUrl = tempUrl.substring(0, start);
                    } else {
                        String ope = tempUrl.substring(start, start + 1);
                        if (";".equals(ope))
                            ope = "?";
                        tempUrl = tempUrl.substring(0, start) + ope + tempUrl.substring(stop + 1);
                    }
                }
            }
        }
        return tempUrl;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:org.talend.core.runtime.maven.MavenUrlHelper.java

public static String generateMvnUrl(String username, String password, String repositoryId, String groupId,
        String artifactId, String version, String packaging, String classifier, boolean encryptPassword) {
    Assert.isNotNull(groupId);// w ww. j av  a2 s.co m
    Assert.isNotNull(artifactId);

    StringBuffer mvnUrl = new StringBuffer(100);
    mvnUrl.append(MVN_PROTOCOL);

    if (StringUtils.isNotEmpty(repositoryId)) {
        String repositoryUrl = repositoryId;
        if (StringUtils.isNotEmpty(username)) {
            if (password == null) {
                password = "";
            }
            if (encryptPassword) {
                password = encryptPassword(password);
            }
            String usernamePassword = username + USER_PASSWORD_SPLITER + password;
            try {
                URL repoWithoutUserPasswordUrl = new URL(repositoryId);
                if (repoWithoutUserPasswordUrl != null) {
                    if (StringUtils.isEmpty(repoWithoutUserPasswordUrl.getHost())) {
                        throw new Exception("Bad url, can't resolve it: " + repositoryId);
                    } else {
                        URI repoWithUserPasswordURI = new URI(repoWithoutUserPasswordUrl.getProtocol(),
                                usernamePassword, repoWithoutUserPasswordUrl.getHost(),
                                repoWithoutUserPasswordUrl.getPort(), repoWithoutUserPasswordUrl.getPath(),
                                repoWithoutUserPasswordUrl.getQuery(), repoWithoutUserPasswordUrl.getRef());
                        repositoryUrl = repoWithUserPasswordURI.toString();
                    }
                }
            } catch (Exception e) {
                ExceptionHandler.process(e);
            }
        }
        mvnUrl.append(repositoryUrl).append(REPO_SEPERATOR);
    }

    mvnUrl.append(groupId);
    mvnUrl.append(SEPERATOR);
    mvnUrl.append(artifactId);

    if (version != null) {
        mvnUrl.append(SEPERATOR);
        mvnUrl.append(version);
    } else {
        if (packaging != null || classifier != null) { // if has packaging or classifier
            // add one empty seperator
            mvnUrl.append(SEPERATOR);
        }
    }
    if (packaging != null) {
        mvnUrl.append(SEPERATOR);
        mvnUrl.append(packaging);
    } else {
        if (classifier != null) { // if has classifier
            // add one empty seperator
            mvnUrl.append(SEPERATOR);
        }
    }
    if (classifier != null) {
        mvnUrl.append(SEPERATOR);
        mvnUrl.append(classifier);
    }

    return mvnUrl.toString();
}

From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java

/**
 * Creates and returns a new URL identical to the specified URL, except using the specified port.
 * @param u the URL on which to base the returned URL
 * @param newPort the new port to use in the returned URL
 * @return a new URL identical to the specified URL, except using the specified port
 * @throws MalformedURLException if there is a problem creating the new URL
 *///from   w w w  . j a v  a 2s .c o  m
public static URL getUrlWithNewPort(final URL u, final int newPort) throws MalformedURLException {
    return createNewUrl(u.getProtocol(), u.getUserInfo(), u.getHost(), newPort, u.getPath(), u.getRef(),
            u.getQuery());
}

From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java

/**
 * Creates and returns a new URL identical to the specified URL, except using the specified host.
 * @param u the URL on which to base the returned URL
 * @param newHost the new host to use in the returned URL
 * @return a new URL identical to the specified URL, except using the specified host
 * @throws MalformedURLException if there is a problem creating the new URL
 *///from w  w  w  . ja v a2 s  . c  o m
public static URL getUrlWithNewHost(final URL u, final String newHost) throws MalformedURLException {
    return createNewUrl(u.getProtocol(), u.getUserInfo(), newHost, u.getPort(), u.getPath(), u.getRef(),
            u.getQuery());
}

From source file:org.imsglobal.lti.toolProvider.ToolConsumer.java

/**
 * Add the OAuth signature to an array of message parameters or to a header string.
 *
 * @return mixed Array of signed message parameters or header string
 *///from  ww  w .  j  a  v  a2s. co  m
public static Map<String, List<String>> addSignature(String endpoint, String consumerKey, String consumerSecret,
        Map<String, List<String>> data, String method, String type) {
    List<Entry<String, String>> oparams = new ArrayList<Entry<String, String>>();
    Map<String, List<String>> params = new HashMap<String, List<String>>();
    if (data != null) {
        params = data;
    }
    if (StringUtils.isEmpty(method)) {
        method = "POST";
    }
    // Check for query parameters which need to be included in the signature
    try {
        URL url = new URL(endpoint);
        Map<String, List<String>> queryParams = OAuthUtil.parse_parameters(url.getQuery());
        params.putAll(queryParams);

        oparams = convert(params);

        // Add OAuth signature
        OAuthMessage message = doSignature(endpoint, oparams, consumerKey, consumerSecret, method);
        oparams = message.getParameters();

        // Remove parameters being passed on the query string
        oparams = removeQueryParams(oparams, queryParams);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (OAuthException e1) {
        e1.printStackTrace();
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
    }

    return convertBack(oparams);

}

From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java

/**
 * Creates and returns a new URL identical to the specified URL, except using the specified host.
 * @param u the URL on which to base the returned URL
 * @param newHost the new host to use in the returned URL
 * @param newPort the new port to use in the returned URL
 * @return a new URL identical to the specified URL, except using the specified host
 * @throws MalformedURLException if there is a problem creating the new URL
 *///from  ww w.jav a  2 s.c  om
public static URL getUrlWithNewHostAndPort(final URL u, final String newHost, final int newPort)
        throws MalformedURLException {
    return createNewUrl(u.getProtocol(), u.getUserInfo(), newHost, newPort, u.getPath(), u.getRef(),
            u.getQuery());
}

From source file:org.apache.nutch.net.urlnormalizer.querystring.QuerystringURLNormalizer.java

public String normalize(String urlString, String scope) throws MalformedURLException {
    URL url = new URL(urlString);

    String queryString = url.getQuery();

    if (queryString == null) {
        return urlString;
    }/*  www.  j av  a  2  s . c  o m*/

    List<String> queryStringParts = Arrays.asList(queryString.split("&"));
    Collections.sort(queryStringParts);

    StringBuilder sb = new StringBuilder();

    sb.append(url.getProtocol());
    sb.append("://");
    sb.append(url.getHost());
    if (url.getPort() > -1) {
        sb.append(":");
        sb.append(url.getPort());
    }
    sb.append(url.getPath());
    sb.append("?");
    sb.append(StringUtils.join(queryStringParts, "&"));
    if (url.getRef() != null) {
        sb.append("#");
        sb.append(url.getRef());
    }

    return sb.toString();
}

From source file:org.asqatasun.rules.seo.SeoRule01071.java

@Override
protected ProcessResult processImpl(SSPHandler sspHandler) {

    ProcessRemarkService processRemarkService = sspHandler.getProcessRemarkService();
    processRemarkService.resetService();
    TestSolution testSolution = TestSolution.PASSED;

    try {/* w w w . ja  va 2s .  c  o m*/
        URL url = new URL(sspHandler.getSSP().getURI());
        if (StringUtils.isNotBlank(url.getQuery())) {
            testSolution = TestSolution.FAILED;
        }
    } catch (MalformedURLException ex) {
        testSolution = TestSolution.NOT_APPLICABLE;
    }

    if (testSolution.equals(TestSolution.FAILED)) {
        processRemarkService.addProcessRemark(TestSolution.FAILED, RemarkMessageStore.URL_PARAMETERS_DETECTED);
    }

    return processResultDataService.getDefiniteResult(test, sspHandler.getPage(), testSolution,
            processRemarkService.getRemarkList());
}

From source file:nl.tue.gale.event.LocalFactory.java

/**
 * Returns a <code>EventListener</code> proxy to the webservice running on
 * the specified <code>url</code>. The <code>url</code> should specify the
 * actual location of the webservice and some additional parameters.
 * /* w  ww  .  j av  a  2  s.  co m*/
 * Two additional parameters are required in the query string of the
 * <code>url</code>, 'service' and 'port'. They should refer to the name of
 * the service and the name of the port respectively. The wsdl will be
 * assumed to be located at the specified <code>url</code> without query
 * string and adding '?wsdl'.
 * 
 * @param url
 *            the <code>URL</code> specifying the location of the webservice
 * @return the <code>EventListener</code> proxy to the specified webservice
 */
public EventListener getListener(URL url) {
    try {
        Map<String, String> params = GaleUtil.getQueryParameters(url.getQuery());
        return (EventListener) ac.getBean(params.get("service").toLowerCase() + postfix);
    } catch (Exception e) {
        throw new IllegalArgumentException(
                "unable to create EventListener for '" + url + "': " + e.getMessage(), e);
    }
}

From source file:com.textuality.keybase.lib.prover.HackerNews.java

@Override
public String getPresenceLabel() throws KeybaseException {
    String answer = mProof.getServiceUrl();
    try {//from ww w .j  ava  2 s  .  c o m
        URL u = new URL(answer);
        answer = u.getHost() + u.getPath() + '?' + u.getQuery();
    } catch (MalformedURLException e) {
        answer = super.getPresenceLabel();
    }
    return answer;
}