Search code examples
javasoapsoaphandler

Get the request of a SOAP response using SOAPHandler


I have an SOAPHandler. I need to capture the Request of the Response

public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> {

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        writeMessageLogging(context);
        return true;
    }
    public boolean handleMessage(SOAPMessageContext context) {
        writeMessageLogging(context);
        return true;
    }

    private void writeMessageLogging(SOAPMessageContext smc) {
        Boolean outboundProperty = (Boolean) smc
                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (logger.isDebugEnabled()) {
            if (outboundProperty.booleanValue()) {
                logger.debug("Request message");
            } else {
                logger.debug("Response message:");
            }
        }
        SOAPMessage message = smc.getMessage();
        ByteArrayOutputStream out=null;
        try {
            if (!outboundProperty.booleanValue()) {
                String requestXML="Request of the Response, is possible?";
            }
                out = new ByteArrayOutputStream();
             message.writeTo(out);
             String strMsg = new String(out.toByteArray());
             logger.debug("strMsg:" + strMsg);
            out.close();
        } catch (Exception e) {
            logger.error("Exception in handler:", e);
        }finally{
            IOUtils.closeQuietly(out);
        }

    }
 }

See:

String requestXML="Request of the Response, is possible?";

Is possible capturing the Request in a Response Handle?


Solution

  • I finally resolve my problem:

    
    
        public class SOAPLoggingHandler implements SOAPHandler {
    
            private static Logger logger = Logger.getLogger(SOAPLoggingHandler.class
                    .getCanonicalName());
    
            public static final String REQUEST_XML="REQUEST_XML";
    
    
            @Override
            public boolean handleFault(SOAPMessageContext context) {
    
                writeMessageLogging(context);
                return true;
            }
    
            @Override
            public boolean handleMessage(SOAPMessageContext context) {
                writeMessageLogging(context);
                return true;
    
            }
    
            private void writeMessageLogging(SOAPMessageContext smc) {
                Boolean outboundProperty = (Boolean) smc
                        .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    
                SOAPMessage message = smc.getMessage();
                ByteArrayOutputStream out=null;
                try {
                     out = new ByteArrayOutputStream();
                     message.writeTo(out);
                     String strMsg = new String(out.toByteArray());
    
                    if (!outboundProperty.booleanValue()) {
                        String requestXML=(String)smc.get(REQUEST_XML);
                        logger.debug("Request of Response:"+requestXML);
                    }else{
                        smc.put(REQUEST_XML,strMsg);
                    }
                    logger.debug("strMsg:" + strMsg);
                    out.close();
                } catch (Exception e) {
                    logger.error("Exception in handler:", e);
                }finally{
                    IOUtils.closeQuietly(out);
                }
    
            }
        }
    
    
    

    I use the SOAPMessageContext for that.