Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #170 from Yasara123/master
Browse files Browse the repository at this point in the history
adding http sample
  • Loading branch information
mohanvive authored Jun 7, 2017
2 parents 25fd5ca + 8760db0 commit 927e8d9
Show file tree
Hide file tree
Showing 15 changed files with 687 additions and 0 deletions.
Binary file not shown.
5 changes: 5 additions & 0 deletions modules/samples/artifacts/0010/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[1] Copy {WSO2DASHome}/samples/0010/http-io-sample.siddhi file to {WSO2DASHome}/deployment/siddhi-files.
[2] Navigate to {WSO2DASHome}/bin and start the server using ./worker.sh
[3] Navigate to {WSO2DASHome}/samples/sample-clients/http-server and run the "ant" command without arguments to start the HTTP Server
[4] Navigate to {WSO2DASHome}/samples/sample-clients/http-client and run the "ant" command without arguments to publish events to HTTP/HTTPS endpoint of the DAS server.

26 changes: 26 additions & 0 deletions modules/samples/artifacts/0010/http-io-sample.siddhi
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@Plan:name("ExecutionPlanHttpIO")

@Source(type = 'http', receiver.url='http://localhost:5005/inputStream', basic.auth.enabled='false',
@map(type='text'))
define stream inputStreamHttp (name string, age int, country string);

@Source(type = 'http', receiver.url='https://localhost:8005/inputStream', basic.auth.enabled='false',
@map(type='text'))
define stream inputStreamHttps (name string, age int, country string);


@sink(type='http',publisher.url='http://localhost:8080/abc', method='POST',headers='test:EEEE',
@map(type='text', @payload('{{message}}')))
define stream FooStream (message string,method String,headers String);

from inputStreamHttp
select name as message, 'POST' as method,'Name:John#Age:23' as headers
insert into FooStream;

from inputStreamHttp#log("Sorce LOG HTTP :: ")
insert into tempHttp;

from inputStreamHttps#log("Sorce LOG HTTPS :: ")
insert into tempHttps;


71 changes: 71 additions & 0 deletions modules/samples/sample-clients/http-client/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<!--
~ Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project default="run">

<property name="carbon.home" value="../../../"/>
<property name="src.dir" value="src/main/java"/>
<property name="resources.dir" value="src/main/resources"/>
<property name="temp.dir" value="temp"/>
<property name="class.dir" value="${temp.dir}/classes"/>
<property name="main-class" value="org.wso2.das.http.client.HttpClient"/>
<property name="lib.dir" value="../lib"/>

<property name="host" value="localhost"/>
<property name="port" value="5005"/>

<path id="javac.classpath">
<pathelement path="${class.dir}"/>
<fileset dir="${lib.dir}"/>
<fileset dir="${carbon.home}/wso2/lib/plugins">
<include name="org.apache.commons.logging_*.jar"/>
<include name="io.netty.*.jar"/>
<include name="slf4j.log4j*.jar"/>
<include name="org.ops4j.*.jar"/>
<include name="slf4j.api_*.jar"/>
<include name="siddhi-*.jar"/>
</fileset>
</path>

<target name="clean">
<delete dir="${class.dir}" quiet="true"/>
<delete dir="${temp.dir}"/>
</target>

<target name="init">
<mkdir dir="${temp.dir}"/>
<mkdir dir="${class.dir}"/>
</target>

<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${class.dir}" compiler="modern">
<include name="*/**"/>
<classpath refid="javac.classpath"/>
</javac>
</target>

<target name="run" depends="compile">
<echo>Publishing events to http/https endpoints to port 5005</echo>
<java classname="${main-class}"
classpathref="javac.classpath" fork="true">
<arg value="${host}"/>
<arg value="${port}"/>
</java>
</target>

