Java CXF消息';拦截器外部的上下文

Java CXF消息';拦截器外部的上下文,java,soap,cxf,Java,Soap,Cxf,我正在使用ApacheCXF发送一条SOAP消息,我想要的是在调用完成后获得请求和响应有效负载。目前我正在使用两个拦截器,并将有效负载放入消息的上下文中,如message.getExchange().put(ExchangeContextEnum.RESPONSE_PAYLOAD.toString(),新字符串(PAYLOAD,Charset.forName(StandardCharsets.UTF_8.name()) 我不想马上在拦截器中处理它们,因为我需要一系列调用的请求和响应。另外,为了简

我正在使用ApacheCXF发送一条SOAP消息,我想要的是在调用完成后获得请求和响应有效负载。目前我正在使用两个拦截器,并将有效负载放入消息的上下文中,如
message.getExchange().put(ExchangeContextEnum.RESPONSE_PAYLOAD.toString(),新字符串(PAYLOAD,Charset.forName(StandardCharsets.UTF_8.name())

我不想马上在拦截器中处理它们,因为我需要一系列调用的请求和响应。另外,为了简单起见,我希望避免使用任何类型的存储,并且不必处理可能的并发问题

我可以在调用完成或上下文完全丢失后获取这些值吗

一些代码:

webService.call(object)
//here i'd like to get payloads
用于响应的拦截器:

public class LogInInterceptor extends AbstractPhaseInterceptor<Message> {

public LogInInterceptor() {
    super(Phase.RECEIVE);
}

@Override
public void handleMessage(Message message) throws Fault {
    InputStream in = message.getContent(InputStream.class);
    byte payload[] = new byte[0];
    try {
        payload = IOUtils.readBytesFromStream(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ByteArrayInputStream bin = new ByteArrayInputStream(payload);
    message.setContent(InputStream.class, bin);

    message.getExchange().put(ExchangeContextEnum.RESPONSE_PAYLOAD.toString(), new String(payload, Charset.forName(StandardCharsets.UTF_8.name())));
}
}

我最终得到了以下解决方案:

我没有在消息的交换中输入值,而是在拦截器中输入
message.put(key,value)
。在呼叫您之后获取这些值 需要获取响应上下文,如
(字符串)((BindingProvider)webService)。getResponseContext().get(key)
,其中
key
与您之前用于在消息中放入有效负载的值相同。现在问题来了——在响应上下文中找不到放在传出链中的值。您可以使用简单的解决方法,在消息的交换中输入值,然后在输入链中获取并将其放入消息中。请注意我使用的阶段(POST_协议),如果您使用WSS,它会很有帮助

代码如下:

public class LoggingOutPayloadInterceptor extends AbstractSoapInterceptor {

public static final String OUT_PAYLOAD_KEY = "use.your.package.name.OUT_PAYLOAD_KEY";

public LoggingOutPayloadInterceptor() {
    super(Phase.POST_PROTOCOL);
}

@Override
public void handleMessage(SoapMessage soapMessage) throws Fault {

    Document document = soapMessage.getContent(SOAPMessage.class).getSOAPPart();
    StringWriter stringWriter = new StringWriter();
    try {
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(stringWriter));
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    soapMessage.getExchange().put(OUT_PAYLOAD_KEY, stringWriter.toString());
}
}

}

public class LoggingOutPayloadInterceptor extends AbstractSoapInterceptor {

public static final String OUT_PAYLOAD_KEY = "use.your.package.name.OUT_PAYLOAD_KEY";

public LoggingOutPayloadInterceptor() {
    super(Phase.POST_PROTOCOL);
}

@Override
public void handleMessage(SoapMessage soapMessage) throws Fault {

    Document document = soapMessage.getContent(SOAPMessage.class).getSOAPPart();
    StringWriter stringWriter = new StringWriter();
    try {
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(stringWriter));
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    soapMessage.getExchange().put(OUT_PAYLOAD_KEY, stringWriter.toString());
}
public class LoggingInPayloadInterceptor extends AbstractSoapInterceptor {

public static final String IN_PAYLOAD_KEY = "use.your.package.name.IN_PAYLOAD";

public LoggingInPayloadInterceptor() {
    super(Phase.POST_PROTOCOL);
    addAfter(SAAJInInterceptor.class.getName());
}

@Override
public void handleMessage(SoapMessage message) throws Fault {
    Document document = message.getContent(SOAPMessage.class).getSOAPPart();
    StringWriter stringWriter = new StringWriter();
    try {
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(stringWriter));
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    message.put(IN_PAYLOAD_KEY, stringWriter.toString());
    message.put(LoggingOutPayloadInterceptor.OUT_PAYLOAD_KEY, message.getExchange().get(LoggingOutPayloadInterceptor.OUT_PAYLOAD_KEY));
}
webService.call(...);
String inPayload = (String)((BindingProvider)webService).getResponseContext().get(LoggingInPayloadInterceptor.IN_PAYLOAD_KEY);
String outPayload = (String) ((BindingProvider) webService).getResponseContext().get(LoggingOutPayloadInterceptor.OUT_PAYLOAD_KEY);