Using Registry Resources for Output Mapping
WSO2 DAS allows you to map custom content from registry for the XML, JSON and Text mapping  types. Custom mapping is stored as a registry resource to be used by the publisher instead of being specified as inline input. WSO2 DAS caches this resource at runtime, and the cache is updated after every cache timeout specified in minutes. If caching is not required, specify the cache timeout as 0.
The following tutorial illustrates how to carry out custom mapping using a registry resource.
Save the following configuration as an XML file in a preferred location in your machine.
<eventPublisher ... xmlns="http://wso2.org/carbon/eventpublisher"> <from ... /> <mapping customMapping="enable" type="text"> <registry cacheTimeoutDuration="15">conf:/templates/en/message</registry> </mapping> <to ... /> </eventPublisher>
- Log into the WSO2 DAS Management Console.
- Define an event stream as follows. For detailed instructions to create event streams, see Understanding Event Streams and Event Tables.
Enter basic information for the stream as follows.
Parameter Value Event Stream Name sensorsteam
Event Stream Version 1.0.0
Add attributes as follows.
Attribute Category Attribute Name Attribute Type Meta Data sensorId
int
 sensorName
string
 language
string
Correlation Data longitude
double
 latitude
double
Payload Data sensorValue
double
- Add the XML file you created as a registry resource as follows. For more information about the registry, see Registry.
- In the Main tab expand the Registry section and click Browse.
- Under Tree View, navigate to
/_system/config
directory and click Detail View. Click Add Collection. In the Name field, enterÂTemplate
 and click Add. This creates a new sub directory namedÂTemplate
 in theÂ/_system/config
 directory. Navigate to theÂ
/_system/config/Template
sub directory and click Add Resource. Then enter information as shown in the table below, and click Add.
Field Value Method Upload content from file
File Browse and select the XML file you saved in step 1. Name CustomOutputMapping
Media Type application/xml
Create a new publisher as follows. For more information, see Creating Alerts - Creating event publishers.
Field Value Event Publisher Name samplelogger
Event Source sensorstream
Output Event Adapter Type logger
Under Mapping Configuration, select Text for the Message Format field. Then click Advanced. This expands the Create a New Event Publisher page to display the Text Mapping section. Select the Pick from Registry option. In the Registry Path field, navigate to the registry location where you saved the registry resource created in step 3.
In this example, the default value of
15
is left unchanged for the Cache Timeout (in minutes) field. Therefore, this resource will be cached every 15 minutes.- Click Add Event Publisher.
In the Tools tab, click Event Simulator. In the Event Stream Name field, select sensorstream . Enter the following values for the parameters displayed, and then click Send to simulate a single event.
Parameter Value sensorid 10
sensorName Temperature
language en longitude 79.861256
latitude 6.927131
sensorValue 23.0
Â
The output for this event is logged in the terminal as follows.Â
Â
Using custom registry paths
The registry resource path itself can be parameterized using runtime attribute values. e.g., If the event stream to which the logger
publisher in the example above is connected has an attribute named meta_language
, a different registry source is selected depending on the runtime value of the meta_language
attribute.
The following tutorial illustrates how to change the registry resource path from which a custom output mapping is picked based on the value of an attribute.
The sensor stream
event stream used in the previous tutorial is also used in this tutorial.
Â
Log into the DAS Management Console.
- Create two sub directories in the
Template
directory you created in the previous tutorial, and add a resource to each of these sub directories as described below. For more information about the registry, see Registry.- In the Main tab expand the Registry section and click Browse.
- Under Tree View, navigate to theÂ
/_system/config/Template
 sub directory. Create two more sub directories in it namedÂen
andÂfr
. Navigate to theÂ
/_system/config/Template/en
 sub directory and click Add Resource. Then enter information as shown in the table below, and click Add.Field Value Method Create Text content
Name message
Media Type text/plain
Content Welcome to sensor data Sensor ID : {{meta_sensorId}} Sensor Name : {{meta_sensorName}} Sensor located at ({{correlation_longitude}}, {{correlation_latitude}}) Value : {{sensorValue}} Humidity : {{humidity}}
Navigate to theÂ
/_system/config/Template/fr
 sub directory and click Add Resource. Then enter information as shown in the table below, and click Add.Field Value Method Create Text content
Name message Media Type text/plain
Content Bienvenue to capteur données Sensor ID : {{meta_sensorId}} Sensor Name : {{meta_sensorName}} Sensor located at ({{correlation_longitude}}, {{correlation_latitude}}) Value : {{sensorValue}} Humidity : {{humidity}}
Delete the publisher named
sample logger
and redefine it as follows. For more information, see Creating Alerts - Creating event publishers.
Field Value Event Publisher Name samplelogger
Event Source sensorstream
Output Event Adapter Type logger
Under Mapping Configuration, select Text for the Message Format field. Then click Advanced. This expands the Create a New Event Publisher page to display the Text Mapping section. Select the Pick from Registry option. Then enter information as follows.
Field Value Registry Path conf:/templates/{{meta_language}}/message
Cache Timeout (in minutes) 15 Log into the DAS Management Console.
In the Tools tab, click Event Simulator. In the Event Stream Name field, selectÂ
sensorstream
. Send three events with the following values.Parameter Value for Event 1 Value for Event 2 Value for Event 3 sensorid 10
10
10
sensorName Temperature
Temperature
Temperature
language en
fr
ru
longitude 79.861256
79.861256
79.861256
latitude 6.927131
6.927131
6.927131
sensorValue 23.0
23.0
23.0
The three events are logged as follows.Â
Note that an error is returned for the third event. This is because no output mapping was done for theÂru
language.