</project>
42 changes: 42 additions & 0 deletions modules/samples/sample-clients/http-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.wso2.das</groupId>
<artifactId>sample-clients</artifactId>
<version>4.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<name>WSO2 Data Analytics Server - Sample - HTTP Client</name>
<artifactId>http-client</artifactId>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>org.wso2.siddhi</groupId>
<artifactId>siddhi-core</artifactId>
<version>${siddhi.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.wso2.das.http.client;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.cert.CertificateException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

/**
* This is a sample HTTP client to publish events to HTTP/HTTPS endpoint
*/
public class HttpClient {

private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(HttpClient.class);

public static void main(String[] args) throws InterruptedException, KeyManagementException {
setCarbonHome();
URI baseURI = URI.create(String.format("http://%s:%d", "localhost", 5005));
String event1 = "John,20,SL";
String event2 = "Mike,20,USA";
httpPublishEvent(event1, baseURI, "/inputStream", false, "text"
);
httpPublishEvent(event2, baseURI, "/inputStream", false, "text"
);
Thread.sleep(500);
event1 = "Jane,20,SL";
event2 = "Donna,20,USA";
httpsPublishEvent(event1, "https://localhost:8005/inputStream", false,
"text/plain");
httpsPublishEvent(event2, "https://localhost:8005/inputStream", false,
"text/plain");
Thread.sleep(100);
}

private static void setCarbonHome() {
Path carbonHome = Paths.get("");
carbonHome = Paths.get(carbonHome.toString(), "src", "main", "java", "resources");
System.setProperty("carbon.home", carbonHome.toString());
logger.info("Carbon Home Absolute path set to: " + carbonHome.toAbsolutePath());
}

private static void httpsPublishEvent(String event, String baseURI, Boolean auth, String mapping) throws
KeyManagementException {
try {
System.setProperty("javax.net.ssl.trustStore", System.getProperty("carbon.home") + "/" +
"client-truststore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
char[] passphrase = "wso2carbon".toCharArray(); //password
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(new FileInputStream(System.getProperty("carbon.home") + "/" +
"client-truststore.jks"), passphrase); //path
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keystore);
SSLContext context = SSLContext.getInstance("TLS");
TrustManager[] trustManagers = tmf.getTrustManagers();
context.init(null, trustManagers, null);
SSLSocketFactory sf = context.getSocketFactory();
URL url = new URL(baseURI);
HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection();
httpsCon.setSSLSocketFactory(sf);
httpsCon.setRequestMethod("POST");
httpsCon.setRequestProperty("Content-Type", mapping);
httpsCon.setRequestProperty("HTTP_METHOD", "POST");
if (auth) {
httpsCon.setRequestProperty("Authorization",
"Basic " + java.util.Base64.getEncoder().encodeToString(("admin" + ":" + "admin").getBytes()));
}
httpsCon.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(httpsCon.getOutputStream());
out.write(event);
out.close();
logger.info("Event response code " + httpsCon.getResponseCode());
logger.info("Event response message " + httpsCon.getResponseMessage());
httpsCon.disconnect();
} catch (IOException e) {
logger.error("IO Error", e);
} catch (NoSuchAlgorithmException e) {
logger.error("NoSuchAlgorithmException Error", e);
} catch (CertificateException e) {
logger.error("CertificateException Error", e);
} catch (KeyStoreException e) {
logger.error("KeyStoreException Error", e);
}
}

private static void httpPublishEvent(String event, URI baseURI, String path, Boolean auth, String mapping) {
try {
HttpURLConnection urlConn = null;
try {
urlConn = HttpServerUtil.request(baseURI, path, "POST", true);
} catch (IOException e) {
logger.error("IOException occurred while running the HttpsSourceTestCaseForSSL", e);
}
if (auth) {
HttpServerUtil.setHeader(urlConn, "Authorization",
"Basic " + java.util.Base64.getEncoder().encodeToString(("admin" + ":" + "admin")
.getBytes()));
}
HttpServerUtil.writeContent(urlConn, event);
assert urlConn != null;
logger.info("Event response code " + urlConn.getResponseCode());
logger.info("Event response message " + urlConn.getResponseMessage());
urlConn.disconnect();
} catch (IOException e) {
logger.error("IOException occurred while running the HttpsSourceTestCaseForSSL", e);
}
}

private static class HttpServerUtil {

private HttpServerUtil() {
}

static void writeContent(HttpURLConnection urlConn, String content) throws IOException {
OutputStreamWriter out = new OutputStreamWriter(
urlConn.getOutputStream());
out.write(content);
out.close();
}

static HttpURLConnection request(URI baseURI, String path, String method, boolean keepAlive)
throws IOException {
URL url = baseURI.resolve(path).toURL();
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
if (method.equals("POST") || method.equals("PUT")) {
urlConn.setDoOutput(true);
}
urlConn.setRequestMethod(method);
if (!keepAlive) {
urlConn.setRequestProperty("Connection", "Keep-Alive");
}
return urlConn;
}

static void setHeader(HttpURLConnection urlConnection, String key, String value) {
urlConnection.setRequestProperty(key, value);
}

}
}
Binary file not shown.
Binary file not shown.
72 changes: 72 additions & 0 deletions modules/samples/sample-clients/http-server/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!--
~ Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project default="run">

<property name="carbon.home" value="../../../"/>
<property name="src.dir" value="src/main/java"/>
<property name="resources.dir" value="src/main/resources"/>
<property name="temp.dir" value="temp"/>
<property name="class.dir" value="${temp.dir}/classes"/>
<property name="main-class" value="org.wso2.das.http.server.HttpServerMain"/>
<property name="lib.dir" value="../lib"/>

<property name="host" value="localhost"/>
<property name="port" value="8080"/>

<path id="javac.classpath">
<pathelement path="${class.dir}"/>
<fileset dir="${lib.dir}"/>
<fileset dir="${carbon.home}/wso2/lib/plugins">
<include name="org.apache.commons.logging_*.jar"/>
<include name="org.apache.commons.collections4*.jar"/>
<include name="io.netty.*.jar"/>
<include name="slf4j.log4j*.jar"/>
<include name="org.ops4j.*.jar"/>
<include name="slf4j.api_*.jar"/>
<include name="siddhi-*.jar"/>
</fileset>
</path>

<target name="clean">
<delete dir="${class.dir}" quiet="true"/>
<delete dir="${temp.dir}"/>
</target>

<target name="init">
<mkdir dir="${temp.dir}"/>
<mkdir dir="${class.dir}"/>
</target>

<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${class.dir}" compiler="modern">
<include name="*/**"/>
<classpath refid="javac.classpath"/>
</javac>
</target>

<target name="run" depends="compile">
<echo>Starting HTTP Server in http://localhost:8080/abc</echo>
<java classname="${main-class}"
classpathref="javac.classpath" fork="true">
<arg value="${host}"/>
<arg value="${port}"/>
</java>
</target>

</project>
Loading

0 comments on commit 927e8d9

Please sign in to comment.