How to print SOAPMessage

jakarta-ee, java, soap

Solution

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

Problem

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

Original source