gpt4 book ai didi

java - CXF 消息的上下文位于拦截器之外

转载 作者:行者123 更新时间:2023-12-02 10:38:22 24 4
gpt4 key购买 nike

我正在使用 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);
}
}
}

最佳答案

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

我只是在拦截器中执行 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);

关于java - CXF 消息的上下文位于拦截器之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53105290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com