package com.lenovo.shop.filter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import com.lenovo.platform.security.exception.AuthenticateException;
import com.lenovo.platform.security.service.SecurityManage;
import com.lenovo.platform.security.service.StaffDetails;
/**
* 日志Filter
*
* @author pro
*
*/
public class LogFilter implements Filter {
Log log = LogFactoryImpl.getLog(LogFilter.class);
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
try {
HttpServletRequest request = (HttpServletRequest) servletRequest;
StaffDetails staffDetails = null;
try {
staffDetails = SecurityManage.getInstance().getCurrentUser();
} catch (AuthenticateException e) {
}
String requestType = request.getMethod();
String clazz = request.getServletPath();
String contentType = request.getContentType();
StringBuffer params = new StringBuffer("{");
String method = null;
if (clazz.contains(".do")) {
clazz = clazz.substring(1).substring(0, 1).toUpperCase() + clazz.substring(2);
clazz = clazz.substring(0, clazz.lastIndexOf(".")) + "Action.java";
}
if ("POST".equals(requestType)) {
if (contentType.contains("multipart/form-data")) {
params.append("此请求的请求类型是multipart/form-data");
}else if((request.getRequestURI().contains("/androidClient.do")) && ("actions=saveGoods".equals(request.getQueryString()) || "actions=updatGoods".equals(request.getQueryString()))){
params.append("android接口");
}else {
ServletInputStream inputStream = request.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = inputStream.read(b)) != -1) {
byteArrayOutputStream.write(b, 0, len);
}
byteArrayOutputStream.close();
inputStream.close();
if (byteArrayOutputStream.size() != 0) {
String p = byteArrayOutputStream.toString();
String[] ps = p.split("&");
for (String item : ps) {
if (!item.contains("actions")) {
params.append("(").append(URLDecoder.decode(item, "UTF-8")).append(")").append("||");
} else {
method = item;
}
}
}
}
}
for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
String key = e.nextElement();
String val = request.getParameter(key);//new String(request.getParameter(key).getBytes("ISO-8859-1"), "utf-8");
if (!"actions".equals(key)) {
params.append("(" + key + ":" + val + ")||");
} else {
method = val;
}
}
String ip = request.getRemoteAddr();
String agent = request.getHeader("User-Agent");
int len = params.length();
if (params.length() != 1)
params = params.delete(len - 2, len);
params.append("}");
System.err.println("操作用户名:" + staffDetails.getEmployeeName() + ";url:" + request.getRequestURI() + ";请求类型:" + requestType + ";请求类:" + clazz + ";请求方法:" + method + ";其他参数:" + params.toString()+" 客户端信息:{客户端IP:"+ip+";详细信息:"+agent+"}");
log.info("操作用户名:" + staffDetails.getEmployeeName() + ";url:" + request.getRequestURI() + ";请求类型:" + requestType + ";请求类:" + clazz + ";请求方法:" + method + ";其他参数:" + params.toString() + " 客户端信息:{客户端IP:"+ip+";详细信息:"+agent+"}");
} catch (Exception e) {
} finally {
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}