wso2wso2-enterprise-integratorwso2-integration-studio

Is it possible to make your REST API redirect link dynamic/configurable?


I have a REST API on Enterprise Integrator that uses a db lookup mediator to search a microsoft sql server database and redirects based on the whether or not the data exists in the db. I need to make the redirect part of the code configurable/dynamic as it wouldn't make sense to constantly update the url and redeploy every time the url changes.

<api xmlns="http://ws.apache.org/ns/synapse" name="DBLookupAPI" context="/dblookup">
   <resource methods="GET" uri-template="/{UserCode}">
      <inSequence>
         <log level="custom">
            <property name="Value" expression="get-property('uri.var.UserCode')"/>
         </log>
         <dblookup>
            <connection>
               <pool>
                  <driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver>
                  <url>jdbc:sqlserver://10.1.1.111\test;databaseName=UserDB</url>
                  <user>admin</user>
                  <password>admin</password>
               </pool>
            </connection>
            <statement>
               <sql>select UserCode from UserDB.dbo.Users where UserCode =?;</sql>
               <parameter expression="get-property('uri.var.UserCode ')" type="CHAR"/>
               <result name="foundnr" column="UserCode "/>
            </statement>
         </dblookup>
         <log level="custom">
            <property name="Value" expression="get-property('foundnr')"/>
         </log>
         <filter source="boolean(get-property('foundnr'))" regex="true">
            <then>
               <log>
                  <property name="Message" value="Name Exists Lets redirect"/>
               </log>
               <property name="HTTP_SC" value="302"/>
               <property name="Location" value="https://wso2.com/" scope="transport"/>
            </then>
            <else>
               <log>
                  <property name="Message" value="Name Does Not Exist Lets redirect"/>
               </log>
               <property name="HTTP_SC" value="302"/>
               <property name="Location" value="https://www.youtube.com/" scope="transport"/>
            </else>
         </filter>
         <respond/>
      </inSequence>
      <outSequence/>
      <faultSequence/>
   </resource>
</api> 

                
                    

Solution

  • You can use the property mediator as below.

    <property name="Location" expression="get-property('registry','REGISTRY_PATH')"/>
    

    Below are the possible options for get-property method.

    1. Read from registry

    get-property('registry', String registryPath@propertyName)

    get-property('registry', String registryPath)

    1. Read from Java System property

    get-property('system', String propertyName)

    1. Read from the environment

    get-property('env', String propertyName)

    1. Read from a file

    get-property('file', String propertyName)

    1. Read from the context

    You can use the class mediator or any other mediator and set the redirect url to the context and use the following property mediator to retrieve it from the context.

    <property name="Location" expression="$ctx:ERROR_MESSAGE"/>
    

    Please refer the documentation - https://ei.docs.wso2.com/en/latest/micro-integrator/references/mediators/property-reference/accessing-properties-with-xpath/#get-property-function