java - 使用 SOAPHandler 获取 SOAP 响应的请求

标签 java soap soaphandler

我有一个 SOAPHandler。我需要捕获响应的请求

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

    }
 }

参见:

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

是否可以在响应句柄中捕获请求?

最佳答案

我终于解决了我的问题:



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

        }
    }


为此我使用了 SOAPMessageContext。

关于java - 使用 SOAPHandler 获取 SOAP 响应的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738635/

相关文章:

java - 如何在 c :\using Paths(not File)? 处创建文件夹

PHP SoapClient 发送转义的 XML 字符

servlets - 在soapMessageContext - Axis Handler中获取 "HttpServletRequest"

java - 列出 codenameone 中的渲染器问题

java - Spring data rest findBy securityCheck 用于嵌套实体

PHP SOAP 和命名空间。 PHP fatal error : Class 'Jira\SoapClient' not found

soap - 将 BusinessObjects 存储库用作 Crystal Report 的数据源

java - 从 Java Soap 处理程序获取响应

java - HttpClient 异常 : java. lang.IllegalArgumentException: 主机参数为空