Apache CXF自定義攔截器

爲何設計攔截器?
1.爲了在webservice請求過程當中,能動態操做請求和響應數據,CXF設計了攔截器
攔截器分類:
1.按所處的位置分:服務器端攔截器,客戶端攔截器。
2.按消息的方向分:入攔截器,出攔截器。
3.按定義者分:系統攔截器,自定義攔截器。java

 

客戶端添加日誌攔截器web

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()....");
    }

}

 

 

服務器端攔截器:apache

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);
            
    }

}

 

 

 服務端app

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");
    }

}
相關文章
相關標籤/搜索