Mapping mediators

Mapping mediators are used for mapping HTTP Requests from API Manager to other endpoints. Each API requires its own mediator, writing of which will be described in sections below.

Current mediators are at api_manager repository on main branch.

git clone https://git.inter-iot.eu/Inter-IoT/api_manager.git 
cd api_manager/Definitions/

Writing mapping mediator

Each mediator consists of specific components which decide where to map the requests.

Example:
<sequence name="mediator_name" trace="disable" xmlns="http://ws.apache.org/ns/synapse">

    <property name="endpoint_1" value="http://example.com/endpoint/one/"/>
    <property name="endpoint_2" value="http://example.com/endpoint/two/"/>

    <switch source="syn:get-property('To')">
        <case regex=".*/test/one/.*">
            <property name="service_ep" expression="fn:concat(get-property('endpoint_1'), 'test')"/>
        </case>
        <case regex=".*/test/two/.*">
            <property name="service_ep" expression="fn:concat(get-property('endpoint_2'), 'test')"/>
        </case>
    </switch>

    <log level="full">
        <property name="url" expression="get-property('service_ep')"/>
        <property name="Http_Method" expression="get-property('axis2', 'HTTP_METHOD')"/>
        <property name="body" expression="$body"/>
        <property name="JSON-Payload" expression="json-eval($.)"/>
    </log>

    <property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
    <header name="To" expression="syn:get-property('service_ep')"/>
</sequence>

Components:

  • Each mediator starts with a <sequence> tag, which describes the start of the sequence, you can then add the sequence name and etc.
  • For storing variables we use <properties> tag, which stores some value, we can later use for some operations like concating and etc.
  • The core of the whole mapping are the <switch> statements, but as opposed in Java, switch statements in mediators are like else if statements in other programming languages. Each switch has a source property, which decides what case statement it should execute.
  • Each switch statement must contain at least one <case> statement, each case should contain a regex property, which decides if the case is right for this specific request.
  • The <log> tag is useful for logging and displaying informational data to the user and or developer. Whatever property is declared in the log tag it will be displayed in the system logs.
  • At the end there is a property with a name "REST_URL_POSTFIX", this property removes postfix of the request, but there are many different properties like this, for very specific property or use-case we suggest looking it up at the official WSO2 API Manager documentation for the most up-to-date informations.
  • At the very end we set the exit endpoint value which is saved in the property "service_ep" and we use <header> tag for it. The name of the header must be "To", that is how WSO2 API Manager names their request address.