分享

处理get请求时编码的拦截器

 yetao_study 2015-12-17
  1. <struts>  
  2.     <package name="entity.workspaceManager" extends="json-default" namespace="/entity/manager">  
  3.         <interceptors>  
  4.             <interceptor name="urlDecodeFilterInterceptor"  
  5.                 class="com.whaty.platform.sso.web.interceptor.UrlDecodeFilterInterceptor">  
  6.             </interceptor>  
  7.               
  8.             <interceptor-stack name="urlDecodeStack">  
  9.                 <interceptor-ref name="urlDecodeFilterInterceptor"></interceptor-ref>  
  10.                 <interceptor-ref name="defaultStack"></interceptor-ref>  
  11.             </interceptor-stack>  
  12.               
  13.         </interceptors>  
  14.           
  15.         <default-interceptor-ref name="interceptorStack"></default-interceptor-ref>  
  16.         <global-results>  


具体实现:

  1. package com.whaty.platform.sso.web.interceptor;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4. import java.net.URLDecoder;  
  5. import java.util.Iterator;  
  6. import java.util.Map;  
  7.   
  8. import javax.servlet.http.HttpServletRequest;  
  9.   
  10. import org.apache.commons.logging.Log;  
  11. import org.apache.commons.logging.LogFactory;  
  12. import org.apache.struts2.ServletActionContext;  
  13.   
  14. import com.opensymphony.xwork2.ActionInvocation;  
  15. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  16.    
  17. public class UrlDecodeFilterInterceptor extends AbstractInterceptor {  
  18.     private static final long serialVersionUID = -2335290125344040914L;  
  19.     private static final Log LOG = LogFactory.getLog(UrlDecodeFilterInterceptor.class);  
  20.   
  21.     public String intercept(ActionInvocation invocation) throws Exception {  
  22.         Map parameters = invocation.getInvocationContext().getParameters();  
  23.         HttpServletRequest request = ServletActionContext. getRequest();   
  24.           
  25.         if("get".equalsIgnoreCase(request.getMethod())){  
  26.             for (Iterator i = parameters.keySet().iterator(); i.hasNext();) {  
  27.                 String param = (String) i.next();  
  28.                 String[] values = (String[])parameters.get(param);  
  29.                 if(values != null && values.length > 0){  
  30.                     for (int j = 0; j < values.length; j++) {  
  31.                         if(values[j] != null && !"".equals(values[j])){  
  32.                             try {  
  33.                                 values[j] = new String(values[j].getBytes("iso8859-1"),"UTF-8");  
  34.                                 parameters.put(param, values[j]);  
  35.                             } catch (UnsupportedEncodingException e1) {  
  36.                                 e1.printStackTrace();  
  37.                             }   
  38.                         }  
  39.                     }  
  40.                 }  
  41.             }  
  42.         }  
  43.           
  44.         if("application/x-www-form-urlencoded;charset=UTF-8".equals(request.getContentType())){  
  45.             for (Iterator i = parameters.keySet().iterator(); i.hasNext();) {  
  46.                 String param = (String) i.next();  
  47.                 String[] values = (String[])parameters.get(param);  
  48.                 if(values != null && values.length > 0){  
  49.                     for (int j = 0; j < values.length; j++) {  
  50.                         if(values[j] != null && !"".equals(values[j])){  
  51.                             try {  
  52.                                 values[j] = URLDecoder.decode(values[j], "UTF-8");  
  53.                                 parameters.put(param, values[j]);  
  54.                             } catch (UnsupportedEncodingException e1) {  
  55.                                 e1.printStackTrace();  
  56.                             }   
  57.                         }  
  58.                     }  
  59.                 }  
  60.             }  
  61.         }         
  62.   
  63.         return invocation.invoke();  
  64.     }  
  65. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多