第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

CXF 消息在攔截器之外的上下文

CXF 消息在攔截器之外的上下文

溫溫醬 2021-12-22 20:13:27
我正在使用 apache cxf 發(fā)送 SOAP 消息,我想要的是在調(diào)用完成后獲取請求和響應(yīng)有效負(fù)載。目前我正在使用兩個攔截器并將有效載荷放入消息的上下文中message.getExchange().put(ExchangeContextEnum.RESPONSE_PAYLOAD.toString(), new String(payload, Charset.forName(StandardCharsets.UTF_8.name())));。我不想在攔截器本身中立即處理它們,因?yàn)槲倚枰獙σ幌盗姓{(diào)用的請求和響應(yīng)。此外,為了簡單起見,我想避免進(jìn)行任何類型的存儲,而不必處理可能的并發(fā)問題。在調(diào)用完成或此時上下文完全丟失后,我可以獲取這些值嗎?一些代碼:webService.call(object)//here i'd like to get payloads響應(yīng)攔截器:public class LogInInterceptor extends AbstractPhaseInterceptor<Message> {public LogInInterceptor() {    super(Phase.RECEIVE);}@Overridepublic 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);}@Overridepublic 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 回答

?
慕虎7371278

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個贊

我最終得到了以下解決方案:


我只是message.put(key, value)在攔截器中進(jìn)行,而不是將值放入消息交換中。要在調(diào)用后獲取這些值,您需要獲取類似的響應(yīng)上下文,(String) ((BindingProvider) webService).getResponseContext().get(key)其中key與之前用于將有效負(fù)載放入消息中的值相同?,F(xiàn)在問題來了——你不會在響應(yīng)上下文中找到你放在傳出鏈中的值。您可以使用簡單的解決方法并將價(jià)值放入消息的交換中,然后在傳入鏈中獲取它并將其放入消息中。注意我使用的階段(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);


查看完整回答
反對 回復(fù) 2021-12-22
  • 1 回答
  • 0 關(guān)注
  • 158 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號