CXF 中自定義SOAPHeader

攔截器(Interceptor)簡單說明

      Interceptor是CXF架構中一個頗有特點的模式。你能夠在不對核心模塊進行修改的狀況下,動態添加不少功能。這對於CXF這個以處理消息爲中心的服務框架來講是很是有用的,CXF經過在Interceptor中對消息進行特殊處理,實現了不少重要功能模塊,例如:日誌記錄,Soap消息處理,消息的壓縮處。簡單的說,能夠在收到請求後,還未進行業務處理前,進行處理。或者在請求包發送前,進行報文的處理。

幾個的API的介紹

Interceptor

定義兩個方法,一個處理消息 handleMessage, 一個是處理錯誤 handleFault。

InterceptorChain

  單個的Interceptor功能有限,CXF要實現一個SOAP消息處理,須要將許許多多的Interceptor組合在一塊兒使用。所以設計了 InterceptorChain,在我看了InterceptorChain就像是一個Interceptor的小隊長。 小隊長有調配安置Interceptor的權力(add,remove),也有控制消息處理的權力(doInterceptor,pause,resume,reset,abort),同時也有交付錯誤處理的權力( {get|set}FaultObserver)。更有意思的是爲靈活控制Interceptor的處理消息順序(doInterceptStartingAt,doInterceptorStartingAfter),這也是InterceptorChain比較難理解的地方。
Fault
  定義了CXF中的錯誤消息。

InterceptorProvider

  這裏定義了Interceptor的後備保障部隊。咱們能夠在InterceptorProvider中設置In,Out,InFault,OutFault 後備小分隊,添加咱們所但願添加的Interceptor。而InterceptorChain會根據這些後備小分隊,組建本身的小分隊實例,完成具體的做戰功能任務。

AbstractAttributedInterceptorProvider

   InterceptorProvider實現的抽象類,因爲這個類來繼承了HashMap,咱們能夠像這個類中存儲一些屬性信息。

AbrstractBasicInterceptorProvider

   與AbstractAttributedInterceptorProvider不一樣,這個Interceptor只是簡單實現了InterceptorProvider的功能,並不提供對其屬性存儲的擴展。

Message

   因爲Interceptor是針對Message來進行處理的,當你打開Message這個類文件時,你會發如今Message中定義了不少常量,同時你還能夠從Message中獲取到不少與Message操做相關的信息。能夠獲取設置的對象有InterceptorChain Exchange Destination,還有獲取設置Content的泛型接口,是否是感受Message和Bus差很少,都成了大雜貨鋪,一切與消息處理相關的信息均可以放在Message中。 java

服務端攔截器

package hs.cxf.soapHeader;

import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.NodeList;

/**
 * 
 * @Title:獲取soap頭信息
 * 
 * @Description:
 * 
 * @Copyright:
 * 
 * @author zz
 * @version 1.00.000
 * 
 */
public class ReadSoapHeader extends AbstractPhaseInterceptor<SoapMessage> {

    private SAAJInInterceptor saa = new SAAJInInterceptor();

    public ReadSoapHeader() {
        super(Phase.PRE_PROTOCOL);
        getAfter().add(SAAJInInterceptor.class.getName());
    }

    public void handleMessage(SoapMessage message) throws Fault {

        SOAPMessage mess = message.getContent(SOAPMessage.class);
        if (mess == null) {
            saa.handleMessage(message);
            mess = message.getContent(SOAPMessage.class);
        }
        SOAPHeader head = null;
        try {
            head = mess.getSOAPHeader();
        } catch (SOAPException e) {
            e.printStackTrace();
        }
        if (head == null) {
            return;
        }
        try {
            //讀取自定義的節點
            NodeList nodes = head.getElementsByTagName("tns:spId");
            NodeList nodepass = head.getElementsByTagName("tns:spPassword");
            //獲取節點值,簡單認證
            if (nodes.item(0).getTextContent().equals("wdw")) {
                if (nodepass.item(0).getTextContent().equals("wdwsb")) {
                    System.out.println("認證成功");
                }
            } else {
                SOAPException soapExc = new SOAPException("認證錯誤");
                throw new Fault(soapExc);
            }

        } catch (Exception e) {
            SOAPException soapExc = new SOAPException("認證錯誤");
            throw new Fault(soapExc);
        }
    }

}

配置文件中新增攔截器配置

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xsi:schemaLocation="   
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">   
  
    <import resource="classpath:META-INF/cxf/cxf.xml" />   
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />   
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />   
  
    <bean id="jaxWsServiceFactoryBean"  
        class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">   
        <property name="wrapped" value="true" />   
        <property name="dataBinding" ref="aegisBean" />   
    </bean>   
  
    <bean id="aegisBean"  
        class="org.apache.cxf.aegis.databinding.AegisDatabinding" />   
  
    <jaxws:endpoint id="CollectiveServices"  
        implementor="hs.cxf.server.WebServiceSampleImpl" address="/HelloWorld">   
        <jaxws:inInterceptors>   
          <!-- 日誌攔截器 -->      
          <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>   
          <!-- 自定義攔截器 --> 
          <bean class="hs.cxf.soapHeader.ReadSoapHeader"/>   
          </jaxws:inInterceptors>    
        <jaxws:serviceFactory>   
            <ref bean="jaxWsServiceFactoryBean"/>   
        </jaxws:serviceFactory>   
    </jaxws:endpoint>   
</beans>
客戶端攔截器:

