Java URI to Parameter getQueryParameters(URI uri)

Here you can find the source of getQueryParameters(URI uri)

Description

get Query Parameters

License

Open Source License

Declaration

public static Map<String, String> getQueryParameters(URI uri) 

Method Source Code


//package com.java2s;
/*//  w w  w . jav  a  2s  .c o  m
 * Copyright (c) 2013 Game Salutes.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * Contributors:
 *     Game Salutes - Repackaging and modifications of original work under University of Chicago and Apache License 2.0 shown below
 * 
 * Repackaging from edu.uchicago.nsit.iteco.utils to com.gamesalutes.utils
 * 
 * Copyright 2008 - 2011 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.io.UnsupportedEncodingException;
import java.net.URI;

import java.net.URLDecoder;

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    private static final String ENC = "UTF-8";

    public static Map<String, String> getQueryParameters(URI uri) {
        if (uri == null)
            throw new NullPointerException("uri");
        return urlDecode(uri.getRawQuery(), false);
    }

    /**
     * Decodes the parameters of a query string using either url encoding (rfc1738) or form encoding and returns
     * them as a <code>Map</code>.
     * 
     * @param query the parameters to encode
     * @param formDecode <code>true</code> to form-encode and <code>false</code> to url encode
     * @return the decoded parameters
     */
    public static Map<String, String> urlDecode(String query, boolean formDecode) {
        if (query == null)
            return null;

        // split along the "&"
        String[] entries = query.split("&");
        Map<String, String> params = new LinkedHashMap<String, String>();

        for (String entry : entries) {
            // split along the "="
            String[] keyValue = entry.split("=");
            if (keyValue.length != 2)
                throw new IllegalArgumentException("query=" + query + "; invalid entry=" + entry);

            params.put(decode(keyValue[0], formDecode), decode(keyValue[1], formDecode));
        }

        return params;
    }

    /**
     * Url-decodes the path part of a http url according to <code>rfc1738</code>. If an url form-decoding
     * is required, use <code>formDecode</code>.
     * 
     * @param s the path string
     * @return the url-decoded path string
     */
    public static String urlDecode(String s) {
        if (s == null)
            return null;

        StringBuilder dec = new StringBuilder(s.length() * 2);

        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (c != '%')
                dec.append(c);
            else // decode
            {
                if (i + 2 >= s.length())
                    throw new IllegalArgumentException("s=" + s);

                int c1 = s.charAt(++i);
                int c2 = s.charAt(++i);
                // convert to hex int
                if (c1 >= 'A')
                    c1 = c1 - 'A' + 10;
                else
                    c1 -= '0';

                if (c2 >= 'A')
                    c2 = c2 - 'A' + 10;
                else
                    c2 -= '0';

                // convert to dec
                char d = (char) (c1 * 16 + c2);

                dec.append(d);

            }
        } // for

        return dec.toString();
    }

    private static String decode(String s, boolean formDecode) {
        if (s == null)
            return null;
        if (formDecode)
            return formDecode(s);
        return urlDecode(s);
    }

    /**
     * Decodes <code>s</code> that has been form-encoded using the 
     * MIME <code>application/x-www-form-urlencoded</code> type.
     * 
     * @param s the form-encoded string
     * @return the decoded string
     */
    public static String formDecode(String s) {
        if (s == null)
            return null;

        try {
            return URLDecoder.decode(s, ENC);
        } catch (UnsupportedEncodingException e) {
            throw new AssertionError(e);
        }
    }
}

Related

  1. getParameters(URI uri)
  2. getQueryKeyValuePairs(URI uri)
  3. getQueryKeyValuePairs(URI uri)
  4. getQueryParameter(final String param, URI data)
  5. getQueryParameters(URI uri)
  6. getQueryParameterValues(String uri, String name)