Add WebService Call Log

Source: Internet
Author: User

Before I wanted to use spring AOP to add facets to the webservice, but using the around slice, incredibly called the end of the webservice returned results, and the details of the message is not known, it is embarrassing, so stole a lazy. But what to do is still to do, do not want to look at the call log later, what can not be taken out, not an embarrassment to fix.

I am using CXF-based webservice, so the method of adding call logs is based on CXF, followed by sping development WebService. The implementation of basic webservice is not explained here.

First, the use of loggingininterceptor implementation

The first approach is to use the already implemented log interception, which needs to be configured well log.properties.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 ">    <Jaxws:endpointimplementor= "Com.test.DepartServiceImpl"Address= "/departservice">        <jaxws:ininterceptors>            <Beanclass= "Org.apache.cxf.interceptor.LoggingInInterceptor" />        </jaxws:ininterceptors>        <jaxws:outinterceptors>            <Beanclass= "Org.apache.cxf.interceptor.LoggingOutInterceptor" />        </jaxws:outinterceptors>    </Jaxws:endpoint>    </Beans>
<jaxws:inInterceptors> is an input interceptor that is intercepted when the interface is called. The following is the log output after the interface is called.
----------------------------id:1address:http://localhost:8080/notices/ws/departservice?wsdlencoding: Utf-8http-method:postcontent-type:text/xml; Charset=utf-8headers: {accept=[*/*], Cache-control=[no-cache], connection=[keep-alive], Content-Length=[221], Content-type=[text/xml, Charset=utf-8], host=[localhost:18080], Pragma=[no-cache], soapaction=["" ", user-agent=[ Apache CXF 2.7.7]}payload:<Soap:envelopeXmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/"><Soap:body><ns2:findemployeesXmlns:ns2= "http://cxf.test.com/"><arg0>Zhang San</arg0><arg1></arg1></ns2:findemployees></Soap:body></Soap:envelope>--------------------------------------
<jaxws:outInterceptors> is an output interceptor that is intercepted when the interface call is complete. The following is the log output after the interface call is complete.
---------------------------id:1encoding:utf-8content-type:text/xmlheaders: {}payload:<Soap:envelopeXmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/"><Soap:body><Ns2:findemployeesresponseXmlns:ns2= "http://cxf.test.com/"><return><User_depart>it</User_depart><user_name> Zhang San</user_name><user_state> Job</user_state><User_tele></User_tele><User_type>General Staff</User_type></return></Ns2:findemployeesresponse></Soap:body></Soap:envelope>--------------------------------------

We can see that the basic implementation has been very detailed, even to see some important basic information. If this method is not enough for your needs, look behind.

II. realization of Abstractphaseinterceptor
ImportOrg.apache.cxf.interceptor.Fault;ImportOrg.apache.cxf.message.Message;ImportOrg.apache.cxf.phase.AbstractPhaseInterceptor;Importorg.apache.cxf.phase.Phase;ImportJava.io.ByteArrayInputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.io.UnsupportedEncodingException; Public classWebserviceloginterceptorextendsAbstractphaseinterceptor<message> {     PublicWebserviceloginterceptor () {Super(phase.receive); } @Override Public voidHandlemessage (Message message)throwsFault {stringbuffer sb=NewStringBuffer (); //here, you can do the convection, read the data from the stream, and then modify the data for what you want.InputStream is = Message.getcontent (InputStream.class); String encoding=(String) message.get (message.encoding); intLen = 0; byte[] bytes =New byte[1024 * 4]; Try {             while(len = is.read (bytes))!=-1) {sb.append (NewString (Bytes, 0, Len, encoding)); }        } Catch(IOException e) {e.printstacktrace (); } System.out.println (Sb.tostring ());
//one thing to note here is that if the modified SOAP message format does not conform to the WebService framework format, for example: The format of the framework package is//<soap:envelope xmlns:soap= "Http://www.w3.org/2001/12/soap-envelope"<soap:Body>//< Here is the namespace that invokes the server-side method >< This is the parameter name >//This is the real news.//</Here is the namespace that invokes the service-side method ></This is the parameter name >//</soap:Body>//</soap:Envelope> Try{message.setcontent (inputstream).class,NewBytearrayinputstream (Sb.tostring (). GetBytes (encoding))); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } }}

The configuration file adds the corresponding interceptor.

<Jaxws:endpointimplementor= "Com.test.cxf.impl.DepartServiceImpl"Address= "/departservice">        <jaxws:ininterceptors>            <Beanclass= "Org.apache.cxf.interceptor.LoggingInInterceptor" />            <Beanclass= "Com.test.interceptor.WebserviceLogInterceptor" />        </jaxws:ininterceptors>    </Jaxws:endpoint>

It is important to note that after the data has been processed, it needs to be written back to the stream, otherwise the interface will not be able to parameters.

Importorg.apache.cxf.helpers.IOUtils;ImportOrg.apache.cxf.interceptor.Fault;ImportOrg.apache.cxf.io.CachedOutputStream;ImportOrg.apache.cxf.message.Message;ImportOrg.apache.cxf.phase.AbstractPhaseInterceptor;Importorg.apache.cxf.phase.Phase;ImportJava.io.*; Public classWebserviceLogInterceptor1extendsAbstractphaseinterceptor<message> {     PublicWebserviceLogInterceptor1 () {Super(Phase.pre_stream); } @Override Public voidHandlemessage (Message message)throwsFault {stringbuffer sb=NewStringBuffer (); OutputStream OS= Message.getcontent (outputstream.class); Message.setcontent (OutputStream.class,NewCachedoutputstream ());        Message.getinterceptorchain (). dointercept (message); Cachedoutputstream csnew= (Cachedoutputstream) message.getcontent (outputstream.class); Try{String encoding=(String) message.get (message.encoding); InputStream in=Csnew.getinputstream (); intLen = 0; byte[] bytes =New byte[1024 * 4];  while(len = in.read (bytes))!=-1) {sb.append (NewString (Bytes, 0, Len, encoding));            } System.out.println (Sb.tostring ()); //here the XML processing, after processing the same, write the reflowIoutils.copy (NewBytearrayinputstream (Sb.tostring (). GetBytes (encoding)), OS);        Os.flush (); } Catch(IOException e) {e.printstacktrace (); } message.setcontent (OutputStream.class, OS); }}

Similarly, after reading the data, you still need to write back to the stream, otherwise the caller will not get the result.

Add WebService Call Log

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.