<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    胖熊熊

    得到多少又失去了什么

    CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header

    基于CXF2.0前2個(gè)學(xué)習(xí)筆記,對原先服務(wù)端與客戶端進(jìn)行修改,實(shí)現(xiàn)在SOAP Header里面添加自定義的數(shù)據(jù)進(jìn)行認(rèn)證

    在做之前,先要理解如下的信息
    攔截器(Interceptor)簡單說明
          Interceptor是CXF架構(gòu)中一個(gè)很有特色的模式。你可以在不對核心模塊進(jìn)行修改的情況下,動(dòng)態(tài)添加很多功能。這對于CXF這個(gè)以處理消息為中心的服務(wù)框架來說是非常有用的,CXF通過在Interceptor中對消息進(jìn)行特殊處理,實(shí)現(xiàn)了很多重要功能模塊,例如:日志記錄,Soap消息處理,消息的壓縮處。簡單的說,可以在收到請求后,還未進(jìn)行業(yè)務(wù)處理前,進(jìn)行處理。或者在請求包發(fā)送前,進(jìn)行報(bào)文的處理。
    幾個(gè)的API的介紹
    Interceptor

    定義兩個(gè)方法,一個(gè)處理消息 handleMessage, 一個(gè)是處理錯(cuò)誤 handleFault。

    InterceptorChain
      單個(gè)的Interceptor功能有限,CXF要實(shí)現(xiàn)一個(gè)SOAP消息處理,需要將許許多多的Interceptor組合在一起使用。因此設(shè)計(jì)了 InterceptorChain,在我看了InterceptorChain就像是一個(gè)Interceptor的小隊(duì)長。 小隊(duì)長有調(diào)配安置Interceptor的權(quán)力(add,remove),也有控制消息處理的權(quán)力(doInterceptor,pause,resume,reset,abort),同時(shí)也有交付錯(cuò)誤處理的權(quán)力( {get|set}FaultObserver)。更有意思的是為靈活控制Interceptor的處理消息順序(doInterceptStartingAt,doInterceptorStartingAfter),這也是InterceptorChain比較難理解的地方。
    Fault
      定義了CXF中的錯(cuò)誤消息。
    InterceptorProvider
      這里定義了Interceptor的后備保障部隊(duì)。我們可以在InterceptorProvider中設(shè)置In,Out,InFault,OutFault 后備小分隊(duì),添加我們所希望添加的Interceptor。而InterceptorChain會根據(jù)這些后備小分隊(duì),組建自己的小分隊(duì)實(shí)例,完成具體的作戰(zhàn)功能任務(wù)。
    AbstractAttributedInterceptorProvider
       InterceptorProvider實(shí)現(xiàn)的抽象類,由于這個(gè)類來繼承了HashMap,我們可以像這個(gè)類中存儲一些屬性信息。
    AbstractBasicInterceptorProvider
       與AbstractAttributedInterceptorProvider不同,這個(gè)Interceptor只是簡單實(shí)現(xiàn)了InterceptorProvider的功能,并不提供對其屬性存儲的擴(kuò)展。
    Message
       由于Interceptor是針對Message來進(jìn)行處理的,當(dāng)你打開Message這個(gè)類文件時(shí),你會發(fā)現(xiàn)在Message中定義了很多常量,同時(shí)你還可以從Message中獲取到很多與Message操作相關(guān)的信息。可以獲取設(shè)置的對象有InterceptorChain Exchange Destination,還有獲取設(shè)置Content的泛型接口,是不是感覺Message和Bus差不多,都成了大雜貨鋪,一切與消息處理相關(guān)的信息都可以放在Message中。

    理解了Interceptor功能,下面的修改就很簡單了


    服務(wù)端修改

    1.新建一個(gè)攔截器(Interceptor)

    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 {
                
    //讀取自定義的節(jié)點(diǎn)
                NodeList nodes = head.getElementsByTagName("tns:spId");
                NodeList nodepass 
    = head.getElementsByTagName("tns:spPassword");
                
    //獲取節(jié)點(diǎn)值,簡單認(rèn)證
                if (nodes.item(0).getTextContent().equals("wdw")) {
                    
    if (nodepass.item(0).getTextContent().equals("wdwsb")) {
                        System.out.println(
    "認(rèn)證成功");
                    }

                }
     else {
                    SOAPException soapExc 
    = new SOAPException("認(rèn)證錯(cuò)誤");
                    
    throw new Fault(soapExc);
                }


            }
     catch (Exception e) {
                SOAPException soapExc 
    = new SOAPException("認(rèn)證錯(cuò)誤");
                
    throw new Fault(soapExc);
            }

        }


    }

    2.配置文件中新增攔截器配置
    <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>  

    服務(wù)端的配置就告一段落了,接下來是客戶端的修改
    客戶端
    1.同樣新增一個(gè)Interceptor
    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:在發(fā)送消息前,封裝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();   
                
    //自定義節(jié)點(diǎn)
                Element spId=doc.createElement("tns:spId");   
                spId.setTextContent(spName);   
                
    //自定義節(jié)點(diǎn)
                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<<<<<<<");
            }
       

    }


    2.客戶端調(diào)用程序修改
    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();

            
    //超時(shí)時(shí)間設(shè)置
            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(
    ">>>>>>發(fā)送消息<<<<<<<<<");
            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<<<<<<<<<<<<<");
        }


    }

    到這里就結(jié)束了,可以進(jìn)行測試了

    posted on 2011-04-19 14:23 fatbear 閱讀(8956) 評論(16)  編輯  收藏 所屬分類: web service

    評論

    # re: CXF2.0學(xué)習(xí)筆記-3 SOAP Header 2011-04-21 19:01 akana

    hello,我想問一下你的ReadSoapHeader這個(gè)服務(wù)端讀取soap header實(shí)現(xiàn)成功了嗎?我這里用這種方式總是出錯(cuò): head = mess.getSOAPHeader();出現(xiàn)java.lang.NullPointerException錯(cuò)誤。。  回復(fù)  更多評論   

    # re: CXF2.0學(xué)習(xí)筆記-3 SOAP Header 2011-04-21 19:02 akana

    另外問一下你學(xué)習(xí)CXF2.0的資料有哪些呢?可以推薦一下嗎?  回復(fù)  更多評論   

    # re: CXF2.0學(xué)習(xí)筆記-3 SOAP Header 2011-04-22 11:55 akana

    @akana
    找到錯(cuò)誤了。我的ReadSoapHeader引用了
    import org.apache.xmlbeans.impl.soap.SOAPException;
    import org.apache.xmlbeans.impl.soap.SOAPHeader;
    import org.apache.xmlbeans.impl.soap.SOAPMessage;
    而應(yīng)該是
    import javax.xml.soap.SOAPException;
    import javax.xml.soap.SOAPHeader;
    import javax.xml.soap.SOAPMessage;  回復(fù)  更多評論   

    # re: CXF2.0學(xué)習(xí)筆記-3 SOAP Header 2011-04-22 21:53 fatbear

    @akana
    呵呵,我也是看官方文檔與網(wǎng)絡(luò)上的資料后,自己寫的,一般都有經(jīng)過測試的  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header 2011-08-31 11:08

    您好,ReadSoapHeader這個(gè)服務(wù)端讀取soap header時(shí),mess這個(gè)一直都是空的,這是怎么回事呀?  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header 2012-07-12 15:27 林子

    factory.setOutInterceptors(list);
    factory.setServiceClass(WebServiceSample.class);
    factory.setAddress("http://127.0.0.1:8080/cxfTest/ws/HelloWorld");

    類org.apache.cxf.jaxws.JaxWsProxyFactoryBean 沒有以上3個(gè)方法,引用的路徑完全一樣。。  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header 2012-07-12 16:45 林子

    @林子
    我找到原因了,那是因?yàn)檫@3個(gè)方法時(shí)父類的方法而我沒導(dǎo)入他父類的jar包,但是又拋了一個(gè)新異常:
    Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Could not find conduit initiator for transport http://schemas.xmlsoap.org/soap/http
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:146)
    at $Proxy25.sendSms(Unknown Source)
    at Test.main(Test.java:48)
    Caused by: java.lang.RuntimeException: Could not find conduit initiator for transport http://schemas.xmlsoap.org/soap/http
    at org.apache.cxf.binding.soap.SoapTransportFactory.getConduit(SoapTransportFactory.java:230)
    at org.apache.cxf.endpoint.AbstractConduitSelector.getSelectedConduit(AbstractConduitSelector.java:81)
    at org.apache.cxf.endpoint.UpfrontConduitSelector.prepare(UpfrontConduitSelector.java:61)
    at org.apache.cxf.endpoint.ClientImpl.prepareConduitSelector(ClientImpl.java:809)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:505)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:440)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:343)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:295)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:124)
    ... 2 more
      回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header 2012-07-26 08:45 fatbear

    @林子
    現(xiàn)在開發(fā)cxf已經(jīng)很簡單了,eclipse3.7已經(jīng)自帶有cxf插件,可以很方便的生成服務(wù)端與客戶端,你可以用那個(gè)來弄,基本就不會有問題了  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header[未登錄] 2012-12-18 14:40 randy

    能解釋下這兩句代碼的意思嗎?
    if (mess == null) {
    saa.handleMessage(message);
    mess = message.getContent(SOAPMessage.class);
    }

    還有什么情況下mess會為null?謝謝  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header[未登錄] 2013-06-06 12:08 Eason

    xml格式:
    <soap:Header>
    <auth:authentication xmlns:auth="http://gd.chinamobile.com//authentication">
    <auth:systemID>1</auth:systemID>
    <auth:userID>test</auth:userID>
    <auth:password>test</auth:password>
    </auth:authentication>
    </soap:Header> 這個(gè)如何生成?  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header 2013-07-29 17:45 sdfa

    @Eason
    sss  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header 2013-07-29 17:47 小陳

    Document doc=DOMUtils.createDocument();
    Element request=doc.createElementNS(""http://gd.chinamobile.com//authentication","auth:authentication");  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header 2014-04-12 15:37 何治明

    Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Could not find conduit initiator for transport http://schemas.xmlsoap.org/soap/http

    求此異常缺少的jar包  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header 2014-04-21 17:14 張磊磊

    @林子
    缺少jar包c(diǎn)xf-rt-transports-http-2.2.3.jar  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header 2014-04-21 17:14 張磊磊

    @何治明
    缺少jar包c(diǎn)xf-rt-transports-http-2.2.3.jar  回復(fù)  更多評論   

    # re: CXF 2.0 學(xué)習(xí)筆記-3 SOAP Header 2014-07-18 10:30 海洋一霸

    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-jetty</artifactId>
    <version>2.7.11</version>
    </dependency>

    確實(shí)是少了樓上說的jar,友情給出maven的地址吧  回復(fù)  更多評論   

    主站蜘蛛池模板: 亚洲午夜国产精品无卡| 日本黄页网站免费| yellow免费网站| 在线观看免费亚洲| 亚洲精品国产啊女成拍色拍| 午夜亚洲国产理论秋霞| 亚洲AV无码一区二区乱孑伦AS| 亚洲综合精品香蕉久久网| 国产aⅴ无码专区亚洲av麻豆| 亚洲男人的天堂在线播放| 亚洲AV无码成人精品区蜜桃| 亚洲国产人成在线观看| 亚洲av无码成人精品国产| 麻豆亚洲AV成人无码久久精品 | 大学生一级特黄的免费大片视频| 国产成在线观看免费视频| 最新欧洲大片免费在线| 国产一区二区三区免费视频| 免费v片在线观看品善网| 大胆亚洲人体视频| 亚洲午夜精品一级在线播放放| 亚洲欧洲久久av| 亚洲国产成人一区二区三区| 久久亚洲精品成人av无码网站| 亚洲精品福利网泷泽萝拉| 亚洲国产成人久久精品软件| 久久亚洲AV成人无码国产最大| 中国毛片免费观看| 免费女人高潮流视频在线观看| 亚洲免费在线视频观看| 毛片大全免费观看| 免费A级毛片无码久久版| 亚洲av不卡一区二区三区| 亚洲第一成年网站视频| 无码国产精品一区二区免费16| 免费观看激色视频网站(性色)| 亚洲av无码不卡私人影院| 免费人成视网站在线观看不卡| 亚洲丝袜美腿视频| 人妻免费久久久久久久了| 久草视频在线免费看|