16.12. POJO モードで SOAP ヘッダーを取得および設定する方法


POJO は、camel-cxf エンドポイントが Camel エクスチェンジを生成または消費した場合に、データフォーマットが Java オブジェクトのリストであることを意味します。Camel はメッセージボディーをこのモードで POJO として公開しても、camel-cxf は SOAP ヘッダーの読み取りおよび書き込みへのアクセスを提供します。ただし、CXF インターセプターは処理後にヘッダーリストから帯域外 SOAP ヘッダーを削除するため、POJO モードの camel-cxf で利用できるのは帯域外 SOAP ヘッダーのみです。

以下の例は、SOAP ヘッダーの取得/設定方法を示しています。ある Camel-cxf エンドポイントから別のエンドポイントに転送するルートがあるとします。つまり、SOAP Client Camel CXF service です。要求が CXF サービスに送信される前に(1)で SOAP ヘッダーの取得/挿入に 2 つのプロセッサーをアタッチし、応答が SOAP クライアントに戻る前に(2)を割り当てることができます。この例では プロセッサー(1)および(2)は InsertRequestOutHeaderProcessor および InsertResponseOutHeaderProcessor です。ルートは以下のようになります。

from("cxf:bean:routerRelayEndpointWithInsertion")
    .process(new InsertRequestOutHeaderProcessor())
    .to("cxf:bean:serviceRelayEndpointWithInsertion")
    .process(new InsertResponseOutHeaderProcessor());
Copy to Clipboard Toggle word wrap

Bean routerRelayEndpointWithInsertion および serviceRelayEndpointWithInsertion は以下のように定義されます。

@Bean
public CxfEndpoint routerRelayEndpointWithInsertion() {
    CxfSpringEndpoint cxfEndpoint = new CxfSpringEndpoint();
    cxfEndpoint.setServiceClass(org.apache.camel.component.cxf.soap.headers.HeaderTester.class);
    cxfEndpoint.setAddress("/CxfMessageHeadersRelayTest/HeaderService/routerRelayEndpointWithInsertion");
    cxfEndpoint.setWsdlURL("soap_header.wsdl");
    cxfEndpoint.setEndpointNameAsQName(
        QName.valueOf("{http://apache.org/camel/component/cxf/soap/headers}SoapPortRelayWithInsertion"));
    cxfEndpoint.setServiceNameAsQName(SERVICENAME);
    cxfEndpoint.getFeatures().add(new LoggingFeature());
    return cxfEndpoint;
}

@Bean
public CxfEndpoint serviceRelayEndpointWithInsertion() {
    CxfSpringEndpoint cxfEndpoint = new CxfSpringEndpoint();
    cxfEndpoint.setServiceClass(org.apache.camel.component.cxf.soap.headers.HeaderTester.class);
    cxfEndpoint.setAddress("http://localhost:" + port + "/services/CxfMessageHeadersRelayTest/HeaderService/routerRelayEndpointWithInsertionBackend");
    cxfEndpoint.setWsdlURL("soap_header.wsdl");
    cxfEndpoint.setEndpointNameAsQName(
        QName.valueOf("{http://apache.org/camel/component/cxf/soap/headers}SoapPortRelayWithInsertion"));
    cxfEndpoint.setServiceNameAsQName(SERVICENAME);
    cxfEndpoint.getFeatures().add(new LoggingFeature());
    return cxfEndpoint;
}
Copy to Clipboard Toggle word wrap

SOAP ヘッダーは Camel メッセージヘッダーとの間で伝播されます。Camel メッセージヘッダー名は org.apache.cxf.headers.Header.list です。これは CXF で定義された定数です(org.apache.cxf.headers.Header.HEADER_LIST)。ヘッダーの値は、CXF SoapHeader オブジェクト(org.apache.cxf.binding.soap.SoapHeader)の List です。以下のスニペットは InsertResponseOutHeaderProcessor (応答メッセージに新しい SOAP ヘッダーを挿入)です。InsertResponseOutHeaderProcessor と InsertRequestOutHeaderProcessor の両方で SOAP ヘッダーにアクセスする方法は実際には同じです。2 つのプロセッサーの唯一の違いは、挿入された SOAP ヘッダーの方向を設定することです。

InsertResponseOutHeaderProcessor の例は、CxfMessageHeadersRelayTest にあります。

public static class InsertResponseOutHeaderProcessor implements Processor {

    public void process(Exchange exchange) throws Exception {
        List<SoapHeader> soapHeaders = CastUtils.cast((List<?>)exchange.getIn().getHeader(Header.HEADER_LIST));

        // Insert a new header
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><outofbandHeader "
            + "xmlns=\"http://cxf.apache.org/outofband/Header\" hdrAttribute=\"testHdrAttribute\" "
            + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:mustUnderstand=\"1\">"
            + "<name>New_testOobHeader</name><value>New_testOobHeaderValue</value></outofbandHeader>";
        SoapHeader newHeader = new SoapHeader(soapHeaders.get(0).getName(),
                       DOMUtils.readXml(new StringReader(xml)).getDocumentElement());
        // make sure direction is OUT since it is a response message.
        newHeader.setDirection(Direction.DIRECTION_OUT);
        //newHeader.setMustUnderstand(false);
        soapHeaders.add(newHeader);

    }

}
Copy to Clipboard Toggle word wrap
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat