'Print Axis2 Request Response XML

I want to print RAW Request Response XML to console. I have created the stubs using wsdl2java axis2.

wsdl2java has created 2 java files, one for stub and one for callbackhandler.

I am trying with below method but getting null value for

operationContext.getMessageContext("Out");
/
operationContext.getMessageContext("In");.

Code

public void SOAPLogHandler(Stub stub){  

ServiceContext serviceConxt = stub._getServiceClient().getServiceContext();

//**** Enable the Cache to hold the last operation
OperationContext OperationContext  = new OperationContext();
boolean cacheLastOperationContext = true;
OperationContext.setComplete(true); // Enable the Cache value
serviceConxt.setCachingOperationContext(cacheLastOperationContext);
serviceConxt.setLastOperationContext(OperationContext);

OperationContext operationContext = serviceConxt.getLastOperationContext();

if (operationContext != null) {
         MessageContext outMessageContext = operationContext.getMessageContext("Out");
             operationContext.getMessageContexts();
        if (outMessageContext != null) {
            System.out.println("OUT SOAP: "+outMessageContext.getEnvelope().toString());
             }
     MessageContext inMessageContext = operationContext.getMessageContext("In");
if (inMessageContext != null) {
    System.out.println("IN SOAP: "+ inMessageContext.getEnvelope().toString());
                  }
           }

Can you please let me know is there any other way to get the raw xml



Solution 1:[1]

unfortunately unless you intercept the message via say tcp monitor, you will have a tough time getting the raw message.

Solution 2:[2]

I managed to do so by adding a couple of lines in a method of the stub (the one you call to invoke the web service). I identified the line, i.e.

_operationClient.execute(true);

that effectively contacts the web service and added just after it:

_operationClient.getMessageContext("Out").getEnvelope().serialize(System.out);
_operationClient.getMessageContext("In").getEnvelope().serialize(System.out);

Solution 3:[3]

In the above code:

  OperationContext OperationContext  = new OperationContext();
  boolean cacheLastOperationContext = true;
  OperationContext.setComplete(true); // Enable the Cache value
  serviceConxt.setCachingOperationContext(cacheLastOperationContext);
  serviceConxt.setLastOperationContext(OperationContext);

You are explicitly creating new operationContext and and setting it to LastOperationContext. That is why you are getting null.

I used same code, just removed part which is setting setLastOperationContext.

It is working for me atleast for Out Message.

for In I am getting exception stating that "Attempted read on closed stream".

Solution 4:[4]

Starting with kekolab solution above, I wrote a method that can be used to log the outgoing and ingoing xmls :

After each _operation.execute(true), call logPayload :

_operationClient.execute(true);
logPayload(_operationClient);

which is defined as :

  private void logPayload(OperationClient operationClient) {
    try {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      operationClient.getMessageContext("Out").getEnvelope().serialize(out);
      logger.info(out.toString() + "\n\n");
      operationClient.getMessageContext("In").getEnvelope().serialize(out);
      logger.info(out.toString() + "\n\n");
    } catch (XMLStreamException e) {
      e.printStackTrace();
    } catch (AxisFault e) {
      e.printStackTrace();
    }
  }

Worked really nice for me and included the security tags inserted inside the soap enveloppe header as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anonymous
Solution 2 kekolab
Solution 3 Kavan
Solution 4 Pierre C