Tracing Xml Request/Responses with Jax-Ws

Tracing XML request/responses with JAX-WS

Here is the solution in raw code (put together thanks to stjohnroe and Shamik):

Endpoint ep = Endpoint.create(new WebserviceImpl());
List<Handler> handlerChain = ep.getBinding().getHandlerChain();
handlerChain.add(new SOAPLoggingHandler());
ep.getBinding().setHandlerChain(handlerChain);
ep.publish(publishURL);

Where SOAPLoggingHandler is (ripped from linked examples):

package com.myfirm.util.logging.ws;

import java.io.PrintStream;
import java.util.Map;
import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

/*
* This simple SOAPHandler will output the contents of incoming
* and outgoing messages.
*/
public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> {

// change this to redirect output if desired
private static PrintStream out = System.out;

public Set<QName> getHeaders() {
return null;
}

public boolean handleMessage(SOAPMessageContext smc) {
logToSystemOut(smc);
return true;
}

public boolean handleFault(SOAPMessageContext smc) {
logToSystemOut(smc);
return true;
}

// nothing to clean up
public void close(MessageContext messageContext) {
}

/*
* Check the MESSAGE_OUTBOUND_PROPERTY in the context
* to see if this is an outgoing or incoming message.
* Write a brief message to the print stream and
* output the message. The writeTo() method can throw
* SOAPException or IOException
*/
private void logToSystemOut(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean)
smc.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);

if (outboundProperty.booleanValue()) {
out.println("\nOutbound message:");
} else {
out.println("\nInbound message:");
}

SOAPMessage message = smc.getMessage();
try {
message.writeTo(out);
out.println(""); // just to add a newline
} catch (Exception e) {
out.println("Exception in handler: " + e);
}
}
}

Trace SOAP request/responses with JAX-WS on the client side

Why not use @HandlerChain(file = "....") annotation?

From my pov, you can not mix constructor- and annotation-based configurations as on-deploy webservice initialization and creating new instance of your service class are performed in absolutely different contexts.

Find exact soap request and response xml created

If you are using Apache CXF you can use the classes LoggingOutInterceptor and LoggingInInterceptor:

StringWriter soapMessageWriter=new StringWriter();
LoggingOutInterceptor loi=new LoggingOutInterceptor(new PrintWriter(soapMessageWriter));
loi.setPrettyLogging(true);
ClientProxy.getClient(serviceSoap).getOutInterceptors().add(loi);
LoggingInInterceptor lii=new LoggingInInterceptor(new PrintWriter(soapMessageWriter));
lii.setPrettyLogging(true);
ClientProxy.getClient(serviceSoap).getInInterceptors().add(lii);

//do your stuff

String soapContent=soapMessageWriter.getString();

How can I change response xml in JAX-WS

Finally I found the solution. We have to use following annotation:

@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)

Logging soap requests and responses body only Jax WS

I have successfully solved the problem used the detachnode method for the SOAP header as follows:

 message.getSOAPHeader().detachNode();

Here's a code sample of how the code becomes (notice how I test on whether the soapHeader is non-null, because otherwise it'd throw a null pointer exception):

    private void logToSystemOut(SOAPMessageContext smc) {
Logger logger = Logger.getLogger(LogHandler.class);
Boolean outboundProperty = (Boolean) smc
.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
logger.info("Outbound message:");
} else {
logger.info("Inbound message:");
}

SOAPMessage message = smc.getMessage();
try {
if(message.getSOAPHeader()!=null)
message.getSOAPHeader().detachNode();
Source source = message.getSOAPPart().getContent();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
transformer.transform(source, new StreamResult(baos));
logger.info(baos.toString());
} catch (Exception e) {
logger.warn("Exception in handler: " + e);
}
}}


Related Topics



Leave a reply



Submit