Java URI to Parameter getParameters(URI uri)

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

Description

Extract and decode query parameters from a URI and return them as a map.

License

Apache License

Parameter

Parameter Description
uri The uri from which the parameters should be parsed.

Return

The map containing the result.

Declaration

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

Method Source Code

//package com.java2s;
/**//from   w w  w .  ja  va  2 s .c  o m
 * Copyright 2012-, Cloudsmith Inc.
 *
 * 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.Collections;
import java.util.HashMap;
import java.util.Map;

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

    /**
     * Extract and decode query parameters from a URI and return them as
     * a map.
     * 
     * @param uri The uri from which the parameters should be parsed.
     * @return The map containing the result.
     */
    public static Map<String, String> getParameters(URI uri) {
        return parseQuery(uri.getRawQuery());
    }

    /**
     * Extract and decode query parameters from an encoded query string
     * 
     * @param query The raw query string of a URI or URL.
     * @return The map containing the result.
     */
    public static Map<String, String> parseQuery(String query) {
        if (query == null || query.isEmpty())
            return Collections.emptyMap();

        Map<String, String> result = new HashMap<String, String>();
        int top = query.length();
        int start = 0;
        for (int idx = 0; idx < top; ++idx) {
            char c = query.charAt(idx);
            if (c == '&') {
                parsePair(result, query.substring(start, idx));
                start = idx + 1;
            }
        }
        if (start < top)
            parsePair(result, query.substring(start));
        return result;
    }

    private static void parsePair(Map<String, String> result, String pair) {
        int sep = pair.indexOf('=');
        if (sep < 0)
            result.put(decode(pair), null);
        else
            result.put(decode(pair.substring(0, sep)), decode(pair.substring(sep + 1)));
    }

    /**
     * Decode a value using the URLDecoder and UTF-8
     * 
     * @param value The value to decode
     * @return The decoded value.
     */
    public static String decode(String value) {
        try {
            return URLDecoder.decode(value, QUERY_ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

Related

  1. extractUriParameters(String uriString)
  2. extractUriParameters(URI uri)
  3. getKey(String uri)
  4. getKey(URI uri)
  5. getParameters(URI uri)
  6. getQueryKeyValuePairs(URI uri)
  7. getQueryKeyValuePairs(URI uri)
  8. getQueryParameter(final String param, URI data)
  9. getQueryParameters(URI uri)