// Path: mtools/src/main/java/com/mtools/core/plugin/helper/CookieUtil.java
// public class CookieUtil  {
// 	private static final Log log = LogFactory.getLog(CookieUtil.class);
// 	
// 	/**
// 	 * 功能：获取原请求uri
// 	 * 2014-4-18
// 	 * @throws UnsupportedEncodingException 
// 	 */
// 	public static String initOrgReqUri(ServletRequest request, ServletResponse response) throws UnsupportedEncodingException{
// 		String orgUri="login";
// 		try{
// 			HttpServletRequest req=(HttpServletRequest) request;
// 			HttpServletResponse resp=(HttpServletResponse) response;
// 			String method=req.getMethod();
// 			String queryStr=req.getQueryString();
// 			String reqestUri=req.getServletPath();//.getRequestURI();
// 			if(!"/admin/index".equals(reqestUri)&&!"/index".equals(reqestUri)){
// 				if("GET".equals(method)&&queryStr!=null){
// 					reqestUri+="?"+queryStr;
// 				}
// 				orgUri=  CookieUtil.getCookie(req, CoreConstans.REQESTURI);
// 				log.info("原请求uri:"+reqestUri);
// 				CookieUtil.setCookie(resp, CoreConstans.REQESTURI, reqestUri);
// 			}
// 		}catch(Exception e){
// 			e.printStackTrace();
// 			log.error("初始化cookie异常:"+e.getMessage());
// 		}
// 		return orgUri;
// 	}
// 	
// 	
// 	public static void setCookie(HttpServletResponse response,String key,String value) throws UnsupportedEncodingException{
// 		Cookie oItem;  
// 		oItem = new Cookie(key,Base64.encodeBase64URLSafeString(value.getBytes()));  
// 		oItem.setMaxAge(-1); //关闭浏览器后，cookie立即失效          
// 	    oItem.setPath("/");  
// 	    response.addCookie(oItem);
// 	}
// 	
// 	public static String getCookie(HttpServletRequest request,String key) throws UnsupportedEncodingException{
// 		final Cookie[] oCookies = request.getCookies();  
// 	    if (oCookies != null)  
// 	    {  
// 	        for (final Cookie oItem : oCookies)  
// 	        {  
// 	            final String sName = oItem.getName();  
// 	  
// 	            if (sName.equals(key))  
// 	            {  
// 	            	String value=new String(Base64.decodeBase64(oItem.getValue()));
// 	            	log.info("cookies key:"+key+" value:"+value);
// 	            	return value; //解码 CookieUtil.decode()是BASE64解码方法,略..  
// 	            }  
// 	        }  
// 	    }  
// 	    return null;  
// 	}
// }
// 
// Path: mtools/src/main/java/com/mtools/core/plugin/helper/JsonParse.java
// public class JsonParse  {
// 	
// 	public static Object fromObject(String jsonString,Class<?> c) {
// 		 
// 		try {
// 			Object obj=JSON.parseObject(jsonString,c);
// 			return obj;
// 		} catch (Exception ex) {
// 			System.out.print(ex.getMessage());
// 		} finally {
// 		}
// 		return null;
// 	}
// 	
// 	public static String obj2json(Object obj){
// 		String jsonString = JSON.toJSONString(obj);
// 		return jsonString;
// 	}
// 
// }


