Learn the CXF of WebService (7): CXF Custom Interceptor

Source: Internet
Author: User
Tags soap

In the server method

 PackageCom.rg2.webservice.impl;ImportOrg.apache.cxf.jaxws.JaxWsServerFactoryBean;ImportCom.rg2.intercepter.MyIntercepter;ImportCom.rg2.webservice.HelloWorld; Public classServer { Public Static voidMain (string[] args) {System.out.println ("Web Service Start"); HelloWorld implementor=NewHelloworldimpl (); String Address= "Http://localhost/helloWorld";//endpoint.publish (address, implementor);//JDK implementation exposes WebService interfaceJaxwsserverfactorybean Factorybean =NewJaxwsserverfactorybean (); Factorybean.setaddress (address);//set the exposure addressFactorybean.setserviceclass (HelloWorld.class);//Interface ClassFactorybean.setservicebean (implementor);//Setting the implementation classFactorybean.getininterceptors (). Add (NewLoggingininterceptor ());//Add in Interceptor log blockerFactorybean.getoutinterceptors (). Add (NewLoggingininterceptor ());//add an Out interceptor log interceptorfactorybean.getininterceptors (). Add (NewMyintercepter ()); Factorybean.create ();//creating the WebService interfaceSYSTEM.OUT.PRINTLN ("Web service Started"); }}
 PackageCom.rg2.intercepter;Importjava.util.List;ImportOrg.apache.cxf.binding.soap.SoapMessage;ImportOrg.apache.cxf.headers.Header;ImportOrg.apache.cxf.interceptor.Fault;ImportOrg.apache.cxf.phase.AbstractPhaseInterceptor;Importorg.apache.cxf.phase.Phase;Importorg.w3c.dom.Element;Importorg.w3c.dom.NodeList; Public classMyintercepterextendsAbstractphaseinterceptor<soapmessage> {     PublicMyintercepter () {Super(Phase.pre_invoke);//calling a custom interceptor before calling a method} @Override Public voidHandlemessage (SoapMessage message)throwsFault {List<Header> headers =message.getheaders (); if(NULL= = Headers && headers.size () = = 0) {            Throw NewFault (NewIllegalArgumentException ("No header, interceptor implementation interception")); } Header Firstheader= Headers.get (0); Element Ele=(Element) firstheader.getobject (); NodeList uList= Ele.getelementsbytagname ("UserName"); NodeList pList= Ele.getelementsbytagname ("Password"); if(Ulist.getlength ()!=1){            Throw NewFault (NewIllegalArgumentException ("User name is not formatted")); }        if(Plist.getlength ()!=1){            Throw NewFault (NewIllegalArgumentException ("Incorrect password format")); } String userName= Ulist.item (0). Gettextcontent (); String Password= Plist.item (0). Gettextcontent (); if(!username.equals ("Rg2") | |! Password.equals ("123456")){            Throw NewFault (NewIllegalArgumentException ("Username or password is wrong!") ")); }    }}

Client

 PackageCom.rg2.webservice;Importjava.util.List;ImportOrg.apache.cxf.frontend.ClientProxy;ImportOrg.apache.cxf.interceptor.LoggingInInterceptor;ImportCom.rg2.interceptor.AddHeaderIntercepter; Public classClient { Public Static voidMain (string[] args) {HelloWorldService service=NewHelloWorldService (); HelloWorld Helloworldport=Service.gethelloworldport (); Org.apache.cxf.endpoint.Client Client=clientproxy.getclient (Helloworldport); Client.getoutinterceptors (). Add (NewAddheaderintercepter ("Rg2", "123456"));//Add a custom interceptorClient.getininterceptors (). Add (NewLoggingininterceptor ());//Add in Interceptor log blockerClient.getoutinterceptors (). Add (NewLoggingininterceptor ());//add an Out interceptor log interceptorMyrolearray getroles=Helloworldport.getroles (); List<MyRole> rolelist =Getroles.item;  for(inti = 0; I < rolelist.size (); i++) {Myrole my=Rolelist.get (i); System.out.print (My.key+ ":");  for(Role role:my.value) {System.out.print (Role.getid ()+","+role.getrolename ()); } System.out.println ("==============="); }    }}
 PackageCom.rg2.interceptor;Importjava.util.List;ImportJavax.xml.namespace.QName;ImportOrg.apache.cxf.binding.soap.SoapMessage;ImportOrg.apache.cxf.headers.Header;Importorg.apache.cxf.helpers.DOMUtils;ImportOrg.apache.cxf.interceptor.Fault;ImportOrg.apache.cxf.interceptor.Interceptor;ImportOrg.apache.cxf.message.Message;ImportOrg.apache.cxf.phase.AbstractPhaseInterceptor;Importorg.apache.cxf.phase.Phase;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element; Public classAddheaderintercepterextendsAbstractphaseinterceptor<soapmessage> {        PrivateString UserName; PrivateString password;  Publicaddheaderintercepter (String userName, string password) {Super(Phase.prepare_send);//Call Interceptor when preparing to send SOAP message         This. UserName =UserName;  This. Password =password; } @Override Public voidHandlemessage (SoapMessage message)throwsFault {List<Header> headerlist =message.getheaders (); Document Doc=domutils.createdocument (); Element Ele= Doc.createelement ("Authheader"); Element uelement= Doc.createelement ("UserName");        Uelement.settextcontent (UserName); Element pelement= Doc.createelement ("Password");                Pelement.settextcontent (password);        Ele.appendchild (uelement);                Ele.appendchild (pelement); Headerlist.add (NewHeader (NewQName ("Rg2") , ele)); }}

Learn the CXF of WebService (7): CXF Custom Interceptor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.