Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print SOAPMessage

SOAPMessage has writeTo() method which is used to print its content into a stream. But How I can write SOAPMessage content into StringBuffer?

code line "message.writeTo(System.out);" has to be modified..

public boolean handleMessage(SOAPMessageContext smc) {
     StringBuffer sbuf = new StringBuffer();
     sbuf.append("\n------------------------------------\n");
     sbuf.append("In SOAPHandler " + HandlerName + ":handleMessage()\n");

     Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

     if (outboundProperty.booleanValue()) {
        sbuf.append("\ndirection = outbound ");
     }
     else {
        sbuf.append("\ndirection = inbound ");
     }

     SOAPMessage message = smc.getMessage();      
     try {
        sbuf.append("\n");
        sbuf.append(message.toString());         
        //message.writeTo(System.out);         
        sbuf.append("\nMessage Desc:");         
        sbuf.append("\n");
     }
     catch (Exception e) {
        sbuf.append("Exception in SOAP Handler: " + e);
     }

     sbuf.append("Exiting SOAPHandler " + HandlerName + ":handleMessage()\n");
     sbuf.append("------------------------------------\n");
     logger.debug(sbuf.toString());
     return true;
  }
like image 918
Ahmet Karakaya Avatar asked Dec 12 '12 14:12

Ahmet Karakaya


2 Answers

OK I have solved the question. Modified code block is as follow.

  public boolean handleMessage(SOAPMessageContext smc) {
     StringBuffer sbuf = new StringBuffer();
     sbuf.append("\n------------------------------------\n");
     sbuf.append("In SOAPHandler " + HandlerName + ":handleMessage()\n");

     Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

     if (outboundProperty.booleanValue()) {
        sbuf.append("\ndirection = outbound ");
     }
     else {
        sbuf.append("\ndirection = inbound ");
     }

     SOAPMessage message = smc.getMessage();      
     try {
        sbuf.append("\n");
        sbuf.append(message.toString()); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        message.writeTo(baos);                     
        sbuf.append("\nMessage Desc:"+baos.toString());         
        sbuf.append("\n");
     }
     catch (Exception e) {
        sbuf.append("Exception in SOAP Handler: " + e);
     }

     sbuf.append("Exiting SOAPHandler " + HandlerName + ":handleMessage()\n");
     sbuf.append("------------------------------------\n");
     logger.debug(sbuf.toString());
     return true;
  }
like image 86
Ahmet Karakaya Avatar answered Sep 30 '22 14:09

Ahmet Karakaya


If you need to log the SOAP message just for debugging purposes then it would be easier to do by turning on SOAP messages logging for your JAX-WS implementation. You will not need to write any code at all in this case.

like image 34
Andrii Polunin Avatar answered Sep 30 '22 12:09

Andrii Polunin