package hs.cxf.client.SoapHeader;


import java.util.List;
import javax.xml.namespace.QName;
import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
 * 
 * @Title:在發送消息前,封裝Soap Header 信息
 * 
 * @Description:
 * 
 * @Copyright: 
 *
 * @author zz
 * @version 1.00.000
 *
 */

public class AddSoapHeader extends AbstractSoapInterceptor {
      private static String nameURI="http://127.0.0.1:8080/cxfTest/ws/HelloWorld";   
      
        public AddSoapHeader(){   
            super(Phase.WRITE);   
        }   
        
        public void handleMessage(SoapMessage message) throws Fault {   
            String spPassword="wdwsb";   
            String spName="wdw";   
               
            QName qname=new QName("RequestSOAPHeader");   
            Document doc=DOMUtils.createDocument();   
            //自定義節點
            Element spId=doc.createElement("tns:spId");   
            spId.setTextContent(spName);   
            //自定義節點
            Element spPass=doc.createElement("tns:spPassword");   
            spPass.setTextContent(spPassword);   
               
            Element root=doc.createElementNS(nameURI, "tns:RequestSOAPHeader");   
            root.appendChild(spId);   
            root.appendChild(spPass);   
               
            SoapHeader head=new SoapHeader(qname,root);   
            List<Header> headers=message.getHeaders();   
            headers.add(head);   
            System.out.println(">>>>>添加header<<<<<<<");
        }   

}
客戶端調用時:

package hs.cxf.client;

import hs.cxf.client.SoapHeader.AddSoapHeader;
import java.util.ArrayList;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

/**
 * @Title:
 * 
 * @Description:
 * 
 * @Copyright: 
 * 
 * @author zz
 * @version 1.00.000
 * 
 */
public class TestClient {

    /**
     * 測試1
     */
    @SuppressWarnings("unchecked")
    public void testSend1() {
        try {
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

            ArrayList<Interceptor> list = new ArrayList<Interceptor>();
            // 添加soap header 
            list.add(new AddSoapHeader());
            // 添加soap消息日誌打印
            list.add(new org.apache.cxf.interceptor.LoggingOutInterceptor());
            factory.setOutInterceptors(list);
            factory.setServiceClass(WebServiceSample.class);
            factory.setAddress("http://127.0.0.1:8080/cxfTest/ws/HelloWorld");

            Object obj = factory.create();
            System.out.println(obj == null ? "NULL" : obj.getClass().getName());
            if (obj != null) {
                WebServiceSample ws = (WebServiceSample) obj;
                String str = ws.say("test");
                System.out.println(str);

                str = ws.say("1111");
                System.out.println(str);

                User u = new User();
                JAXBElement<String> je = new JAXBElement<String>(new QName(
                        "http://bean.cxf.hs", "name"), String.class, "張三");
                u.setName(je);
                str = ws.sayUserName(u);
                System.out.println(str);

                // 經過對象來交互
                ReqBean req = new ReqBean();
                req.setExp(new JAXBElement<String>(new QName(
                        "http://bean.cxf.hs", "exp"), String.class,
                        "<exp>111<exp>"));
                req.setSeqId(new JAXBElement<String>(new QName(
                        "http://bean.cxf.hs", "seqId"), String.class,
                        "12345678"));
                RespBean resp = ws.action(req);
                System.out.println("resp_id:" + resp.getRespId().getValue());
                System.out.println("resp_exp:" + resp.getExp().getValue());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 測試2
     */
    @SuppressWarnings("unchecked")
    public void testSend2() {
        String webServiceUrl = "http://127.0.0.1:8080/cxfTest/ws/HelloWorld";
        String webServiceConTimeout = "60000";
        String webServiceRevTimeout = "60000";
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

        ArrayList<Interceptor> list = new ArrayList<Interceptor>();
        // 添加soap header 信息
        list.add(new AddSoapHeader());
        // 添加soap消息日誌打印
        list.add(new org.apache.cxf.interceptor.LoggingOutInterceptor());
        factory.setOutInterceptors(list);
        factory.setServiceClass(WebServiceSample.class);
        factory.setAddress(webServiceUrl);
        WebServiceSample service = (WebServiceSample) factory.create();

        //超時時間設置
        Client clientP = ClientProxy.getClient(service);
        HTTPConduit http = (HTTPConduit) clientP.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(Integer
                .valueOf(webServiceConTimeout));
        httpClientPolicy.setReceiveTimeout(Integer
                .valueOf(webServiceRevTimeout));
        httpClientPolicy.setAllowChunking(false);
        http.setClient(httpClientPolicy);
        
    
        // 經過對象來交互
        ReqBean req = new ReqBean();
        req.setExp(new JAXBElement<String>(new QName(
                "http://bean.cxf.hs", "exp"), String.class,
                "<exp>111<exp>"));
        req.setSeqId(new JAXBElement<String>(new QName(
                "http://bean.cxf.hs", "seqId"), String.class,
                "12345678"));
        System.out.println(">>>>>>發送消息<<<<<<<<<");
        RespBean resp = service.action(req);
        System.out.println("resp_id:" + resp.getRespId().getValue());
        System.out.println("resp_exp:" + resp.getExp().getValue());

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        TestClient tc = new TestClient();
        tc.testSend1();
        System.out.println(">>>>>>>>>>>>2<<<<<<<<<<<<<");
        tc.testSend2();
        System.out.println(">>>>>>>>>>>>END<<<<<<<<<<<<<");
    }

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