├── .github └── release-drafter.yml ├── .gitignore ├── LICENSE ├── README.md ├── component ├── pom.xml └── src │ ├── main │ └── java │ │ └── io │ │ └── siddhi │ │ └── extension │ │ └── io │ │ └── http │ │ ├── metrics │ │ ├── EndpointStatus.java │ │ ├── Metrics.java │ │ ├── SinkMetrics.java │ │ └── SourceMetrics.java │ │ ├── sink │ │ ├── ClientConnector.java │ │ ├── HttpCallSink.java │ │ ├── HttpRequestSink.java │ │ ├── HttpResponseSink.java │ │ ├── HttpServiceResponseSink.java │ │ ├── HttpSink.java │ │ ├── SSEConnectorListener.java │ │ ├── SSEConnectorRegistry.java │ │ ├── SSERequestListener.java │ │ ├── SSEServerSink.java │ │ ├── SSESyncConnectorListener.java │ │ ├── SSESyncConnectorRegistry.java │ │ ├── SSEWorkerThread.java │ │ ├── WebSubHubSink.java │ │ ├── WebSubSubscriptionDTO.java │ │ ├── exception │ │ │ └── HttpSinkAdaptorRuntimeException.java │ │ ├── updatetoken │ │ │ ├── AccessTokenCache.java │ │ │ ├── DefaultListener.java │ │ │ ├── HttpRequest.java │ │ │ └── HttpsClient.java │ │ └── util │ │ │ └── HttpSinkUtil.java │ │ ├── source │ │ ├── ConnectorStartupSynchronizer.java │ │ ├── HTTPConnectorListener.java │ │ ├── HTTPSyncConnectorListener.java │ │ ├── HttpAuthenticator.java │ │ ├── HttpCallResponseConnectorListener.java │ │ ├── HttpCallResponseSource.java │ │ ├── HttpCallResponseSourceConnectorRegistry.java │ │ ├── HttpConnectorPortBindingListener.java │ │ ├── HttpConnectorRegistry.java │ │ ├── HttpRequestSource.java │ │ ├── HttpResponseMessageListener.java │ │ ├── HttpResponseProcessor.java │ │ ├── HttpResponseSource.java │ │ ├── HttpServiceSource.java │ │ ├── HttpSource.java │ │ ├── HttpSourceListener.java │ │ ├── HttpSyncConnectorRegistry.java │ │ ├── HttpSyncSourceListener.java │ │ ├── HttpSyncWorkerThread.java │ │ ├── HttpWebSubResponseProcessor.java │ │ ├── HttpWebSubSource.java │ │ ├── HttpWorkerThread.java │ │ ├── SSEResponseConnectorListener.java │ │ ├── SSEResponseListener.java │ │ ├── SSESource.java │ │ ├── SSESourceConnectorRegistry.java │ │ ├── exception │ │ │ ├── HttpSourceAdaptorException.java │ │ │ └── HttpSourceAdaptorRuntimeException.java │ │ ├── internal │ │ │ ├── HttpIODataHolder.java │ │ │ └── ServiceComponent.java │ │ └── util │ │ │ └── HttpSourceUtil.java │ │ └── util │ │ ├── HTTPSinkRegistry.java │ │ ├── HTTPSourceRegistry.java │ │ ├── HttpConstants.java │ │ ├── HttpIoUtil.java │ │ ├── StringDataSource.java │ │ └── TrpPropertyTypes.java │ └── test │ ├── java │ └── io │ │ └── siddhi │ │ └── extension │ │ └── io │ │ └── http │ │ ├── sink │ │ ├── HttpAuthTestCase.java │ │ ├── HttpCallResponseTestCase.java │ │ ├── HttpMultipleEventSinkTestCase.java │ │ ├── HttpOAuthTestCase.java │ │ ├── HttpRequestResponseTestCase.java │ │ ├── HttpSinkCustomConfigurationTestCase.java │ │ ├── HttpSinkMappingTestCase.java │ │ ├── HttpSinkMethodTypeTestCase.java │ │ ├── HttpSinkTestCase.java │ │ ├── HttpsSinkTestCase.java │ │ ├── SSEServerSinkTestCase.java │ │ └── util │ │ │ ├── HttpFileServerListener.java │ │ │ ├── HttpFileServerListenerHandler.java │ │ │ ├── HttpOAuthEndpointListener.java │ │ │ ├── HttpOAuthListenerHandler.java │ │ │ ├── HttpOAuthTokenEndpointListener.java │ │ │ ├── HttpServerListener.java │ │ │ ├── HttpServerListenerHandler.java │ │ │ ├── HttpsServerListenerHandler.java │ │ │ └── UnitTestAppender.java │ │ └── source │ │ ├── HttpBasicTestCase.java │ │ ├── HttpCustomConfigTestCase.java │ │ ├── HttpCustomThreadPoolConfigTestCase.java │ │ ├── HttpMultipleFormatSourcesTestCase.java │ │ ├── HttpOrderlyProcessTestCase.java │ │ ├── HttpSourceBasicAuthTestCase.java │ │ ├── HttpSourceConflictsTestCase.java │ │ ├── HttpSourceMappingTestCase.java │ │ ├── HttpSyncRequestResponseTestCase.java │ │ ├── HttpSyncServiceResponseTestCase.java │ │ ├── HttpsSSLSourceTestCase.java │ │ ├── SSESourceTestCase.java │ │ └── util │ │ ├── Constants.java │ │ ├── HttpServerUtil.java │ │ └── HttpTestUtil.java │ └── resources │ ├── events │ ├── events_map.txt │ └── events_text.txt │ ├── files │ └── testFile.txt │ ├── log4j2.xml │ ├── mb.properties │ ├── qpid.properties │ ├── security │ ├── client-truststore.jks │ ├── secrets.properties │ ├── security.policy │ ├── store.jks │ └── wso2carbon.jks │ └── testng.xml ├── coverage-report └── pom.xml ├── docs ├── api │ ├── 1.0.10.md │ ├── 1.0.18.md │ ├── 1.0.29.md │ ├── 1.0.39.md │ ├── 1.0.4.md │ ├── 1.0.45.md │ ├── 1.1.0.md │ ├── 1.1.2.md │ ├── 1.1.5.md │ ├── 1.2.0.md │ ├── 1.2.1.md │ ├── 1.2.2.md │ ├── 2.0.0.md │ ├── 2.0.4.md │ ├── 2.0.6.md │ ├── 2.0.7.md │ ├── 2.0.8.md │ ├── 2.1.0.md │ ├── 2.1.1.md │ ├── 2.1.2.md │ ├── 2.2.0.md │ ├── 2.2.1.md │ ├── 2.2.2.md │ ├── 2.2.3.md │ ├── 2.2.4.md │ ├── 2.3.0.md │ ├── 2.3.1.md │ ├── 2.3.2.md │ ├── 2.3.3.md │ ├── 2.3.4.md │ ├── 2.3.5.md │ ├── 2.3.6.md │ ├── 2.3.7.md │ └── latest.md ├── assets │ ├── javascripts │ │ └── extra.js │ ├── lib │ │ ├── backtotop │ │ │ ├── img │ │ │ │ └── cd-top-arrow.svg │ │ │ └── js │ │ │ │ ├── main.js │ │ │ │ └── util.js │ │ └── highlightjs │ │ │ ├── default.min.css │ │ │ └── highlight.min.js │ └── stylesheets │ │ └── extra.css ├── images │ ├── favicon.ico │ └── siddhi-logo.svg ├── index.md └── license.md ├── findbugs-exclude.xml ├── issue_template.md ├── mkdocs.yml ├── pom.xml └── pull_request_template.md /.github/release-drafter.yml: -------------------------------------------------------------------------------- 1 | name-template: Siddhi IO HTTP $NEXT_PATCH_VERSION Release 2 | tag-template: v$NEXT_PATCH_VERSION 3 | change-template: '* $TITLE (#$NUMBER)' 4 | 5 | categories: 6 | - title: 'Features and Improvements' 7 | labels: 8 | - type/new-feature 9 | - type/improvement 10 | - title: 'Bug Fixes' 11 | labels: 12 | - type/bug 13 | 14 | template: | 15 | > This Release is compatible with Siddhi 5.x.x 16 | 17 | ## Overview 18 | > Include overview 19 | > Recheck all PR listed and finalise 20 | 21 | $CHANGES 22 | 23 | ## Complete Changes 24 | Please find the complete changes [here](https://github.com/siddhi-io/REPO_NAME/compare/$PREVIOUS_TAG...v$NEXT_PATCH_VERSION) 25 | 26 | ## Download 27 | Download the release from [here](https://mvnrepository.com/artifact/GROUP_ID/REPO_NAME/$NEXT_PATCH_VERSION) 28 | 29 | replacers: 30 | - search: 'REPO_NAME' 31 | replace: 'siddhi-io-http' 32 | - search: 'GROUP_ID' 33 | replace: 'io.siddhi.extension.io.http' 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | target/ 11 | 12 | # Mobile Tools for Java (J2ME) 13 | .mtj.tmp/ 14 | 15 | # Package Files # 16 | *.jar 17 | *.war 18 | *.ear 19 | *.zip 20 | *.tar.gz 21 | *.rar 22 | 23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 24 | hs_err_pid* 25 | 26 | #idea files 27 | .idea/ 28 | *.iml 29 | 30 | .classpath 31 | .project 32 | .settings/ 33 | .cache-tests 34 | .cache-main 35 | 36 | site/ 37 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/metrics/EndpointStatus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.metrics; 19 | 20 | /** 21 | * Enum to define endpoint status 22 | */ 23 | public enum EndpointStatus { 24 | OFFLINE, 25 | ONLINE 26 | } 27 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/metrics/Metrics.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.metrics; 19 | 20 | /** 21 | * Parent metrics class 22 | */ 23 | public class Metrics { 24 | protected final String siddhiAppName; 25 | protected final String streamName; 26 | 27 | protected Metrics(String siddhiAppName, String streamName) { 28 | this.siddhiAppName = siddhiAppName; 29 | this.streamName = streamName; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/metrics/SinkMetrics.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.metrics; 19 | 20 | import org.wso2.carbon.metrics.core.Counter; 21 | import org.wso2.carbon.metrics.core.Level; 22 | import org.wso2.carbon.si.metrics.core.internal.MetricsDataHolder; 23 | 24 | /** 25 | * Class to publish sink metrics. 26 | */ 27 | public class SinkMetrics extends Metrics { 28 | 29 | public SinkMetrics(String siddhiAppName, String streamName) { 30 | super(siddhiAppName, streamName); 31 | } 32 | 33 | // To count the total writes from siddhi app level 34 | public Counter getTotalWritesMetric() { 35 | return MetricsDataHolder.getInstance().getMetricService() 36 | .counter(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Total.Writes.%s", siddhiAppName, "http"), 37 | Level.INFO); 38 | } 39 | 40 | // To count the total HTTP reads 41 | public Counter getTotalHttpWritesMetric(String url) { 42 | return MetricsDataHolder.getInstance().getMetricService() 43 | .counter(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Sink.Total.Writes.%s.%s", 44 | siddhiAppName, streamName, url), Level.INFO); 45 | } 46 | 47 | // To count the total HTTP errors 48 | public Counter getTotalHttpErrorsMetric(String url) { 49 | return MetricsDataHolder.getInstance().getMetricService() 50 | .counter(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Sink.Total.Errors.%s.%s", 51 | siddhiAppName, streamName, url), Level.INFO); 52 | } 53 | 54 | // To count the request size 55 | public Counter getRequestSizeMetric(String url) { 56 | return MetricsDataHolder.getInstance().getMetricService() 57 | .counter(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Sink.Total.Request.Size.%s.%s", 58 | siddhiAppName, streamName, url), Level.INFO); 59 | } 60 | 61 | // To calculate the response latency 62 | public void setLatencyMetric(String url, long latency) { 63 | MetricsDataHolder.getInstance().getMetricService() 64 | .gauge(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Sink.Average.Latency.%s.%s", 65 | siddhiAppName, streamName, url), Level.INFO, () -> latency); 66 | } 67 | 68 | // To set the online/offline status 69 | public void setEndpointStatusMetric(String url, EndpointStatus status) { 70 | MetricsDataHolder.getInstance().getMetricService() 71 | .gauge(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Sink.Endpoint.Status.%s.%s", 72 | siddhiAppName, streamName, url), Level.INFO, status::ordinal); 73 | } 74 | 75 | // To set the last event time 76 | public void setLastEventTime(String url, long lastEventTime) { 77 | MetricsDataHolder.getInstance().getMetricService() 78 | .gauge(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Sink.Last.Event.Time.%s.%s", 79 | siddhiAppName, streamName, url), Level.INFO, () -> lastEventTime); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/metrics/SourceMetrics.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.metrics; 19 | 20 | import org.wso2.carbon.metrics.core.Counter; 21 | import org.wso2.carbon.metrics.core.Level; 22 | import org.wso2.carbon.si.metrics.core.internal.MetricsDataHolder; 23 | 24 | /** 25 | * Class to publish source metrics. 26 | */ 27 | public class SourceMetrics extends Metrics { 28 | private final String url; 29 | 30 | public SourceMetrics(String siddhiAppName, String streamName, String url) { 31 | super(siddhiAppName, streamName); 32 | this.url = url; 33 | } 34 | 35 | // To count the total reads from siddhi app level 36 | public Counter getTotalReadsMetric() { 37 | return MetricsDataHolder.getInstance().getMetricService() 38 | .counter(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Total.Reads.%s", 39 | siddhiAppName, "http"), Level.INFO); 40 | } 41 | 42 | // To count the total HTTP reads 43 | public Counter getTotalHttpReadsMetric() { 44 | return MetricsDataHolder.getInstance().getMetricService() 45 | .counter(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Source.Total.Reads.%s.%s", 46 | siddhiAppName, streamName, url), Level.INFO); 47 | } 48 | 49 | // To count the total HTTP errors 50 | public Counter getTotalHttpErrorsMetric() { 51 | return MetricsDataHolder.getInstance().getMetricService() 52 | .counter(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Source.Total.Errors.%s.%s", 53 | siddhiAppName, streamName, url), Level.INFO); 54 | } 55 | 56 | // To count the request size 57 | public Counter getRequestSizeMetric() { 58 | return MetricsDataHolder.getInstance().getMetricService() 59 | .counter(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Source.Total.Request.Size.%s.%s", 60 | siddhiAppName, streamName, url), Level.INFO); 61 | } 62 | 63 | // To set the online/offline status 64 | public void setEndpointStatusMetric(EndpointStatus status) { 65 | MetricsDataHolder.getInstance().getMetricService() 66 | .gauge(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Source.Endpoint.Status.%s.%s", 67 | siddhiAppName, streamName, url), Level.INFO, status::ordinal); 68 | } 69 | 70 | // To set the last event time 71 | public void setLastEventTime(long lastEventTime) { 72 | MetricsDataHolder.getInstance().getMetricService() 73 | .gauge(String.format("io.siddhi.SiddhiApps.%s.Siddhi.Http.Source.Last.Event.Time.%s.%s", 74 | siddhiAppName, streamName, url), Level.INFO, () -> lastEventTime); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/ClientConnector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package io.siddhi.extension.io.http.sink; 20 | 21 | import org.wso2.transport.http.netty.contract.HttpClientConnector; 22 | import org.wso2.transport.http.netty.contract.HttpResponseFuture; 23 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 24 | 25 | import java.util.Map; 26 | 27 | /** 28 | * Class to have the client connector and related properties 29 | */ 30 | public class ClientConnector { 31 | 32 | private String publisherURL; 33 | private Map httpURLProperties; 34 | private HttpClientConnector httpClientConnector; 35 | 36 | public ClientConnector(String publisherURL, Map httpURLProperties, 37 | HttpClientConnector httpClientConnector) { 38 | this.publisherURL = publisherURL; 39 | this.httpURLProperties = httpURLProperties; 40 | this.httpClientConnector = httpClientConnector; 41 | } 42 | 43 | public String getPublisherURL() { 44 | return publisherURL; 45 | } 46 | 47 | public HttpResponseFuture send(HttpCarbonMessage cMessage) { 48 | return httpClientConnector.send(cMessage); 49 | } 50 | 51 | public Map getHttpURLProperties() { 52 | return httpURLProperties; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/HttpResponseSink.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink; 20 | 21 | import io.siddhi.annotation.Example; 22 | import io.siddhi.annotation.Extension; 23 | import io.siddhi.annotation.Parameter; 24 | import io.siddhi.annotation.util.DataType; 25 | 26 | /** 27 | * {@code HttpResponseSink} Handle the HTTP publishing tasks. 28 | */ 29 | @Extension(name = "http-response", namespace = "sink", 30 | deprecated = true, 31 | description = "" + 32 | "_(Use http-service-response sink instead)._\n" + 33 | "The http-response sink send responses of the requests consumed by its corresponding " + 34 | "http-request source, by mapping the response messages to formats such as `text`, `XML` and `JSON`.", 35 | parameters = { 36 | @Parameter( 37 | name = "source.id", 38 | description = "Identifier to correlate the http-response sink to its corresponding " + 39 | "http-request source which consumed the request.", 40 | type = {DataType.STRING}), 41 | @Parameter( 42 | name = "message.id", 43 | description = "Identifier to correlate the response with the request received " + 44 | "by http-request source.", 45 | dynamic = true, 46 | type = {DataType.STRING}), 47 | @Parameter( 48 | name = "headers", 49 | description = "HTTP request headers in format `\"':',':'\"`.\n" + 50 | "When the `Content-Type` header is not provided the system decides the " + 51 | "Content-Type based on the provided sink mapper as following: \n" + 52 | " - `@map(type='xml')`: `application/xml`\n" + 53 | " - `@map(type='json')`: `application/json`\n" + 54 | " - `@map(type='text')`: `plain/text`\n" + 55 | " - `@map(type='keyvalue')`: `application/x-www-form-urlencoded`\n" + 56 | " - For all other cases system defaults to `plain/text`\n" + 57 | "Also the `Content-Length` header need not to be provided, as the system " + 58 | "automatically defines it by calculating the size of the payload.", 59 | type = {DataType.STRING}, 60 | optional = true, 61 | defaultValue = "Content-Type and Content-Length headers"), 62 | }, 63 | examples = { 64 | @Example(syntax = "" + 65 | "@source(type='http-request', receiver.url='http://localhost:5005/add',\n" + 66 | " source.id='adder',\n" + 67 | " @map(type='json, @attributes(messageId='trp:messageId',\n" + 68 | " value1='$.event.value1',\n" + 69 | " value2='$.event.value2')))\n" + 70 | "define stream AddStream (messageId string, value1 long, value2 long);\n" + 71 | "\n" + 72 | "@sink(type='http-response', source.id='adder',\n" + 73 | " message.id='{{messageId}}', @map(type = 'json'))\n" + 74 | "define stream ResultStream (messageId string, results long);\n" + 75 | "\n" + 76 | "@info(name = 'query1')\n" + 77 | "from AddStream \n" + 78 | "select messageId, value1 + value2 as results \n" + 79 | "insert into ResultStream;", 80 | description = "The http-request source on stream `AddStream` listens on " + 81 | "url `http://localhost:5005/stocks` for JSON messages with format:\n" + 82 | "```{\n" + 83 | " \"event\": {\n" + 84 | " \"value1\": 3,\n" + 85 | " \"value2\": 4\n" + 86 | " }\n" + 87 | "}```\n" + 88 | "and when events arrive it maps to `AddStream` events and pass " + 89 | "them to query `query1` for processing. The query results produced on `ResultStream` " + 90 | "are sent as a response via http-response sink with format:" + 91 | "```{\n" + 92 | " \"event\": {\n" + 93 | " \"results\": 7\n" + 94 | " }\n" + 95 | "}```" + 96 | "Here the request and response are correlated by passing the `messageId` " + 97 | "produced by the http-request to the respective http-response sink." 98 | ), 99 | 100 | } 101 | ) 102 | @Deprecated 103 | public class HttpResponseSink extends HttpServiceResponseSink { 104 | 105 | } 106 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/SSEConnectorListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.sink; 19 | 20 | import io.siddhi.extension.io.http.source.HTTPConnectorListener; 21 | import io.siddhi.extension.io.http.source.exception.HttpSourceAdaptorRuntimeException; 22 | import io.siddhi.extension.io.http.source.util.HttpSourceUtil; 23 | import io.siddhi.extension.io.http.util.HttpConstants; 24 | import org.apache.logging.log4j.LogManager; 25 | import org.apache.logging.log4j.Logger; 26 | import org.wso2.transport.http.netty.contract.Constants; 27 | import org.wso2.transport.http.netty.contract.HttpConnectorListener; 28 | import org.wso2.transport.http.netty.contract.exceptions.ClientClosedConnectionException; 29 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 30 | 31 | /** 32 | * This class is responsible for listening to SSE connection. 33 | */ 34 | public class SSEConnectorListener implements HttpConnectorListener { 35 | 36 | private static final Logger log = LogManager.getLogger(HTTPConnectorListener.class); 37 | 38 | public SSEConnectorListener() { 39 | } 40 | 41 | @Override 42 | public void onMessage(HttpCarbonMessage carbonMessage) { 43 | try { 44 | if (isValidRequest(carbonMessage)) { 45 | //Check the message type to match GET or POST 46 | if (HttpConstants.HTTP_METHOD_GET.equalsIgnoreCase(carbonMessage.getHttpMethod()) 47 | || HttpConstants.HTTP_METHOD_POST.equalsIgnoreCase(carbonMessage.getHttpMethod())) { 48 | StringBuilder sourceListenerKey = new StringBuilder().append(String 49 | .valueOf(carbonMessage.getProperty(HttpConstants.LISTENER_PORT))) 50 | .append(HttpConstants.PORT_CONTEXT_KEY_SEPARATOR) 51 | .append(carbonMessage.getProperty(HttpConstants.TO)); 52 | SSERequestListener requestListener = getSourceListener(sourceListenerKey); 53 | if (requestListener != null) { 54 | requestListener.send(carbonMessage); 55 | } else { 56 | HttpSourceUtil.handleCallback(carbonMessage, 404); 57 | } 58 | } else if (HttpConstants.HTTP_METHOD_OPTIONS.equalsIgnoreCase(carbonMessage.getHttpMethod())) { 59 | HttpSourceUtil.handleCORS(carbonMessage); 60 | } else { 61 | throw new HttpSourceAdaptorRuntimeException(carbonMessage, 62 | "Request type is not a type of GET or POST ", 400); 63 | } 64 | } else { 65 | if (log.isDebugEnabled()) { 66 | log.debug("Event is not type of http which has received to the uri '{}{}", 67 | carbonMessage.getProperty(HttpConstants.LISTENER_PORT), 68 | carbonMessage.getProperty(HttpConstants.TO)); 69 | } 70 | HttpSourceUtil.handleCallback(carbonMessage, 404); 71 | } 72 | } finally { 73 | carbonMessage.waitAndReleaseAllEntities(); 74 | } 75 | } 76 | 77 | protected boolean isValidRequest(HttpCarbonMessage carbonMessage) { 78 | 79 | return HttpConstants.PROTOCOL_ID.equals(carbonMessage.getProperty(HttpConstants.PROTOCOL)) && 80 | SSEConnectorRegistry.getInstance().getServerConnectorPool().containsKey(getInterface(carbonMessage)); 81 | } 82 | 83 | protected SSERequestListener getSourceListener(StringBuilder sourceListenerKey) { 84 | 85 | return SSEConnectorRegistry.getInstance().getSourceListenersMap().get(sourceListenerKey.toString()); 86 | } 87 | 88 | protected String getInterface(HttpCarbonMessage cMsg) { 89 | String interfaceId = (String) cMsg.getProperty(Constants.LISTENER_INTERFACE_ID); 90 | if (interfaceId == null) { 91 | if (log.isDebugEnabled()) { 92 | log.debug("Interface id not found on the message, hence using the default interface"); 93 | } 94 | interfaceId = HttpConstants.DEFAULT_INTERFACE; 95 | } 96 | 97 | return interfaceId; 98 | } 99 | 100 | @Override 101 | public void onError(Throwable throwable) { 102 | if (throwable instanceof ClientClosedConnectionException) { 103 | log.debug("Error in http server connector", throwable); 104 | } else { 105 | log.error("Error in http server connector", throwable); 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/SSERequestListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.sink; 19 | 20 | import io.siddhi.extension.io.http.source.HttpAuthenticator; 21 | import io.siddhi.extension.io.http.source.exception.HttpSourceAdaptorRuntimeException; 22 | import org.apache.logging.log4j.LogManager; 23 | import org.apache.logging.log4j.Logger; 24 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 25 | 26 | import java.util.concurrent.ExecutorService; 27 | import java.util.concurrent.Executors; 28 | import java.util.concurrent.locks.Condition; 29 | import java.util.concurrent.locks.ReentrantLock; 30 | 31 | /** 32 | * This class is responsible for listening to incoming events. 33 | */ 34 | public class SSERequestListener { 35 | private static final Logger logger = LogManager.getLogger(SSERequestListener.class); 36 | private ExecutorService executorService; 37 | private boolean paused; 38 | private ReentrantLock lock; 39 | private Condition condition; 40 | private String url; 41 | private Boolean isAuthEnabled; 42 | private String siddhiAppName; 43 | private String streamId; 44 | 45 | public SSERequestListener(int workerThread, String url, Boolean auth, 46 | String streamId, String siddhiAppName) { 47 | this.executorService = Executors.newFixedThreadPool(workerThread); 48 | this.siddhiAppName = siddhiAppName; 49 | this.paused = false; 50 | this.lock = new ReentrantLock(); 51 | this.condition = lock.newCondition(); 52 | this.url = url; 53 | this.isAuthEnabled = auth; 54 | this.streamId = streamId; 55 | } 56 | 57 | protected void send(HttpCarbonMessage carbonMessage) { 58 | if (isAuthEnabled) { 59 | if (!HttpAuthenticator.authenticate(carbonMessage)) { 60 | throw new HttpSourceAdaptorRuntimeException(carbonMessage, "Authorisation fails", 401); 61 | } 62 | } 63 | 64 | executorService.execute(new SSEWorkerThread(carbonMessage, streamId)); 65 | } 66 | 67 | public String getSiddhiAppName() { 68 | return siddhiAppName; 69 | } 70 | 71 | void disconnect() { 72 | executorService.shutdown(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/SSESyncConnectorListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.sink; 19 | 20 | import io.siddhi.extension.io.http.util.HttpConstants; 21 | import org.apache.logging.log4j.LogManager; 22 | import org.apache.logging.log4j.Logger; 23 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 24 | 25 | /** 26 | * This class will synchronously listen to the connections. 27 | */ 28 | public class SSESyncConnectorListener extends SSEConnectorListener { 29 | 30 | private static final Logger log = LogManager.getLogger(SSESyncConnectorListener.class); 31 | 32 | protected boolean isValidRequest(HttpCarbonMessage carbonMessage) { 33 | return HttpConstants.PROTOCOL_ID.equals(carbonMessage.getProperty(HttpConstants.PROTOCOL)) && 34 | SSESyncConnectorRegistry.getInstance().getServerConnectorPool().containsKey(getInterface 35 | (carbonMessage)); 36 | } 37 | 38 | protected SSERequestListener getSourceListener(StringBuilder sourceListenerKey) { 39 | 40 | return SSESyncConnectorRegistry.getInstance().getSyncSourceListenersMap().get(sourceListenerKey.toString()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/SSEWorkerThread.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink; 20 | 21 | import io.siddhi.extension.io.http.source.HttpWorkerThread; 22 | import io.siddhi.extension.io.http.util.HTTPSinkRegistry; 23 | import org.apache.logging.log4j.LogManager; 24 | import org.apache.logging.log4j.Logger; 25 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 26 | import org.wso2.transport.http.netty.message.HttpMessageDataStreamer; 27 | 28 | import java.io.BufferedReader; 29 | import java.io.IOException; 30 | import java.io.InputStreamReader; 31 | import java.nio.charset.Charset; 32 | import java.util.stream.Collectors; 33 | 34 | /** 35 | * Handles the send data to source listener. 36 | */ 37 | public class SSEWorkerThread implements Runnable { 38 | private static final Logger logger = LogManager.getLogger(HttpWorkerThread.class); 39 | private HttpCarbonMessage carbonMessage; 40 | private String streamID; 41 | 42 | SSEWorkerThread(HttpCarbonMessage cMessage, String streamID) { 43 | this.carbonMessage = cMessage; 44 | this.streamID = streamID; 45 | } 46 | 47 | @Override 48 | public void run() { 49 | BufferedReader buf = new BufferedReader( 50 | new InputStreamReader( 51 | new HttpMessageDataStreamer(carbonMessage).getInputStream(), Charset.defaultCharset())); 52 | String payload = buf.lines().collect(Collectors.joining("\n")); 53 | carbonMessage.setStreaming(true); 54 | HTTPSinkRegistry.findAndGetSSESource(streamID).registerCallback(carbonMessage); 55 | if (logger.isDebugEnabled()) { 56 | logger.debug("Submitted Event {} Stream", payload); 57 | } 58 | 59 | try { 60 | buf.close(); 61 | } catch (IOException e) { 62 | logger.error("Error occurred when closing the byte buffer in source {}", streamID, e); 63 | } finally { 64 | carbonMessage.waitAndReleaseAllEntities(); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/WebSubSubscriptionDTO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.sink; 19 | 20 | /** 21 | * DTO class to hold web sub subscription details 22 | **/ 23 | public class WebSubSubscriptionDTO { 24 | private String hubId; 25 | private String callback; 26 | private String secret; 27 | private String topic; 28 | private long leaseSeconds; 29 | private long timestamp; 30 | 31 | public WebSubSubscriptionDTO(String hubId, Object callback, Object topic, Object secret, Object leaseSeconds, 32 | Object timestamp) { 33 | this.hubId = hubId; 34 | this.callback = (String) callback; 35 | this.secret = (String) secret; 36 | this.topic = (String) topic; 37 | this.leaseSeconds = (long) leaseSeconds; 38 | this.timestamp = (long) timestamp; 39 | } 40 | 41 | public String getHubId() { 42 | return hubId; 43 | } 44 | 45 | public void setHubId(String hubId) { 46 | this.hubId = hubId; 47 | } 48 | 49 | public String getCallback() { 50 | return callback; 51 | } 52 | 53 | public void setCallback(String callback) { 54 | this.callback = callback; 55 | } 56 | 57 | public String getSecret() { 58 | return secret; 59 | } 60 | 61 | public void setSecret(String secret) { 62 | this.secret = secret; 63 | } 64 | 65 | public String getTopic() { 66 | return topic; 67 | } 68 | 69 | public void setTopic(String topic) { 70 | this.topic = topic; 71 | } 72 | 73 | public long getLeaseSeconds() { 74 | return leaseSeconds; 75 | } 76 | 77 | public void setLeaseSeconds(long leaseSeconds) { 78 | this.leaseSeconds = leaseSeconds; 79 | } 80 | 81 | public long getTimestamp() { 82 | return timestamp; 83 | } 84 | 85 | public void setTimestamp(long timestamp) { 86 | this.timestamp = timestamp; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/exception/HttpSinkAdaptorRuntimeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.exception; 20 | 21 | /** 22 | * {@code HttpSinkAdaptorRuntimeException }HTTP output adaptor specific exception. 23 | */ 24 | public class HttpSinkAdaptorRuntimeException extends RuntimeException { 25 | 26 | public HttpSinkAdaptorRuntimeException(String message) { 27 | super(message); 28 | } 29 | 30 | public HttpSinkAdaptorRuntimeException(String message, Throwable cause) { 31 | super(message, cause); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/updatetoken/AccessTokenCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.updatetoken; 20 | 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | 24 | /** 25 | * {@code AccessTokenCache} Handle the access token caching. 26 | */ 27 | public class AccessTokenCache { 28 | private static Map accessToken; 29 | private static Map refreshToken; 30 | private static Map responseCode; 31 | 32 | private static AccessTokenCache accessTokenCache; 33 | 34 | private AccessTokenCache() { 35 | } 36 | 37 | public static synchronized AccessTokenCache getInstance() { 38 | if (accessTokenCache == null) { 39 | accessTokenCache = new AccessTokenCache(); 40 | accessToken = new HashMap<>(); 41 | refreshToken = new HashMap<>(); 42 | responseCode = new HashMap<>(); 43 | return accessTokenCache; 44 | } else { 45 | return accessTokenCache; 46 | } 47 | } 48 | 49 | public void setAccessToken(String key, String value) { 50 | accessToken.put(key, value); 51 | } 52 | 53 | public void setRefreshtoken(String key, String value) { 54 | refreshToken.put(key, value); 55 | } 56 | 57 | public void setResponseCode(String key, int value) { 58 | responseCode.put(key, value); 59 | } 60 | 61 | public int getResponseCode(String key) { 62 | return responseCode.get(key); 63 | } 64 | 65 | public String getAccessToken(String key) { 66 | return accessToken.get(key); 67 | } 68 | 69 | public String getRefreshtoken(String key) { 70 | return refreshToken.get(key); 71 | } 72 | 73 | public boolean checkAvailableKey(String value) { 74 | return accessToken.containsKey(value); 75 | } 76 | 77 | public boolean checkRefreshAvailableKey(String value) { 78 | return refreshToken.containsKey(value); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/updatetoken/DefaultListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.updatetoken; 20 | 21 | import io.siddhi.extension.io.http.util.HttpConstants; 22 | import org.wso2.transport.http.netty.contract.HttpConnectorListener; 23 | import org.wso2.transport.http.netty.message.Http2PushPromise; 24 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 25 | 26 | import java.util.concurrent.CountDownLatch; 27 | 28 | /** 29 | * {@code DefaultListner} Handle the HTTP listner. 30 | */ 31 | public class DefaultListener implements HttpConnectorListener { 32 | private HttpCarbonMessage httpMessage; 33 | private CountDownLatch latch; 34 | private String authType; 35 | 36 | public DefaultListener(CountDownLatch latch, String authType) { 37 | this.latch = latch; 38 | this.authType = authType; 39 | } 40 | 41 | @Override 42 | public void onMessage(HttpCarbonMessage httpMessage) { 43 | this.httpMessage = httpMessage; 44 | if (HttpConstants.OAUTH.equals(authType)) { 45 | latch.countDown(); 46 | } 47 | } 48 | 49 | @Override 50 | public void onError(Throwable throwable) { 51 | latch.countDown(); 52 | } 53 | 54 | @Override 55 | public void onPushPromise(Http2PushPromise pushPromise) { 56 | 57 | } 58 | 59 | public HttpCarbonMessage getHttpResponseMessage() { 60 | return httpMessage; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/sink/updatetoken/HttpRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.updatetoken; 20 | 21 | import io.netty.handler.codec.http.HttpHeaderValues; 22 | import io.siddhi.extension.io.http.util.HttpConstants; 23 | import okhttp3.Headers; 24 | import okhttp3.MediaType; 25 | import okhttp3.OkHttpClient; 26 | import okhttp3.Request; 27 | import okhttp3.RequestBody; 28 | import okhttp3.Response; 29 | import org.apache.logging.log4j.LogManager; 30 | import org.apache.logging.log4j.Logger; 31 | 32 | import java.io.IOException; 33 | import java.nio.charset.StandardCharsets; 34 | import java.util.ArrayList; 35 | import java.util.List; 36 | import java.util.Map; 37 | 38 | import static io.siddhi.extension.io.http.util.HttpConstants.HTTP_CONTENT_TYPE; 39 | import static io.siddhi.extension.io.http.util.HttpConstants.HTTP_METHOD_POST; 40 | 41 | /** 42 | * {@code HttpRequest} Handle the HTTP request for invalid access token. 43 | */ 44 | public class HttpRequest { 45 | 46 | private static final Logger log = LogManager.getLogger(HttpRequest.class); 47 | public static List getResponse(String tokenUrl, String encodedAuth, String payload, OkHttpClient client, 48 | Map headers) { 49 | List responses = new ArrayList<>(); 50 | MediaType mediaType = MediaType.parse(String.valueOf(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED)); 51 | RequestBody requestBody = RequestBody.create(payload.getBytes(StandardCharsets.UTF_8), mediaType); 52 | headers.put(HTTP_CONTENT_TYPE, String.valueOf(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED)); 53 | headers.put(HttpConstants.AUTHORIZATION_HEADER, encodedAuth); 54 | 55 | Headers headerbuilder = Headers.of(headers); 56 | 57 | Request request = new Request.Builder().url(tokenUrl).method(HTTP_METHOD_POST, requestBody) 58 | .headers(headerbuilder).build(); 59 | try { 60 | Response response = client.newCall(request).execute(); 61 | responses.add(String.valueOf(response.code())); 62 | responses.add(response.body().string()); 63 | response.close(); 64 | } catch (IOException e) { 65 | log.error("Error occurred while generating a new access token ", e); 66 | } 67 | return responses; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/ConnectorStartupSynchronizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package io.siddhi.extension.io.http.source; 19 | 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | import java.util.concurrent.CountDownLatch; 23 | 24 | /** 25 | * A utility class for facilitating the parallel start up of connectors. Any exceptions which occur during connector 26 | * start up can be wrapped in this class and later thrown in the siddhi side. 27 | * 28 | * @since 0.94 29 | */ 30 | public class ConnectorStartupSynchronizer { 31 | 32 | private Map exceptions = new HashMap<>(); 33 | private CountDownLatch countDownLatch; 34 | 35 | public ConnectorStartupSynchronizer(CountDownLatch countDownLatch) { 36 | this.countDownLatch = countDownLatch; 37 | } 38 | 39 | public void addException(String connectorId, Exception ex) { 40 | exceptions.put(connectorId, ex); 41 | } 42 | 43 | public Map getExceptions() { 44 | return exceptions; 45 | } 46 | 47 | public CountDownLatch getCountDownLatch() { 48 | return countDownLatch; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/HTTPConnectorListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package io.siddhi.extension.io.http.source; 19 | 20 | import io.siddhi.extension.io.http.source.exception.HttpSourceAdaptorRuntimeException; 21 | import io.siddhi.extension.io.http.source.util.HttpSourceUtil; 22 | import io.siddhi.extension.io.http.util.HttpConstants; 23 | import org.apache.logging.log4j.LogManager; 24 | import org.apache.logging.log4j.Logger; 25 | import org.wso2.transport.http.netty.contract.Constants; 26 | import org.wso2.transport.http.netty.contract.HttpClientConnector; 27 | import org.wso2.transport.http.netty.contract.HttpConnectorListener; 28 | import org.wso2.transport.http.netty.contract.config.TransportsConfiguration; 29 | import org.wso2.transport.http.netty.contract.exceptions.ClientClosedConnectionException; 30 | import org.wso2.transport.http.netty.contract.exceptions.ServerConnectorException; 31 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 32 | 33 | import java.net.URI; 34 | 35 | /** 36 | * HTTP connector listener for Siddhi. 37 | */ 38 | public class HTTPConnectorListener implements HttpConnectorListener { 39 | 40 | private static final Logger log = LogManager.getLogger(HTTPConnectorListener.class); 41 | private TransportsConfiguration configuration; 42 | private HttpClientConnector clientConnector; 43 | 44 | public HTTPConnectorListener(TransportsConfiguration configuration) { 45 | this.configuration = configuration; 46 | } 47 | 48 | public HTTPConnectorListener() { 49 | } 50 | 51 | @Override 52 | public void onMessage(HttpCarbonMessage carbonMessage) { 53 | try { 54 | if (isValidRequest(carbonMessage)) { 55 | //Check the message is a response or direct message 56 | if (carbonMessage.getProperty(org.wso2.carbon.messaging.Constants.DIRECTION) != null && 57 | carbonMessage.getProperty(org.wso2.carbon.messaging.Constants.DIRECTION) 58 | .equals(org.wso2.carbon.messaging.Constants.DIRECTION_RESPONSE)) { 59 | try { 60 | carbonMessage.respond(carbonMessage); 61 | } catch (ServerConnectorException e) { 62 | log.error("Error occurred during message notification: ", e); 63 | } 64 | } else { 65 | if (HttpConstants.HTTP_METHOD_POST.equalsIgnoreCase(carbonMessage.getHttpMethod())) { 66 | //get the required source listener 67 | 68 | String rawPath = URI.create((String) carbonMessage.getProperty(HttpConstants.TO)).getRawPath(); 69 | StringBuilder sourceListenerKey = new StringBuilder().append(String 70 | .valueOf(carbonMessage.getProperty(HttpConstants.LISTENER_PORT))) 71 | .append(HttpConstants.PORT_CONTEXT_KEY_SEPARATOR) 72 | .append(rawPath); 73 | HttpSourceListener sourceListener = getSourceListener(sourceListenerKey); 74 | if (sourceListener != null) { 75 | sourceListener.send(carbonMessage); 76 | } else { 77 | HttpSourceUtil.handleCallback(carbonMessage, 404); 78 | } 79 | } else if (HttpConstants.HTTP_METHOD_OPTIONS.equalsIgnoreCase(carbonMessage.getHttpMethod())) { 80 | HttpSourceUtil.handleCORS(carbonMessage); 81 | } else { 82 | throw new HttpSourceAdaptorRuntimeException(carbonMessage, "Request type is not a type of POST ", 83 | 400); 84 | } 85 | } 86 | } else { 87 | if (log.isDebugEnabled()) { 88 | log.debug("Event is not type of http which has received to the uri '{}{}", String 89 | .valueOf(carbonMessage.getProperty(HttpConstants.LISTENER_PORT)), 90 | carbonMessage.getProperty(HttpConstants.TO)); 91 | } 92 | HttpSourceUtil.handleCallback(carbonMessage, 404); 93 | } 94 | } finally { 95 | carbonMessage.waitAndReleaseAllEntities(); 96 | } 97 | } 98 | 99 | protected boolean isValidRequest(HttpCarbonMessage carbonMessage) { 100 | 101 | return HttpConstants.PROTOCOL_ID.equals(carbonMessage.getProperty(HttpConstants.PROTOCOL)) && 102 | HttpConnectorRegistry.getInstance().getServerConnectorPool().containsKey(getInterface(carbonMessage)); 103 | } 104 | 105 | protected HttpSourceListener getSourceListener(StringBuilder sourceListenerKey) { 106 | 107 | return HttpConnectorRegistry.getInstance().getSourceListenersMap().get(sourceListenerKey.toString()); 108 | } 109 | 110 | protected String getInterface(HttpCarbonMessage cMsg) { 111 | String interfaceId = (String) cMsg.getProperty(Constants.LISTENER_INTERFACE_ID); 112 | if (interfaceId == null) { 113 | if (log.isDebugEnabled()) { 114 | log.debug("Interface id not found on the message, hence using the default interface"); 115 | } 116 | interfaceId = HttpConstants.DEFAULT_INTERFACE; 117 | } 118 | 119 | return interfaceId; 120 | } 121 | 122 | @Override 123 | public void onError(Throwable throwable) { 124 | if (throwable instanceof ClientClosedConnectionException) { 125 | log.debug("Error in http server connector", throwable); 126 | } else { 127 | log.error("Error in http server connector", throwable); 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/HTTPSyncConnectorListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package io.siddhi.extension.io.http.source; 19 | 20 | import io.siddhi.extension.io.http.util.HttpConstants; 21 | import org.apache.logging.log4j.LogManager; 22 | import org.apache.logging.log4j.Logger; 23 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 24 | 25 | /** 26 | * HTTP connector listener for Siddhi. 27 | */ 28 | public class HTTPSyncConnectorListener extends HTTPConnectorListener { 29 | 30 | private static final Logger log = LogManager.getLogger(HTTPSyncConnectorListener.class); 31 | 32 | protected boolean isValidRequest(HttpCarbonMessage carbonMessage) { 33 | 34 | return HttpConstants.PROTOCOL_ID.equals(carbonMessage.getProperty(HttpConstants.PROTOCOL)) && 35 | HttpSyncConnectorRegistry.getInstance().getServerConnectorPool().containsKey(getInterface 36 | (carbonMessage)); 37 | } 38 | 39 | protected HttpSourceListener getSourceListener(StringBuilder sourceListenerKey) { 40 | 41 | return HttpSyncConnectorRegistry.getInstance().getSyncSourceListenersMap().get(sourceListenerKey.toString()); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/HttpAuthenticator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source; 20 | 21 | import io.netty.buffer.ByteBuf; 22 | import io.netty.buffer.Unpooled; 23 | import io.netty.handler.codec.base64.Base64; 24 | import io.siddhi.extension.io.http.source.internal.HttpIODataHolder; 25 | import io.siddhi.extension.io.http.util.HttpConstants; 26 | import org.apache.logging.log4j.LogManager; 27 | import org.apache.logging.log4j.Logger; 28 | import org.wso2.carbon.analytics.idp.client.core.api.IdPClient; 29 | import org.wso2.carbon.analytics.idp.client.core.exception.IdPClientException; 30 | import org.wso2.carbon.analytics.idp.client.core.utils.IdPClientConstants; 31 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 32 | 33 | import java.nio.charset.Charset; 34 | import java.util.HashMap; 35 | import java.util.Map; 36 | 37 | import static io.siddhi.extension.io.http.util.HttpConstants.CREDENTIAL_SEPARATOR; 38 | import static io.siddhi.extension.io.http.util.HttpConstants.EMPTY_STRING; 39 | 40 | /** 41 | * Basic authentication handler of http io implementation. 42 | */ 43 | public class HttpAuthenticator { 44 | private static final Logger logger = LogManager.getLogger(HttpAuthenticator.class); 45 | 46 | public static boolean authenticate(HttpCarbonMessage httpCarbonMessage) { 47 | if (HttpIODataHolder.getInstance().getBundleContext() == null) { 48 | //this will handle the events at non osgi mode. 49 | return true; 50 | } else { 51 | String authHeader = httpCarbonMessage.getHeaders().get(HttpConstants.AUTHORIZATION_HEADER); 52 | if (authHeader != null) { 53 | String usernamePasswordEncoded = authHeader.replace(HttpConstants.AUTHORIZATION_METHOD, EMPTY_STRING); 54 | ByteBuf usernamePasswordBuf = Base64.decode(Unpooled.copiedBuffer(usernamePasswordEncoded.getBytes 55 | (Charset.defaultCharset()))); 56 | String[] credentials = usernamePasswordBuf.toString(Charset.defaultCharset()) 57 | .split(CREDENTIAL_SEPARATOR); 58 | IdPClient idPClient = HttpIODataHolder.getInstance().getClient(); 59 | if ((idPClient != null) && (credentials.length == 2)) { 60 | try { 61 | Map loginProperties = new HashMap<>(); 62 | loginProperties.put(IdPClientConstants.USERNAME, credentials[0]); 63 | loginProperties.put(IdPClientConstants.PASSWORD, credentials[1]); 64 | loginProperties.put(IdPClientConstants.GRANT_TYPE, IdPClientConstants.PASSWORD_GRANT_TYPE); 65 | Map login = idPClient.login(loginProperties); 66 | String loginStatus = login.get(IdPClientConstants.LOGIN_STATUS); 67 | if (loginStatus.equals(IdPClientConstants.LoginStatus.LOGIN_SUCCESS)) { 68 | return true; 69 | } else { 70 | logger.error("Authentication failed for username '{}'. Error : '{}'. Error Description :" + 71 | " '{}'", credentials[0], login.get(IdPClientConstants.ERROR), 72 | login.get(IdPClientConstants.ERROR_DESCRIPTION)); 73 | return false; 74 | } 75 | } catch (IdPClientException e) { 76 | logger.error("Authorization process fails for user '{}'", credentials[0], e); 77 | return false; 78 | } 79 | } else { 80 | logger.error("Authorization header in incorrect format. header: {}", usernamePasswordEncoded); 81 | return false; 82 | } 83 | } else { 84 | logger.error("Authorization header 'null' "); 85 | return false; 86 | } 87 | } 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/HttpCallResponseConnectorListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | 20 | package io.siddhi.extension.io.http.source; 21 | 22 | import io.siddhi.core.stream.input.source.SourceEventListener; 23 | import io.siddhi.extension.io.http.metrics.SourceMetrics; 24 | import org.apache.logging.log4j.LogManager; 25 | import org.apache.logging.log4j.Logger; 26 | import org.wso2.transport.http.netty.contract.HttpConnectorListener; 27 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 28 | 29 | import java.util.concurrent.ExecutorService; 30 | import java.util.concurrent.Executors; 31 | 32 | /** 33 | * Connector Listener for HttpResponseSource 34 | */ 35 | public class HttpCallResponseConnectorListener implements HttpConnectorListener { 36 | private static final Logger log = LogManager.getLogger(HttpCallResponseConnectorListener.class); 37 | private SourceEventListener sourceEventListener; 38 | private String sinkId; 39 | private ExecutorService executorService; 40 | private String siddhiAppName; 41 | private String[] trpPropertyNames; 42 | private boolean shouldAllowStreamingResponses; 43 | private SourceMetrics metrics; 44 | 45 | public HttpCallResponseConnectorListener(int numberOfThreads, SourceEventListener sourceEventListener, 46 | boolean shouldAllowStreamingResponses, 47 | String sinkId, String[] trpPropertyNames, String siddhiAppName, 48 | SourceMetrics metrics) { 49 | this.sourceEventListener = sourceEventListener; 50 | this.sinkId = sinkId; 51 | this.executorService = Executors.newFixedThreadPool(numberOfThreads); 52 | this.siddhiAppName = siddhiAppName; 53 | this.trpPropertyNames = trpPropertyNames.clone(); 54 | this.shouldAllowStreamingResponses = shouldAllowStreamingResponses; 55 | this.metrics = metrics; 56 | } 57 | 58 | @Override 59 | public void onMessage(HttpCarbonMessage carbonMessage) { 60 | String[] properties = new String[trpPropertyNames.length]; 61 | for (int i = 0; i < trpPropertyNames.length; i++) { 62 | Object property = carbonMessage.getProperty(trpPropertyNames[i]); 63 | if (property != null) { 64 | properties[i] = carbonMessage.getProperty(trpPropertyNames[i]).toString(); 65 | } 66 | } 67 | HttpResponseProcessor workerThread = 68 | new HttpResponseProcessor(carbonMessage, sourceEventListener, shouldAllowStreamingResponses, 69 | sinkId, properties, metrics); 70 | executorService.execute(workerThread); 71 | } 72 | 73 | @Override 74 | public void onError(Throwable throwable) { 75 | log.error("Error occurred during processing response for the request sent by http-call sink with 'sink.id' = " + 76 | "{} in Siddhi app {}.", sinkId, siddhiAppName, throwable); 77 | } 78 | 79 | /** 80 | * Returns the siddhi app name 81 | * 82 | * @return siddhi app name 83 | */ 84 | String getSiddhiAppName() { 85 | return siddhiAppName; 86 | } 87 | 88 | /** 89 | * Disconnect pool execution. 90 | */ 91 | void disconnect() { 92 | executorService.shutdown(); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/HttpCallResponseSourceConnectorRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source; 20 | 21 | import io.siddhi.core.exception.SiddhiAppCreationException; 22 | 23 | import java.util.Map; 24 | import java.util.concurrent.ConcurrentHashMap; 25 | 26 | /** 27 | * {@code HttpConnectorRegistry} The code is responsible for maintaining the all active connector listeners for 28 | * http-response source. 29 | */ 30 | class HttpCallResponseSourceConnectorRegistry { 31 | private static HttpCallResponseSourceConnectorRegistry instance = new HttpCallResponseSourceConnectorRegistry(); 32 | private Map sourceListenersMap = new ConcurrentHashMap<>(); 33 | 34 | private HttpCallResponseSourceConnectorRegistry() { 35 | 36 | } 37 | 38 | /** 39 | * Get HttpCallResponseSourceConnectorRegistry instance. 40 | * 41 | * @return HttpCallResponseSourceConnectorRegistry instance 42 | */ 43 | static HttpCallResponseSourceConnectorRegistry getInstance() { 44 | return instance; 45 | } 46 | 47 | 48 | /** 49 | * Get the source listener map. 50 | * 51 | * @return the source listener map 52 | */ 53 | Map getSourceListenersMap() { 54 | return this.sourceListenersMap; 55 | } 56 | 57 | 58 | /** 59 | * Register new source listener. 60 | * 61 | * @param sinkId the sink id for the source 62 | */ 63 | void registerSourceListener(HttpCallResponseConnectorListener httpResponseSourceListener, String sinkId, 64 | String statusCode) { 65 | HttpCallResponseConnectorListener sourceListener = 66 | this.sourceListenersMap.putIfAbsent((sinkId + statusCode), httpResponseSourceListener); 67 | if (sourceListener != null) { 68 | throw new SiddhiAppCreationException("There is a connection already established for the source with " + 69 | "sink.id : '" + sinkId + "' and http.status.code : '" + statusCode + "'."); 70 | } 71 | } 72 | 73 | /** 74 | * Unregister the source listener. 75 | * 76 | * @param sinkId the sink id of the source 77 | * @param siddhiAppName name of the siddhi app 78 | */ 79 | void unregisterSourceListener(String sinkId, String statusCode, String siddhiAppName) { 80 | HttpCallResponseConnectorListener httpSourceListener = 81 | this.sourceListenersMap.get(sinkId + statusCode); 82 | if (httpSourceListener != null && httpSourceListener.getSiddhiAppName().equals(siddhiAppName)) { 83 | sourceListenersMap.remove(sinkId + statusCode); 84 | httpSourceListener.disconnect(); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/HttpConnectorPortBindingListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package io.siddhi.extension.io.http.source; 19 | 20 | import io.siddhi.extension.io.http.metrics.EndpointStatus; 21 | import io.siddhi.extension.io.http.metrics.SourceMetrics; 22 | import org.apache.logging.log4j.LogManager; 23 | import org.apache.logging.log4j.Logger; 24 | import org.wso2.transport.http.netty.contract.PortBindingEventListener; 25 | 26 | import java.net.BindException; 27 | 28 | /** 29 | * An implementation of the LifeCycleEventListener. This can be used to listen to the HTTP connector life cycle events. 30 | * 31 | * @since 0.94 32 | */ 33 | public class HttpConnectorPortBindingListener implements PortBindingEventListener { 34 | 35 | private static final Logger log = LogManager.getLogger(HttpConnectorPortBindingListener.class); 36 | 37 | private ConnectorStartupSynchronizer connectorStartupSynchronizer; 38 | private String serverConnectorId; 39 | private SourceMetrics metrics; 40 | 41 | public HttpConnectorPortBindingListener(ConnectorStartupSynchronizer connectorStartupSynchronizer, 42 | String serverConnectorId, SourceMetrics metrics) { 43 | this.connectorStartupSynchronizer = connectorStartupSynchronizer; 44 | this.serverConnectorId = serverConnectorId; 45 | this.metrics = metrics; 46 | } 47 | 48 | @Override 49 | public void onOpen(String serverConnectorId, boolean isHttps) { 50 | if (metrics != null) { 51 | metrics.setEndpointStatusMetric(EndpointStatus.ONLINE); 52 | } 53 | 54 | if (isHttps) { 55 | log.info("HTTPS source {} has been started", serverConnectorId); 56 | } else { 57 | log.info("HTTP source {} has been started", serverConnectorId); 58 | } 59 | 60 | connectorStartupSynchronizer.getCountDownLatch().countDown(); 61 | } 62 | 63 | @Override 64 | public void onClose(String serverConnectorId, boolean isHttps) { 65 | if (metrics != null) { 66 | metrics.setEndpointStatusMetric(EndpointStatus.OFFLINE); 67 | } 68 | 69 | if (isHttps) { 70 | log.info("HTTPS source {} has been closed", serverConnectorId); 71 | } else { 72 | log.info("HTTP source {} has been closed", serverConnectorId); 73 | } 74 | } 75 | 76 | @Override 77 | public void onError(Throwable throwable) { 78 | if (metrics != null) { 79 | metrics.setEndpointStatusMetric(EndpointStatus.OFFLINE); 80 | } 81 | 82 | log.error("Error in http source ", throwable); 83 | 84 | if (throwable instanceof BindException) { 85 | connectorStartupSynchronizer.addException(serverConnectorId, (BindException) throwable); 86 | connectorStartupSynchronizer.getCountDownLatch().countDown(); 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/HttpSyncSourceListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source; 20 | 21 | import io.siddhi.core.stream.input.source.SourceEventListener; 22 | import io.siddhi.extension.io.http.metrics.SourceMetrics; 23 | import io.siddhi.extension.io.http.source.exception.HttpSourceAdaptorRuntimeException; 24 | import io.siddhi.extension.io.http.source.util.HttpSourceUtil; 25 | import org.apache.logging.log4j.LogManager; 26 | import org.apache.logging.log4j.Logger; 27 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 28 | 29 | import java.util.UUID; 30 | 31 | /** 32 | * {@code HttpSourceListener } This class maintain the states of each and every source listener which are created 33 | * such as is currently paused or not, need isAuthEnabled or not. 34 | */ 35 | public class HttpSyncSourceListener extends HttpSourceListener { 36 | 37 | private static final Logger logger = LogManager.getLogger(HttpSyncSourceListener.class); 38 | private String sourceId; 39 | 40 | protected HttpSyncSourceListener(int workerThread, String url, Boolean auth, 41 | SourceEventListener sourceEventListener, 42 | String[] requestedTransportPropertyNames, 43 | String sourceId, String siddhiAppName, SourceMetrics metrics, boolean isWebSub) { 44 | 45 | super(workerThread, url, auth, sourceEventListener, requestedTransportPropertyNames, siddhiAppName, metrics); 46 | this.sourceId = sourceId; 47 | } 48 | 49 | /** 50 | * This method is handle the submit carbon message to executor service. 51 | * 52 | * @param carbonMessage the carbon message received from carbon transport. 53 | */ 54 | protected void send(HttpCarbonMessage carbonMessage) { 55 | 56 | if (paused) { 57 | lock.lock(); 58 | try { 59 | while (paused) { 60 | condition.await(); 61 | } 62 | } catch (InterruptedException ie) { 63 | Thread.currentThread().interrupt(); 64 | if (metrics != null) { 65 | metrics.getTotalHttpErrorsMetric().inc(); 66 | } 67 | 68 | logger.error("Thread interrupted while pausing ", ie); 69 | HttpSourceUtil.handleCallback(carbonMessage, 500); 70 | } finally { 71 | lock.unlock(); 72 | } 73 | } 74 | if (isAuthEnabled) { 75 | if (!HttpAuthenticator.authenticate(carbonMessage)) { 76 | if (metrics != null) { 77 | metrics.getTotalHttpErrorsMetric().inc(); 78 | } 79 | 80 | throw new HttpSourceAdaptorRuntimeException(carbonMessage, "Authorisation fails", 401); 81 | } 82 | } 83 | String[] trpProperties = new String[requestedTransportPropertyNames.length]; 84 | populateTransportHeaders(carbonMessage, trpProperties); 85 | populateTransportProperties(carbonMessage, trpProperties); 86 | String messageId = UUID.randomUUID().toString(); 87 | populateTransportProperties(trpProperties, messageId); 88 | executorService.execute(new HttpSyncWorkerThread(carbonMessage, 89 | sourceEventListener, sourceEventListener.getStreamDefinition().toString(), trpProperties, 90 | sourceId, messageId, metrics)); 91 | 92 | } 93 | 94 | private void populateTransportProperties(String[] properties, String messageId) { 95 | 96 | if (requestedTransportPropertyNames.length > 0) { //cannot be null according to siddhi impl 97 | int i = 0; 98 | for (String property : requestedTransportPropertyNames) { 99 | if ("messageId".equalsIgnoreCase(property)) { 100 | properties[i] = messageId; 101 | break; 102 | } 103 | i++; 104 | } 105 | } 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/HttpSyncWorkerThread.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source; 20 | 21 | import io.siddhi.core.stream.input.source.SourceEventListener; 22 | import io.siddhi.extension.io.http.metrics.SourceMetrics; 23 | import io.siddhi.extension.io.http.source.util.HttpSourceUtil; 24 | import io.siddhi.extension.io.http.util.HTTPSourceRegistry; 25 | import io.siddhi.extension.io.http.util.HttpConstants; 26 | import org.apache.logging.log4j.LogManager; 27 | import org.apache.logging.log4j.Logger; 28 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 29 | import org.wso2.transport.http.netty.message.HttpMessageDataStreamer; 30 | 31 | import java.io.BufferedReader; 32 | import java.io.IOException; 33 | import java.io.InputStreamReader; 34 | import java.nio.charset.Charset; 35 | import java.util.stream.Collectors; 36 | 37 | /** 38 | * Handles the send data to source listener. 39 | */ 40 | public class HttpSyncWorkerThread implements Runnable { 41 | private static final Logger logger = LogManager.getLogger(HttpWorkerThread.class); 42 | private HttpCarbonMessage carbonMessage; 43 | private SourceEventListener sourceEventListener; 44 | private String sourceID; 45 | private String[] trpProperties; 46 | private String sourceId; 47 | private String messageId; 48 | private SourceMetrics metrics; 49 | 50 | HttpSyncWorkerThread(HttpCarbonMessage cMessage, SourceEventListener sourceEventListener, 51 | String sourceID, String[] trpProperties, String sourceId, String messageId, 52 | SourceMetrics metrics) { 53 | this.carbonMessage = cMessage; 54 | this.sourceEventListener = sourceEventListener; 55 | this.sourceID = sourceID; 56 | this.trpProperties = trpProperties; 57 | this.messageId = messageId; 58 | this.sourceId = sourceId; 59 | this.metrics = metrics; 60 | } 61 | 62 | @Override 63 | public void run() { 64 | BufferedReader buf = new BufferedReader( 65 | new InputStreamReader( 66 | new HttpMessageDataStreamer(carbonMessage).getInputStream(), Charset.defaultCharset())); 67 | try { 68 | String payload = buf.lines().collect(Collectors.joining("\n")); 69 | 70 | if (!payload.equals(HttpConstants.EMPTY_STRING)) { 71 | HTTPSourceRegistry.getServiceSource(sourceId).registerCallback(carbonMessage, messageId); 72 | 73 | if (metrics != null) { 74 | metrics.getTotalReadsMetric().inc(); 75 | metrics.getTotalHttpReadsMetric().inc(); 76 | metrics.getRequestSizeMetric().inc(HttpSourceUtil.getByteSize(payload)); 77 | metrics.setLastEventTime(System.currentTimeMillis()); 78 | } 79 | 80 | sourceEventListener.onEvent(payload, trpProperties); 81 | if (logger.isDebugEnabled()) { 82 | logger.debug("Submitted Event {} Stream", payload); 83 | } 84 | } else { 85 | if (metrics != null) { 86 | metrics.getTotalHttpErrorsMetric().inc(); 87 | } 88 | 89 | HttpSourceUtil.handleCallback(carbonMessage, 405); 90 | if (logger.isDebugEnabled()) { 91 | logger.debug("Empty payload event, hence dropping the event chunk at source {}", sourceID); 92 | } 93 | } 94 | } finally { 95 | try { 96 | buf.close(); 97 | } catch (IOException e) { 98 | if (metrics != null) { 99 | metrics.getTotalHttpErrorsMetric().inc(); 100 | } 101 | logger.error("Error occurred when closing the byte buffer in source {}", sourceID, e); 102 | } finally { 103 | carbonMessage.waitAndReleaseAllEntities(); 104 | } 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/HttpWorkerThread.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source; 20 | 21 | import io.siddhi.core.stream.input.source.SourceEventListener; 22 | import io.siddhi.extension.io.http.metrics.SourceMetrics; 23 | import io.siddhi.extension.io.http.source.util.HttpSourceUtil; 24 | import io.siddhi.extension.io.http.util.HttpConstants; 25 | import org.apache.logging.log4j.LogManager; 26 | import org.apache.logging.log4j.Logger; 27 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 28 | import org.wso2.transport.http.netty.message.HttpMessageDataStreamer; 29 | 30 | import java.io.BufferedReader; 31 | import java.io.IOException; 32 | import java.io.InputStreamReader; 33 | import java.nio.charset.Charset; 34 | import java.util.stream.Collectors; 35 | 36 | /** 37 | * Handles the send data to source listener. 38 | */ 39 | public class HttpWorkerThread implements Runnable { 40 | private static final Logger logger = LogManager.getLogger(HttpWorkerThread.class); 41 | private HttpCarbonMessage carbonMessage; 42 | private SourceEventListener sourceEventListener; 43 | private String sourceID; 44 | private String[] trpProperties; 45 | private SourceMetrics metrics; 46 | 47 | HttpWorkerThread(HttpCarbonMessage cMessage, SourceEventListener sourceEventListener, 48 | String sourceID, String[] trpProperties, SourceMetrics metrics) { 49 | this.carbonMessage = cMessage; 50 | this.sourceEventListener = sourceEventListener; 51 | this.sourceID = sourceID; 52 | this.trpProperties = trpProperties; 53 | this.metrics = metrics; 54 | } 55 | 56 | @Override 57 | public void run() { 58 | BufferedReader buf = new BufferedReader( 59 | new InputStreamReader( 60 | new HttpMessageDataStreamer(carbonMessage).getInputStream(), Charset.defaultCharset())); 61 | try { 62 | String payload = buf.lines().collect(Collectors.joining("\n")); 63 | 64 | if (!payload.equals(HttpConstants.EMPTY_STRING)) { 65 | 66 | if (metrics != null) { 67 | metrics.getTotalReadsMetric().inc(); 68 | metrics.getTotalHttpReadsMetric().inc(); 69 | metrics.getRequestSizeMetric().inc(HttpSourceUtil.getByteSize(payload)); 70 | metrics.setLastEventTime(System.currentTimeMillis()); 71 | } 72 | 73 | sourceEventListener.onEvent(payload, trpProperties); 74 | HttpSourceUtil.handleCallback(carbonMessage, 200); 75 | if (logger.isDebugEnabled()) { 76 | logger.debug("Submitted Event {} Stream", payload); 77 | } 78 | } else { 79 | if (metrics != null) { 80 | metrics.getTotalHttpErrorsMetric().inc(); 81 | } 82 | 83 | HttpSourceUtil.handleCallback(carbonMessage, 405); 84 | if (logger.isDebugEnabled()) { 85 | logger.debug("Empty payload event, hence dropping the event chunk at source {}", sourceID); 86 | } 87 | } 88 | } finally { 89 | try { 90 | buf.close(); 91 | } catch (IOException e) { 92 | if (metrics != null) { 93 | metrics.getTotalHttpErrorsMetric().inc(); 94 | } 95 | logger.error("Error occurred when closing the byte buffer in source {}", sourceID, e); 96 | } finally { 97 | carbonMessage.waitAndReleaseAllEntities(); 98 | } 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/SSEResponseConnectorListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.source; 19 | 20 | import io.siddhi.core.stream.input.source.SourceEventListener; 21 | import io.siddhi.extension.io.http.metrics.SourceMetrics; 22 | 23 | /** 24 | * Extended HttpCallResponseConnectorListener for HttpSSESource, where shouldAllowStreamingResponses is true. 25 | */ 26 | public class SSEResponseConnectorListener extends HttpCallResponseConnectorListener { 27 | public SSEResponseConnectorListener(int numberOfThreads, SourceEventListener sourceEventListener, 28 | String streamId, String[] trpPropertyNames, String siddhiAppName, 29 | SourceMetrics metrics) { 30 | super(numberOfThreads, sourceEventListener, true, streamId, 31 | trpPropertyNames, siddhiAppName, metrics); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/SSEResponseListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.source; 19 | 20 | import io.siddhi.extension.io.http.metrics.SourceMetrics; 21 | import io.siddhi.extension.io.http.util.HTTPSourceRegistry; 22 | import io.siddhi.extension.io.http.util.HttpConstants; 23 | import org.apache.logging.log4j.LogManager; 24 | import org.apache.logging.log4j.Logger; 25 | import org.wso2.transport.http.netty.contract.HttpConnectorListener; 26 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 27 | 28 | import java.io.IOException; 29 | import java.util.concurrent.CountDownLatch; 30 | 31 | /** 32 | * Connector Listener for HttpSSESource. 33 | */ 34 | public class SSEResponseListener implements HttpConnectorListener { 35 | private static final Logger log = LogManager.getLogger(SSEResponseListener.class); 36 | private String streamId; 37 | private CountDownLatch latch; 38 | private SSESource source; 39 | private SourceMetrics metrics; 40 | 41 | public SSEResponseListener(SSESource source, String streamId, CountDownLatch latch, 42 | SourceMetrics metrics) { 43 | this.streamId = streamId; 44 | this.latch = latch; 45 | this.source = source; 46 | this.metrics = metrics; 47 | } 48 | 49 | @Override 50 | public void onMessage(HttpCarbonMessage carbonMessage) { 51 | carbonMessage.setProperty(HttpConstants.IS_DOWNLOADABLE_CONTENT, false); 52 | if (latch != null) { 53 | latch.countDown(); 54 | } 55 | 56 | SSESource responseSource = HTTPSourceRegistry.findAndGetSSESource(streamId); 57 | if (responseSource != null) { 58 | SSEResponseConnectorListener responseConnectorListener = responseSource.getConnectorListener(); 59 | responseConnectorListener.onMessage(carbonMessage); 60 | } else { 61 | log.error("No sse source is registered for the stream '{}'. Hence dropping the response message.", 62 | streamId); 63 | } 64 | } 65 | 66 | @Override 67 | public void onError(Throwable throwable) { 68 | if (throwable instanceof IOException) { 69 | source.createClientConnector(); 70 | } 71 | 72 | if (latch != null) { 73 | latch.countDown(); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/SSESourceConnectorRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.source; 19 | 20 | import io.siddhi.core.exception.SiddhiAppCreationException; 21 | 22 | import java.util.Map; 23 | import java.util.concurrent.ConcurrentHashMap; 24 | 25 | /** 26 | * {@code HttpSSESourceConnectorRegistry} The code is responsible for maintaining the all active connector listeners 27 | * for http-sse source. 28 | */ 29 | class SSESourceConnectorRegistry { 30 | private static SSESourceConnectorRegistry instance = new SSESourceConnectorRegistry(); 31 | private Map sourceListenersMap = new ConcurrentHashMap<>(); 32 | 33 | private SSESourceConnectorRegistry() { 34 | 35 | } 36 | 37 | /** 38 | * Get HttpSSESourceConnectorRegistry instance. 39 | * 40 | * @return HttpSSESourceConnectorRegistry instance 41 | */ 42 | static SSESourceConnectorRegistry getInstance() { 43 | return instance; 44 | } 45 | 46 | 47 | /** 48 | * Get the source listener map. 49 | * 50 | * @return the source listener map 51 | */ 52 | Map getSourceListenersMap() { 53 | return this.sourceListenersMap; 54 | } 55 | 56 | 57 | /** 58 | * Register a new source listener. 59 | * 60 | * @param streamId the sink id for the source 61 | */ 62 | void registerSourceListener(SSEResponseConnectorListener httpSSEResponseConnectorListener, String streamId) { 63 | SSEResponseConnectorListener sourceListener = 64 | this.sourceListenersMap.putIfAbsent((streamId), httpSSEResponseConnectorListener); 65 | if (sourceListener != null) { 66 | throw new SiddhiAppCreationException("There is a connection already established for the sse source with " 67 | + "stream id : '" + streamId + "'."); 68 | } 69 | } 70 | 71 | /** 72 | * Unregister the source listener. 73 | * 74 | * @param streamId the stream id of the source 75 | * @param siddhiAppName name of the siddhi app 76 | */ 77 | void unregisterSourceListener(String streamId, String siddhiAppName) { 78 | SSEResponseConnectorListener httpSourceListener = 79 | this.sourceListenersMap.get(streamId); 80 | if (httpSourceListener != null && httpSourceListener.getSiddhiAppName().equals(siddhiAppName)) { 81 | sourceListenersMap.remove(streamId); 82 | httpSourceListener.disconnect(); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/exception/HttpSourceAdaptorException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source.exception; 20 | 21 | /** 22 | * HTTP source specific exception. 23 | */ 24 | public class HttpSourceAdaptorException extends RuntimeException { 25 | 26 | public HttpSourceAdaptorException(String message, Throwable e) { 27 | super(message, e); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/exception/HttpSourceAdaptorRuntimeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source.exception; 20 | 21 | import io.siddhi.extension.io.http.util.HttpIoUtil; 22 | import org.wso2.transport.http.netty.message.HttpCarbonMessage; 23 | 24 | /** 25 | * HTTP source adaptor specific exception. 26 | */ 27 | public class HttpSourceAdaptorRuntimeException extends RuntimeException { 28 | 29 | public HttpSourceAdaptorRuntimeException(String message, Throwable e) { 30 | super(message, e); 31 | } 32 | 33 | public HttpSourceAdaptorRuntimeException(String message) { 34 | super(message); 35 | } 36 | 37 | public HttpSourceAdaptorRuntimeException(HttpCarbonMessage carbonMessage, String message, int code) { 38 | super(message); 39 | HttpIoUtil.handleFailure(carbonMessage, this, code, message); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/internal/HttpIODataHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source.internal; 20 | 21 | import org.osgi.framework.BundleContext; 22 | import org.wso2.carbon.analytics.idp.client.core.api.IdPClient; 23 | 24 | /** 25 | * Data holder class for http IO class. 26 | */ 27 | public class HttpIODataHolder { 28 | private static HttpIODataHolder instance = new HttpIODataHolder(); 29 | private IdPClient client; 30 | private BundleContext bundleContext; 31 | 32 | private HttpIODataHolder() { 33 | } 34 | 35 | public static HttpIODataHolder getInstance() { 36 | return instance; 37 | } 38 | 39 | public IdPClient getClient() { 40 | return client; 41 | } 42 | 43 | public void setClient(IdPClient client) { 44 | this.client = client; 45 | } 46 | 47 | public BundleContext getBundleContext() { 48 | return bundleContext; 49 | } 50 | 51 | public void setBundleContext(BundleContext bundleContext) { 52 | this.bundleContext = bundleContext; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/source/internal/ServiceComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source.internal; 20 | 21 | import io.siddhi.extension.io.http.source.exception.HttpSourceAdaptorException; 22 | import org.apache.logging.log4j.LogManager; 23 | import org.apache.logging.log4j.Logger; 24 | import org.osgi.framework.BundleContext; 25 | import org.osgi.service.component.annotations.Activate; 26 | import org.osgi.service.component.annotations.Component; 27 | import org.osgi.service.component.annotations.Deactivate; 28 | import org.osgi.service.component.annotations.Reference; 29 | import org.osgi.service.component.annotations.ReferenceCardinality; 30 | import org.osgi.service.component.annotations.ReferencePolicy; 31 | import org.wso2.carbon.analytics.idp.client.core.api.IdPClient; 32 | 33 | /** 34 | * Service component which handle the IDP client . 35 | */ 36 | @Component( 37 | name = "io.siddhi.extension.io.http.source.internal.ServiceComponent", 38 | service = ServiceComponent.class, 39 | immediate = true 40 | ) 41 | public class ServiceComponent { 42 | private static final Logger log = LogManager.getLogger(ServiceComponent.class); 43 | 44 | /** 45 | * This is the activation method of Http io declarative service. This will be called when its references are 46 | * satisfied. 47 | * 48 | * @param bundleContext the bundle context instance of this bundle. 49 | * @throws Exception this will be thrown if an issue occurs while executing the activate method 50 | */ 51 | @Activate 52 | protected void start(BundleContext bundleContext) throws Exception { 53 | try { 54 | HttpIODataHolder.getInstance().setBundleContext(bundleContext); 55 | } catch (Throwable throwable) { 56 | log.error("Error occurred when initializing HTTP IO service component.", throwable); 57 | throw new HttpSourceAdaptorException("Error occurred when initializing HTTP IO service component.", 58 | throwable); 59 | } 60 | } 61 | 62 | /** 63 | * This is the deactivation method of Http io data service. This will be called when this component 64 | * is being stopped or references are satisfied during runtime. 65 | * 66 | * @throws Exception this will be thrown if an issue occurs while executing the de-activate method 67 | */ 68 | @Deactivate 69 | protected void stop() throws Exception { 70 | HttpIODataHolder.getInstance().setBundleContext(null); 71 | } 72 | 73 | @Reference( 74 | name = "org.wso2.carbon.analytics.idp.client.core.api.IdPClient", 75 | service = IdPClient.class, 76 | cardinality = ReferenceCardinality.MANDATORY, 77 | policy = ReferencePolicy.DYNAMIC, 78 | unbind = "unregisterIdP" 79 | ) 80 | protected void registerIdP(IdPClient client) { 81 | HttpIODataHolder.getInstance().setClient(client); 82 | } 83 | 84 | protected void unregisterIdP(IdPClient client) { 85 | HttpIODataHolder.getInstance().setClient(null); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/util/HTTPSinkRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.util; 20 | 21 | import io.siddhi.extension.io.http.sink.SSEServerSink; 22 | 23 | import java.util.Map; 24 | import java.util.concurrent.ConcurrentHashMap; 25 | 26 | /** 27 | * Result handler. 28 | */ 29 | public class HTTPSinkRegistry { 30 | 31 | private static Map sseSourceRegistry = new ConcurrentHashMap<>(); 32 | 33 | public static void registerSSESink(String streamId, SSEServerSink source) { 34 | sseSourceRegistry.put(streamId, source); 35 | } 36 | 37 | public static void removeSSESink(String streamId) { 38 | sseSourceRegistry.remove(streamId); 39 | } 40 | 41 | public static SSEServerSink findAndGetSSESource(String streamId) { 42 | for (SSEServerSink sseSink : sseSourceRegistry.values()) { 43 | if (sseSink.matches(streamId)) { 44 | return sseSink; 45 | } 46 | } 47 | return null; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/util/HTTPSourceRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.util; 20 | 21 | import io.siddhi.extension.io.http.source.HttpCallResponseSource; 22 | import io.siddhi.extension.io.http.source.HttpServiceSource; 23 | import io.siddhi.extension.io.http.source.SSESource; 24 | 25 | import java.util.Map; 26 | import java.util.concurrent.ConcurrentHashMap; 27 | 28 | /** 29 | * Result handler. 30 | */ 31 | public class HTTPSourceRegistry { 32 | 33 | private static Map serviceSourceRegistry = new ConcurrentHashMap<>(); 34 | private static Map callResponseSourceRegistry = new ConcurrentHashMap<>(); 35 | private static Map sseSourceRegistry = new ConcurrentHashMap<>(); 36 | 37 | // handle service sources 38 | public static HttpServiceSource getServiceSource(String sourceId) { 39 | return serviceSourceRegistry.get(sourceId); 40 | } 41 | 42 | public static void registerServiceSource(String sourceId, HttpServiceSource source) { 43 | serviceSourceRegistry.put(sourceId, source); 44 | } 45 | 46 | public static void removeServiceSource(String sourceId) { 47 | serviceSourceRegistry.remove(sourceId); 48 | } 49 | 50 | public static void registerCallResponseSource(String sinkId, String statusCode, HttpCallResponseSource source) { 51 | callResponseSourceRegistry.put(sinkId + statusCode, source); 52 | } 53 | 54 | public static void removeCallResponseSource(String sinkId, String statusCode) { 55 | callResponseSourceRegistry.remove(sinkId + statusCode); 56 | } 57 | 58 | public static HttpCallResponseSource findAndGetResponseSource(String sinkId, String statusCode) { 59 | for (HttpCallResponseSource responseSource : callResponseSourceRegistry.values()) { 60 | if (responseSource.matches(sinkId, statusCode)) { 61 | return responseSource; 62 | } 63 | } 64 | return null; 65 | } 66 | 67 | public static void registerSSESource(String streamId, SSESource source) { 68 | sseSourceRegistry.put(streamId, source); 69 | } 70 | 71 | public static void removeSSESource(String streamId) { 72 | sseSourceRegistry.remove(streamId); 73 | } 74 | 75 | public static SSESource findAndGetSSESource(String streamId) { 76 | for (SSESource sseSource : sseSourceRegistry.values()) { 77 | if (sseSource.matches(streamId)) { 78 | return sseSource; 79 | } 80 | } 81 | return null; 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/util/StringDataSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.util; 20 | 21 | import io.siddhi.extension.io.http.source.exception.HttpSourceAdaptorRuntimeException; 22 | import org.wso2.carbon.messaging.MessageDataSource; 23 | 24 | import java.io.IOException; 25 | import java.io.OutputStream; 26 | import java.nio.charset.Charset; 27 | import java.util.Map; 28 | 29 | /** 30 | * {@code StringValue} represents a string value in siddhi. 31 | * 32 | * @since 0.8.0 33 | */ 34 | public class StringDataSource implements MessageDataSource, Cloneable { 35 | private String value; 36 | private OutputStream outputStream; 37 | 38 | /** 39 | * Create a String datasource with a string. 40 | * 41 | * @param value String value 42 | */ 43 | public StringDataSource(String value) { 44 | this.value = value; 45 | this.outputStream = null; 46 | } 47 | 48 | /** 49 | * Create a String datasource with a string and a target output stream. 50 | * 51 | * @param value String value 52 | * @param outputStream Target outputstream 53 | */ 54 | public StringDataSource(String value, OutputStream outputStream) { 55 | this.value = value; 56 | this.outputStream = outputStream; 57 | } 58 | 59 | public String getValue() { 60 | return value; 61 | } 62 | 63 | public void setValue(String value) { 64 | this.value = value; 65 | } 66 | 67 | @Override 68 | public String getValueAsString(String s) { 69 | return null; 70 | } 71 | 72 | @Override 73 | public String getValueAsString(String s, Map map) { 74 | return null; 75 | } 76 | 77 | @Override 78 | public Object getValue(String s) { 79 | return null; 80 | } 81 | 82 | @Override 83 | public Object getDataObject() { 84 | return null; 85 | } 86 | 87 | @Override 88 | public String getContentType() { 89 | return null; 90 | } 91 | 92 | @Override 93 | public void setContentType(String s) { 94 | 95 | } 96 | 97 | @Override 98 | public void serializeData() { 99 | try { 100 | this.outputStream.write(this.value.getBytes(Charset.defaultCharset())); 101 | this.outputStream.close(); 102 | } catch (IOException e) { 103 | throw new HttpSourceAdaptorRuntimeException("Error occurred during writing the string message" + 104 | " to the output stream", e); 105 | } 106 | } 107 | 108 | 109 | public void setOutputStream(OutputStream outputStream) { 110 | this.outputStream = outputStream; 111 | } 112 | 113 | @Override 114 | public String getMessageAsString() { 115 | return this.value; 116 | } 117 | 118 | @Override 119 | public MessageDataSource clone() throws CloneNotSupportedException { 120 | super.clone(); 121 | String clonedContent = this.getMessageAsString(); 122 | return new StringDataSource(clonedContent); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /component/src/main/java/io/siddhi/extension/io/http/util/TrpPropertyTypes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package io.siddhi.extension.io.http.util; 19 | 20 | /** 21 | * Transport Property types. 22 | */ 23 | public enum TrpPropertyTypes { 24 | STRING, 25 | BOOLEAN, 26 | INTEGER, 27 | LONG, 28 | DOUBLE, 29 | } 30 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/HttpSinkCustomConfigurationTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink; 20 | 21 | import io.siddhi.core.SiddhiAppRuntime; 22 | import io.siddhi.core.SiddhiManager; 23 | import io.siddhi.core.stream.input.InputHandler; 24 | import io.siddhi.extension.io.http.sink.util.HttpServerListenerHandler; 25 | import io.siddhi.extension.map.xml.sinkmapper.XMLSinkMapper; 26 | import org.apache.logging.log4j.LogManager; 27 | import org.apache.logging.log4j.Logger; 28 | import org.testng.Assert; 29 | import org.testng.annotations.Test; 30 | 31 | 32 | /** 33 | * Test case for mapping type. 34 | */ 35 | public class HttpSinkCustomConfigurationTestCase { 36 | private static final Logger log = LogManager.getLogger(HttpSinkCustomConfigurationTestCase.class); 37 | 38 | /** 39 | * Creating test for publishing events with XML mapping. 40 | * 41 | * @throws Exception Interrupted exception 42 | */ 43 | @Test 44 | public void testHTTPCustomConfig() throws Exception { 45 | log.info("Creating test for publishing events with XML mapping."); 46 | SiddhiManager siddhiManager = new SiddhiManager(); 47 | siddhiManager.setExtension("xml-output-mapper", XMLSinkMapper.class); 48 | String inStreamDefinition = "Define stream FooStream (message String,method String,headers String);" 49 | + "@sink(type='http'," + 50 | "publisher.url='http://localhost:8005/abc'," + 51 | "method='{{method}}'," + 52 | "headers='{{headers}}'," + 53 | "socket.idle.timeout='150000'," + 54 | "ssl.protocol='TLS'," + 55 | "chunk.disabled='false'," + 56 | "parameters=\"'ciphers : TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256','sslEnabledProtocols : TLSv1.1'\"," + 57 | "client.bootstrap.configurations=\"'client.bootstrap.nodelay:true','client.bootstrap" + 58 | ".recievebuffersize:1048576'\"," + 59 | "@map(type='xml', @payload('{{message}}'))) " + 60 | "Define stream BarStream (message String,method String,headers String);"; 61 | String query = ("@info(name = 'query') " + 62 | "from FooStream select message,method,headers insert into BarStream;"); 63 | SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inStreamDefinition + 64 | query); 65 | InputHandler fooStream = siddhiAppRuntime.getInputHandler("FooStream"); 66 | siddhiAppRuntime.start(); 67 | HttpServerListenerHandler lst = new HttpServerListenerHandler(8005); 68 | lst.run(); 69 | String payload = "" 70 | + "" 71 | + "WSO2" 72 | + "55.645" 73 | + "100" 74 | + "" 75 | + ""; 76 | fooStream.send(new Object[]{payload, "POST", "'Name:John','Age:23'"}); 77 | while (!lst.getServerListener().isMessageArrive()) { 78 | Thread.sleep(10); 79 | } 80 | String eventData = lst.getServerListener().getData(); 81 | String expected = "" 82 | + "" 83 | + "WSO2" 84 | + "55.645" 85 | + "100" 86 | + "" 87 | + "\n"; 88 | Assert.assertEquals(eventData, expected); 89 | siddhiAppRuntime.shutdown(); 90 | lst.shutdown(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/SSEServerSinkTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package io.siddhi.extension.io.http.sink; 19 | 20 | import io.siddhi.core.SiddhiAppRuntime; 21 | import io.siddhi.core.SiddhiManager; 22 | import io.siddhi.core.event.Event; 23 | import io.siddhi.core.query.output.callback.QueryCallback; 24 | import io.siddhi.core.stream.input.InputHandler; 25 | import io.siddhi.core.util.EventPrinter; 26 | import io.siddhi.core.util.SiddhiTestHelper; 27 | import io.siddhi.core.util.persistence.InMemoryPersistenceStore; 28 | import io.siddhi.core.util.persistence.PersistenceStore; 29 | import io.siddhi.extension.map.json.sinkmapper.JsonSinkMapper; 30 | import io.siddhi.extension.map.json.sourcemapper.JsonSourceMapper; 31 | import org.testng.Assert; 32 | import org.testng.annotations.Test; 33 | 34 | import java.util.ArrayList; 35 | import java.util.Date; 36 | import java.util.List; 37 | import java.util.concurrent.atomic.AtomicInteger; 38 | 39 | /** 40 | * Tests http sse sink. 41 | */ 42 | public class SSEServerSinkTestCase { 43 | private static final int SLEEP_TIME = 50; 44 | private static final int EVENT_COUNT = 10; 45 | private static final int TIMEOUT = 30000; 46 | private AtomicInteger eventCount = new AtomicInteger(0); 47 | 48 | @Test 49 | public void testSSESource() throws Exception { 50 | List receivedEventList = new ArrayList<>(2); 51 | PersistenceStore persistenceStore = new InMemoryPersistenceStore(); 52 | SiddhiManager siddhiManager = new SiddhiManager(); 53 | siddhiManager.setPersistenceStore(persistenceStore); 54 | siddhiManager.setExtension("json-output-mapper", JsonSinkMapper.class); 55 | siddhiManager.setExtension("json-input-mapper", JsonSourceMapper.class); 56 | String sinkStreamDefinition = "@sink(type = 'sse-server', server.url='http://localhost:8010/send', " + 57 | "@map(type='json'))\n" + 58 | "define stream PublishStream (param1 string);\n"; 59 | String sourceStreamDefinition = "@Source(type = 'sse', receiver.url='http://localhost:8010/send', " + 60 | "@map(type='json'))\n" + 61 | "@sink(type='log')\n" + 62 | "define stream ListenStream (param1 string);\n"; 63 | String logStreamDefinition = "@sink(type='log')\n" + 64 | "define stream LogStream(param1 string);\n"; 65 | String query = "@info(name='log')\n" + 66 | "from ListenStream\n" + 67 | "select *\n" + 68 | "insert into LogStream"; 69 | SiddhiAppRuntime sinkAppRuntime = siddhiManager.createSiddhiAppRuntime(sinkStreamDefinition); 70 | InputHandler publishStream = sinkAppRuntime.getInputHandler("PublishStream"); 71 | SiddhiAppRuntime sourceAppRuntime = siddhiManager.createSiddhiAppRuntime(sourceStreamDefinition + 72 | logStreamDefinition + query); 73 | sourceAppRuntime.addCallback("log", new QueryCallback() { 74 | @Override 75 | public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { 76 | EventPrinter.print(timeStamp, inEvents, removeEvents); 77 | for (Event event : inEvents) { 78 | eventCount.incrementAndGet(); 79 | receivedEventList.add(event.getData(0).toString()); 80 | } 81 | } 82 | }); 83 | sinkAppRuntime.start(); 84 | sourceAppRuntime.start(); 85 | List expected = new ArrayList<>(10); 86 | for (int i = 0; i < EVENT_COUNT; i++) { 87 | Thread.sleep(SLEEP_TIME * 10); 88 | String timestamp = String.valueOf(new Date().getTime()); 89 | publishStream.send(new Object[]{timestamp}); 90 | expected.add(timestamp); 91 | } 92 | 93 | SiddhiTestHelper.waitForEvents(SLEEP_TIME, EVENT_COUNT, eventCount, TIMEOUT); 94 | Assert.assertEquals(receivedEventList.toString(), expected.toString()); 95 | sinkAppRuntime.shutdown(); 96 | sourceAppRuntime.shutdown(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/util/HttpFileServerListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.util; 20 | 21 | import com.sun.net.httpserver.Headers; 22 | import com.sun.net.httpserver.HttpExchange; 23 | import com.sun.net.httpserver.HttpHandler; 24 | import org.apache.commons.io.IOUtils; 25 | import org.apache.logging.log4j.LogManager; 26 | import org.apache.logging.log4j.Logger; 27 | 28 | import java.io.File; 29 | import java.io.FileInputStream; 30 | import java.io.IOException; 31 | import java.io.InputStream; 32 | import java.net.HttpURLConnection; 33 | import java.util.concurrent.atomic.AtomicBoolean; 34 | 35 | /** 36 | * Test handler for file downloading feature. 37 | */ 38 | public class HttpFileServerListener implements HttpHandler { 39 | private static final Logger logger = LogManager.getLogger(HttpFileServerListener.class); 40 | private AtomicBoolean isEventArrived = new AtomicBoolean(false); 41 | private Headers headers; 42 | private String filePath; 43 | private int expectedStatusCode = 2; 44 | 45 | public HttpFileServerListener() { 46 | ClassLoader classLoader = getClass().getClassLoader(); 47 | filePath = classLoader.getResource("files").getFile(); 48 | } 49 | 50 | public HttpFileServerListener(int expectedStatusCode) { 51 | this.expectedStatusCode = expectedStatusCode; 52 | } 53 | 54 | @Override 55 | public void handle(HttpExchange event) throws IOException { 56 | // Get the paramString form the request 57 | if (expectedStatusCode == 2) { 58 | headers = event.getRequestHeaders(); 59 | InputStream inputStream = event.getRequestBody(); 60 | // initiating 61 | File file = new File(filePath + File.separator + "testFile.txt"); 62 | InputStream fileInputStream = new FileInputStream(file); 63 | 64 | logger.info("Event Arrived"); 65 | 66 | byte[] response = IOUtils.toByteArray(fileInputStream); 67 | event.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length); 68 | event.getResponseBody().write(response); 69 | inputStream.close(); 70 | event.close(); 71 | isEventArrived.set(true); 72 | } else if (expectedStatusCode == 4) { 73 | byte[] response = "Requested file cannot be found.".getBytes(); 74 | event.sendResponseHeaders(HttpURLConnection.HTTP_BAD_REQUEST, response.length); 75 | event.getResponseBody().write(response); 76 | event.close(); 77 | isEventArrived.set(true); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/util/HttpFileServerListenerHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.util; 20 | 21 | import com.sun.net.httpserver.HttpServer; 22 | import org.apache.logging.log4j.LogManager; 23 | import org.apache.logging.log4j.Logger; 24 | 25 | import java.io.IOException; 26 | import java.net.InetSocketAddress; 27 | 28 | /** 29 | * Http test sever listener for file downloading feature. 30 | */ 31 | public class HttpFileServerListenerHandler implements Runnable { 32 | private static final Logger logger = LogManager.getLogger(HttpFileServerListenerHandler.class); 33 | private HttpFileServerListener fileServerListener; 34 | private HttpServer server; 35 | private int port; 36 | 37 | public HttpFileServerListenerHandler(int port) { 38 | this.fileServerListener = new HttpFileServerListener(); 39 | this.port = port; 40 | } 41 | 42 | public HttpFileServerListenerHandler(int port, int expectedStatusCode) { 43 | this.fileServerListener = new HttpFileServerListener(expectedStatusCode / 100); 44 | this.port = port; 45 | } 46 | 47 | @Override 48 | public void run() { 49 | try { 50 | server = HttpServer.create(new InetSocketAddress(port), 5); 51 | server.createContext("/files", fileServerListener); 52 | logger.info("Start server on port '" + port + "'"); 53 | server.start(); 54 | } catch (IOException e) { 55 | logger.error("Error in creating test server.", e); 56 | } 57 | } 58 | 59 | public void shutdown() { 60 | if (server != null) { 61 | logger.info("Shutting down server on port '" + port + "'"); 62 | server.stop(1); 63 | } 64 | } 65 | 66 | public HttpFileServerListener getFileServerListener() { 67 | return fileServerListener; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/util/HttpOAuthEndpointListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.util; 20 | 21 | import com.sun.net.httpserver.Headers; 22 | import com.sun.net.httpserver.HttpExchange; 23 | import com.sun.net.httpserver.HttpHandler; 24 | 25 | import java.io.BufferedReader; 26 | import java.io.IOException; 27 | import java.io.InputStream; 28 | import java.io.InputStreamReader; 29 | import java.net.HttpURLConnection; 30 | import java.util.List; 31 | import java.util.concurrent.atomic.AtomicBoolean; 32 | 33 | /** 34 | * Test Server Listener Manger for API end point. 35 | */ 36 | 37 | public class HttpOAuthEndpointListener implements HttpHandler { 38 | private AtomicBoolean isEventArrived = new AtomicBoolean(false); 39 | private StringBuilder stringBuilder; 40 | private Headers headers; 41 | private int responseCode; 42 | 43 | 44 | public HttpOAuthEndpointListener() { 45 | } 46 | 47 | @Override 48 | public void handle(HttpExchange event) throws IOException { 49 | // Get the paramString form the request 50 | String line; 51 | headers = event.getRequestHeaders(); 52 | List authKey = headers.get("Authorization"); 53 | String authValue = authKey.get(0); 54 | if (authValue.equals("Bearer xxxxx")) { 55 | responseCode = HttpURLConnection.HTTP_UNAUTHORIZED; 56 | } 57 | if (authValue.equals("Bearer yyyyy")) { 58 | responseCode = HttpURLConnection.HTTP_OK; 59 | } 60 | 61 | InputStream inputStream = event.getRequestBody(); 62 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 63 | stringBuilder = new StringBuilder(); 64 | while ((line = bufferedReader.readLine()) != null) { 65 | stringBuilder = stringBuilder.append(line).append("\n"); 66 | } 67 | byte[] response = stringBuilder.toString().getBytes(); 68 | event.sendResponseHeaders(responseCode, response.length); 69 | event.getResponseBody().write(response); 70 | event.close(); 71 | isEventArrived.set(true); 72 | } 73 | 74 | public String getData() { 75 | String data = stringBuilder.toString(); 76 | isEventArrived = new AtomicBoolean(false); 77 | return data; 78 | } 79 | 80 | public Headers getHeaders() { 81 | return headers; 82 | } 83 | 84 | public boolean isMessageArrive() { 85 | return isEventArrived.get(); 86 | } 87 | 88 | 89 | } 90 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/util/HttpOAuthListenerHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.util; 20 | 21 | import com.sun.net.httpserver.HttpsConfigurator; 22 | import com.sun.net.httpserver.HttpsParameters; 23 | import com.sun.net.httpserver.HttpsServer; 24 | import org.apache.logging.log4j.LogManager; 25 | import org.apache.logging.log4j.Logger; 26 | 27 | import java.io.FileInputStream; 28 | import java.io.IOException; 29 | import java.net.InetSocketAddress; 30 | import java.security.KeyManagementException; 31 | import java.security.KeyStore; 32 | import java.security.KeyStoreException; 33 | import java.security.NoSuchAlgorithmException; 34 | import java.security.SecureRandom; 35 | import java.security.UnrecoverableKeyException; 36 | import java.security.cert.CertificateException; 37 | import javax.net.ssl.KeyManagerFactory; 38 | import javax.net.ssl.SSLContext; 39 | import javax.net.ssl.SSLParameters; 40 | import javax.net.ssl.TrustManagerFactory; 41 | 42 | /** 43 | * Http test sever listener handler for OAuth requests. 44 | */ 45 | public class HttpOAuthListenerHandler implements Runnable { 46 | private static final Logger logger = LogManager.getLogger(HttpServerListenerHandler.class); 47 | private HttpOAuthTokenEndpointListener httpOAuthTokenEndpointListener; 48 | private HttpOAuthEndpointListener httpOAuthEndpointListener; 49 | 50 | private HttpsServer tokenEndPointServer; 51 | private HttpsServer oauthEndPointServer; 52 | private int tokenEndpointPort; 53 | private int oauthEndPointPort; 54 | private KeyStore keyStore; 55 | 56 | public HttpOAuthListenerHandler(int tokenEndpointPort, int oauthEndPointPort) throws KeyStoreException { 57 | this.httpOAuthTokenEndpointListener = new HttpOAuthTokenEndpointListener(); 58 | this.httpOAuthEndpointListener = new HttpOAuthEndpointListener(); 59 | this.tokenEndpointPort = tokenEndpointPort; 60 | this.oauthEndPointPort = oauthEndPointPort; 61 | keyStore = KeyStore.getInstance("JKS"); 62 | } 63 | 64 | public HttpOAuthTokenEndpointListener getServerListener() { 65 | return httpOAuthTokenEndpointListener; 66 | } 67 | 68 | @Override 69 | public void run() { 70 | try { 71 | char[] passphrase = "wso2carbon".toCharArray(); 72 | keyStore.load(new FileInputStream(System.getProperty("carbon.home") + 73 | "/resources/security/wso2carbon.jks"), passphrase); 74 | KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); 75 | kmf.init(keyStore, passphrase); 76 | TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); 77 | tmf.init(keyStore); 78 | SSLContext ssl = SSLContext.getInstance("TLS"); 79 | ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom()); 80 | 81 | tokenEndPointServer = HttpsServer.create(new InetSocketAddress(tokenEndpointPort), 5); 82 | oauthEndPointServer = HttpsServer.create(new InetSocketAddress(oauthEndPointPort), 5); 83 | 84 | tokenEndPointServer.setHttpsConfigurator(new HttpsConfigurator(ssl) { 85 | public void configure(HttpsParameters params) { 86 | SSLParameters sslParameters = getSSLContext().getDefaultSSLParameters(); 87 | params.setSSLParameters(sslParameters); 88 | } 89 | }); 90 | oauthEndPointServer.setHttpsConfigurator(new HttpsConfigurator(ssl) { 91 | public void configure(HttpsParameters params) { 92 | SSLParameters sslParameters = getSSLContext().getDefaultSSLParameters(); 93 | params.setSSLParameters(sslParameters); 94 | } 95 | }); 96 | tokenEndPointServer.createContext("/token", httpOAuthTokenEndpointListener); 97 | tokenEndPointServer.start(); 98 | oauthEndPointServer.createContext("/abc", httpOAuthEndpointListener); 99 | oauthEndPointServer.start(); 100 | } catch (NoSuchAlgorithmException e) { 101 | logger.error("No such algorithm in while trying to up test https server.", e); 102 | } catch (CertificateException e) { 103 | logger.error("Certificate exception in basic authentication", e); 104 | } catch (KeyStoreException e) { 105 | logger.error("Keystore exception in while trying to up test https server.", e); 106 | } catch (IOException e) { 107 | logger.error("IOException while trying to up test https server. ", e); 108 | } catch (UnrecoverableKeyException e) { 109 | logger.error("UnrecoverableKeyException while trying to up test https server.", e); 110 | } catch (KeyManagementException e) { 111 | logger.error("KeyManagementException while trying to up test https server.", e); 112 | } 113 | } 114 | 115 | public void shutdown() { 116 | if (tokenEndPointServer != null) { 117 | logger.info("Shutting down token endpoint server"); 118 | tokenEndPointServer.stop(1); 119 | } 120 | if (oauthEndPointServer != null) { 121 | logger.info("Shutting down OAuth endpoint server"); 122 | oauthEndPointServer.stop(1); 123 | } 124 | } 125 | 126 | public HttpOAuthEndpointListener getHttpOAuthTokenEndpointListener() { 127 | return httpOAuthEndpointListener; 128 | } 129 | } 130 | 131 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/util/HttpOAuthTokenEndpointListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.util; 20 | 21 | import com.sun.net.httpserver.HttpExchange; 22 | import com.sun.net.httpserver.HttpHandler; 23 | 24 | import java.io.BufferedReader; 25 | import java.io.ByteArrayInputStream; 26 | import java.io.IOException; 27 | import java.io.InputStream; 28 | import java.io.InputStreamReader; 29 | import java.net.HttpURLConnection; 30 | import java.util.concurrent.atomic.AtomicBoolean; 31 | 32 | /** 33 | * Test Server Listener Manger for TokenEndPoint. 34 | */ 35 | public class HttpOAuthTokenEndpointListener implements HttpHandler { 36 | private AtomicBoolean isEventArrived = new AtomicBoolean(false); 37 | 38 | public HttpOAuthTokenEndpointListener() { 39 | } 40 | 41 | @Override 42 | public void handle(HttpExchange event) throws IOException { 43 | int responseCode; 44 | responseCode = HttpURLConnection.HTTP_OK; 45 | StringBuilder gettingGrantType = new StringBuilder(); 46 | String outputStream; 47 | String line; 48 | InputStream grantInputStream = event.getRequestBody(); 49 | BufferedReader grantBufferedReader = new BufferedReader(new InputStreamReader(grantInputStream)); 50 | while ((line = grantBufferedReader.readLine()) != null) { 51 | gettingGrantType = gettingGrantType.append(line).append("\n"); 52 | } 53 | if (gettingGrantType.toString().contains("password")) { 54 | outputStream = "{\"scope\":\"default\",\"token_type\":\"Bearer\",\"expires_in\":3600," + 55 | "\"access_token\":\"yyyyy\",\"refresh_token\":\"ppppp\"}"; 56 | } else if (gettingGrantType.toString().contains("refresh_token")) { 57 | if ("ppppp".contains(gettingGrantType.toString())) { 58 | outputStream = "{\"scope\":\"default\",\"token_type\":\"Bearer\",\"expires_in\":3600," + 59 | "\"access_token\":\"yyyyy\",\"refresh_token\":\"ppppp\"}"; 60 | } else { 61 | responseCode = HttpURLConnection.HTTP_UNAUTHORIZED; 62 | outputStream = "{\"access_token\":\"\",\"refresh_token\":\"\"}"; 63 | } 64 | } else { 65 | outputStream = "{\"token_type\":\"Bearer\",\"expires_in\":3600,\"access_token\":\"yyyyy\"}"; 66 | } 67 | 68 | InputStream responseInputStream = new ByteArrayInputStream(outputStream.getBytes()); 69 | BufferedReader responseBufferedReader = new BufferedReader(new InputStreamReader(responseInputStream)); 70 | StringBuilder responseString = new StringBuilder(); 71 | while ((line = responseBufferedReader.readLine()) != null) { 72 | responseString = responseString.append(line); 73 | } 74 | byte[] response = responseString.toString().getBytes(); 75 | event.sendResponseHeaders(responseCode, response.length); 76 | event.getResponseBody().write(response); 77 | event.close(); 78 | isEventArrived.set(true); 79 | } 80 | 81 | public boolean isMessageArrive() { 82 | return isEventArrived.get(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/util/HttpServerListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.util; 20 | 21 | import com.sun.net.httpserver.Headers; 22 | import com.sun.net.httpserver.HttpExchange; 23 | import com.sun.net.httpserver.HttpHandler; 24 | import org.apache.logging.log4j.LogManager; 25 | import org.apache.logging.log4j.Logger; 26 | 27 | import java.io.BufferedReader; 28 | import java.io.IOException; 29 | import java.io.InputStream; 30 | import java.io.InputStreamReader; 31 | import java.net.HttpURLConnection; 32 | import java.util.concurrent.atomic.AtomicBoolean; 33 | 34 | /** 35 | * Test Server Listener Manger. 36 | */ 37 | public class HttpServerListener implements HttpHandler { 38 | private static final Logger logger = LogManager.getLogger(HttpServerListener.class); 39 | private AtomicBoolean isEventArrived = new AtomicBoolean(false); 40 | private StringBuilder stringBuilder; 41 | private Headers headers; 42 | 43 | public HttpServerListener() { 44 | } 45 | 46 | @Override 47 | public void handle(HttpExchange event) throws IOException { 48 | // Get the paramString form the request 49 | String line; 50 | headers = event.getRequestHeaders(); 51 | InputStream inputStream = event.getRequestBody(); 52 | // initiating 53 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 54 | stringBuilder = new StringBuilder(); 55 | while ((line = bufferedReader.readLine()) != null) { 56 | stringBuilder = stringBuilder.append(line).append("\n"); 57 | } 58 | logger.info("Event Arrived: " + stringBuilder.toString()); 59 | 60 | byte[] response = stringBuilder.toString().getBytes(); 61 | event.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length); 62 | event.getResponseBody().write(response); 63 | event.close(); 64 | isEventArrived.set(true); 65 | } 66 | 67 | public String getData() { 68 | String data = stringBuilder.toString(); 69 | isEventArrived = new AtomicBoolean(false); 70 | return data; 71 | } 72 | 73 | public Headers getHeaders() { 74 | return headers; 75 | } 76 | 77 | public boolean isMessageArrive() { 78 | return isEventArrived.get(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/util/HttpServerListenerHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.util; 20 | 21 | import com.sun.net.httpserver.HttpServer; 22 | import org.apache.logging.log4j.LogManager; 23 | import org.apache.logging.log4j.Logger; 24 | 25 | import java.io.IOException; 26 | import java.net.InetSocketAddress; 27 | 28 | /** 29 | * Http test sever listener. 30 | */ 31 | public class HttpServerListenerHandler implements Runnable { 32 | private static final Logger logger = LogManager.getLogger(HttpServerListenerHandler.class); 33 | private HttpServerListener serverListener; 34 | private HttpServer server; 35 | private int port; 36 | 37 | public HttpServerListenerHandler(int port) { 38 | this.serverListener = new HttpServerListener(); 39 | this.port = port; 40 | } 41 | 42 | @Override 43 | public void run() { 44 | try { 45 | server = HttpServer.create(new InetSocketAddress(port), 5); 46 | server.createContext("/abc", serverListener); 47 | logger.info("Start server on port '" + port + "'"); 48 | server.start(); 49 | } catch (IOException e) { 50 | logger.error("Error in creating test server.", e); 51 | } 52 | } 53 | 54 | public void shutdown() { 55 | if (server != null) { 56 | logger.info("Shutting down server on port '" + port + "'"); 57 | server.stop(1); 58 | } 59 | } 60 | 61 | public HttpServerListener getServerListener() { 62 | return serverListener; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/util/HttpsServerListenerHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.sink.util; 20 | 21 | import com.sun.net.httpserver.HttpsConfigurator; 22 | import com.sun.net.httpserver.HttpsParameters; 23 | import com.sun.net.httpserver.HttpsServer; 24 | import org.apache.logging.log4j.LogManager; 25 | import org.apache.logging.log4j.Logger; 26 | 27 | import java.io.FileInputStream; 28 | import java.io.IOException; 29 | import java.net.InetSocketAddress; 30 | import java.security.KeyManagementException; 31 | import java.security.KeyStore; 32 | import java.security.KeyStoreException; 33 | import java.security.NoSuchAlgorithmException; 34 | import java.security.UnrecoverableKeyException; 35 | import java.security.cert.CertificateException; 36 | import javax.net.ssl.KeyManagerFactory; 37 | import javax.net.ssl.SSLContext; 38 | import javax.net.ssl.SSLParameters; 39 | import javax.net.ssl.TrustManagerFactory; 40 | 41 | /** 42 | * Https test sever listener. 43 | */ 44 | public class HttpsServerListenerHandler implements Runnable { 45 | private static final Logger logger = LogManager.getLogger(HttpsServerListenerHandler.class); 46 | private HttpServerListener serverListener; 47 | private int port; 48 | private KeyStore keyStore; 49 | private HttpsServer server; 50 | 51 | public HttpsServerListenerHandler(int port) throws KeyStoreException { 52 | this.serverListener = new HttpServerListener(); 53 | this.port = port; 54 | keyStore = KeyStore.getInstance("JKS"); 55 | } 56 | 57 | public HttpServerListener getServerListener() { 58 | return serverListener; 59 | } 60 | 61 | @Override 62 | public void run() { 63 | try { 64 | char[] passphrase = "wso2carbon".toCharArray(); 65 | keyStore.load(new FileInputStream(System.getProperty("carbon.home") + "/resources/security/wso2carbon.jks"), 66 | passphrase); 67 | KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); 68 | kmf.init(keyStore, passphrase); 69 | TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); 70 | tmf.init(keyStore); 71 | SSLContext ssl = SSLContext.getInstance("TLS"); 72 | ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); 73 | server = HttpsServer.create(new InetSocketAddress(port), 5); 74 | server.setHttpsConfigurator(new HttpsConfigurator(ssl) { 75 | public void configure(HttpsParameters params) { 76 | // get the remote address if needed 77 | SSLContext c = getSSLContext(); 78 | // get the default parameters 79 | SSLParameters sslparams = c.getDefaultSSLParameters(); 80 | params.setSSLParameters(sslparams); 81 | } 82 | }); 83 | server.createContext("/abc", serverListener); 84 | server.start(); 85 | } catch (NoSuchAlgorithmException e) { 86 | logger.error("No such algorithm in while trying to up test https server.", e); 87 | } catch (CertificateException e) { 88 | logger.error("Certificate exception in basic authentication", e); 89 | } catch (KeyStoreException e) { 90 | logger.error("Keystore exception in while trying to up test https server.", e); 91 | } catch (IOException e) { 92 | logger.error("IOException while trying to up test https server. ", e); 93 | } catch (UnrecoverableKeyException e) { 94 | logger.error("UnrecoverableKeyException while trying to up test https server.", e); 95 | } catch (KeyManagementException e) { 96 | logger.error("KeyManagementException while trying to up test https server.", e); 97 | } 98 | } 99 | 100 | public void shutdown() { 101 | if (server != null) { 102 | server.stop(1); 103 | } 104 | 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/sink/util/UnitTestAppender.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package io.siddhi.extension.io.http.sink.util; 20 | 21 | import org.apache.logging.log4j.core.Appender; 22 | import org.apache.logging.log4j.core.Core; 23 | import org.apache.logging.log4j.core.Filter; 24 | import org.apache.logging.log4j.core.LogEvent; 25 | import org.apache.logging.log4j.core.appender.AbstractAppender; 26 | import org.apache.logging.log4j.core.config.plugins.Plugin; 27 | import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 28 | import org.apache.logging.log4j.core.config.plugins.PluginElement; 29 | import org.apache.logging.log4j.core.config.plugins.PluginFactory; 30 | import org.mvel2.util.StringAppender; 31 | 32 | @Plugin(name = "UnitTestAppender", 33 | category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE) 34 | public class UnitTestAppender extends AbstractAppender { 35 | 36 | private StringAppender messages = new StringAppender(); 37 | 38 | public UnitTestAppender(String name, Filter filter) { 39 | 40 | super(name, filter, null); 41 | } 42 | 43 | @PluginFactory 44 | public static UnitTestAppender createAppender( 45 | @PluginAttribute("name") String name, 46 | @PluginElement("Filter") Filter filter) { 47 | 48 | return new UnitTestAppender(name, filter); 49 | } 50 | 51 | public String getMessages() { 52 | 53 | String results = messages.toString(); 54 | return results; 55 | } 56 | 57 | @Override 58 | public void append(LogEvent event) { 59 | 60 | messages.append(event.getMessage().getFormattedMessage()); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/source/HttpCustomThreadPoolConfigTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source; 20 | 21 | import io.siddhi.core.SiddhiAppRuntime; 22 | import io.siddhi.core.SiddhiManager; 23 | import io.siddhi.core.event.Event; 24 | import io.siddhi.core.query.output.callback.QueryCallback; 25 | import io.siddhi.core.util.EventPrinter; 26 | import io.siddhi.core.util.SiddhiTestHelper; 27 | import io.siddhi.core.util.persistence.InMemoryPersistenceStore; 28 | import io.siddhi.core.util.persistence.PersistenceStore; 29 | import io.siddhi.extension.io.http.source.util.HttpTestUtil; 30 | import io.siddhi.extension.map.xml.sourcemapper.XmlSourceMapper; 31 | import org.apache.logging.log4j.LogManager; 32 | import org.apache.logging.log4j.Logger; 33 | import org.testng.Assert; 34 | import org.testng.annotations.BeforeMethod; 35 | import org.testng.annotations.Test; 36 | 37 | import java.net.URI; 38 | import java.util.ArrayList; 39 | import java.util.List; 40 | import java.util.concurrent.atomic.AtomicInteger; 41 | 42 | /** 43 | * Test case for HTTPS protocol. 44 | */ 45 | public class HttpCustomThreadPoolConfigTestCase { 46 | private static final Logger logger = LogManager.getLogger(HttpCustomThreadPoolConfigTestCase.class); 47 | private AtomicInteger eventCount = new AtomicInteger(0); 48 | 49 | @BeforeMethod 50 | public void init() { 51 | eventCount.set(0); 52 | } 53 | 54 | /** 55 | * Creating test for publishing events with XML mapping. 56 | * 57 | * @throws Exception Interrupted exception 58 | */ 59 | @Test 60 | public void testCustomPoolConfig() throws Exception { 61 | logger.info("Creating test for publishing events with XML mapping."); 62 | URI baseURI = URI.create(String.format("http://%s:%d", "localhost", 8005)); 63 | List receivedEventNameList = new ArrayList<>(2); 64 | PersistenceStore persistenceStore = new InMemoryPersistenceStore(); 65 | SiddhiManager siddhiManager = new SiddhiManager(); 66 | siddhiManager.setPersistenceStore(persistenceStore); 67 | siddhiManager.setExtension("xml-input-mapper", XmlSourceMapper.class); 68 | String inStreamDefinition = "@source(type='http', @map(type='xml'), " 69 | + "receiver.url='http://localhost:8005/endpoints/RecPro', basic.auth.enabled='false',worker" + 70 | ".count='8')" 71 | + "define stream inputStream (name string, age int, country string);"; 72 | String query = ( 73 | "@info(name = 'query') " 74 | + "from inputStream " 75 | + "select * " 76 | + "insert into outputStream;" 77 | ); 78 | SiddhiAppRuntime siddhiAppRuntime = siddhiManager 79 | .createSiddhiAppRuntime(inStreamDefinition + query); 80 | siddhiAppRuntime.addCallback("query", new QueryCallback() { 81 | @Override 82 | public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { 83 | EventPrinter.print(timeStamp, inEvents, removeEvents); 84 | for (Event event : inEvents) { 85 | eventCount.incrementAndGet(); 86 | receivedEventNameList.add(event.getData(0).toString()); 87 | } 88 | } 89 | }); 90 | siddhiAppRuntime.start(); 91 | // publishing events 92 | List expected = new ArrayList<>(2); 93 | expected.add("John"); 94 | expected.add("Mike"); 95 | String event1 = "" 96 | + "" 97 | + "John" 98 | + "100" 99 | + "AUS" 100 | + "" 101 | + ""; 102 | String event2 = "" 103 | + "" 104 | + "Mike" 105 | + "20" 106 | + "USA" 107 | + "" 108 | + ""; 109 | HttpTestUtil.httpPublishEvent(event1, baseURI, "/endpoints/RecPro", 110 | "POST"); 111 | HttpTestUtil.httpPublishEvent(event2, baseURI, "/endpoints/RecPro", 112 | "POST"); 113 | int waitTime = 50; 114 | int timeout = 30000; 115 | SiddhiTestHelper.waitForEvents(waitTime, 2, eventCount, timeout); 116 | Assert.assertEquals(receivedEventNameList.toString(), expected.toString()); 117 | siddhiAppRuntime.shutdown(); 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/source/HttpOrderlyProcessTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source; 20 | 21 | import io.siddhi.core.SiddhiAppRuntime; 22 | import io.siddhi.core.SiddhiManager; 23 | import io.siddhi.core.event.Event; 24 | import io.siddhi.core.query.output.callback.QueryCallback; 25 | import io.siddhi.core.util.EventPrinter; 26 | import io.siddhi.core.util.SiddhiTestHelper; 27 | import io.siddhi.core.util.persistence.InMemoryPersistenceStore; 28 | import io.siddhi.core.util.persistence.PersistenceStore; 29 | import io.siddhi.extension.io.http.source.util.HttpTestUtil; 30 | import io.siddhi.extension.map.xml.sourcemapper.XmlSourceMapper; 31 | import org.apache.logging.log4j.LogManager; 32 | import org.apache.logging.log4j.Logger; 33 | import org.testng.Assert; 34 | import org.testng.annotations.BeforeMethod; 35 | import org.testng.annotations.Test; 36 | 37 | import java.net.URI; 38 | import java.util.ArrayList; 39 | import java.util.List; 40 | import java.util.concurrent.atomic.AtomicInteger; 41 | 42 | /** 43 | * Test case for HTTPS protocol. 44 | */ 45 | public class HttpOrderlyProcessTestCase { 46 | private static final Logger logger = LogManager.getLogger(HttpOrderlyProcessTestCase.class); 47 | private AtomicInteger eventCount = new AtomicInteger(0); 48 | 49 | @BeforeMethod 50 | public void init() { 51 | eventCount.set(0); 52 | } 53 | 54 | /** 55 | * Creating test for publishing events with XML mapping. 56 | * 57 | * @throws Exception Interrupted exception 58 | */ 59 | @Test 60 | public void testCustomPoolConfig() throws Exception { 61 | logger.info("Creating test for publishing events with XML mapping."); 62 | URI baseURI = URI.create(String.format("http://%s:%d", "localhost", 8005)); 63 | List receivedEventNameList = new ArrayList<>(2); 64 | PersistenceStore persistenceStore = new InMemoryPersistenceStore(); 65 | SiddhiManager siddhiManager = new SiddhiManager(); 66 | siddhiManager.setPersistenceStore(persistenceStore); 67 | siddhiManager.setExtension("xml-input-mapper", XmlSourceMapper.class); 68 | String inStreamDefinition = "@source(type='http', @map(type='xml'), " 69 | + "receiver.url='http://localhost:8005/endpoints/RecPro', basic.auth.enabled='false',worker" 70 | + ".count='1',server.bootstrap.boss.group.size='4',server.bootstrap.worker.group.size='8')" 71 | + "define stream inputStream (name string, age int, country string);"; 72 | String query = ( 73 | "@info(name = 'query') " 74 | + "from inputStream " 75 | + "select * " 76 | + "insert into outputStream;" 77 | ); 78 | SiddhiAppRuntime siddhiAppRuntime = siddhiManager 79 | .createSiddhiAppRuntime(inStreamDefinition + query); 80 | 81 | siddhiAppRuntime.addCallback("query", new QueryCallback() { 82 | @Override 83 | public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { 84 | EventPrinter.print(timeStamp, inEvents, removeEvents); 85 | for (Event event : inEvents) { 86 | eventCount.incrementAndGet(); 87 | receivedEventNameList.add(event.getData(0).toString()); 88 | } 89 | } 90 | }); 91 | siddhiAppRuntime.start(); 92 | // publishing events 93 | List expected = new ArrayList<>(2); 94 | expected.add("A"); 95 | expected.add("B"); 96 | expected.add("C"); 97 | expected.add("D"); 98 | expected.add("A"); 99 | String event1 = "" + 100 | "" + 101 | "A" + 102 | "100" + 103 | "USA" + 104 | "" + 105 | ""; 106 | String event2 = "" + 107 | "" + 108 | "B" + 109 | "100" + 110 | "USA" + 111 | "" + 112 | ""; 113 | String event3 = "" + 114 | "" + 115 | "C" + 116 | "100" + 117 | "USA" + 118 | "" + 119 | ""; 120 | String event4 = "" + 121 | "" + 122 | "D" + 123 | "100" + 124 | "USA" + 125 | "" + 126 | ""; 127 | String[] events = {event1, event2, event3, event4}; 128 | int k = 0; 129 | for (int i = 0; i < 5; i++) { 130 | HttpTestUtil.httpPublishEvent(events[k++], baseURI, "/endpoints/RecPro", 131 | "POST"); 132 | if (k == 4) { 133 | k = 0; 134 | } 135 | } 136 | int waitTime = 50; 137 | int timeout = 30000; 138 | SiddhiTestHelper.waitForEvents(waitTime, 5, eventCount, timeout); 139 | Assert.assertEquals(receivedEventNameList.toString(), expected.toString()); 140 | siddhiAppRuntime.shutdown(); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/source/HttpSourceConflictsTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package io.siddhi.extension.io.http.source; 19 | 20 | import io.siddhi.core.SiddhiAppRuntime; 21 | import io.siddhi.core.SiddhiManager; 22 | import io.siddhi.core.event.Event; 23 | import io.siddhi.core.query.output.callback.QueryCallback; 24 | import io.siddhi.core.util.EventPrinter; 25 | import io.siddhi.core.util.SiddhiTestHelper; 26 | import io.siddhi.core.util.config.InMemoryConfigManager; 27 | import io.siddhi.extension.io.http.source.util.HttpTestUtil; 28 | import io.siddhi.extension.map.xml.sourcemapper.XmlSourceMapper; 29 | import org.apache.logging.log4j.LogManager; 30 | import org.apache.logging.log4j.Logger; 31 | import org.testng.Assert; 32 | import org.testng.annotations.BeforeMethod; 33 | import org.testng.annotations.Test; 34 | 35 | import java.util.ArrayList; 36 | import java.util.HashMap; 37 | import java.util.List; 38 | import java.util.Map; 39 | import java.util.concurrent.atomic.AtomicInteger; 40 | 41 | /** 42 | * Multiple event synchronously run. 43 | */ 44 | 45 | public class HttpSourceConflictsTestCase { 46 | private static final Logger logger = LogManager.getLogger(HttpSourceConflictsTestCase.class); 47 | private AtomicInteger eventCountA = new AtomicInteger(0); 48 | private AtomicInteger eventCountB = new AtomicInteger(0); 49 | private int waitTime = 50; 50 | private int timeout = 30000; 51 | 52 | @BeforeMethod 53 | public void init() { 54 | eventCountA.set(0); 55 | eventCountB.set(0); 56 | } 57 | 58 | /** 59 | * Creating test for publishing events with multiple formats synchronously. 60 | * 61 | * @throws Exception Interrupted exception 62 | */ 63 | @Test 64 | public void testHTTPDifferentFormat() throws Exception { 65 | logger.info("Creating test for publishing events with https protocol."); 66 | HttpTestUtil.setCarbonHome(); 67 | Map masterConfigs = new HashMap<>(); 68 | masterConfigs.put("source.http.keyStoreLocation", "${carbon.home}/resources/security/wso2carbon.jks"); 69 | masterConfigs.put("source.http.keyStorePassword", "wso2carbon"); 70 | masterConfigs.put("source.http.certPassword", "wso2carbon"); 71 | List receivedEventNameList = new ArrayList<>(2); 72 | SiddhiManager siddhiManager = new SiddhiManager(); 73 | InMemoryConfigManager inMemoryConfigManager = new InMemoryConfigManager(masterConfigs, null); 74 | inMemoryConfigManager.generateConfigReader("source", "http"); 75 | siddhiManager.setConfigManager(inMemoryConfigManager); 76 | siddhiManager.setExtension("xml-input-mapper", XmlSourceMapper.class); 77 | String inStreamDefinitionA = "@source(type='http', @map(type='xml'), receiver.url='https://localhost:8005" 78 | + "/endpoints/RecPro2')" 79 | + "define stream inputStream (name string, age int, country string);"; 80 | String queryA = ("@info(name = 'query') " 81 | + "from inputStream " 82 | + "select * " 83 | + "insert into outputStream;" 84 | ); 85 | SiddhiAppRuntime siddhiAppRuntime = siddhiManager 86 | .createSiddhiAppRuntime(inStreamDefinitionA + queryA); 87 | siddhiAppRuntime.addCallback("query", new QueryCallback() { 88 | @Override 89 | public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { 90 | EventPrinter.print(timeStamp, inEvents, removeEvents); 91 | for (Event event : inEvents) { 92 | eventCountA.incrementAndGet(); 93 | receivedEventNameList.add(event.getData(0).toString()); 94 | } 95 | } 96 | }); 97 | try { 98 | siddhiAppRuntime.start(); 99 | masterConfigs.put("source.http.keyStorePassword", "wso2carbon2"); 100 | masterConfigs.put("source.http.certPassword", "wso2carbon2"); 101 | String inStreamDefinitionB = "@source(type='http', @map(type='xml'), receiver.url='https://localhost:8005" 102 | + "/endpoints/RecPro')" 103 | + "define stream inputStream2 (name string, age int, country string);"; 104 | String queryB = ("@info(name = 'query2') " 105 | + "from inputStream2 " 106 | + "select * " 107 | + "insert into outputStream2;" 108 | ); 109 | SiddhiAppRuntime siddhiAppRuntime2 = siddhiManager 110 | .createSiddhiAppRuntime(inStreamDefinitionB + queryB); 111 | siddhiAppRuntime2.start(); 112 | // publishing events 113 | List expected = new ArrayList<>(2); 114 | String event1 = "" 115 | + "" 116 | + "John" 117 | + "100" 118 | + "AUS" 119 | + "" 120 | + ""; 121 | String event2 = "" 122 | + "" 123 | + "Mike" 124 | + "20" 125 | + "USA" 126 | + "" 127 | + ""; 128 | HttpTestUtil.httpsPublishEvent(event1); 129 | HttpTestUtil.httpsPublishEvent(event2); 130 | SiddhiTestHelper.waitForEvents(waitTime, 2, eventCountA, timeout); 131 | Assert.assertEquals(receivedEventNameList.toString(), expected.toString()); 132 | } catch (InterruptedException t) { 133 | logger.error(t.getMessage(), t); 134 | } finally { 135 | siddhiAppRuntime.shutdown(); 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/source/HttpSyncRequestResponseTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package io.siddhi.extension.io.http.source; 17 | 18 | import io.siddhi.core.SiddhiAppRuntime; 19 | import io.siddhi.core.SiddhiManager; 20 | import io.siddhi.core.util.persistence.InMemoryPersistenceStore; 21 | import io.siddhi.core.util.persistence.PersistenceStore; 22 | import io.siddhi.extension.io.http.source.util.HttpTestUtil; 23 | import io.siddhi.extension.map.xml.sinkmapper.XMLSinkMapper; 24 | import io.siddhi.extension.map.xml.sourcemapper.XmlSourceMapper; 25 | import org.testng.Assert; 26 | import org.testng.annotations.Test; 27 | 28 | import java.net.URI; 29 | 30 | /** 31 | * Tests http request source and http response source. 32 | */ 33 | 34 | public class HttpSyncRequestResponseTestCase { 35 | 36 | @Test 37 | public void testHTTPTextMappingXML() throws Exception { 38 | 39 | URI baseURI = URI.create(String.format("http://%s:%d", "localhost", 8010)); 40 | PersistenceStore persistenceStore = new InMemoryPersistenceStore(); 41 | 42 | SiddhiManager siddhiManager = new SiddhiManager(); 43 | siddhiManager.setPersistenceStore(persistenceStore); 44 | siddhiManager.setExtension("xml-output-mapper", XMLSinkMapper.class); 45 | siddhiManager.setExtension("xml-input-mapper", XmlSourceMapper.class); 46 | String inStreamDefinition = "@Source(type = 'http-request', source.id='testsource', basic.auth" + 47 | ".enabled='false',receiver.url='http://localhost:8010/TestHTTPRequestResponse/InputStream', @map" + 48 | "(type='xml', @attributes(messageId='trp:messageId',param1='/event/param1'," + 49 | "param2='/event/param2')))\n" + 50 | "define stream InputStream (messageId string, param1 string, param2 string);\n" + 51 | "@sink(type='http-response', source.id='testsource', message.id='{{messageId}}', @map(type='xml', " + 52 | "@payload('{{param1}}')))\n" + 53 | "define stream OutputStream (messageId string, param1 string);\n" + 54 | "from InputStream\n" + 55 | "select messageId, param1\n" + 56 | "insert into OutputStream;"; 57 | 58 | SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inStreamDefinition); 59 | siddhiAppRuntime.start(); 60 | 61 | String event1 = "\n" + 62 | " param1\n" + 63 | " param2\n" + 64 | ""; 65 | 66 | 67 | String response = HttpTestUtil.sendHttpEvent(event1, baseURI, "/TestHTTPRequestResponse/InputStream", 68 | false, "application/xml"); 69 | Assert.assertEquals(response, "param1"); 70 | 71 | siddhiAppRuntime.shutdown(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/source/HttpSyncServiceResponseTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package io.siddhi.extension.io.http.source; 19 | 20 | import io.siddhi.core.SiddhiAppRuntime; 21 | import io.siddhi.core.SiddhiManager; 22 | import io.siddhi.core.util.persistence.InMemoryPersistenceStore; 23 | import io.siddhi.core.util.persistence.PersistenceStore; 24 | import io.siddhi.extension.io.http.source.util.HttpTestUtil; 25 | import io.siddhi.extension.map.xml.sinkmapper.XMLSinkMapper; 26 | import io.siddhi.extension.map.xml.sourcemapper.XmlSourceMapper; 27 | import org.testng.Assert; 28 | import org.testng.annotations.Test; 29 | 30 | import java.net.URI; 31 | 32 | /** 33 | * Tests http request source and http response source. 34 | */ 35 | 36 | public class HttpSyncServiceResponseTestCase { 37 | 38 | @Test 39 | public void testHTTPTextMappingXML() throws Exception { 40 | 41 | URI baseURI = URI.create(String.format("http://%s:%d", "localhost", 8010)); 42 | PersistenceStore persistenceStore = new InMemoryPersistenceStore(); 43 | 44 | SiddhiManager siddhiManager = new SiddhiManager(); 45 | siddhiManager.setPersistenceStore(persistenceStore); 46 | siddhiManager.setExtension("xml-output-mapper", XMLSinkMapper.class); 47 | siddhiManager.setExtension("xml-input-mapper", XmlSourceMapper.class); 48 | String inStreamDefinition = "@Source(type = 'http-service', source.id='testsource', basic.auth" + 49 | ".enabled='false',receiver.url='http://localhost:8010/TestHTTPRequestResponse/InputStream', @map" + 50 | "(type='xml', @attributes(messageId='trp:messageId',param1='/event/param1'," + 51 | "param2='/event/param2')))\n" + 52 | "define stream InputStream (messageId string, param1 string, param2 string);\n" + 53 | "@sink(type='http-service-response', source.id='testsource', message.id='{{messageId}}', " + 54 | "@map(type='xml', @payload('{{param1}}')))\n" + 55 | "define stream OutputStream (messageId string, param1 string);\n" + 56 | "from InputStream\n" + 57 | "select messageId, param1\n" + 58 | "insert into OutputStream;"; 59 | 60 | SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inStreamDefinition); 61 | siddhiAppRuntime.start(); 62 | 63 | String event1 = "\n" + 64 | " param1\n" + 65 | " param2\n" + 66 | ""; 67 | 68 | 69 | String response = HttpTestUtil.sendHttpEvent(event1, baseURI, "/TestHTTPRequestResponse/InputStream", 70 | false, "application/xml"); 71 | Assert.assertEquals(response, "param1"); 72 | 73 | siddhiAppRuntime.shutdown(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/source/util/Constants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023 WSO2 LLC. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 LLC. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source.util; 20 | 21 | /** 22 | * Constants used by the test cases. 23 | */ 24 | public class Constants { 25 | public static final String BASIC_AUTH_HEADER = "Authorization"; 26 | public static final String BASIC_AUTH_HEADER_VALUE = "Basic YWRtaW46YWRtaW4="; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /component/src/test/java/io/siddhi/extension/io/http/source/util/HttpServerUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | * 18 | */ 19 | package io.siddhi.extension.io.http.source.util; 20 | 21 | import io.netty.handler.codec.http.HttpMethod; 22 | import org.apache.logging.log4j.LogManager; 23 | import org.apache.logging.log4j.Logger; 24 | 25 | import java.io.IOException; 26 | import java.io.OutputStreamWriter; 27 | import java.net.HttpURLConnection; 28 | import java.net.URI; 29 | import java.net.URL; 30 | 31 | /** 32 | * A util class to be used for tests. 33 | */ 34 | class HttpServerUtil { 35 | 36 | private static final Logger log = LogManager.getLogger(HttpServerUtil.class); 37 | 38 | static void writeContent(HttpURLConnection urlConn, String content) throws IOException { 39 | OutputStreamWriter out = new OutputStreamWriter( 40 | urlConn.getOutputStream()); 41 | out.write(content); 42 | out.close(); 43 | } 44 | 45 | static HttpURLConnection request(URI baseURI, String path, String method) 46 | throws IOException { 47 | URL url = baseURI.resolve(path).toURL(); 48 | HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 49 | if (method.equals(HttpMethod.POST.name()) || method.equals(HttpMethod.PUT.name())) { 50 | urlConn.setDoOutput(true); 51 | } 52 | urlConn.setRequestMethod(method); 53 | urlConn.setRequestProperty("Connection", "Keep-Alive"); 54 | return urlConn; 55 | } 56 | 57 | static void setHeader(HttpURLConnection urlConnection, String key, String value) { 58 | urlConnection.setRequestProperty(key, value); 59 | } 60 | 61 | static void handleException(IOException ex) { 62 | log.error("IOException occurred while running the HttpsSSLSourceTestCase", ex); 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /component/src/test/resources/events/events_map.txt: -------------------------------------------------------------------------------- 1 | name(string):John 2 | age(int):26 3 | country(string):USA 4 | ***** 5 | name(string):Mike 6 | age(age):32 7 | country(string):Germany 8 | -------------------------------------------------------------------------------- /component/src/test/resources/events/events_text.txt: -------------------------------------------------------------------------------- 1 | John,26,USA 2 | ***** 3 | Mike,32,Germany 4 | -------------------------------------------------------------------------------- /component/src/test/resources/files/testFile.txt: -------------------------------------------------------------------------------- 1 | This is a test. -------------------------------------------------------------------------------- /component/src/test/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /component/src/test/resources/mb.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | # 4 | # WSO2 Inc. licenses this file to you under the Apache License, 5 | # Version 2.0 (the "License"); you may not use this file except 6 | # in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | # 18 | java.naming.factory.initial=org.wso2.andes.jndi.PropertiesFileInitialContextFactory 19 | connectionfactory.qpidConnectionFactory=amqp://admin:admin@clientid/carbon?brokerlist='tcp://localhost:5672' 20 | -------------------------------------------------------------------------------- /component/src/test/resources/qpid.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | # 4 | # WSO2 Inc. licenses this file to you under the Apache License, 5 | # Version 2.0 (the "License"); you may not use this file except 6 | # in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | # 18 | java.naming.factory.initial=org.apache.qpid.jndi.PropertiesFileInitialContextFactory 19 | connectionfactory.qpidConnectionFactory=amqp://admin:admin@clientid/default?brokerlist='tcp://localhost:5672' 20 | -------------------------------------------------------------------------------- /component/src/test/resources/security/client-truststore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siddhi-io/siddhi-io-http/a1082bb4a2fb010705d7c63119f91241ea150e05/component/src/test/resources/security/client-truststore.jks -------------------------------------------------------------------------------- /component/src/test/resources/security/secrets.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | #============================================================================== 17 | # secrets.properties 18 | # This file contains all the secrets(passwords) in the carbon server. 19 | # Usage: 20 | # alias=[plainText|cipherText] value 21 | # eg: 22 | # wso2.sample.password1=plainText ABC@123 23 | # wso2.sample.password2=cipherText SnBSWKjtZZOo0UsmOpPRhP6ZMNYTb80+BZHRDC/kxNT9ExcTswAbFjb/aip2KgQNaVuIT27UtrBaIv77Mb5sNPGiwyPrfajLNhSOlke2p8YmMkegx/mG2ytJhJa5j9iMGtCsbMt+SAf85v6kGIiH0gZA20qDZ9jnveT7/Ifz7v0\= 24 | #============================================================================== 25 | -------------------------------------------------------------------------------- /component/src/test/resources/security/security.policy: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | *******************************************************************************/ 16 | 17 | grant { 18 | permission java.security.AllPermission; 19 | }; 20 | -------------------------------------------------------------------------------- /component/src/test/resources/security/store.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siddhi-io/siddhi-io-http/a1082bb4a2fb010705d7c63119f91241ea150e05/component/src/test/resources/security/store.jks -------------------------------------------------------------------------------- /component/src/test/resources/security/wso2carbon.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siddhi-io/siddhi-io-http/a1082bb4a2fb010705d7c63119f91241ea150e05/component/src/test/resources/security/wso2carbon.jks -------------------------------------------------------------------------------- /component/src/test/resources/testng.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /coverage-report/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | siddhi-io-http-parent 23 | io.siddhi.extension.io.http 24 | 2.3.8-SNAPSHOT 25 | ../pom.xml 26 | 27 | 4.0.0 28 | io.siddhi.extension.io.http.coverge.test 29 | Siddhi IO HTTP - Test Coverage 30 | Coverage reports Siddhi IO HTTP module 31 | http://wso2.com 32 | 33 | 34 | with-tests 35 | 36 | 37 | ../component/target/coverage-reports 38 | 39 | 40 | 41 | 42 | 43 | org.apache.maven.plugins 44 | maven-dependency-plugin 45 | 46 | 47 | copy-jacoco-dependencies 48 | compile 49 | 50 | copy-dependencies 51 | 52 | 53 | ${project.build.directory} 54 | jar 55 | org.jacoco.ant 56 | true 57 | 58 | 59 | 60 | 61 | 62 | 63 | org.apache.maven.plugins 64 | maven-antrun-plugin 65 | 66 | 67 | package 68 | 69 | run 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | org.jacoco 106 | org.jacoco.ant 107 | ${org.jacoco.ant.version} 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /docs/assets/javascripts/extra.js: -------------------------------------------------------------------------------- 1 | /* 2 | ~ Copyright (c) WSO2 Inc. (http://wso2.com) All Rights Reserved. 3 | ~ 4 | ~ Licensed under the Apache License, Version 2.0 (the "License"); 5 | ~ you may not use this file except in compliance with the License. 6 | ~ You may obtain a copy of the License at 7 | ~ 8 | ~ http://www.apache.org/licenses/LICENSE-2.0 9 | ~ 10 | ~ Unless required by applicable law or agreed to in writing, software 11 | ~ distributed under the License is distributed on an "AS IS" BASIS, 12 | ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | ~ See the License for the specific language governing permissions and 14 | ~ limitations under the License. 15 | */ 16 | 17 | var logo = document.querySelector('.md-logo'); 18 | var logoTitle = logo.title; 19 | logo.setAttribute('href', 'https://siddhi.io/') 20 | 21 | var header = document.querySelector('.md-header-nav__title'); 22 | var headerContent = document.querySelectorAll('.md-header-nav__title span')[1].textContent; 23 | var url = document.querySelector('.md-nav__item a.md-nav__link').href 24 | header.innerHTML = '' + logoTitle + '' + 25 | '' + headerContent + '' 26 | 27 | 28 | /* 29 | * TOC position highlight on scroll 30 | */ 31 | 32 | var observeeList = document.querySelectorAll(".md-sidebar__inner > .md-nav--secondary .md-nav__link"); 33 | var listElems = document.querySelectorAll(".md-sidebar__inner > .md-nav--secondary > ul li"); 34 | var config = {attributes: true, childList: true, subtree: true}; 35 | 36 | var callback = function (mutationsList, observer) { 37 | for (var mutation of mutationsList) { 38 | if (mutation.type == 'attributes') { 39 | mutation.target.parentNode.setAttribute(mutation.attributeName, 40 | mutation.target.getAttribute(mutation.attributeName)); 41 | scrollerPosition(mutation); 42 | } 43 | } 44 | }; 45 | var observer = new MutationObserver(callback); 46 | 47 | listElems[0].classList.add('active'); 48 | 49 | for (var i = 0; i < observeeList.length; i++) { 50 | var el = observeeList[i]; 51 | 52 | observer.observe(el, config); 53 | 54 | el.onclick = function (e) { 55 | listElems.forEach(function (elm) { 56 | if (elm.classList) { 57 | elm.classList.remove('active'); 58 | } 59 | }); 60 | 61 | e.target.parentNode.classList.add('active'); 62 | } 63 | } 64 | 65 | function scrollerPosition(mutation) { 66 | var blurList = document.querySelectorAll(".md-sidebar__inner > .md-nav--secondary > ul li > .md-nav__link[data-md-state='blur']"); 67 | 68 | listElems.forEach(function (el) { 69 | if (el.classList) { 70 | el.classList.remove('active'); 71 | } 72 | }); 73 | 74 | if (blurList.length > 0) { 75 | if (mutation.target.getAttribute('data-md-state') === 'blur') { 76 | if (mutation.target.parentNode.querySelector('ul li')) { 77 | mutation.target.parentNode.querySelector('ul li').classList.add('active'); 78 | } else { 79 | setActive(mutation.target.parentNode); 80 | } 81 | } else { 82 | mutation.target.parentNode.classList.add('active'); 83 | } 84 | } else { 85 | if (listElems.length > 0) { 86 | listElems[0].classList.add('active'); 87 | } 88 | } 89 | } 90 | 91 | function setActive(parentNode, i) { 92 | i = i || 0; 93 | if (i === 5) { 94 | return; 95 | } 96 | if (parentNode.nextElementSibling) { 97 | parentNode.nextElementSibling.classList.add('active'); 98 | return; 99 | } 100 | setActive(parentNode.parentNode.parentNode.parentNode, ++i); 101 | } 102 | -------------------------------------------------------------------------------- /docs/assets/lib/backtotop/img/cd-top-arrow.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /docs/assets/lib/backtotop/js/main.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | // Back to Top - by CodyHouse.co 3 | var backTop = document.getElementsByClassName('js-cd-top')[0], 4 | offset = 300, // browser window scroll (in pixels) after which the "back to top" link is shown 5 | offsetOpacity = 1200, //browser window scroll (in pixels) after which the "back to top" link opacity is reduced 6 | scrollDuration = 700, 7 | scrolling = false; 8 | 9 | if( backTop ) { 10 | //update back to top visibility on scrolling 11 | window.addEventListener("scroll", function(event) { 12 | if( !scrolling ) { 13 | scrolling = true; 14 | (!window.requestAnimationFrame) ? setTimeout(checkBackToTop, 250) : window.requestAnimationFrame(checkBackToTop); 15 | } 16 | }); 17 | 18 | //smooth scroll to top 19 | backTop.addEventListener('click', function(event) { 20 | event.preventDefault(); 21 | (!window.requestAnimationFrame) ? window.scrollTo(0, 0) : Util.scrollTo(0, scrollDuration); 22 | }); 23 | } 24 | 25 | function checkBackToTop() { 26 | var windowTop = window.scrollY || document.documentElement.scrollTop; 27 | ( windowTop > offset ) ? Util.addClass(backTop, 'cd-top--is-visible') : Util.removeClass(backTop, 'cd-top--is-visible cd-top--fade-out'); 28 | ( windowTop > offsetOpacity ) && Util.addClass(backTop, 'cd-top--fade-out'); 29 | scrolling = false; 30 | } 31 | })(); -------------------------------------------------------------------------------- /docs/assets/lib/backtotop/js/util.js: -------------------------------------------------------------------------------- 1 | // Utility function 2 | function Util () {}; 3 | 4 | /* 5 | class manipulation functions 6 | */ 7 | Util.hasClass = function(el, className) { 8 | if (el.classList) return el.classList.contains(className); 9 | else return !!el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)')); 10 | }; 11 | 12 | Util.addClass = function(el, className) { 13 | var classList = className.split(' '); 14 | if (el.classList) el.classList.add(classList[0]); 15 | else if (!Util.hasClass(el, classList[0])) el.className += " " + classList[0]; 16 | if (classList.length > 1) Util.addClass(el, classList.slice(1).join(' ')); 17 | }; 18 | 19 | Util.removeClass = function(el, className) { 20 | var classList = className.split(' '); 21 | if (el.classList) el.classList.remove(classList[0]); 22 | else if(Util.hasClass(el, classList[0])) { 23 | var reg = new RegExp('(\\s|^)' + classList[0] + '(\\s|$)'); 24 | el.className=el.className.replace(reg, ' '); 25 | } 26 | if (classList.length > 1) Util.removeClass(el, classList.slice(1).join(' ')); 27 | }; 28 | 29 | Util.toggleClass = function(el, className, bool) { 30 | if(bool) Util.addClass(el, className); 31 | else Util.removeClass(el, className); 32 | }; 33 | 34 | Util.setAttributes = function(el, attrs) { 35 | for(var key in attrs) { 36 | el.setAttribute(key, attrs[key]); 37 | } 38 | }; 39 | 40 | /* 41 | DOM manipulation 42 | */ 43 | Util.getChildrenByClassName = function(el, className) { 44 | var children = el.children, 45 | childrenByClass = []; 46 | for (var i = 0; i < el.children.length; i++) { 47 | if (Util.hasClass(el.children[i], className)) childrenByClass.push(el.children[i]); 48 | } 49 | return childrenByClass; 50 | }; 51 | 52 | /* 53 | Animate height of an element 54 | */ 55 | Util.setHeight = function(start, to, element, duration, cb) { 56 | var change = to - start, 57 | currentTime = null; 58 | 59 | var animateHeight = function(timestamp){ 60 | if (!currentTime) currentTime = timestamp; 61 | var progress = timestamp - currentTime; 62 | var val = parseInt((progress/duration)*change + start); 63 | element.setAttribute("style", "height:"+val+"px;"); 64 | if(progress < duration) { 65 | window.requestAnimationFrame(animateHeight); 66 | } else { 67 | cb(); 68 | } 69 | }; 70 | 71 | //set the height of the element before starting animation -> fix bug on Safari 72 | element.setAttribute("style", "height:"+start+"px;"); 73 | window.requestAnimationFrame(animateHeight); 74 | }; 75 | 76 | /* 77 | Smooth Scroll 78 | */ 79 | 80 | Util.scrollTo = function(final, duration, cb) { 81 | var start = window.scrollY || document.documentElement.scrollTop, 82 | currentTime = null; 83 | 84 | var animateScroll = function(timestamp){ 85 | if (!currentTime) currentTime = timestamp; 86 | var progress = timestamp - currentTime; 87 | if(progress > duration) progress = duration; 88 | var val = Math.easeInOutQuad(progress, start, final-start, duration); 89 | window.scrollTo(0, val); 90 | if(progress < duration) { 91 | window.requestAnimationFrame(animateScroll); 92 | } else { 93 | cb && cb(); 94 | } 95 | }; 96 | 97 | window.requestAnimationFrame(animateScroll); 98 | }; 99 | 100 | /* 101 | Focus utility classes 102 | */ 103 | 104 | //Move focus to an element 105 | Util.moveFocus = function (element) { 106 | if( !element ) element = document.getElementsByTagName("body")[0]; 107 | element.focus(); 108 | if (document.activeElement !== element) { 109 | element.setAttribute('tabindex','-1'); 110 | element.focus(); 111 | } 112 | }; 113 | 114 | /* 115 | Misc 116 | */ 117 | 118 | Util.getIndexInArray = function(array, el) { 119 | return Array.prototype.indexOf.call(array, el); 120 | }; 121 | 122 | Util.cssSupports = function(property, value) { 123 | if('CSS' in window) { 124 | return CSS.supports(property, value); 125 | } else { 126 | var jsProperty = property.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase();}); 127 | return jsProperty in document.body.style; 128 | } 129 | }; 130 | 131 | /* 132 | Polyfills 133 | */ 134 | //Closest() method 135 | if (!Element.prototype.matches) { 136 | Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; 137 | } 138 | 139 | if (!Element.prototype.closest) { 140 | Element.prototype.closest = function(s) { 141 | var el = this; 142 | if (!document.documentElement.contains(el)) return null; 143 | do { 144 | if (el.matches(s)) return el; 145 | el = el.parentElement || el.parentNode; 146 | } while (el !== null && el.nodeType === 1); 147 | return null; 148 | }; 149 | } 150 | 151 | //Custom Event() constructor 152 | if ( typeof window.CustomEvent !== "function" ) { 153 | 154 | function CustomEvent ( event, params ) { 155 | params = params || { bubbles: false, cancelable: false, detail: undefined }; 156 | var evt = document.createEvent( 'CustomEvent' ); 157 | evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); 158 | return evt; 159 | } 160 | 161 | CustomEvent.prototype = window.Event.prototype; 162 | 163 | window.CustomEvent = CustomEvent; 164 | } 165 | 166 | /* 167 | Animation curves 168 | */ 169 | Math.easeInOutQuad = function (t, b, c, d) { 170 | t /= d/2; 171 | if (t < 1) return c/2*t*t + b; 172 | t--; 173 | return -c/2 * (t*(t-2) - 1) + b; 174 | }; -------------------------------------------------------------------------------- /docs/assets/lib/highlightjs/default.min.css: -------------------------------------------------------------------------------- 1 | .hljs{display:block;overflow-x:auto;padding:.5em;background:#F0F0F0}.hljs,.hljs-subst{color:#444}.hljs-comment{color:#888888}.hljs-keyword,.hljs-attribute,.hljs-selector-tag,.hljs-meta-keyword,.hljs-doctag,.hljs-name{font-weight:bold}.hljs-type,.hljs-string,.hljs-number,.hljs-selector-id,.hljs-selector-class,.hljs-quote,.hljs-template-tag,.hljs-deletion{color:#880000}.hljs-title,.hljs-section{color:#880000;font-weight:bold}.hljs-regexp,.hljs-symbol,.hljs-variable,.hljs-template-variable,.hljs-link,.hljs-selector-attr,.hljs-selector-pseudo{color:#BC6060}.hljs-literal{color:#78A960}.hljs-built_in,.hljs-bullet,.hljs-code,.hljs-addition{color:#397300}.hljs-meta{color:#1f7199}.hljs-meta-string{color:#4d99bf}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:bold} -------------------------------------------------------------------------------- /docs/images/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siddhi-io/siddhi-io-http/a1082bb4a2fb010705d7c63119f91241ea150e05/docs/images/favicon.ico -------------------------------------------------------------------------------- /docs/images/siddhi-logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 47 | -------------------------------------------------------------------------------- /findbugs-exclude.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /issue_template.md: -------------------------------------------------------------------------------- 1 | **Description:** 2 | 3 | 4 | **Suggested Labels:** 5 | 6 | 7 | **Suggested Assignees:** 8 | 9 | 10 | **Affected Product Version:** 11 | 12 | **OS, DB, other environment details and versions:** 13 | 14 | **Steps to reproduce:** 15 | 16 | 17 | **Related Issues:** 18 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: Siddhi IO HTTP 2 | site_description: Siddhi IO HTTP Extension 3 | repo_name: Siddhi IO HTTP 4 | repo_url: https://github.com/siddhi-io/siddhi-io-http/ 5 | edit_uri: https://github.com/siddhi-io/siddhi-io-http/blob/master/ 6 | copyright: Siddhi - Documentation 7 | theme: 8 | name: material 9 | logo: images/siddhi-logo.svg 10 | favicon: images/favicon.ico 11 | palette: 12 | primary: teal 13 | accent: teal 14 | extra_css: 15 | - assets/stylesheets/extra.css 16 | extra_javascript: 17 | - assets/javascripts/extra.js 18 | extra: 19 | social: 20 | - type: github 21 | link: https://github.com/siddhi-io/siddhi 22 | - type: medium 23 | link: https://medium.com/siddhi-io 24 | - type: twitter 25 | link: https://twitter.com/siddhi_io 26 | - type: linkedin 27 | link: https://www.linkedin.com/groups/13553064 28 | google_analytics: 29 | - UA-103065-28 30 | - auto 31 | markdown_extensions: 32 | - markdown.extensions.admonition 33 | pages: 34 | - Information: index.md 35 | - API Docs: 36 | - latest: api/latest.md 37 | - 2.3.7: api/2.3.7.md 38 | - 2.3.6: api/2.3.6.md 39 | - 2.3.5: api/2.3.5.md 40 | - 2.3.4: api/2.3.4.md 41 | - 2.3.3: api/2.3.3.md 42 | - 2.3.2: api/2.3.2.md 43 | - 2.3.1: api/2.3.1.md 44 | - 2.3.0: api/2.3.0.md 45 | - 2.2.4: api/2.2.4.md 46 | - 2.2.3: api/2.2.3.md 47 | - 2.2.2: api/2.2.2.md 48 | - 2.2.1: api/2.2.1.md 49 | - 2.2.0: api/2.2.0.md 50 | - 2.1.2: api/2.1.2.md 51 | - 2.1.1: api/2.1.1.md 52 | - 2.1.0: api/2.1.0.md 53 | - 2.0.8: api/2.0.8.md 54 | - 2.0.7: api/2.0.7.md 55 | - 2.0.6: api/2.0.6.md 56 | - 2.0.4: api/2.0.4.md 57 | - 2.0.0: api/2.0.0.md 58 | - 1.2.2: api/1.2.2.md 59 | - 1.2.1: api/1.2.1.md 60 | - 1.2.0: api/1.2.0.md 61 | - 1.1.5: api/1.1.5.md 62 | - 1.1.2: api/1.1.2.md 63 | - 1.1.0: api/1.1.0.md 64 | - 1.0.45: api/1.0.45.md 65 | - 1.0.39: api/1.0.39.md 66 | - 1.0.29: api/1.0.29.md 67 | - 1.0.18: api/1.0.18.md 68 | - 1.0.10: api/1.0.10.md 69 | - 1.0.4: api/1.0.4.md 70 | - License: license.md 71 | -------------------------------------------------------------------------------- /pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## Purpose 2 | > Describe the problems, issues, or needs driving this feature/fix and include links to related issues in the following format: Resolves issue1, issue2, etc. 3 | 4 | ## Goals 5 | > Describe the solutions that this feature/fix will introduce to resolve the problems described above 6 | 7 | ## Approach 8 | > Describe how you are implementing the solutions. Include an animated GIF or screenshot if the change affects the UI (email documentation@wso2.com to review all UI text). Include a link to a Markdown file or Google doc if the feature write-up is too long to paste here. 9 | 10 | ## User stories 11 | > Summary of user stories addressed by this change> 12 | 13 | ## Release note 14 | > Brief description of the new feature or bug fix as it will appear in the release notes 15 | 16 | ## Documentation 17 | > Link(s) to product documentation that addresses the changes of this PR. If no doc impact, enter “N/A” plus brief explanation of why there’s no doc impact 18 | 19 | ## Training 20 | > Link to the PR for changes to the training content in https://github.com/wso2/WSO2-Training, if applicable 21 | 22 | ## Certification 23 | > Type “Sent” when you have provided new/updated certification questions, plus four answers for each question (correct answer highlighted in bold), based on this change. Certification questions/answers should be sent to certification@wso2.com and NOT pasted in this PR. If there is no impact on certification exams, type “N/A” and explain why. 24 | 25 | ## Marketing 26 | > Link to drafts of marketing content that will describe and promote this feature, including product page changes, technical articles, blog posts, videos, etc., if applicable 27 | 28 | ## Automation tests 29 | - Unit tests 30 | > Code coverage information 31 | - Integration tests 32 | > Details about the test cases and coverage 33 | 34 | ## Security checks 35 | - Followed secure coding standards in http://wso2.com/technical-reports/wso2-secure-engineering-guidelines? yes/no 36 | - Ran FindSecurityBugs plugin and verified report? yes/no 37 | - Confirmed that this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets? yes/no 38 | 39 | ## Samples 40 | > Provide high-level details about the samples related to this feature 41 | 42 | ## Related PRs 43 | > List any other related PRs 44 | 45 | ## Migrations (if applicable) 46 | > Describe migration steps and platforms on which migration has been tested 47 | 48 | ## Test environment 49 | > List all JDK versions, operating systems, databases, and browser/versions on which this feature/fix was tested 50 | 51 | ## Learning 52 | > Describe the research phase and any blog posts, patterns, libraries, or add-ons you used to solve the problem. --------------------------------------------------------------------------------