Konrad Garus
Konrad Garus

Reputation: 54045

How to get request and response with Axis2?

I have a SOAP client generated with Axis2. It uses JAXB-RI, though that probably does not matter.

I do not have any XML configuration. Just get the Stub generated with wsdl2java, prepare the response (as POJO) and execute method on the Stub.

How can I get the raw XML (as String) for request and response that are exchanged on this call?

I figured out service._getServiceClient().getLastOperationContext().getMessageContext("Out").getEnvelope() (and similar for "In"), but one of them throws an exception because the stream has already been processed.

It sounds very obvious, yet somehow I cannot figure out how to do it, and the official documentation is intimidatingly scarce.

Upvotes: 4

Views: 3771

Answers (2)

Amit Sharma
Amit Sharma

Reputation: 163

i was facing similar issue , you can take reference from below code

private void ResponseSOAPMessage(OperationContext opCtx, OMElement responseOM)
        throws AxisFault
      {
        MessageContext msgCtxIn = opCtx.getMessageContext("In");
        if (!msgCtxIn.getEnvelope().isComplete())
        {
          msgCtxIn.getEnvelope().getBody().getFirstOMChild().close(false);
          msgCtxIn.getEnvelope().getBody().addChild(responseOM);
          msgCtxIn.getEnvelope().getBody().getFirstOMChild().detach();
        }
        CommonsTransportHeaders inHeaders = (CommonsTransportHeaders)msgCtxIn.getProperty("TRANSPORT_HEADERS");
        if (msgCtxIn.getEnvelope().isComplete())
        {
           System.out.println("SOAP Response:");
           System.out.println(msgCtxIn.getEnvelope().toString());
        }
        else
        {
           System.out.println("SOAP Response Message Body:");
           System.out.println(responseOM.toString());
        }
      }

Upvotes: 1

Kavan
Kavan

Reputation: 569

Not sure what is issue with "In" Message Lable,

But while searching, found following JIRA ticket https://issues.apache.org/jira/browse/AXIS2-5469 which points to https://issues.apache.org/jira/browse/AXIS2-5202 And in discussion found one of the WA to solve this issue using following code, I am able to listen Response message for the soap Request.

 stub._getServiceClient().getAxisService().addMessageContextListener(
 new MessageContextListener() {
        public void attachServiceContextEvent(ServiceContext sc,
            MessageContext mc) {}
        public void attachEnvelopeEvent(MessageContext mc) {
            try
            { mc.getEnvelope().cloneOMElement().serialize(System.out); }
            catch (XMLStreamException e) {}
        }
  });

Upvotes: 1

Related Questions