Not able to call Dynamic Endpoints/URLs on the basis of Message Mediation polices in WSO2 API Manager

谁说我不能喝 提交于 2021-02-05 09:45:05

问题


I'm using APIM-3.1.0 and I need to Redirect APIs’ based upon header or request parameter. I have tried for request parameter but unable to call different API's. I have used below custom mediation policy and added it to a test API, but unable to call the different URLs. Every time I was calling API, I was getting output for the else part (URL mention in the else part) in below code even if I am passing the value of operation as menu.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="dynamic-endpoint-seq">
<property expression="json-eval($.operation)" name="operation" />
<filter regex="menu" source="$ctx:operation">
<then>
    <property name="C" expression="fn:concat('http://localhost:8080/Test/','getC')"/>
    <header name="To" expression="get-property('C')"/> 
</then>
<else>
    <property name="B" expression="fn:concat('http://localhost:8080/Test/','getB')"/>
    <header name="To" expression="get-property('B')"/>
</else>
</filter>
</sequence>

I was getting warning in Console even passing value of parameter as shown below:

[2020-07-24 17:20:38,643] WARN - SynapseJsonPath Json Payload is empty.

Is there a way to do the same or there is any error in mediation policy?


回答1:


Here, you can pass value in header with some variable name and define the same in mediation policies.

The below Code calls different endpoints on the basis of variable "check" is present or not, if it is present in header with any value,the endpoint_B will get called, if value of check is not present or check is not present in header, then it will call endpoint_C.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="dynamic-endpoint-seq-boolean">
<!--it checks the value for variabe check in header if there exist value for check 
then it will call B, if value not exist or check is not present then C called-->
<property name="uri.var.check" expression="get-property('transport','check')"/>
<filter source="boolean(get-property('uri.var.check'))" regex="false">
<then>
    <property name="C" expression="fn:concat('http://localhost:8080/Test/','getC')"/>
    <header name="To" expression="get-property('C')"/> 
</then>
<else>
    <property name="B" expression="fn:concat('http://localhost:8080/Test/','getB')"/>
    <header name="To" expression="get-property('B')"/>
</else>
</filter>
</sequence>

There is one more way to do the same thing, you can use Switch case in XML,as shown below and configure multiple endpoints. Here, if you pass the value of check as 'B' in header, endpoint_B will called, else if you pass value of 'check' as 'C' in header, then endpoint_C will get called, and if you pass any other value than 'B' or 'C' or not pass any value or even not pass check in header than default endpoint here endpoint_A will get called.

<sequence name="dynamic_ep_switch" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property name="uri.var.check" expression="get-property('transport','check')"/>
<switch source="get-property('uri.var.check')">
    <case regex="B">
       <!-- We are then assigning the endpoint which we need to route to in a property named service_ep in this step -->
        <property name="B" expression="fn:concat('http://localhost:8080/Test/','getB')"/>
        <header name="To" expression="get-property('B')"/>
    </case>
    <case regex="C">
        <property name="C" expression="fn:concat('http://localhost:8080/Test/','getC')"/>
        <header name="To" expression="get-property('C')"/>
    </case>
    <default>
        <property name="A" expression="fn:concat('http://localhost:8080/Test/','getA')"/>
        <header name="To" expression="get-property('A')"/>
    </default>
</switch>

There is one more way to use switch if your endpoint are running on same host and port by using property="rest_url_postfix" as shown in below code. Here,output will be same as above but some changes that you need to make are for the above code you need to select dynamic endpoint in Endpoints tab in WSO2-APIM publisher while for the below code, you select REST Endpoints and select value of endpoint as http://<host_name>:<port_number>// . For example, http://localhost:8080/Test/

<sequence name="dynamic_same_ep_switch" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<!-- The property which is retrieved as get-property('To')" stores the request URI for the API. Based on this value we will determine the endpoint which the request needs to be routed to.-->
<property name="uri.var.check" expression="get-property('transport','check')"/>
<switch source="get-property('uri.var.check')">
    <case regex="B">
       <!-- We are then assigning the endpoint which we need to route to in a property named service_ep in this step -->
        <property name="REST_URL_POSTFIX" value="getB" scope="axis2"/>
    </case>
    <case regex="C">
        <property name="REST_URL_POSTFIX" value="getC" scope="axis2"/>
    </case>
    <default>
        <property name="REST_URL_POSTFIX" value="getA" scope="axis2"/>
    </default>
</switch>
</sequence>

For anymore information regarding sample mediation sequences refer following links:

  • https://docs.wso2.com/display/APICloud/Sample+Mediation+Sequences
  • https://docs.wso2.com/display/ESB480/HTTP+Transport+Properties

Hope. This resolves your Query.



来源:https://stackoverflow.com/questions/63073764/not-able-to-call-dynamic-endpoints-urls-on-the-basis-of-message-mediation-police

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