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:org.osaf.cosmo.dav.caldav.report.MultigetReport.java

private static URL normalizeHref(URL context, String href) throws DavException {
    URL url = null;
    try {/*from  w  w w  .j ava2  s . c o m*/
        url = new URL(context, href);
        // check that the URL is escaped. it's questionable whether or
        // not we should all unescaped URLs, but at least as of
        // 10/02/2007, iCal 3.0 generates them
        url.toURI();
        return url;
    } catch (URISyntaxException e) {
        try {
            URI escaped = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(),
                    url.getRef());
            return new URL(escaped.toASCIIString());
        } catch (Exception e2) {
            throw new BadRequestException("Malformed unescaped href " + href + ": " + e.getMessage());
        }
    } catch (MalformedURLException e) {
        throw new BadRequestException("Malformed href " + href + ": " + e.getMessage());
    }
}

From source file:io.seldon.importer.articles.FileItemAttributesImporter.java

public static String getUrlEncodedString(String input) {
    URL url = null;
    try {/*ww w  . j  av  a2s .c o  m*/
        url = new URL(input);

        URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);

        String encoded = uri.toASCIIString();

        return encoded;

    } catch (MalformedURLException mue) {
        logger.error("Malformed url " + input);
        return null;
    } catch (URISyntaxException e) {
        logger.error("Failed to tranform url into uri ", e);
        return null;
    }
}

From source file:com.atinternet.tracker.Tool.java

/**
 * Get parameters// ww w . j  a  v a 2s.c o  m
 *
 * @param hit String
 * @return HashMap
 */
