HttpServletRequestUtils.java 3.04 KB
package com.yoho.search.common.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ArrayUtils;

public class HttpServletRequestUtils {

	public static Map<String, String> transParamType(HttpServletRequest request) {
		Map<String, String> resultMap = new HashMap<>();
		if (request.getParameterMap() != null) {
			for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
				String value = ArrayUtils.isEmpty(entry.getValue()) ? null : entry.getValue()[0];
				resultMap.put(entry.getKey(), value);
			}
		}
		return resultMap;
	}

	public static String getRequestUrl(HttpServletRequest request) {
		StringBuffer sb = new StringBuffer();
		sb.append(request.getRequestURI());
		Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
		if (paramMap != null && !paramMap.isEmpty()) {
			sb.append('?');
			for (Map.Entry<String, String> entry : paramMap.entrySet()) {
				sb.append(entry.getKey()).append('=').append(entry.getValue()).append('&');
			}
			sb.deleteCharAt(sb.length() - 1);
		}
		return sb.toString();
	}

	public static String genParamString(Map<?, ?> paramMap) {
		if (paramMap == null || paramMap.isEmpty()) {
			return "";
		}
		StringBuilder paramStringBuilder = new StringBuilder();
		for (Map.Entry<?, ?> entry : paramMap.entrySet()) {
			String key = String.valueOf(entry.getKey());
			String value = String.valueOf(entry.getValue());
			if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
				continue;
			}
			paramStringBuilder.append('&').append(key).append('=').append(value);
		}
		return paramStringBuilder.toString().replaceFirst("&", "");
	}

	public static String genParamStringWithIncludeParams(Map<?, ?> paramMap, List<String> includeParams) {
		if (paramMap == null || paramMap.isEmpty()) {
			return "";
		}
		StringBuilder paramStringBuilder = new StringBuilder();
		for (Map.Entry<?, ?> entry : paramMap.entrySet()) {
			String key = String.valueOf(entry.getKey());
			String value = String.valueOf(entry.getValue());
			if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
				continue;
			}
			if (!includeParams.contains(key)) {
				continue;
			}
			paramStringBuilder.append('&').append(key).append('=').append(value);
		}
		return paramStringBuilder.toString().replaceFirst("&", "");
	}

	public static String genParamStringWithExcludeParams(Map<?, ?> paramMap, List<String> excludeParams) {
		if (paramMap == null || paramMap.isEmpty()) {
			return "";
		}
		StringBuilder paramStringBuilder = new StringBuilder();
		for (Map.Entry<?, ?> entry : paramMap.entrySet()) {
			String key = String.valueOf(entry.getKey());
			String value = String.valueOf(entry.getValue());
			if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
				continue;
			}
			if (excludeParams.contains(key)) {
				continue;
			}
			paramStringBuilder.append('&').append(key).append('=').append(value);
		}
		return paramStringBuilder.toString().replaceFirst("&", "");
	}

}