@App:name("HttpRequestResponseSample")
@App:description("Publish http requests, receive their responses and process them")

/*
Purpose:
	This application demonstrates how to configure WSO2 Streaming Integrator to send sweet production events via HTTP
	transport in JSON default format, Receive response from the http server and process the response using siddhi.

Executing the Sample
    1. Copy this HttpRequestResponseSample.siddhi file to {SIHome}/wso2/server/deployment/siddhi-files/
    2. start the Streaming Integrator by issuing the command ./server.sh in {SIHome}/bin

    The following logs will be printed if it starts successfully
    INFO {io.siddhi.extension.io.http.sink.HttpSink} - HttpRequestResponseSample:@sink( type = "http-request",
        sink.id = "production-request", publisher.url = "http://0.0.0.0:8080/abc", @map( type = "json"))define stream
        LowProductionAlertStream (name string, amount double) has successfully connected to http://0.0.0.0:8080/abc
    INFO {org.wso2.carbon.streaming.integrator.core.internal.StreamProcessorService} - Siddhi App
        HttpRequestResponseSample deployed successfully
    INFO {org.wso2.carbon.kernel.internal.CarbonStartupHandler} - WSO2 Streaming Integrator started in 10.517 sec

Testing the Sample:
   1) Open a terminal and navigate to {WSO2SIHome}/samples/sample-clients/http-server and run "ant" command without any
        arguments.
   2) Send events as follows,
      Send events to the HTTP endpoint defined by 'publish.url' in the Sink configuration using the curl command:
      Open a new terminal and issue the following command:
            * curl -X POST -d '<events><event><name>toffee</name><amount>75.6</amount></event></events>' http://0.0.0.0:8006/SweetProductionStream --header "Content-Type:application/xml"

    3) When publishing the events,
       http-request sink will send the request to the http server and the server will echo the received request as the
            response with a 200 http status code.
       Then,
       - That successful response will be received by the defined http-response source which has the relevant http
            status code.
       - Received response will be converted to a siddhi event using using json default mapping and pushed to the
            ResponseStream.

  VViewing the Results:
    The received responses will be logged in the terminal/editor console as following.
    * INFO {io.siddhi.core.stream.output.sink.LogSink} - HttpRequestResponseSample : ResponseStream :
            Event{timestamp=1568913620809, data=[toffee, 75.6], isExpired=false}

 Notes:
  If the message "LowProductionAlertStream' stream could not connect to '0.0.0.0:8080", it could be due to port 8080
  defined in the Siddhi application is already being used by a different program. To resolve this issue, do the
    following:
        1) Stop this Siddhi application (Click 'Run' on menu bar -> 'Stop').
        2) Change the port from 8080 to an unused port in this Siddhi application's source configuration and in the
            http-server file.
        3) Start the application and check whether the expected output appears on the console.
*/

@source(type='http', receiver.url='http://0.0.0.0:8006/SweetProductionStream', @map(type = 'xml'))
define stream SweetProductionStream (name string, amount double);

@sink(type='http-request', sink.id='production-request', publisher.url='http://0.0.0.0:8080/abc',
@map(type='json'))
define stream LowProductionAlertStream (name string, amount double);

@sink(type='log')
@source(type='http-response' , sink.id='production-request', http.status.code='200',
@map(type='json'))
define stream ResponseStream(name string, amount double);

@info(name='query1')
from SweetProductionStream
select *
insert into LowProductionAlertStream;