static LinkedHashMap<String, String> getParameters(String hit) {
    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
    try {
        URL url = new URL(hit);
        map.put("ssl", url.getProtocol().equals("http") ? "Off" : "On");
        map.put("log", url.getHost());
        String[] queryComponents = url.getQuery().split("&");
        for (String queryComponent : queryComponents) {
            String[] elem = queryComponent.split("=");
            if (elem.length > 1) {
                elem[1] = Tool.percentDecode(elem[1]);
                if (Tool.parseJSON(elem[1]) instanceof JSONObject) {
                    JSONObject json = (JSONObject) Tool.parseJSON(elem[1]);
                    if (json != null && elem[0].equals(Hit.HitParam.JSON.stringValue())) {
                        map.put(elem[0], json.toString(3));
                    } else {
                        map.put(elem[0], elem[1]);
                    }
                } else {
                    map.put(elem[0], elem[1]);
                }
            } else {
                map.put(elem[0], "");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return map;
}

From source file:com.skcraft.launcher.util.HttpRequest.java

/**
 * URL may contain spaces and other nasties that will cause a failure.
 *
 * @param existing the existing URL to transform
 * @return the new URL, or old one if there was a failure
 *//*  w w  w  . j a  v a 2s.  c  o m*/
private static URL reformat(URL existing) {
    try {
        URL url = new URL(existing.toString());
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        url = uri.toURL();
        return url;
    } catch (MalformedURLException e) {
        return existing;
    } catch (URISyntaxException e) {
        return existing;
    }
}

From source file:org.wso2.carbon.identity.sso.saml.common.Util.java

/**
 * Generate OpenID for a given user./*from w w  w.  j a  v  a  2s .  co  m*/
 *
 * @param user User
 * @return Generated OpenID
 * @throws org.wso2.carbon.identity.base.IdentityException
 */
public static String generateOpenID(String user) throws IdentityException {
    String openIDUserUrl = null;
    String openID = null;
    URI uri = null;
    URL url = null;
    openIDUserUrl = IdentityUtil.getProperty(IdentityConstants.ServerConfig.OPENID_USER_PATTERN);
    user = normalizeUrlEncoding(user);
    openID = openIDUserUrl + user;
    try {
        uri = new URI(openID);
    } catch (URISyntaxException e) {
        throw new IdentityException("Invalid OpenID URL :" + openID, e);
    }
    try {
        url = uri.normalize().toURL();
        if (url.getQuery() != null || url.getRef() != null) {
            throw new IdentityException("Invalid user name for OpenID :" + openID);
        }
    } catch (MalformedURLException e) {
        throw new IdentityException("Malformed OpenID URL :" + openID, e);
    }
    openID = url.toString();
    return openID;
}

From source file:org.unitedinternet.cosmo.dav.caldav.report.MultigetReport.java

private static URL normalizeHref(URL context, String href) throws CosmoDavException {
    URL url = null;//  ww  w. ja  v  a 2s  .c o  m
    try {
        url = new URL(context, href);
        // check that the URL is escaped. it's questionable whether or
        // not we should all unescaped URLs, but at least as of
        // 10/02/2007, iCal 3.0 generates them
        url.toURI();
        return url;
    } catch (URISyntaxException e) {
        try {
            URI escaped = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(),
                    url.getRef());
            return new URL(escaped.toString());
        } catch (URISyntaxException | MalformedURLException e2) {
            throw new BadRequestException("Malformed unescaped href " + href + ": " + e.getMessage());
        }
    } catch (MalformedURLException e) {
        throw new BadRequestException("Malformed href " + href + ": " + e.getMessage());
    }
}

From source file:eu.trentorise.opendata.commons.TodUtils.java

/**
 * Extracts parameters from given url. Works also with multiple params with
 * same name.//  www .ja  v  a  2  s .c o  m
 *
 * @return map of param name : [args]
 * @throws IllegalArgumentException
 * @since 1.1
 */
public static Multimap<String, String> parseUrlParams(String url) {
    URL u;
    try {
        u = new URL(url);
    } catch (MalformedURLException ex) {
        throw new IllegalArgumentException("Ill formed url!", ex);
    }
    Multimap<String, String> queryPairs = LinkedListMultimap.create();
    final String[] pairs = u.getQuery().split("&");

    try {
        for (String pair : pairs) {
            final int idx = pair.indexOf("=");

            final String key;

            key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;

            final String value = idx > 0 && pair.length() > idx + 1
                    ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8")
                    : "";
            queryPairs.put(key, value);
        }
        return queryPairs;
    } catch (UnsupportedEncodingException ex) {
        throw new IllegalArgumentException("Encoding not supported!", ex);
    }
}

From source file:org.opennms.netmgt.provision.service.dns.DnsRequisitionUrlConnection.java

protected static Map<String, String> getUrlArgs(URL url) {

    if (url.getQuery() == null) {
        return null;
    }//from w w  w  .  j  av  a  2  s . co  m

    //TODO: need to throw exception if query is null
    String query = decodeQueryString(url);

    //TODO: need to handle exception
    List<String> queryArgs = tokenizeQueryArgs(query);
    Map<String, String> args = new HashMap<String, String>();
    for (String queryArg : queryArgs) {
        String[] argTokens = StringUtils.split(queryArg, '=');

        if (argTokens.length < 2) {
            LOG.warn("getUrlArgs: syntax error in URL query string, missing '=' in query argument: {}",
                    queryArg);
        } else {
            LOG.debug("adding arg tokens {}, {}", argTokens[1], argTokens[0].toLowerCase());
            args.put(argTokens[0].toLowerCase(), argTokens[1]);
        }
    }

    return args;
}

From source file:org.opennms.netmgt.provision.service.dns.DnsRequisitionUrlConnection.java

/**
 * <p>decodeQueryString</p>// ww w. j  a v  a2s  .  c o m
 *
 * @param url a {@link java.net.URL} object.
 * @return a {@link java.lang.String} object.
 */
protected static String decodeQueryString(URL url) {
    if (url == null || url.getQuery() == null) {
        throw new IllegalArgumentException("The URL or the URL query is null: " + url);
    }

    String query = null;
    try {
        query = URLDecoder.decode(url.getQuery(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        LOG.error("decodeQueryString", e);
    }

    return query;
}

From source file:sce.RESTAppMetricJob.java

public static URL convertToURLEscapingIllegalCharacters(String string) {
    try {//from www .j ava2s  . c  o  m
        String decodedURL = URLDecoder.decode(string, "UTF-8");
        URL url = new URL(decodedURL);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        return uri.toURL();
    } catch (MalformedURLException | URISyntaxException | UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}