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:illab.nabal.util.ParameterHelper.java

/**
 * Parse a URL query and fragment parameters into a key-value bundle.
 * //from www . ja  v a 2  s .c o  m
 * @param url - the URL to parse
 * @return bundle - a dictionary bundle of keys and values
 */
public static Bundle parseGetParams(String url) {

    // prepare a bundle
    Bundle bundle = null;
    try {
        URL u = new URL(url);
        bundle = decodeGetParams(u.getQuery());
        bundle.putAll(decodeGetParams(u.getRef()));
    } catch (MalformedURLException e) {
        Log.e(TAG, "malformed URL");
        Log.e(TAG, e.getMessage());
    }
    return bundle;
}

From source file:org.apache.maven.wagon.shared.http.EncodingUtil.java

/**
 * Parses and returns an encoded version of the given URL string.
 *
 * @param url Raw/decoded string form of a URL to parse/encode.
 * @return Parsed/encoded {@link URI} that represents the string form URL passed in.
 * @throws MalformedURLException//from w w  w.  j  ava 2 s .  c om
 * @throws URISyntaxException
 */
public static URI encodeURL(String url) throws MalformedURLException, URISyntaxException {
    URL urlObject = new URL(url);

    URI uriEncoded = new URI(urlObject.getProtocol(), //
            urlObject.getAuthority(), //
            urlObject.getPath(), //
            urlObject.getQuery(), //
            urlObject.getRef());

    return uriEncoded;
}

From source file:com.ibm.jaggr.core.util.PathUtil.java

/**
 * Convenience method to convert a URL to a URI that doesn't throw a
 * URISyntaxException if the path component for the URL contains
 * spaces (like {@link URL#toURI()} does).
 *
 * @param url The input URL/*  ww w.  ja v a  2s .  c  o  m*/
 * @return The URI
 * @throws URISyntaxException
 */
public static URI url2uri(URL url) throws URISyntaxException {
    return new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(), url.getRef());
}

From source file:fr.dutra.confluence2wordpress.util.UrlUtils.java

/**
 * Sanitizes the given URL by escaping the path and query parameters, if necessary.
 * @see "http://stackoverflow.com/questions/724043/http-url-address-encoding-in-java"
 * @param url/*from   www . ja v a 2 s  .c o  m*/
 * @return
 * @throws URISyntaxException
 * @throws MalformedURLException
 */
public static String sanitize(String url) throws URISyntaxException, MalformedURLException {
    URL temp = new URL(url);
    URI uri = new URI(temp.getProtocol(), temp.getUserInfo(), temp.getHost(), temp.getPort(), temp.getPath(),
            temp.getQuery(), temp.getRef());
    return uri.toASCIIString();
}

From source file:org.geotools.data.wfs.protocol.http.HttpUtil.java

