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

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

import javax.servlet.http.HttpServletRequest;

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 transParamParamStr(HttpServletRequest request) {
		if(request.getParameterMap() != null) {
			StringBuilder paramStringBuilder = new StringBuilder();
			for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
				String value = ArrayUtils.isEmpty(entry.getValue()) ? null : entry.getValue()[0];
				paramStringBuilder.append('&').append(entry.getKey()).append('=').append(value);
			}
			return paramStringBuilder.toString().replaceFirst("&", "");
		}
		return "";
	}
	
	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 getIpAddress(HttpServletRequest request){
		String ip = request.getHeader("x-forwarded-for"); 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("Proxy-Client-IP"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("WL-Proxy-Client-IP"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("HTTP_CLIENT_IP"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getRemoteAddr(); 
	    } 
	    return ip; 
	}
	
	public static String genParamString(Map<String, String> paramMap) {
		if(paramMap==null || paramMap.isEmpty()){
			return "";
		}
		StringBuilder paramStringBuilder = new StringBuilder();
		for (Map.Entry<String, String> entry : paramMap.entrySet()) {
			String key = entry.getKey();
			paramStringBuilder.append('&').append(key).append('=').append(entry.getValue());
		}
		return paramStringBuilder.toString().replaceFirst("&", "");
	}
	
}