Java URL Query Build getQueryParameterMap(String queryString)

Here you can find the source of getQueryParameterMap(String queryString)

Description

URL-decodes names and values

License

Open Source License

Declaration

public static Map<String, String> getQueryParameterMap(String queryString) 

Method Source Code


//package com.java2s;

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.*;

public class Main {
    /**/*from ww  w  .j av  a 2s .c  om*/
     * URL-decodes names and values
     */
    public static Map<String, String> getQueryParameterMap(String queryString) {
        Map<String, String> parameters = new HashMap<String, String>();
        if (queryString != null && queryString.trim().length() > 0) {
            for (String pair : queryString.split("&")) {
                int equals = pair.indexOf('=');
                if (equals == 0)
                    throw new IllegalArgumentException("invalid query parameter: " + pair);

                String key = equals > 0 ? pair.substring(0, equals) : pair;
                String value = equals > 0 ? pair.substring(equals + 1) : null;

                if (key.trim().length() == 0)
                    throw new IllegalArgumentException("query parameters must have a name");

                parameters.put(urlDecode(key), urlDecode(value));
            }
        }
        return parameters;
    }

    private static void put(Map<String, List<Object>> multiValueMap, String key, Object value, boolean single) {
        synchronized (multiValueMap) {
            // save calling code some headaches
            if (value == null) {
                if (single)
                    multiValueMap.remove(key);
                return;
            }
            List<Object> values = multiValueMap.get(key);
            if (values == null) {
                values = new ArrayList<Object>();
                multiValueMap.put(key, values);
            } else if (single)
                values.clear();
            values.add(value);
        }
    }

    public static String urlDecode(String value) {
        return urlDecode(value, true);
    }

    public static String urlDecode(String value, boolean preservePlus) {
        if (value == null)
            return null;
        try {
            // don't want '+' decoded to a space
            if (preservePlus)
                value = value.replace("+", "%2B");
            return URLDecoder.decode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8 encoding isn't supported on this system", e); // unrecoverable
        }
    }

    public static void add(Map<String, List<Object>> multiValueMap, String key, Object value) {
        put(multiValueMap, key, value, false);
    }
}

Related

  1. getQueryFromURL(URL url)
  2. getQueryMap(String query)
  3. getQueryMap(String urlString)
  4. getQueryMap(URL url)
  5. getQueryParameter(String query, String key, String encoding)
  6. getQueryParameters(URL url)
  7. getQueryParameters(URL url)
  8. getQueryParameters(URL url)
  9. getQueryParametersFromUrl(String url)