拦截器之外的CXF消息上下文

时间:2018-11-01 16:21:12

标签: java soap cxf

我正在使用apache cxf发送SOAP消息,我想要的是在调用完成后获取请求和响应有效负载。目前,我正在使用两个拦截器,并将有效负载放入message.getExchange().put(ExchangeContextEnum.RESPONSE_PAYLOAD.toString(), new String(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())));
}
}

请求拦截器

public class WSSLogOutInterceptor extends AbstractSoapInterceptor {

public WSSLogOutInterceptor() {
    super(Phase.USER_PROTOCOL);
}

@Override
public void handleMessage(SoapMessage message) throws Fault {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        SOAPMessage messageContent = message.getContent(SOAPMessage.class);

        messageContent.writeTo(baos);
        message.getExchange().put(ExchangeContextEnum.REQUEST_PAYLOAD.toString(), baos.toString());

    } catch (SOAPException | IOException e) {
        throw new Fault(e);
    }
}
}

1 个答案:

答案 0 :(得分:1)

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

我只是在拦截器中执行message.put(key, value),而不是在消息的交换中放入值。在通话后获取这些值 需要获得类似(String) ((BindingProvider) webService).getResponseContext().get(key)这样的响应上下文,其中key是您之前在消息中放入有效负载之前使用的相同值。现在是问题所在-您将找不到在响应上下文中放入传出链中的值。您可以使用简单的解决方法,并在消息交换中放入价值,然后在传入链中将其放入消息中。请注意我使用的阶段(POST_PROTOCOL),如果您使用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 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);