Monday, February 4, 2013

Develop web service with jaxws - print request/response SOAP messages

1) add chain configuration file LogMessage_handler.xml under folder WEB-INF\classes


<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
  <handler-chain>
    <handler>
      <handler-name>com.abc.ws.demows.skeleton.LogSoapMessageHandler</handler-name>
     <handler-class>com.abc.ws.demows.skeleton.LogSoapMessageHandler</handler-class>
    </handler>
  </handler-chain>
</handler-chains>


2) add above configured class LogSoapMessageHandler


package com.abc.ws.demows.skeleton;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.LogicalMessage;
import javax.xml.ws.handler.LogicalHandler;
import javax.xml.ws.handler.LogicalMessageContext;
import javax.xml.ws.handler.MessageContext;

public class LogSoapMessageHandler implements
LogicalHandler<LogicalMessageContext> {

public void close(MessageContext msgCtx) {

}

public boolean handleFault(LogicalMessageContext lmc) {

return false;
}

public boolean handleMessage(LogicalMessageContext context) {
boolean direction = ((Boolean) context
.get(LogicalMessageContext.MESSAGE_OUTBOUND_PROPERTY))
.booleanValue();

try {
LogicalMessage lm = ((LogicalMessageContext) context).getMessage();
if (lm != null) {
Source source = lm.getPayload();
if (source != null) {
System.out
.println(direction == true ? "Response SOAP Message : \n"
+ getSourceAsString(source)
: "Request SOAP Message : \n"
+ getSourceAsString(source));

} else {
System.out.println(" No Message payload was present");
}
} else {
System.out.println("\n No Message was present");
}
} catch (Exception e) {
System.out.println("Exception while dumping soap message." + e);
}

return true;
}

private String getSourceAsString(Source s) throws Exception {

Transformer transformer = TransformerFactory.newInstance()
.newTransformer();

OutputStream out = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult();
streamResult.setOutputStream(out);
transformer.transform(s, streamResult);

return streamResult.getOutputStream().toString();
}

}


3) add above class to skeleton class

@WebService( endpointInterface = "com.abc.ws.demows.DemoService" )
@HandlerChain( file = "LogMessage_handler.xml" )
public class DemoServiceImpl implements DemoService {
...
}


4) test result

Request SOAP Message :
<?xml version="1.0" encoding="UTF-8"?><createReqParams xmlns="http://ws.abc.com/demows/" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://ws.abc.com/common/"><header><ns2:clientId
>CID0001</ns2:clientId><ns2:timeStamp>2013-02-04T12:46:36.693+08:00</ns2:timeStamp></header><prodId>PID02</prodId></createReqParams>

request data: prodId=PID02,clientId=CID0001,timestamp=Mon Feb 04 12:46:36 SGT 2013

Response SOAP Message :
<?xml version="1.0" encoding="UTF-8"?><createRespParams xmlns="http://ws.abc.com/demows/" xmlns:ns2="http://ws.abc.com/common/"><header><ns2:returnCode>9999</ns2:returnCode><ns2:returnMessage>OK</ns2:
returnMessage></header><prodId>PID01</prodId><prodStatus>Pass</prodStatus></createRespParams>


3 comments:

  1. thanks man found after many searches and it is working fine.

    ReplyDelete
  2. This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring such topic to discussion.

    ReplyDelete
  3. With the support of this blog one can easily develop web services and also make printing concept more easier.

    brochure printing

    ReplyDelete