public static String createUri(final URL baseUrl, final Map<String, String> queryStringKvp) {
    final String query = baseUrl.getQuery();
    Map<String, String> finalKvpMap = new HashMap<String, String>(queryStringKvp);
    if (query != null && query.length() > 0) {
        Map<String, String> userParams = new CaseInsensitiveMap(queryStringKvp);
        String[] rawUrlKvpSet = query.split("&");
        for (String rawUrlKvp : rawUrlKvpSet) {
            int eqIdx = rawUrlKvp.indexOf('=');
            String key, value;//  w ww  .  j  ava  2s.c o m
            if (eqIdx > 0) {
                key = rawUrlKvp.substring(0, eqIdx);
                value = rawUrlKvp.substring(eqIdx + 1);
            } else {
                key = rawUrlKvp;
                value = null;
            }
            try {
                value = URLDecoder.decode(value, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            if (userParams.containsKey(key)) {
                LOGGER.fine("user supplied value for query string argument " + key
                        + " overrides the one in the base url");
            } else {
                finalKvpMap.put(key, value);
            }
        }
    }

    String protocol = baseUrl.getProtocol();
    String host = baseUrl.getHost();
    int port = baseUrl.getPort();
    String path = baseUrl.getPath();

    StringBuilder sb = new StringBuilder();
    sb.append(protocol).append("://").append(host);
    if (port != -1 && port != baseUrl.getDefaultPort()) {
        sb.append(':');
        sb.append(port);
    }
    if (!"".equals(path) && !path.startsWith("/")) {
        sb.append('/');
    }
    sb.append(path).append('?');

    String key, value;
    try {
        Entry<String, String> kvp;
        for (Iterator<Map.Entry<String, String>> it = finalKvpMap.entrySet().iterator(); it.hasNext();) {
            kvp = it.next();
            key = kvp.getKey();
            value = kvp.getValue();
            if (value == null) {
                value = "";
            } else {
                value = URLEncoder.encode(value, "UTF-8");
            }
            sb.append(key);
            sb.append('=');
            sb.append(value);
            if (it.hasNext()) {
                sb.append('&');
            }
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

    final String finalUrlString = sb.toString();
    return finalUrlString;
}

From source file:SageCollegeProject.guideBox.java

public static String GetEncWebCall(String webcall) {
    try {/*from  w ww  . j  ava 2 s . c  o m*/
        URL url = new URL(webcall);
        URI test = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        return test.toASCIIString();
    } catch (URISyntaxException | MalformedURLException ex) {
        Logger.getLogger(guideBox.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}

From source file:com.flipkart.phantom.task.utils.StringUtils.java

public static Map<String, String> getQueryParams(String httpUrl) {
    Map<String, String> params = new HashMap<String, String>();
    if (httpUrl == null) {
        return params;
    }//from   w ww.ja  v a 2  s  .  c  o m

    URL url = null;
    try {
        url = new URL(httpUrl);
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }

    String query = url.getQuery();
    if (query == null) {
        return params;
    }

    StringTokenizer tokenizer = new StringTokenizer(query, "&");
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
        int index = token.indexOf("=");
        params.put(token.substring(0, index).trim(), token.substring(index + 1).trim());
    }

    return params;
}

From source file:com.mingsoft.weixin.http.HttpClientConnectionManager.java

/**
 * ?GET??// w  ww . j a va 2 s . c  o  m
 * 
 * @param url
 * @return
 */
public static HttpGet getGetMethod(String url) {
    URI uri = null;
    try {
        URL _url = new URL(url);
        uri = new URI(_url.getProtocol(), _url.getHost(), _url.getPath(), _url.getQuery(), null);

    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    HttpGet pmethod = new HttpGet(uri);
    // ??
    pmethod.addHeader("Connection", "keep-alive");
    pmethod.addHeader("Cache-Control", "max-age=0");
    pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
    pmethod.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8");
    return pmethod;
}

From source file:com.mingsoft.weixin.http.HttpClientConnectionManager.java

/**
 * ?post??/*from www  .  j a v a 2s. co  m*/
 * 
 * @param url
 * @return
 */
public static HttpPost getPostMethod(String url) {

    URI uri = null;
    try {
        URL _url = new URL(url);
        uri = new URI(_url.getProtocol(), _url.getHost(), _url.getPath(), _url.getQuery(), null);

    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    HttpPost pmethod = new HttpPost(uri); // ??
    pmethod.addHeader("Connection", "keep-alive");
    pmethod.addHeader("Accept", "*/*");
    pmethod.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    pmethod.addHeader("Host", "mp.weixin.qq.com");
    pmethod.addHeader("X-Requested-With", "XMLHttpRequest");
    pmethod.addHeader("Cache-Control", "max-age=0");
    pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
    return pmethod;
}

From source file:net.daporkchop.porkselfbot.util.HTTPUtils.java

/**
 * Concatenates the given {@link java.net.URL} and query.
 *
 * @param url URL to base off/*from  w w  w.  j  ava 2s. c  om*/
 * @param query Query to append to URL
 * @return URL constructed
 */
public static URL concatenateURL(final URL url, final String query) {
    try {
        if (url.getQuery() != null && url.getQuery().length() > 0) {
            return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "&" + query);
        } else {
            return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "?" + query);
        }
    } catch (final MalformedURLException ex) {
        throw new IllegalArgumentException("Could not concatenate given URL with GET arguments!", ex);
    }
}