import java.io.IOException;
import java.net.SocketException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.task.TaskExecutor;
import com.mtools.core.plugin.constant.CoreConstans;
import com.mtools.core.plugin.entity.RespMsg;
import com.mtools.core.plugin.helper.AIPGException;
import com.mtools.core.plugin.helper.CookieUtil;
import com.mtools.core.plugin.helper.FuncUtil;
import com.mtools.core.plugin.helper.JsonParse;
import com.mtools.core.plugin.helper.SpringUtil;
import com.mtools.core.plugin.notify.AsyncNotify;
				if(notify==null)
					notify= (AsyncNotify) SpringUtil.getAnoBean("sysRunningNotify");
				notify.initData(request, e,SpringUtil.getApplicationContext().getApplicationName());
				if(taskExecutor==null)
					taskExecutor=(TaskExecutor) SpringUtil.getAnoBean("taskExecutor");
				taskExecutor.execute(notify);
			}
			//发生异常时，记住当前页面
			if(!errorCode.equals(CoreConstans.EXCEPTON_03)){
				CookieUtil.initOrgReqUri(request, response);
			}

			if(isAjaxRequest((HttpServletRequest)request)){
				respmsg.setMessage(errorMsg);
				request.setAttribute("jsonString",JsonParse.obj2json(respmsg));
				RequestDispatcher dispathcher = request.getRequestDispatcher(ajaxUrl);
				dispathcher.forward(request, response);
			}else{
				request.setAttribute(CoreConstans.ERROR_MESSAGE, errorMsg);
				RequestDispatcher dispathcher = request.getRequestDispatcher(errorUrl);
				dispathcher.forward(request, response);
			}
		}
	}
	
	private boolean isAjaxRequest(HttpServletRequest request) {   
        String header = request.getHeader("X-Requested-With");   
        if (header != null && "XMLHttpRequest".equals(header))   
            return true;   
        else  
// Path: mtools/src/main/java/com/mtools/core/plugin/helper/CookieUtil.java
// public class CookieUtil  {
// 	private static final Log log = LogFactory.getLog(CookieUtil.class);
// 	
// 	/**
// 	 * 功能：获取原请求uri
// 	 * 2014-4-18
// 	 * @throws UnsupportedEncodingException 
// 	 */
// 	public static String initOrgReqUri(ServletRequest request, ServletResponse response) throws UnsupportedEncodingException{
// 		String orgUri="login";
// 		try{
// 			HttpServletRequest req=(HttpServletRequest) request;
// 			HttpServletResponse resp=(HttpServletResponse) response;
// 			String method=req.getMethod();
// 			String queryStr=req.getQueryString();
// 			String reqestUri=req.getServletPath();//.getRequestURI();
// 			if(!"/admin/index".equals(reqestUri)&&!"/index".equals(reqestUri)){
// 				if("GET".equals(method)&&queryStr!=null){
// 					reqestUri+="?"+queryStr;
// 				}
// 				orgUri=  CookieUtil.getCookie(req, CoreConstans.REQESTURI);
// 				log.info("原请求uri:"+reqestUri);
// 				CookieUtil.setCookie(resp, CoreConstans.REQESTURI, reqestUri);
// 			}
// 		}catch(Exception e){
// 			e.printStackTrace();
// 			log.error("初始化cookie异常:"+e.getMessage());
// 		}
// 		return orgUri;
// 	}
// 	
// 	
// 	public static void setCookie(HttpServletResponse response,String key,String value) throws UnsupportedEncodingException{
// 		Cookie oItem;  
// 		oItem = new Cookie(key,Base64.encodeBase64URLSafeString(value.getBytes()));  
// 		oItem.setMaxAge(-1); //关闭浏览器后，cookie立即失效          
// 	    oItem.setPath("/");  
// 	    response.addCookie(oItem);
// 	}
// 	
// 	public static String getCookie(HttpServletRequest request,String key) throws UnsupportedEncodingException{
// 		final Cookie[] oCookies = request.getCookies();  
// 	    if (oCookies != null)  
// 	    {  
// 	        for (final Cookie oItem : oCookies)  
// 	        {  
// 	            final String sName = oItem.getName();  
// 	  
// 	            if (sName.equals(key))  
// 	            {  
// 	            	String value=new String(Base64.decodeBase64(oItem.getValue()));
// 	            	log.info("cookies key:"+key+" value:"+value);
// 	            	return value; //解码 CookieUtil.decode()是BASE64解码方法,略..  
// 	            }  
// 	        }  
// 	    }  
// 	    return null;  
// 	}
// }
// 
// Path: mtools/src/main/java/com/mtools/core/plugin/helper/JsonParse.java
// public class JsonParse  {
// 	
// 	public static Object fromObject(String jsonString,Class<?> c) {
// 		 
// 		try {
// 			Object obj=JSON.parseObject(jsonString,c);
// 			return obj;
// 		} catch (Exception ex) {
// 			System.out.print(ex.getMessage());
// 		} finally {
// 		}
// 		return null;
// 	}
// 	
// 	public static String obj2json(Object obj){
// 		String jsonString = JSON.toJSONString(obj);
// 		return jsonString;
// 	}
// 
// }


// Path: mtools/src/main/java/com/mtools/core/plugin/web/filter/ExceptionFilter.java
import java.io.IOException;
import java.net.SocketException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.task.TaskExecutor;
import com.mtools.core.plugin.constant.CoreConstans;
import com.mtools.core.plugin.entity.RespMsg;
import com.mtools.core.plugin.helper.AIPGException;
import com.mtools.core.plugin.helper.CookieUtil;
import com.mtools.core.plugin.helper.FuncUtil;
import com.mtools.core.plugin.helper.JsonParse;
import com.mtools.core.plugin.helper.SpringUtil;
import com.mtools.core.plugin.notify.AsyncNotify;

				if(notify==null)
					notify= (AsyncNotify) SpringUtil.getAnoBean("sysRunningNotify");
				notify.initData(request, e,SpringUtil.getApplicationContext().getApplicationName());
				if(taskExecutor==null)
					taskExecutor=(TaskExecutor) SpringUtil.getAnoBean("taskExecutor");
				taskExecutor.execute(notify);
			}
			//发生异常时，记住当前页面
			if(!errorCode.equals(CoreConstans.EXCEPTON_03)){
				CookieUtil.initOrgReqUri(request, response);
			}

			if(isAjaxRequest((HttpServletRequest)request)){
				respmsg.setMessage(errorMsg);
				request.setAttribute("jsonString",JsonParse.obj2json(respmsg));
				RequestDispatcher dispathcher = request.getRequestDispatcher(ajaxUrl);
				dispathcher.forward(request, response);
			}else{
				request.setAttribute(CoreConstans.ERROR_MESSAGE, errorMsg);
				RequestDispatcher dispathcher = request.getRequestDispatcher(errorUrl);
				dispathcher.forward(request, response);
			}
		}
	}
	
	private boolean isAjaxRequest(HttpServletRequest request) {   
        String header = request.getHeader("X-Requested-With");   
        if (header != null && "XMLHttpRequest".equals(header))   
            return true;   
        else  
            return false;   