Apache CXF Custom Interceptor

Source: Internet
Author: User

Why design interceptors?
1. In order to dynamically manipulate the request and response data during the WebService request process, CXF designed the Interceptor
Interceptor Classification:
1. Divide by Location: Server-side interceptors, client interceptors.
2. According to the direction of the message: Into the interceptor, out of the interceptor.
3. By definition: System interceptors, custom interceptors.

Client Add log Blocker

 PackageCom.client.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.phase.AbstractPhaseInterceptor;Importorg.apache.cxf.phase.Phase;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element; Public classAdduserinterceptorextendsAbstractphaseinterceptor<soapmessage>{    PrivateString name; PrivateString password; /**     *      * @paramname *@paramPassword*/         Publicadduserinterceptor (String name,string password) {Super(Phase.pre_protocol);//Intercept when ready for protocol//TODO auto-generated Constructor stub         This. name=name;  This. password=password; } @Override Public voidHandlemessage (SoapMessage msg)throwsFault {//TODO auto-generated Method Stublistmsg.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 (NewHeader (NewQName ("Apple") , Rootele)); System.out.println ("Client Handmwssage () ...."); }}

Server-side interceptors:

 PackageCom.service.interceptor;ImportJavax.xml.namespace.QName;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; Public classCheckUserextendsAbstractphaseinterceptor<soapmessage>{     PublicCheckUser () {Super(Phase.pre_protocol); //TODO auto-generated Constructor stub    }           /*<Envelope> */@Override Public voidHandlemessage (SoapMessage message)throwsFault {//TODO auto-generated Method StubHeader Header = Message.getheader (NewQName ("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 via Interceptor"); return; }} System.out.println ("Server did not pass the interceptor ..... "); Throw NewFault (NewException ("The request requires a correct user name and password")); }}

Client

 Packagecom.cxf_client.client;Importjava.util.List;Importorg.apache.cxf.endpoint.Client;ImportOrg.apache.cxf.frontend.ClientProxy;ImportOrg.apache.cxf.interceptor.Interceptor;ImportOrg.apache.cxf.interceptor.LoggingInInterceptor;ImportOrg.apache.cxf.interceptor.LoggingOutInterceptor;ImportOrg.apache.cxf.message.Message;ImportCom.client.interceptor.AddUserInterceptor;Importcom.client.simple3.Simple;ImportCom.client.simple3.SimpleimplService; Public classSimpleclient2 { Public Static voidMain (string[] args) {//TODO auto-generated Method StubSimpleimplservice Simpleimplservice =NewSimpleimplservice (); Simple Simpleimplport=Simpleimplservice.getsimpleimplport (); //sending the requested client dataClient client =clientproxy.getclient (Simpleimplport); //Client Out Interceptorlist<interceptor<?extendsMessage>> outinterceptors =client.getoutinterceptors (); Outinterceptors.add (NewAdduserinterceptor ("Bo Ram", "520"));//             //client-in Interceptor//list<interceptor< extends message>> ininterceptors = Client.getininterceptors ();//Ininterceptors.add (New Loggingininterceptor ());String Favorite= Simpleimplport.favorite ("Tara");                System.out.println (favorite); }}

Service side

 PackageCom.service.server;Importjava.util.List;ImportJavax.xml.ws.Endpoint;ImportOrg.apache.cxf.interceptor.Interceptor;ImportOrg.apache.cxf.interceptor.LoggingInInterceptor;ImportOrg.apache.cxf.interceptor.LoggingOutInterceptor;ImportOrg.apache.cxf.jaxws22.EndpointImpl;ImportOrg.apache.cxf.message.Message;ImportCom.service.impl.Simpleimpl;ImportCom.service.interceptor.CheckUser; Public classSimpleServer2 { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString address= "Http://localhost:8848/simpleserver/simple"; Endpoint Publish= Endpoint.publish (Address,NewSimpleimpl ()); Endpointimpl Endpointimpl=(Endpointimpl) publish; //Service-in blockerlist<interceptor<?extendsMessage>> ininterceptors =endpointimpl.getininterceptors (); Ininterceptors.add (NewCheckUser ());//            //service-side-out interceptors//list<interceptor< extends message>> outinterceptors = Endpointimpl.getoutinterceptors ();//Outinterceptors.add (New Loggingoutinterceptor ());System.out.println ("Published"); }}

Apache 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.