0

I am using camel to implement a proxy over a new backend that looks like an older interface. The older API has username/password credentials in the request body and the new backend service uses basic auth. I have an XSL that will extract the un/pw, do a simple lookup against an XML database (the credentials might not map exactly), and will return the correct credentials as a base64 encoded string. I cannot figure out how to set this as an http Authentication header value (e.g. how to process an XSL transform as an expression in .setHeader() call).

I have SOAP requests that look like this:

<soapenv:Envelope>
<soapenv:Body>
    <XService>
        <_Header  username="demo" password="demo"/>
        <_Body>
            <_RequestParameters xsi:type="RequestServiceReport">
            ...
            </_RequestParameters>
        </_Body>
    </XService>
</soapenv:Body>

and my route (using Java DSL) looks sort of like this:

from("jetty:http://161.228.88.168:8080/sap2rjm")
.choice()
.when().simple("${header.channel}")
    ...
.when().simple("${in.header.emx} == 'authenticate'")
    ...
    .endChoice()
// If the request is for a report, route it to the new service
.when().xpath("//_RequestParameters[@type='RequestServiceReport']")
    // TODO: How to get header from the body of the message and set as the header value?
    // Stylesheet transform_credentials will extract username/password from body, transform
    // for the new service (dev.reportjam) and will base4 encode to produce a string like this one...
    .setHeader("Authorization", constant("Basic ZGVtbzpkZW1v"))
    .to("xslt:transform_request.xsl")
    .to("http://dev.reportjam.com/services/ReportMix?bridgeEndpoint=true")
    .to("xslt:transform_response.xsl")
    .removeHeaders("*")
    .endChoice()

.otherwise()
    ...
    .endChoice()
.end();

I do have another stylesheet that will process the soap request, extract the un/pw, apply some logic to transform it, and then base64 encode it but I do not know how to call this in the setHeader() call above.

Thanks

4

1 回答 1

0

您可以使用 xpath 从 XML 正文中获取某些内容,然后将其存储为标头。 http://camel.apache.org/xpath

.setHeader("foo", xpath("/foo/bar"))

诀窍是编写 xpath 表达式以便它工作。由于您的 XML 消息使用名称空间,因此您还需要在 xpath 表达式中使用它们。有关更多详细信息,请参阅该链接。

此外,您应该启用流缓存,因为您将读取消息正文作为此 xpath 表达式评估的一部分。

有关流缓存和码头,请参阅此链接的顶部:http: //camel.apache.org/jetty

于 2013-05-06T07:00:11.980 回答