码迷,mamicode.com
首页 > Web开发 > 详细

Apache CXF自定义拦截器

时间:2016-03-01 00:45:21      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

为什么设计拦截器?
1.为了在webservice请求过程中,能动态操作请求和响应数据,CXF设计了拦截器
拦截器分类:
1.按所处的位置分:服务器端拦截器,客户端拦截器。
2.按消息的方向分:入拦截器,出拦截器。
3.按定义者分:系统拦截器,自定义拦截器。

 

客户端添加日志拦截器

package com.client.interceptor;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class AddUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

    private String name;
    private String password;
    /**
     * 
     * @param name
     * @param password
     */
    
    public AddUserInterceptor(String name,String password) {
        super(Phase.PRE_PROTOCOL);//准备协议化时拦截
        // TODO Auto-generated constructor stub
        this.name=name;
        this.password=password;
    }

    @Override
    public void handleMessage(SoapMessage msg) throws Fault {
        // TODO Auto-generated method stub
        List<Header> headers = msg.getHeaders();
        
        Document document = DOMUtils.createDocument();
        Element rootEle = document.createElement("apple");
        Element nameEle = document.createElement("name");
        nameEle.setTextContent(name);
        rootEle.appendChild(nameEle);
        
        Element passwordEle = document.createElement("password");
        passwordEle.setTextContent(password);
        rootEle.appendChild(passwordEle);
        
        headers.add(new Header(new QName("apple"), rootEle));
        System.out.println("client handMwssage()....");
    }

}

 

 

服务器端拦截器:

package com.service.interceptor;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;

public class CheckUser extends AbstractPhaseInterceptor<SoapMessage>{

    public CheckUser() {
        super(Phase.PRE_PROTOCOL);
        // TODO Auto-generated constructor stub
    }   
    
    /*
    <Envelope>
        <head>
            <apple>
                <name>bo ram</name>
                <password>520</password>
            <apple>
        </head>
        <Body>
            <favorite>
                <arg0>Tara</arg0>
            <favorite>
        </Body>
    </Envelope>    
     */
    
    
    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        // TODO Auto-generated method stub
        Header header = message.getHeader(new QName("apple"));
        if(header!=null){
            Element appleEle=(Element) header.getObject();
            String name = appleEle.getElementsByTagName("name").item(0).getTextContent();
            String password = appleEle.getElementsByTagName("password").item(0).getTextContent();
            if("bo ram".equals(name)&&"520".equals(password)){
                System.out.println("Server 通过拦截器");
                return;
            }
        }
        
        System.out.println("server 没有通过拦截器。。。。。");
        throw new Fault(new Exception("请求需要一个正确的用户名和密码"));
    }

}

 

客户端

package com.cxf_client.client;

import java.util.List;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.message.Message;

import com.client.interceptor.AddUserInterceptor;
import com.client.simple3.Simple;
import com.client.simple3.SimpleimplService;

public class Simpleclient2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
            SimpleimplService simpleimplService = new SimpleimplService();
             Simple simpleimplPort = simpleimplService.getSimpleimplPort();
             //发送请求的客户端数据
             Client client = ClientProxy.getClient(simpleimplPort);
             //客户端出拦截器
             List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
             outInterceptors.add(new AddUserInterceptor("bo ram", "520"));
//             //客户端入拦截器
//             List<Interceptor<? extends Message>> inInterceptors = client.getInInterceptors();
//             inInterceptors.add(new LoggingInInterceptor());
             
             String favorite = simpleimplPort.favorite("Tara");
             System.out.println(favorite);
            
    }

}

 

 

 服务端

package com.service.server;

import java.util.List;

import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws22.EndpointImpl;
import org.apache.cxf.message.Message;

import com.service.impl.Simpleimpl;
import com.service.interceptor.CheckUser;


public class SimpleServer2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String address="http://localhost:8848/simpleserver/simple";
            Endpoint publish = Endpoint.publish(address, new Simpleimpl());
            EndpointImpl endpointimpl=(EndpointImpl) publish;
            //服务端入拦截器
            List<Interceptor<? extends Message>> inInterceptors = endpointimpl.getInInterceptors();
            inInterceptors.add(new CheckUser());
//            //服务端出拦截器
//            List<Interceptor<? extends Message>> outInterceptors = endpointimpl.getOutInterceptors();
//            outInterceptors.add(new LoggingOutInterceptor());
            
            System.out.println("published");
    }

}

 

Apache CXF自定义拦截器

标签:

原文地址:http://www.cnblogs.com/ChrisMurphy/p/5229425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!