Unknown macro: {next_previous_links}
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Introduction to HTTP REST publisher

Http publisher allows you to send data as a JSON object to a predefined set of data fields in a DAS/CEP server. The data structure with predefined fields is defined in an /wiki/spaces/TESB/pages/32604253. The data is converted to the format defined by the /wiki/spaces/TESB/pages/32604253 and sent via the WSO2 data-bridge component. You can also send custom key-value pairs with data events.

Custom fields with data stream

The data bridge data agent has a map data structure that enables you to send an arbitrary number of string key-value pairs. The other data structures are the three object arrays corresponding to the key-value pairs of metadata, correlation data, and payload data of fixed stream definitions. You can change the key-value pairs in the map data structure from message to message, but they all should be of the string data type.

You can put the data types of these custom key-value pairs into three groups according to the transmission category.

  1. When the key starts with meta: The data field is considered as a metadata custom field. It is sent with metadata and referred as meta_ key prefix.
  2. When the key starts with correlation: The data field is considered as a correlation data custom field. It is sent with correlation data and  referred as correlation_ key prefix.
  3. When the key starts with payload or any other string: The data field is considered as a payload data custom field. It is sent with payload data and saved in referred as key prefix.

Dependencies

In order to publish data to WSO2 DAS/CEP through a custom data agent, you need to have the following dependencies. You can configure the dependencies either using the class path or using the POM file.

Adding dependencies using class path

Add the JAR files listed below to your class path. Note that ${carbon.commons.version} refers to the version of the carbon-commons github repository -  https://github.com/wso2/carbon-commons/. It is always recommended to use the jar file from the latest released version.

  • httpclient-4.2.5.wso2v1.jar

  • httpcore-4.3.3.wso2v1.jar
  • gson-2.1.jar

Adding dependencies using POM file

Alternatively, add the following Maven project dependency entries to your POM file. Note that ${carbon.commons.version} refers to the version of the carbon-commons github repository -  https://github.com/wso2/carbon-commons/. It is always recommended to use the dependency entry from the latest released version.

 

Maven repository
<repositories>
    <repository>
        	<id>wso2.snapshots</id>
        	<name>Apache Snapshot Repository</name>
        	<url>http://maven.wso2.org/nexus/content/repositories/snapshots/</url>
        	<snapshots>
            	<enabled>true</enabled>
            	<updatePolicy>daily</updatePolicy>
        	</snapshots>
        	<releases>
            	<enabled>false</enabled>
        	</releases>
    	</repository>
</repositories>
Maven pom dependency
<dependency>
    <groupId>org.apache.ws.commons.axiom.wso2</groupId>
    <artifactId>axiom</artifactId>
	<version>${orbit.version.axiom}</version>
</dependency>
<dependency>
    <groupId>org.wso2.orbit.org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
	<version>${httpclient.wso2.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents.wso2</groupId>
    <artifactId>httpcore</artifactId>
	<version>${httpcore.wso2.version}</version>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>${gson.version}</version>
</dependency>     

 

HTTP REST publisher

As a prerequisite for this sample, you need to define the streams in the receiver server (WSO2 DAS/CEP). For information on defining event streams, see Working with Event Streams.

Follow the procedure below to use the data publisher.

  1. Initialize the HTTP Client and HTTP POST method as follows.

    HttpClient httpClient = new SystemDefaultHttpClient();
    HttpPost method = new HttpPost(url);
  2. Then we need to build event as a JSON object. Here We have used JsonObject class provided by gson library. Main JSON event could consist of 3 JSON objects for meta, correlation and payload data. We can include required properties and their values for each object. 

    JsonObject event = new JsonObject();
    JsonObject metaData = new JsonObject();
    JsonObject correlationData = new JsonObject();
    JsonObject payLoadData = new JsonObject();
    
    metaData.addProperty("timestamp", System.currentTimeMillis());
    metaData.addProperty("isPowerSaverEnabled", false);
    metaData.addProperty("sensorId", count);
    metaData.addProperty("sensorName", "temperature");
    
    correlationData.addProperty("longitude", 2332.424);
    correlationData.addProperty("latitude", 2323.23232);
    
    payLoadData.addProperty("humidity", 2.3f);
    payLoadData.addProperty("sensorValue", 23423.234);
    
    event.add("metaData", metaData);
    event.add("correlationData", correlationData);
    event.add("payloadData", payLoadData);


  3. Once JSON Object is created, it will be converted as a StringEntity by enclosing as event object. StringEntity is a class provided by apache HTTP library. This entity will be set to HTTP method.

    StringEntity entity = new StringEntity("{\"event\": " + event + "}");;
    method.setEntity(entity);
  4.  If URL endpoint is https, we need to add Authorization Basic header by encoding username and password. Finally we will use HTTP client to execute defined HTTP Method.

    if (url.startsWith("https")) {
    	method.setHeader("Authorization", "Basic " + Base64.encode((username + ":" + password).getBytes()));
    }
    httpClient.execute(method).getEntity().getContent().close();

    For more information on the usage of data publishers, see the sample producer in the <CEP_HOME>/samples/producers/http/ directory.

  • No labels