Well, I guess my question is:
Why would you expect an OUTPUT stream on the incoming side? The message is
coming IN. Thus, it would be an InputStream.
In anycase, I would suggest either using our LoggingInInterceptor directly or
subclassing it.
Dan
On Thursday 19 August 2010 7:47:48 am DazBaz wrote:
> Hi. Hope someone can help.
>
> I am using CXF running on Tomcat for a number of web services. I am trying
> to write an Interceptor that will capture all incoming web service
> requests, extract the whole SOAP envelope (including body) as a String and
> write it to a log file. I thought this would be relatively easy but I
> haven't been able to get it working. I am unable to extract the SOAP
> envelope from the request. I have searched on this and other forums
> (fora?) and have found a couple of similar posts but none of the proposed
> solutions have worked for me. My latest attempt is below. Everytime I send
> a request to the server the interceptor is being called but the
> OutputStream is always null (LINE 2). I have tried each of these phases in
> LINE 1:
>
> RECEIVE, PRE_STREAM, USER_STREAM, POST_STREAM, READ, PRE_PROTOCOL,
> USER_PROTOCOL, POST_PROTOCOL, UNMARSHAL, PRE_LOGICAL, USER_LOGICAL,
> POST_LOGICAL, PRE_INVOKE, INVOKE, POST_INVOKE
>
> but LINE 2 is hit each time.
>
> Has anyone done this before? I would have thought it was relatively
> straight-forward but maybe not. My knowledge of the nuts and bolts of CXF
> isn't the best - I'm not clear why Streams are being used here. I'd
> appreciate any help, thanks.
>
> =============
> build.xml (extract):
> =============
> <bean id="myInterceptor2" class="com.mycomp.MyInterceptor2" />
>
> <bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl">
> <property name="inInterceptors">
> <list>
> <ref bean="myInterceptor2" />
> </list>
> </property>
> </bean>
>
> ===============
> MyInterceptor2 class
> ===============
>
> package com.mycomp;
>
> import java.io.OutputStream;
>
> import org.apache.cxf.interceptor.Fault;
> import org.apache.cxf.io.CacheAndWriteOutputStream;
> import org.apache.cxf.io.CachedOutputStream;
> import org.apache.cxf.io.CachedOutputStreamCallback;
> import org.apache.cxf.message.Message;
> import org.apache.cxf.phase.AbstractPhaseInterceptor;
> import org.apache.cxf.phase.Phase;
>
> public class MyInterceptor2 extends AbstractPhaseInterceptor{
>
> public MyInterceptor2() {
> super(Phase.READ); // LINE 1
> }
>
> public void handleMessage(Message message) throws Fault {
> OutputStream os =
> (OutputStream)message.getContent(OutputStream.class);
> if(os == null)
> return; // LINE 2
> final CacheAndWriteOutputStream newOut = new
> CacheAndWriteOutputStream(os);
> message.setContent(OutputStream.class, newOut);
> newOut.registerCallback(new LoggingCallback());
> }
>
> /**
> * Logging callback classs which caches and
> * writes the soap envelope to a StringBuilder.
> */
> class LoggingCallback implements CachedOutputStreamCallback {
> public void onFlush(CachedOutputStream cos) {
> }
> public void onClose(CachedOutputStream cos) {
> try {
> StringBuilder buffer = new StringBuilder();
> cos.writeCacheTo(buffer);
>
> }catch (Exception e) {
> }
> }
> }
> }
--
Daniel Kulp
[email protected]
http://dankulp.com/blog