WSO2 Synapse: setting a URL parameter

妖精的绣舞 提交于 2019-12-23 19:45:42

问题


I am trying to do something that seems straightforward but can't get it to work. Basically I want the WSO2 API manager to add a URL parameter to a REST call.

Setup and Problem

I have a WSO2 API manager installed. I also have a Tomcat running with a bogus servlet called someservlet that simply logs anything it receives and returns ok. I have added the servlet as an API in the manager, so I can either call the servlet directly or via 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 it's received the path /toto and parameters blob and secret.

I can run

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

And it does exactly the same. So far, so good.

What I want is to run:

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

(note that I've removed the secret parameter)

...and still get the same result.

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

First thing I tried: property mediator

Use Synapse XML config with property REST_URL_POSTFIX.

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"

it's as if I ran someservlet/1.0/blob?toto=titi: all my path and parameters have disappeared and been repaced with the configured ones. Hey, that's how it's supposed to work, isn't it!

Problems:

  1. this doesn't add something to the URL, it sets the URL postfix, meaning that existing parameters disappear (in the above example, blab=blib)
  2. it has to start with a "/" to be a valid value, so I can't just add &secret=pass (of course, because of problem 1, this would be useless anyway)

So basically this doesn't enable me to append the final &secret=pass.

Second thing I tried: rewrite URL mediator

I found this mediator, and although it probably won't do the trick, it's a good lead: I can just call with secret=foo, and get the mediator to replace it with secret=pass.

I put this in the config file:

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

This doesn't work. At first I thought I didn't have the action parameters right. But the error message is:

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

Malformed? There's more detail in the exception stack trace:

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

So what's happening is that the mediators (log or rewrite) receive a message whose "To:" field points to a URL with no protocol!

Of course I've been googling around, and there are some cases where I find other people have logMediator: To: /blabla, and other (most) cases where they have logMediator: To: http ://blabla. I don't really see what's causing the difference. :-(

So this is where I'm stuck!! :-(

Option to be tried

I'm aware that there's probably a sledgehammer solution that should work:

  1. use property to store the full path and all parameters
  2. implement my own mediator (e.g. in Java) to modify these parameters
  3. use property REST_URL_POSTFIX to put the modified postfix on the call

However I feel that this problem should have a simpler solution.

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

Thanks for reading. Any ideas?


回答1:


As i understood, you are trying to append the secret=pass to your endpoint url. When you invoke the API , i belive you are sending that parameter. Same time you are sending "blab=blib" parameter also. But in the ESB you need to change that to "toto=titi"

The method you used is right.(ie: Using property REST_URL_POSTFIX) In that case it will replace all requets parameter. But before using that, you can save the request parameter and using the "REST_URL_POSTFIX" you can achive the final REST url you need. (yes, that is the final option you mentioned as "to be tried") In the "TO" address, you will only have address after port number. If you use "" POST_TO_URI" property yopu will see the full adress URL printed in the log, but we use that if there is any proxy server configured between BE service and ESB




回答2:


I've solved my problem.

Here's what I do:

  1. in the config file, get the URL postfix using REST_URL_POSTFIX and put it in a property
  2. edit the property in a Java mediator
  3. use REST_URL_POSTFIX to set the new postfix in XML

For the 1rst step, I use this line:

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

this took some time to corner, because there are many lines out there that don't work for me, it took the 2 parameters (axis2 and REST...) to get it straight.

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 (without imports):

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 here's the third step in the XML config:

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

So all this basically sets a parameter at the end of the URL. Hope this helps others.




回答3:


For anyone who may have the same issue, here's another solution, simpler and works.

Go to the carbon admin portal, to the list of APIs, find the relevant API and click on it. This leads to the XML config of the API. After the "address" field (and at the same level in the XML) add the field:

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

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



来源:https://stackoverflow.com/questions/14523977/wso2-synapse-setting-a-url-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!