Java URL Parameter Builder canonicalize(final SortedMap sortedParamMap)

Here you can find the source of canonicalize(final SortedMap sortedParamMap)

Description

Canonicalize the query string.

License

Apache License

Parameter

Parameter Description
sortedParamMap Parameter name-value pairs in lexicographical order.

Return

Canonical form of query string.

Declaration

private static String canonicalize(final SortedMap<String, String> sortedParamMap) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.URLDecoder;
import java.net.URLEncoder;

import java.util.*;

public class Main {
    private static final Set<String> IRRELEVANT_PARAMETERS = new HashSet<String>(3);

    /**//from  www. j  av  a2s .  c  o m
     * Canonicalize the query string.
     *
     * @param sortedParamMap Parameter name-value pairs in lexicographical order.
     * @return Canonical form of query string.
     */
    private static String canonicalize(final SortedMap<String, String> sortedParamMap) {
        if (sortedParamMap == null || sortedParamMap.isEmpty()) {
            return "";
        }

        final StringBuilder sb = new StringBuilder(100);
        for (Map.Entry<String, String> pair : sortedParamMap.entrySet()) {
            final String key = pair.getKey().toLowerCase();
            // Ignore irrelevant parameters
            if (IRRELEVANT_PARAMETERS.contains(key) || key.startsWith("utm_")) {
                continue;
            }
            if (sb.length() > 0) {
                sb.append('&');
            }
            sb.append(percentEncodeRfc3986(pair.getKey()));
            if (!pair.getValue().isEmpty()) {
                sb.append('=');
                sb.append(percentEncodeRfc3986(pair.getValue()));
            }
        }
        return sb.toString();
    }

    /**
     * <p>
     * Checks if a CharSequence is empty ("") or null.
     * </p>
     * <p>
     * <pre>
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * </pre>
     * <p>
     * <p>
     * NOTE: This method changed in Lang version 2.0. It no longer trims the
     * CharSequence. That functionality is available in isBlank().
     * </p>
     *
     * @param cs the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is empty or null
     * @since 3.0 Changed signature from isEmpty(String) to
     * isEmpty(CharSequence)
     */
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * Percent-encode values according the RFC 3986. The built-in Java
     * URLEncoder does not encode according to the RFC, so we make the extra
     * replacements.
     *
     * @param string Decoded string.
     * @return Encoded string per RFC 3986.
     */
    private static String percentEncodeRfc3986(String string) {
        try {
            string = string.replace("+", "%2B");
            string = URLDecoder.decode(string, "UTF-8");
            string = URLEncoder.encode(string, "UTF-8");
            return string.replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
        } catch (Exception e) {
            return string;
        }
    }
}

Related

  1. addParameter(Map> map, String pair)
  2. canonicalize(final SortedMap sortedParamMap)
  3. constructParameterString(TreeMap pPostParms)
  4. convertHttpParameters(Map parameters)
  5. convertToDataString(Map data)
  6. convertToParameterString(Map parameterMap)