> -----Original Message-----
> From: Daniel Kulp [mailto:[email protected]]
> Sent: Wednesday, November 03, 2010 7:55 PM
> To: [email protected]
> Cc: KARR, DAVID (ATTSI)
> Subject: Re: Best way to get XML string in interceptor
>
> On Wednesday 03 November 2010 7:28:24 pm KARR, DAVID (ATTSI) wrote:
> > > -----Original Message-----
> > > From: KARR, DAVID (ATTSI)
> > > Sent: Wednesday, November 03, 2010 2:33 PM
> > > To: [email protected]
> > > Subject: Best way to get XML string in interceptor
> > >
> > > I need to write some in/out interceptors that do something with
the
> > > incoming or outgoing XML string, but not modifying it for
> downstream
> > > interceptors or the eventual handler. They are basically logging
> > > interceptors, like the LoggingOutInterceptor.
> > >
> > > How can I write a custom interceptor that can read the XML string,
> but
> > > still let the handler process it normally?
> > >
> > > It seems like subclassing LoggingOutInterceptor might work, and
> >
> > perhaps
> >
> > > setting the "writer" just before calling the
> "super.handleMessage()"
> > > method. I tried this, but the writer ended up with an empty
> string.
> >
> > I can see why I ended up with an empty string.
LoggingOutInterceptor
> > writes the output asynchronously, so the writer doesn't get the data
> > until sometimes after the function returns. I wish I could plug my
> own
> > callback into LoggingOutInterceptor, so I could implement
> > application-specific handling. As it is, I guess I'm going to have
> to
> > duplicate most of what LoggingOutInterceptor is doing.
>
> Well, if you write an interceptor that lives immediately after the
> LoggingOutInterceptor, you should be able to grab the Outputstream,
> cast it to
> the CacheAndWriteOutputStream, add your callback.
Ok, I got the Service, then got the Client, then added the
LoggingOutInterceptor and then my custom interceptor, which extends
LoggingOutInterceptor. In the constructor, I put it in PRE_STREAM, and
made it run after LoggingOutInterceptor (which could be odd, since my
class is an instanceof LoggingOutInterceptor).
I saw it hit the breakpoints in both LoggingOutInterceptor and my custom
interceptor. I saw it get into the callbacks in both classes. When it
got into my callback, I checked several message keys, and they seemed
reasonable (endpoint address, for instance). However, after calling
"writePayload()", my StringBuilder was empty. When I "resume"d from the
debugger, it appeared to send the request and get a valid response.
Here's some code:
Setting up the introspectors on the BindingProvider:
--------------------
Client client = ClientProxy.getClient(bindingProvider);
client.getOutInterceptors().add(new
LoggingOutInterceptor());
client.getOutInterceptors().add(new
MessageOutgoingLogInterceptor());
--------------------
My customer introspector, without the package and imports:
----------------------
public class MessageOutgoingLogInterceptor extends LoggingOutInterceptor
{
public MessageOutgoingLogInterceptor() {
super(Phase.PRE_STREAM);
addAfter(LoggingOutInterceptor.class.getName());
}
@Override
public void handleMessage(Message message) throws Fault {
System.out.println("message[" + message + "]");
GenericService service = (GenericService)
message.get(CSIJAXBUtil.CK_SERVICE_OBJECT);
if (service.isLoggingDebug()) {
final OutputStream os =
message.getContent(OutputStream.class);
if (os == null) {
System.out.println("Outputstream is null?");
return;
}
if (os instanceof CacheAndWriteOutputStream) {
CacheAndWriteOutputStream cawOutputStream =
(CacheAndWriteOutputStream) os;
cawOutputStream.registerCallback(new
LoggingCallback(message, os));
}
}
}
class LoggingCallback implements CachedOutputStreamCallback {
private final Message message;
private final OutputStream origStream;
public LoggingCallback(final Message msg, final OutputStream os)
{
this.message = msg;
this.origStream = os;
}
public void onClose(CachedOutputStream cachedOutputStream) {
System.out.println("message[" + message + "]");
Boolean inboundMessage = (Boolean)
message.get(Message.INBOUND_MESSAGE);
System.out.println("inboundMessage[" +
BooleanUtils.isTrue(inboundMessage) + "]");
String endpointAddress = (String)
message.get(Message.ENDPOINT_ADDRESS);
System.out.println("endpointAddress[" + endpointAddress +
"]");
String encoding = (String)
message.get(Message.ENCODING);
String contentType = (String)
message.get(Message.CONTENT_TYPE);
StringBuilder sb = new StringBuilder();
try {
MessageOutgoingLogInterceptor.this.writePayload(sb,
cachedOutputStream, encoding, contentType);
System.out.println("sb[" + sb.toString() + "]");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void onFlush(CachedOutputStream paramCachedOutputStream)
{ }
}
}
--------------------