程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Apache CXF自定義攔截器,apachecxf

Apache CXF自定義攔截器,apachecxf

編輯:JAVA綜合教程

Apache CXF自定義攔截器,apachecxf


為什麼設計攔截器?
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");
    }

}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved