com.feilong.core.net.ParamExtensionUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.net.ParamExtensionUtil.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * 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.
 */
package com.feilong.core.net;

import static org.apache.commons.lang3.StringUtils.EMPTY;

import java.net.URI;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.feilong.core.CharsetType;
import com.feilong.core.bean.ConvertUtil;
import com.feilong.core.util.MapUtil;

import static com.feilong.core.Validator.isNullOrEmpty;
import static com.feilong.core.bean.ConvertUtil.toList;
import static com.feilong.core.bean.ConvertUtil.toMap;

/**
 * ??.
 *
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @see "org.springframework.web.util.UriComponentsBuilder"
 * @see "org.apache.http.client.utils.URIBuilder"
 * @since 1.0.0
 */
public final class ParamExtensionUtil {

    /** Don't let anyone instantiate this class. */
    private ParamExtensionUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * ?,uri????,?.
     * 
     * @param uri
     *            ? ?,?,??,<br>
     *            ??, ?
     * @param paramName
     *            ???
     * @param parameValue
     *            ?,? {@link String} 
     * @param charsetType
     *            ??, {@link CharsetType}<br>
     *            <span style="color:green">null empty,?,?</span><br>
     *            ??,??,ie?chrome? url ,?
     * @return ?,uri????,?<br>
     *          <code>uriString</code> nullempty, {@link StringUtils#EMPTY}<br>
     * @see #addParameterArrayValueMap(URI, Map, String)
     */
    public static String addParameter(URI uri, String paramName, Object parameValue, String charsetType) {
        return addParameterArrayValueMap(uri, toMap(paramName, new String[] { "" + parameValue }), charsetType);
    }

    /**
     * ?.
     * 
     * <p>
     * uri????,?,?{@code a=1&a=2},a,[3,4],{@code a=3&a=4}.
     * </p>
     * 
     * @param uri
     *            ? ?,?,??,<br>
     *            ??, ?
     * @param arrayValueMap
     *            singleValueMap  request.getParameterMap
     * @param charsetType
     *            ??, {@link CharsetType}<br>
     *            <span style="color:green">null empty,?,?</span><br>
     *            ??,??,ie?chrome? url ,?
     * @return  <code>uri</code> null, {@link StringUtils#EMPTY}<br>
     */
    public static String addParameterArrayValueMap(URI uri, Map<String, String[]> arrayValueMap,
            String charsetType) {
        return null == uri ? EMPTY
                : ParamUtil.addParameterArrayValueMap(uri.toString(), uri.getRawQuery(), arrayValueMap,
                        charsetType);
    }

    // ********************************removeParameter*********************************************************************

    /**
     * ?.
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * 
     * <pre class="code">
     * String uriString = "http://www.feilong.com:8888/search.htm?{@code keyword=&page=&categoryCode=2-5-3-11&label=TopSeller"};
     * String pageParamName = "label";
     * LOGGER.info(ParamUtil.removeParameter(uriString, pageParamName, UTF8));
     * </pre>
     * 
     * <b>:</b>
     * 
     * <pre class="code">
     * http://www.feilong.com:8888/search.htm?keyword={@code %E4%B8%AD%E5%9B%BD&page=&categoryCode=2-5-3-11}
     * </pre>
     * 
     * </blockquote>
     *
     * @param uriString
     *            the uri string
     * @param paramName
     *            ???
     * @param charsetType
     *            ??, {@link CharsetType}<br>
     *            <span style="color:green">null empty,?,?</span><br>
     *            ??,??,ie?chrome? url ,?
     * @return  <code>uriString</code> nullempty, {@link StringUtils#EMPTY}<br>
     *          <code>paramName</code> nullempty, <code>uriString</code><br>
     *         ?  list, {@link #removeParameterList(String, List, String)}
     * @see #removeParameterList(String, List, String)
     */
    public static String removeParameter(String uriString, String paramName, String charsetType) {
        return removeParameterList(uriString, toList(paramName), charsetType);
    }

    /**
     * ?.
     * 
     * @param uri
     *            the uri
     * @param paramName
     *            ???
     * @param charsetType
     *            ??, {@link CharsetType}<br>
     *            <span style="color:green">null empty,?,?</span><br>
     *            ??,??,ie?chrome? url ,?
     * @return  <code>uri</code> null, {@link StringUtils#EMPTY}<br>
     * @see #removeParameterList(URI, List, String)
     */
    public static String removeParameter(URI uri, String paramName, String charsetType) {
        return removeParameterList(uri, toList(paramName), charsetType);
    }

    /**
     * ?.
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * 
     * <pre class="code">
     * 
     * String uriString = "http://www.feilong.com:8888/search.htm?{@code keyword=&page=&categoryCode=2-5-3-11&label=TopSeller"};
     * List{@code <String>} paramNameList = new ArrayList{@code <String>}();
     * paramNameList.add("label");
     * paramNameList.add("keyword");
     * LOGGER.info(ParamUtil.removeParameterList(uriString, paramNameList, UTF8));
     * 
     * </pre>
     * 
     * <b>:</b>
     * 
     * <pre class="code">
     * http://www.feilong.com:8888/search.htm?{@code page=&categoryCode=2-5-3-11}
     * </pre>
     * 
     * </blockquote>
     * 
     * @param uriString
     *            the url
     * @param paramNameList
     *            ??? list
     * @param charsetType
     *            ??, {@link CharsetType}<br>
     *            <span style="color:green">null empty,?,?</span><br>
     *            ??,??,ie?chrome? url ,?
     * @return  <code>uriString</code> nullempty, {@link StringUtils#EMPTY}<br>
     *          <code>paramNameList</code> nullempty, <code>uriString</code><br>
     */
    public static String removeParameterList(String uriString, List<String> paramNameList, String charsetType) {
        return removeParameterList(uriString, URIUtil.getQueryString(uriString), paramNameList, charsetType);
    }

    /**
     * ?.
     * 
     * @param uri
     *            the uri
     * @param paramNameList
     *            ??? list
     * @param charsetType
     *            ??, {@link CharsetType}<br>
     *            <span style="color:green">null empty,?,?</span><br>
     *            ??,??,ie?chrome? url ,?
     * @return  <code>uri</code> null, {@link StringUtils#EMPTY}<br>
     *          <code>paramNameList</code> nullempty, <code>uri.toString()</code><br>
     */
    public static String removeParameterList(URI uri, List<String> paramNameList, String charsetType) {
        return null == uri ? EMPTY
                : removeParameterList(uri.toString(), uri.getRawQuery(), paramNameList, charsetType);
    }

    /**
     *  parameter list.
     *
     * @param uriString
     *            the uri string
     * @param queryString
     *            the query string
     * @param paramNameList
     *            ??? list
     * @param charsetType
     *            ??, {@link CharsetType}<br>
     *            <span style="color:green">null empty,?,?</span><br>
     *            ??,??,ie?chrome? url ,?
     * @return  <code>uriString</code> nullempty, {@link StringUtils#EMPTY}<br>
     *          <code>queryString</code> nullempty, <code>uriString</code><br>
     *          <code>paramNameList</code> nullempty, <code>uriString</code>
     * @since 1.4.0
     */
    private static String removeParameterList(String uriString, String queryString, List<String> paramNameList,
            String charsetType) {
        if (isNullOrEmpty(uriString)) {
            return EMPTY;
        }
        if (isNullOrEmpty(queryString) || isNullOrEmpty(paramNameList)) {
            return uriString;// ??
        }
        Map<String, String[]> map = MapUtil.removeKeys(ParamUtil.toSafeArrayValueMap(queryString, null),
                ConvertUtil.toArray(paramNameList, String.class));
        return ParamUtil.combineUrl(URIUtil.getFullPathWithoutQueryString(uriString), map, charsetType);
    }

    // **************************************retentionParams********************************************************

    /**
     * url???.
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * 
     * <pre class="code">
     * 
     * String uriString = "http://www.feilong.com:8888/search.htm?{@code keyword=&page=&categoryCode=2-5-3-11&label=TopSeller}";
     * List{@code <String>} paramNameList = new ArrayList{@code <String>}();
     * paramNameList.add("label");
     * paramNameList.add("keyword");
     * LOGGER.info(ParamUtil.retentionParamList(uriString, paramNameList, UTF8));
     * 
     * </pre>
     * 
     * <b>:</b>
     * 
     * <pre class="code">
     * http://www.feilong.com:8888/search.htm?{@code label=TopSeller&keyword=%E4%B8%AD%E5%9B%BD}
     * </pre>
     * 
     * </blockquote>
     *
     * @param uriString
     *            the uri string
     * @param paramNameList
     *            ?????list
     * @param charsetType
     *            ??, {@link CharsetType}<br>
     *            <span style="color:green">null empty,?,?</span><br>
     *            ??,??,ie?chrome? url ,?
     * @return  <code>uriString</code> nullempty, {@link StringUtils#EMPTY}<br>
     *         ? {@link #retentionParamList(URI, List, String)}
     * @see #retentionParamList(URI, List, String)
     */
    public static String retentionParamList(String uriString, List<String> paramNameList, String charsetType) {
        return retentionParamList(uriString, URIUtil.getQueryString(uriString), paramNameList, charsetType);
    }

    /**
     * url???.
     * 
     * @param uri
     *            the uri
     * @param paramNameList
     *            ?????list
     * @param charsetType
     *            ??, {@link CharsetType}<br>
     *            <span style="color:green">null empty,?,?</span><br>
     *            ??,??,ie?chrome? url ,?
     * @return  <code>uri</code> null, {@link StringUtils#EMPTY}<br>
     *         ? {@link #retentionParamList(String, String, List, String)}
     * @see #retentionParamList(String, String, List, String)
     */
    public static String retentionParamList(URI uri, List<String> paramNameList, String charsetType) {
        return null == uri ? EMPTY
                : retentionParamList(uri.toString(), uri.getRawQuery(), paramNameList, charsetType);
    }

    /**
     * ??.
     *
     * @param uriString
     *            the uri string
     * @param queryString
     *            the query string
     * @param paramNameList
     *            ?????list
     * @param charsetType
     *            ??, {@link CharsetType}<br>
     *            <span style="color:green">null empty,?,?</span><br>
     *            ??,??,ie?chrome? url ,?
     * @return  <code>uriString</code> nullempty, {@link StringUtils#EMPTY}<br>
     *          <code>queryString</code> nullempty, <code>uriString</code>
     * @since 1.4.0
     */
    private static String retentionParamList(String uriString, String queryString, List<String> paramNameList,
            String charsetType) {
        if (isNullOrEmpty(uriString)) {
            return EMPTY;
        }
        if (isNullOrEmpty(queryString)) {
            return uriString; //??
        }
        Map<String, String[]> originalArrayValueMap = ParamUtil.toSafeArrayValueMap(queryString, null);
        Map<String, String[]> map = MapUtil.getSubMap(originalArrayValueMap,
                ConvertUtil.toArray(paramNameList, String.class));
        return ParamUtil.combineUrl(URIUtil.getFullPathWithoutQueryString(uriString), map, charsetType);
    }

    //*********************************************************************************
    /**
     * Sub.
     *
     * @param <K>
     *            the key type
     * @param <V>
     *            the value type
     * @param originalArrayValueMap
     *            the original array value map
     * @param paramNameList
     *            the param name list
     * @return the map< k, v>
     * @since 1.6.2
     */
    private static <K, V> Map<K, V> sub(Map<K, V> originalArrayValueMap, List<K> paramNameList) {
        Map<K, V> singleValueMap = new LinkedHashMap<K, V>();
        for (K paramName : paramNameList) {
            singleValueMap.put(paramName, originalArrayValueMap.get(paramName));
        }
        return singleValueMap;
    }
}