自定义cxf 拦截器,解决cxf误拦截js/css

1、第一种方法,在配置cxf时,前namespace前缀
如:
  
     < servlet >
         < servlet-name >CXFServlet</ servlet-name >
         < servlet-class >
           org.apache.cxf.transport.servlet.CXFServlet
         </ servlet-class >
         < load-on-startup >1</ load-on-startup >
     </ servlet >
 
     < servlet-mapping >
         < servlet-name >CXFServlet</ servlet-name >
       <!-- <url-pattern>/*</url-pattern> --> 这样配置会拦截掉js等文件,当然struts .action也会被拦截
           < url-pattern >/webservice</ url-pattern > -->
     </ servlet-mapping >
 
2、第二种:如果因为特殊业务要求,不能将namespace访问即需要访问这种的如:http://ioms.360buy.com/Service.asmx?wsdl。那么可以通过自定义拦截器来实现。
拦截器如下:
package mycxf.inteceptor;
 
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
 
import org.apache.commons.io.IOUtils;
import org.apache.cxf.transport.servlet.CXFServlet;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
 
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
public class MyCXFServlet extends CXFServlet {
 
     /**
      *
      */
     private static final long serialVersionUID = 1L;
     private static final Map< String , String> STATIC_CONTENT_TYPES;
 
     static {
         STATIC_CONTENT_TYPES = new HashMap< String , String>();
         STATIC_CONTENT_TYPES.put("html", "text/html");
         STATIC_CONTENT_TYPES.put("txt", "text/plain");
         STATIC_CONTENT_TYPES.put("css", "text/css");
         STATIC_CONTENT_TYPES.put("js", "text/javascript");
         STATIC_CONTENT_TYPES.put("pdf", "application/pdf");
         STATIC_CONTENT_TYPES.put("png", "image/png");
 
         // TODO : add more types if needed
     }
 
     public void service(ServletRequest req, ServletResponse res)
             throws ServletException, IOException {
 
         String url = ((HttpServletRequest)req).getRequestURI();
         if (isSpecialFile(url)) {
             serveStaticContent((HttpServletRequest) req, (HttpServletResponse) res, ((HttpServletRequest)req).getPathInfo());
         } else {
             super.service(req, res);
         }
 
     }
     public boolean isSpecialFile(String url){
 
          for(String key:STATIC_CONTENT_TYPES.keySet()){
              if(url.contains("."+key)){
                 return  true;
              }
          }
         return false;
     }
 
     public void serveStaticContent(HttpServletRequest request,
                                    HttpServletResponse response, String pathInfo)
             throws ServletException {
         InputStream is = super.getServletContext()
                 .getResourceAsStream(pathInfo);
         if (is == null) {
             throw new ServletException("Static resource " + pathInfo
                     + " is not available");
         }
         try {
             int ind = pathInfo.lastIndexOf(".");
             if (ind != -1 && ind < pathInfo.length ()) {
                 String type = STATIC_CONTENT_TYPES .get(pathInfo.substring(ind + 1));
                 if (type != null) {
                     response.setContentType(type);
                 }
             }
 
             ServletOutputStream os = response .getOutputStream();
             IOUtils.copy(is, os);
             os.flush();
         } catch (IOException ex) {
             throw new ServletException("Static resource " + pathInfo
                     + " can not be written to the output stream");
         }
 
     }
 
}
然后在web.xml里配置cxf拦截器:
<servlet>
         < servlet-name >CXFServlet</ servlet-name >
         < servlet-class >
             mycxf.inteceptor.MyCXFServlet
         </ servlet-class >
         < load-on-startup >1</ load-on-startup >
     </ servlet >
 
     < servlet-mapping >
         < servlet-name >CXFServlet</ servlet-name >
         < url-pattern >/*</ url-pattern >
     </ servlet-mapping >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值