Support Questions
Find answers, ask questions, and share your expertise
Announcements
Check out our newest addition to the community, the Cloudera Innovation Accelerator group hub.

Rewrite of Absolute URL in Knox?

Expert Contributor

I am trying to define a rewrite pattern in Knox that would rewrite a URL like https://sandbox:8443/myLogo.png to https://sandbox:8443/gateway/default/myservice/myLogo.png. Is that even possible? An example would be helpful.

1 ACCEPTED SOLUTION

Look for example at this HBase rule which does something similar to what I believe you are asking. This rule however will handle anything under **/hbase due to the ** in {path=**}.

<rule dir="IN" name="WEBHBASE/webhbase/path/inbound" pattern="*://*:*/**/hbase/{path=**}?{**}">
    <rewrite template="{$serviceUrl[WEBHBASE]}/{path=**}?{**}"/>
</rule>

You can certainly also write a pattern for a single file. I assume here that your service's role is MYSERVICE which is why it is upper case in the $serviceUrl[MYSERVICE] function. That could hypothetically look something like this:

<rule dir="IN" name="MYSERVICE/myservice/logo" pattern="*://*:*/**/myservice/myLogo.png?{**}">
    <rewrite template="{$serviceUrl[MYSERVICE]}/myLogo.png?{**}"/>
</rule>

I wasn't sure about including the ?{**} in the rewrite template to propagate the query parameters but it is a good idea to include it in the match pattern to make it as general as possible. If you didn't include the ?{**} in the rewrite template the query parameters would not be copied over from the original matched URL. Either behavior may be what you want.

View solution in original post

4 REPLIES 4

Look for example at this HBase rule which does something similar to what I believe you are asking. This rule however will handle anything under **/hbase due to the ** in {path=**}.

<rule dir="IN" name="WEBHBASE/webhbase/path/inbound" pattern="*://*:*/**/hbase/{path=**}?{**}">
    <rewrite template="{$serviceUrl[WEBHBASE]}/{path=**}?{**}"/>
</rule>

You can certainly also write a pattern for a single file. I assume here that your service's role is MYSERVICE which is why it is upper case in the $serviceUrl[MYSERVICE] function. That could hypothetically look something like this:

<rule dir="IN" name="MYSERVICE/myservice/logo" pattern="*://*:*/**/myservice/myLogo.png?{**}">
    <rewrite template="{$serviceUrl[MYSERVICE]}/myLogo.png?{**}"/>
</rule>

I wasn't sure about including the ?{**} in the rewrite template to propagate the query parameters but it is a good idea to include it in the match pattern to make it as general as possible. If you didn't include the ?{**} in the rewrite template the query parameters would not be copied over from the original matched URL. Either behavior may be what you want.

Expert Contributor

Did not work for me. The special point is that I need the following pattern to be accepted "*://*:*/logo.png" I don't want it under **/myservice

Unfortunately that isn't going to work, at least not the way you are thinking about it. There are a few reasons for this. The first is that each topology file is deployed within the server "under the covers" as a JEE WAR. In particular it is deployed on a context path made up of the value of gateway.path in gateway-site.xml and the name of the topology file. So all of the URLs will start with {gateway.path}/{topology} no matter what. Beyond that each service or app generally then carves out a unique subcontext to avoid collisions.

However, the way you need to be thinking about this is to rewrite the URL in the outbound response in which it is contained. This is certainly more complex but you can find some examples of how to do this in the HBase integration in data/services/hbase/0.98.0. It involves the use of filters on the outbound response bodies.