How to print SOAPMessage

16,560

Solution 1

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;
  }

Solution 2

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.

Share:
16,560
Ahmet Karakaya
Author by

Ahmet Karakaya

IMPossible also says I'M Possible

Updated on June 14, 2022

Comments

  • Ahmet Karakaya
    Ahmet Karakaya almost 2 years

    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;
      }