Publishing JSON/REST Data using Java Client
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.
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
<repositories> <repository> <id>wso2-nexus</id> <name>WSO2 internal Repository</name> <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> <checksumPolicy>ignore</checksumPolicy> </releases> </repository> <repository> <id>wso2.releases</id> <name>WSO2 internal Repository</name> <url>http://maven.wso2.org/nexus/content/repositories/releases/</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> <checksumPolicy>ignore</checksumPolicy> </releases> </repository> <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>
<dependency> <groupId>org.apache.ws.commons.axiom.wso2</groupId> <artifactId>axiom</artifactId> <version>1.2.11.wso2v6</version> </dependency> <dependency> <groupId>org.wso2.orbit.org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.1.wso2v2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents.wso2</groupId> <artifactId>httpcore</artifactId> <version>4.3.3.wso2v1</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.1</version> </dependency>
HTTP REST publisher
As a prerequisite for this sample, you need to define the event streams and a WSO2Event receiver in the server (WSO2 DAS/CEP).
Follow the procedure below to use the data publisher.
Initialize the HTTP Client and HTTP POST method as follows.
HttpClient httpClient = new SystemDefaultHttpClient(); HttpPost method = new HttpPost(url);
JSON event format should be as follows whereas event object is consist of metaData, correlationData and payloadData objects. Each object has set of properties and values.
{ "event":{ "metaData":{ "timestamp":1442921557056, "isPowerSaverEnabled":false, "sensorId":100, "sensorName":"temperature" }, "correlationData":{ "longitude":2332.424, "latitude":2323.23232 }, "payloadData":{ "humidity":2.3, "sensorValue":23423.234 } } }
You can use above event as a string or else you can even build JSON object by using a parser like gson and convert it as a string.
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); String eventString = "{\"event\": " + event + "}";
Once JSON String 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(eventString); method.setEntity(entity);
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.