0
votes

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

1
Please show some code or similar on what you have. What components do you use? It's very hard to figure out what you really want from this question.Petter Nordlander

1 Answers

0
votes

You can use xpath to grab something from an XML body and then store that as a header. http://camel.apache.org/xpath

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

The trick is to write the xpath expression so it works. As your XML message uses namespaces you need to use them in the xpath expression also. See that link for more details.

Also you should enable stream caching as you will read the message body as part of this xpath expression evaluation.

See the top of this link about stream caching and jetty: http://camel.apache.org/jetty