package com.bcxin.ars.rest.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.bcxin.ars.context.SecurityContext;
import com.bcxin.ars.rest.util.ArsRestUtil;
import com.bcxin.ars.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ArsInterceptor implements HandlerInterceptor  {
	/***
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ArsInterceptor.class);
	@Autowired
	private ArsRestUtil arsUtil;
	
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
//		arsUtil.clearSecurityContext();
//		String sid = request.getParameter(Constants.SESSION_ID);
//		if(sid != null && sid.trim().length() > 0) {
//			Object o = arsUtil.getCurrentUser(request, sid);
//			if(o != null) {
//				arsUtil.setCurrentUser(request, (User)o);
//				SecurityContext.currentNative.set(Long.valueOf((String)request.getServletContext().getAttribute(Constants.CURRENT_NATIVE)));
//				return true;
//			}
//		}
		logger.error("request url:"+request.getRequestURL().toString());
		logger.error("request Params:"+JSONObject.toJSONString(request.getParameterMap()));
		SecurityContext.currentNative.set(Long.valueOf((String)request.getServletContext().getAttribute(Constants.CURRENT_NATIVE)));
		return true;
//		String url = request.getRequestURL().toString();
//		if(url.indexOf("login") >= 0 || url.indexOf("forgetPwd") >= 0
//				|| url.indexOf("getResource.do") >= 0 || url.indexOf("uploadFile") >= 0 
//				|| url.endsWith("updatePwd.json") || url.endsWith("verifyMessageCode.json")
//				|| url.indexOf("validateLogin") >= 0 || url.indexOf("getGuidePage") >= 0
//				|| url.indexOf("getAdPage") >= 0 || url.indexOf("getAndroidVersion") >= 0) {
//			return true;
//		}
//		response.getWriter().write("{\"successful\":false,\"code\":\"0000\",\"msg\":\"未找到登录信息。\"}");
//		response.getWriter().flush();
//		return false;
	}
	
}
