Java URL Parameter Builder getUrlParameters(final String url)

Here you can find the source of getUrlParameters(final String url)

Description

Returns url query parameters.

License

Open Source License

Parameter

Parameter Description
url the url to parse

Return

parameters map

Declaration

public static Map<String, List<String>> getUrlParameters(final String url) 

Method Source Code

//package com.java2s;
/*/*from ww w  . j a  v a2  s .c  o m*/
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    /**
     * Returns url query parameters.
     *
     * @param url the url to parse
     * @return parameters map
     */
    public static Map<String, List<String>> getUrlParameters(final String url) {
        final Map<String, List<String>> params = new HashMap<String, List<String>>();
        final String[] urlParts = url.split("\\?");
        if (urlParts.length > 1) {
            final String query = urlParts[1];
            for (final String param : query.split("&")) {
                final String[] pair = param.split("=");
                final String key = decodeUrl(pair[0]);
                String value = "";
                if (pair.length > 1) {
                    value = decodeUrl(pair[1]);
                }
                List<String> values = params.get(key);
                if (values == null) {
                    values = new ArrayList<String>();
                    params.put(key, values);
                }
                values.add(value);
            }
        }
        return params;
    }

    /**
     * Returns decoded url path.
     *
     * @param url the url to decode
     * @return decoded url
     */
    public static String decodeUrl(final String url) {
        try {
            return URLDecoder.decode(url, "UTF-8");
        } catch (final UnsupportedEncodingException e) {
            return url;
        }
    }
}

Related

  1. format(final Map> parameters, final String encoding)
  2. format(Map parameters, final String encoding)
  3. getParameter(String source, int index)
  4. getParameterMap(byte[] bytea)
  5. getParams(String str)
  6. getUrlParameters(String url)
  7. getUrlParameters(String url)
  8. getUrlParameters(String url)
  9. getUrlParameters(String url)