Apache CXF custom interceptor, apachecxf

Source: Internet
Author: User

Apache CXF custom interceptor, apachecxf

Why design an interceptor?
1. to dynamically operate requests and response data during the webservice request process, CXF designs an interceptor
Interceptor category:
1. According to the location: server-side interceptor and client-side interceptor.
2. In the message direction: Inbound interceptor and outbound interceptor.
3. By definition: System interceptor and custom interceptor.

 

Add a log Interceptor to the client

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 actphaseinterceptor <SoapMessage> {private String name; private String password;/***** @ param name * @ param password */public AddUserInterceptor (String name, string password) {super (Phase. PRE_PROTOCOL); // intercept when preparing for 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 ().... ");}}

 

 

Server Interceptor:

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 actphaseinterceptor <SoapMessage> {public CheckUser () {super (P Hase. PRE_PROTOCOL ); // TODO Auto-generated constructor stub}/* <Envelope> 

 

Client

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. simple 3. 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 data sending requests client = ClientProxy. getClient (simpleimplPort); // List of client-side Interceptor <? Extends Message> outInterceptors = client. getOutInterceptors (); outInterceptors. add (new AddUserInterceptor ("bo ram", "520"); /// client inbound Interceptor // List <Interceptor <? Extends Message> inInterceptors = client. getInInterceptors (); // inInterceptors. add (new LoggingInInterceptor (); String favorite = simpleimplPort. favorite ("Tara"); System. out. println (favorite );}}

 

 

Server

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 cl Ass 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 of server-side Interceptor <? Extends Message> inInterceptors = endpointimpl. getInInterceptors (); inInterceptors. add (new CheckUser (); // service-side out Interceptor // List <Interceptor <? Extends Message> outInterceptors = endpointimpl. getOutInterceptors (); // outInterceptors. add (new LoggingOutInterceptor (); System. out. println ("published ");}}

 

Related Article

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.