Printing SOAP messages II

In Printing SOAP messages I was describing a proprietary feature how to monitor SOAP responses and requests on a Metro web service client. Not quite unexpectedly, we got some requests for similar functionality on the server. Marek therefore went and refactored the existing implementation somewhat to enable you to listen to SOAP messages on the server as well. This means that the packages for the client changed a little as well. I will first describe what you need to do with the client if you are using the current Metro builds. Then I will describe what you can do on the server now. Check Marek's blog for a description of some more features that he implemented and that make monitoring on the server more feasible.

Printing SOAP messages on the client

Very little changed actually compared to Printing SOAP messages. The package name is different however and I am repeating the code from the previous article with the new package names:

import com.sun.xml.ws.messagedump.MessageDumpingFeature;
...

    // Prepare to plug in the code that allows to read SOAP messages
    MessageDumpingFeature messageDumper = new MessageDumpingFeature();

    // Instantiate the web service client
    YourService service = new YourService();

    // Plug in the SOAP message dumper
    YourPort port = service.getYourServicePort(messageDumper);

    // Invoke the web service
    YourResult result = port.yourmethod(yourparameter);

    // Read the SOAP messages that were exchanged
    String request = messageDumper.nextMessage();
    String response = messageDumper.nextMessage();
    ...

Printing SOAP messages on the service

This task is a little less straight-forward and the example implementation I am showing is somewhat restricted. Usually, you implement a web service implementation class with some operations. You can access the MessageDumpingFeature inside those operations. That means however that that code will only be able to see the SOAP request that came in. The SOAP response has not been generated at that point in time and you do not have access to it. You will however see the SOAP response to the previous request. There are some ways to work around these restrictions and Marek will discuss them in the coming days. I will only show the simple case here where you are accessing the MessageDumpingFeature inside an operation:

import com.sun.xml.ws.api.WSBinding;
import com.sun.xml.ws.api.message.Packet;
import com.sun.xml.ws.api.server.WSEndpoint;
import com.sun.xml.ws.api.server.WSWebServiceContext;
import com.sun.xml.ws.messagedump.MessageDumping;
import com.sun.xml.ws.messagedump.MessageDumpingFeature;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;

/\*\*
 \* The WebService annotation announces to JAX-WS that this is a web service implementation class.
 \* The second annotation enables the message dumping feature.
 \*/
@WebService
@MessageDumping(storeMessages=true)
public class TestService {

    @Resource
    WebServiceContext context;

    /\*\*
     \* A web service operation
     \*/
    @WebMethod(operationName = "echo")
    public String echo(@WebParam(name = "text") final String text) {
        if (context != null) {

            // We need to get access to the MessageDumpingFeature object. This is a little tricky,
            // we need to work our way through some JAX-WS implementation classes.
            WSWebServiceContext wsContext = (WSWebServiceContext) context;
            Packet packet = wsContext.getRequestPacket();
            WSEndpoint endpoint = packet.endpoint;
            WSBinding binding = endpoint.getBinding();

            // Got it finally
            MessageDumpingFeature messageDump = binding.getFeature(MessageDumpingFeature.class);
            if (messageDump != null) {

                // The first time this method is invoked, it will return the SOAP request. All other invocations will
                // return the SOAP response of the previous invocation.
                String previousResponse = messageDump.nextMessage();

                // The first time this method is invoked, it will return null. All other invocations will return the
                // current SOAP request.
                String request = messageDump.nextMessage();
            }
        }

        // This is the actual business logic. In this case it is a very simple operation.
        return text;
    }
}

Tags: , , , ,

Comments:

This MessageDumping feature works in client only when in WebService code the @MessageDumping is set.

But how about situation when one dont know service implementation and want to get soap messages displayed on client servlet?

Posted by Pawel on September 05, 2008 at 05:15 AM EEST #

The client and service implementations are entirely separate from each other. If you want to access the messages on the client, you just set the MessageDumpingFeature as shown in the first half of the blog. There is no need to set the MessageDumping annotation on the service for that.

Posted by Fabian Ritzmann on September 05, 2008 at 07:59 AM EEST #

Post a Comment:
Comments are closed for this entry.
About

ritzmann

Search

Categories
Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today