Publishing HTTP Events to an OAuth-protected Endpoint
Purpose¶
This application demonstrates how to configure WSO2 Streaming Integrator Tooling to send HTTP events to an OAuth-protected endpoint. Here we use password grant type.
Prerequisites¶
-
Replace the Sink configuration values for following options.
oauth.username
: Sender's OAuth username (Ex:- 'username123')oauth.password
: Sender's OAuth password (Ex:- 'password123')publisher.url
: Publisher URL (Ex:- 'https://localhost:8005/abc')consumer.key
: Consumer key for the HTTP request (Ex:- 'abcdef')consumer.secret
: Consumer secret for the HTTP request (Ex:- 'abcdef')token.url
: URL of the token end point (Ex:- 'https://localhost:8005/token')method
: Method type (Eg:-POST
)
Optional (You can fill this if it is different from default values)
header
(Authorization header): Access token for the HTTP request. By default, it is automatically retrieved using thepassword
,client-credential
, or therefresh-token
grant type. Here the grant type depends on the user input.https.truststore.file
: API trust store path (Ex:-<some-path>client-truststore.jks
). By default, it obtained from the product pack (Ex:-${carbon.home}/resources/security/client-truststore.jks
).https.truststore.password
: API trust store password (Ex:-wso2carbon
). By default, it is set aswso2carbon
.
-
Save this sample. If there is no syntax error, the following message is shown on the console:
Siddhi App PublishHttpOAuthRequestWithOAuthUser successfully deployed.
Executing the Sample¶
- Start the Siddhi application by clicking on 'Run'.
- If the Siddhi application starts successfully, the following messages are shown on the console:
PublishHttpOAuthRequestWithOAuthUser.siddhi - Started Successfully!
'Http' sink at 'LowProductionAlertStream' stream successfully connected to 'https://localhost:8005/abc'.
Testing the Sample¶
Send events through one or more of the following methods:
Send events with http server through the event simulator¶
- Open the event simulator by clicking on the second icon or pressing Ctrl+Shift+I.
- In the Single Simulation tab of the panel, specify the values as follows:
Siddhi App Name
:PublishHttpOAuthRequestWithOAuthUser
Stream Name
:SweetProductionStream
- In the
name
andamount
fields, enter 'toffees' and '75.6' respectively and then click Send to send the event. - Send some more events as desired.
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 '{"streamName": "SweetProductionStream", "siddhiAppName": "PublishHttpOAuthRequestWithOAuthUser","data": ['toffees', 75.6]}' http://localhost:9390/simulation/single -H 'content-type: text/plain'
-
If there is no error, the following messages are shown on the terminal:
{"status":"OK","message":"Single Event simulation started successfully"}
Publish events with Postman¶
- Launch the application.
- Make a
Post
request to thehttp://localhost:9390/simulation/single
endpoint. Set theContent-Type
totext/plain
and set the request body in text as follows:{"streamName": "SweetProductionStream", "siddhiAppName": "PublishHttpOAuthRequestWithOAuthUser","data": ['toffees', 75.6]}
- Click
Send
. If there is no error, the following messages are shown on the console:"status": "OK"
"message": "Single Event simulation started successfully"
Viewing the Results¶
See the output on the terminal:
Siddhi App test successfully deployed.
INFO {org.wso2.extension.siddhi.io.http.sink.HttpSink} - Request sent successfully to https://localhost:8005/abc
[java] [org.wso2.si.http.server.HttpServerListener] : Event Name Arrived: {"event":{"name":"toffees","amount":75.6}}
[java] [org.wso2.si.http.server.HttpServerMain] : Received Event Names:{"event":{"name":"toffees","amount":75.6}} ,
[java] [org.wso2.si.http.server.HttpServerMain] : Received Event Headers key set:[Http_method, Content-type, Content-length]
[java] [org.wso2.si.http.server.HttpServerMain] : Received Event Headers value set:[[POST], [application/json], [42]]
Notes¶
If you get the message Error when pushing events to Siddhi debugger engine
, it could be due to time out problem, do the following:
- Stop this Siddhi application (Click 'Run' on the menu bar -> 'Stop').
- Start the application and check whether the expected output appears on the console.
If you get the status code 400
, it could be due to passing invalid parameter problem, do the following:
- Stop this Siddhi application (Click 'Run' on the menu bar -> 'Stop').
- Recheck all the parameter you are passing and change the correct parameter
- Start the application and check whether the expected output appears on the console.
If you get the status code 500
, it could be due to Internal server error problem, do the following:
- Stop this Siddhi application (Click 'Run' on the menu bar -> 'Stop').
- Start the application again and check whether the expected output appears on the console.
@App:name("PublishHttpOAuthRequestWithOAuthUser")
@App:description("Send HTTP events to an OAuth-protected endpoint. Here we use password grant type.")
define stream SweetProductionStream (name string, amount double);
@sink(type='http', oauth.username="xxxx", oauth.password="xxxx",
publisher.url='https://localhost:8005/abc', headers="'Authorization: Bearer xxxxxxxxx'",
consumer.key="xxxxxxxxxx", consumer.secret="xxxxxxxxxxx", token.url='https://localhost:8005/token',
method="xxxxxx", @map(type='json', @payload( "{'name': {{ name }}, 'amount': {{ amount }}}")))
define stream LowProductionAlertStream ( name string, amount double);
@info(name='query1')
from SweetProductionStream
select *
insert into LowProductionAlertStream;