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 ");}}