WSOP2 Synapse: URL parameter setting

I am trying to do something that seems simple but cannot get it to work. Basically I want the WSO2 API manager to add the url parameter to the REST call.

Setting and problem

I have the WSO2 API manager installed. I also have Tomcat running under a fake servlet someservlet that just logs whatever it receives and returns ok. I added the servlet as an API in the dispatcher, so I can either call the servlet directly or via the WSO2 API mgr.

I can run

curl http://localhost:8080/someservlet/servlet/toto?blob=blib&secret=pass

      

and the servlet works fine, it tells me that it got the / toto path and the blob and secret parameters .

I can run

curl -H "Authorization: Bearer [...]" --url "http://192.168.23.1:8280/someservlet/1.0/toto?blob=blib&secret=pass"

      

And he does the same. So far so good.

I want to run:

curl -H "Authorization: Bearer MqVQuHqLNphtPV3XF1CtXVmbyP8a" --url "http://192.168.23.1:8280/someservlet/1.0/toto?blob=blib"

      

(note that I removed the secret parameter )

... and get the same result.

So I want the API manager to add the URL secret = pass parameter .

First thing I tried: property broker

Use a Synapse XML configuration with the REST_URL_POSTFIX property .

I edited the API config file and added

<property name="REST_URL_POSTFIX" value="/blob?toto=titi" scope="axis2" type="STRING"/>

      

Now if I run

curl -H "Authorization: Bearer [...]" --url "http://192.168.23.1:8280/someservlet/1.0/toti?blab=blib&secret=puss"

      

as if i run someservlet / 1.0 / blob? toto = titi : all my path and options disappeared and got confused with the configured ones. Hey, how is it supposed to work, right?

Problems:

  • this doesn't add anything to the url, it sets the url postfix which means the existing parameters disappear (in the above example blab = blib )
  • it must start with "/" to be a valid value, so I can't just add & secret = pass (of course, due to problem 1, that would be useless anyway)

So basically it prevents me from adding the final & secret = pass .

Second thing I tried: rewrite the proxy url

I found this middleman, and while it probably won't do the trick, it's a nice advantage: I can just call with secret = foo and force the middleman to replace it with secret = pass .

I put this in my config file:

<rewrite>
  <rewriterule>
    <action type="replace" value="pass" fragment="query" regex="foo"/>
  </rewriterule>
</rewrite>

      

This does not work. At first I thought I had no action parameters. But the error message is:

Malformed URL when processing /someservlet/1.0/toti?blab=blib&amp;secret=foo

      

distorted? More details in the exception stack trace:

java.net.MalformedURLException: no protocol: /someservlet/1.0/toti?blab=blib&secret=foo

      

So what happens when intermediaries (log or rewrite) receive a message whose "To:" field points to a URL without a protocol!

Of course I search all over the world and there are cases where others have logMediator: To: / blabla and other (most) cases where they have logMediator: To: http: // blabla . I really don't see what is causing the difference. :-(

So this is where I got stuck !!: - (

Verification capability

I know there should probably be a sledgehammer solution:

  • use property to store full path and all parameters
  • implement your own mediator (in Java for example) to change these parameters
  • use the REST_URL_POSTFIX property to put the modified postfix in the call

However, I believe this problem should have a simpler solution.

I have some sort of hope that someone will point me to a simple resource (mediator, sample, syntax error, something) that I haven't found and that does exactly what I want. Optimism ... :-)

Thanks for reading. Any ideas?

+3


source to share


3 answers


For those who may have the same problem, here's a different solution, simpler and works.

Go to the carbon administration portal, to the API list, find the corresponding API and click on it. This leads to the XML API configuration. After the "address" field (and at the same level in XML) add the field:



<property name="Authorization" value="stuff to add" scope="transport"/>

      

This adds the property "Authorization" with the value "material".

0


source


As I understand it, you are trying to add secret=pass

to your endpoint url. When you call the API, I believe you are sending this parameter. At the same time, you are also sending the parameter "blab=blib"

. But in ESB you need to change this to"toto=titi"



The method you are using is permissions (i.e. using the property REST_URL_POSTFIX

). In this case, it will overwrite all parameter parameters. But before using that, you can save the query parameter and use "REST_URL_POSTFIX"

you can get the final REST url you need. (yes, this is the last option you mentioned as "being tried") In the "TO" address, you will only have the address after the port number. If you use the property "" POST_TO_URI"

, yopu will show the full destination url printed in the log, but we use it if there is any proxy configured between the BE service and ESB

+1


source


I solved my problem.

That's what I'm doing:

  • in config file, get postfix url using REST_URL_POSTFIX and put it in property
  • change property in java proxy
  • use REST_URL_POSTFIX to set new postfix in XML

For the first step, I use this line:

<property name="querystrings" expression="get-property('axis2', 'REST_URL_POSTFIX')"/>

      

it took a while in the corner because there are many lines not working for me, it took 2 parameters (axis2 and REST ...) to get this result.

For the second step, this is the config XML:

<class name="mypackage.AddUrlParamMediator">
    <property name="paramName" value="mykey"/>
    <property name="paramValue" value="mysecret"/>
</class>

      

this is the mediator class (no import):

public class AddUrlParamMediator extends AbstractMediator {
    private String paramName = "default";
    private String paramValue = "default";

    public void setParamName(String paramName) {
        this.paramName = paramName;
    }

    public void setParamValue(String paramValue) {
        this.paramValue = paramValue;
    }

    public boolean mediate(MessageContext synapseMsgContext) {
        SynapseLog log = this.getLog(synapseMsgContext);
        String urlNewParam = this.paramName + "=" + this.paramValue;
        Object queryStringsPpty = synapseMsgContext.getProperty("querystrings");
        if (queryStringsPpty == null) {
            log.error("### queryStringPpty=null, exiting!");
            return true;
        }
        String queryStrings = queryStringsPpty.toString();
        queryStrings = (queryStrings.contains("?"))
            ? queryStrings + "&" + urlNewParam
            : queryStrings + "?" + urlNewParam;
        synapseMsgContext.setProperty("querystrings", queryStrings);
        return true;
    }
}

      

and of course the third step in XML configuration:

<property name="REST_URL_POSTFIX" expression="get-property('querystrings')" scope="axis2" type="STRING"/>

      

So this whole thing basically sets the parameter at the end of the url. Hope this helps others.

+1


source







All Articles