From 5f4bada92a36fe21729ff9a7e8769075b58eb5a0 Mon Sep 17 00:00:00 2001 From: charitha Date: Fri, 17 Nov 2017 12:56:27 +0530 Subject: [PATCH] Add improvments to oauth-http publisher to work with url templates --- ...SO2IoT-DeviceOperation-Publisher_1.0.0.xml | 20 ++++++------- .../org.wso2.iot.operation_1.0.0.json | 8 +++-- .../output/adapter/http/HTTPEventAdapter.java | 30 +++++++++++++++++++ .../adapter/http/HTTPEventAdapterFactory.java | 7 +++++ .../http/util/HTTPEventAdapterConstants.java | 2 ++ .../adapter/http/i18n/Resources.properties | 2 ++ 6 files changed, 57 insertions(+), 12 deletions(-) diff --git a/components/analytics/iot-analytics/org.wso2.carbon.device.mgt.iot.analytics/src/main/resources/carbonapps/device_analytics/WSO2IoT-DeviceOperation-Publisher_1.0.0/WSO2IoT-DeviceOperation-Publisher_1.0.0.xml b/components/analytics/iot-analytics/org.wso2.carbon.device.mgt.iot.analytics/src/main/resources/carbonapps/device_analytics/WSO2IoT-DeviceOperation-Publisher_1.0.0/WSO2IoT-DeviceOperation-Publisher_1.0.0.xml index 326925a9b0..7d52806b57 100644 --- a/components/analytics/iot-analytics/org.wso2.carbon.device.mgt.iot.analytics/src/main/resources/carbonapps/device_analytics/WSO2IoT-DeviceOperation-Publisher_1.0.0/WSO2IoT-DeviceOperation-Publisher_1.0.0.xml +++ b/components/analytics/iot-analytics/org.wso2.carbon.device.mgt.iot.analytics/src/main/resources/carbonapps/device_analytics/WSO2IoT-DeviceOperation-Publisher_1.0.0/WSO2IoT-DeviceOperation-Publisher_1.0.0.xml @@ -19,24 +19,24 @@ - + { - "deviceIdentifiers": [ - {{meta_deviceIdentifier}} - ], + "deviceIdentifiers": {{meta_deviceIdentifiers}}, + "deviceType": "{{meta_deviceType}}", "operation": { - "code": {{code}}, - "type": {{type}}, + "code": "{{code}}", + "type": "{{type}}", "status": "PENDING", - "isEnabled": {{isEnabled}}, - "payLoad": {{payLoad}} + "isEnabled": "{{isEnabled}}", + "payLoad": "{{payLoad}}" } } HttpPost - https://localhost:9443/api/device-mgt/v1.0/devices/android/operations + https://localhost:9443/api/device-mgt/v1.0/devices/{deviceType}/operations + true - \ No newline at end of file + diff --git a/components/analytics/iot-analytics/org.wso2.carbon.device.mgt.iot.analytics/src/main/resources/carbonapps/device_analytics/org.wso2.iot.operation_1.0.0/org.wso2.iot.operation_1.0.0.json b/components/analytics/iot-analytics/org.wso2.carbon.device.mgt.iot.analytics/src/main/resources/carbonapps/device_analytics/org.wso2.iot.operation_1.0.0/org.wso2.iot.operation_1.0.0.json index 44a0b0a453..c5786c0daa 100755 --- a/components/analytics/iot-analytics/org.wso2.carbon.device.mgt.iot.analytics/src/main/resources/carbonapps/device_analytics/org.wso2.iot.operation_1.0.0/org.wso2.iot.operation_1.0.0.json +++ b/components/analytics/iot-analytics/org.wso2.carbon.device.mgt.iot.analytics/src/main/resources/carbonapps/device_analytics/org.wso2.iot.operation_1.0.0/org.wso2.iot.operation_1.0.0.json @@ -1,11 +1,15 @@ { "name": "org.wso2.iot.operation", "version": "1.0.0", - "nickName": "", + "nickName": "Operation Stream", "description": "Operation stream for WSO2 IoT Devices", "metaData": [ { - "name": "deviceIdentifier", + "name": "deviceIdentifiers", + "type": "STRING" + }, + { + "name": "deviceType", "type": "STRING" } ], diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/HTTPEventAdapter.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/HTTPEventAdapter.java index a37f49b71b..b93074c722 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/HTTPEventAdapter.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/HTTPEventAdapter.java @@ -59,13 +59,17 @@ import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class HTTPEventAdapter implements OutputEventAdapter { @@ -181,6 +185,32 @@ public class HTTPEventAdapter implements OutputEventAdapter { .extractHeaders(dynamicProperties.get(HTTPEventAdapterConstants.ADAPTER_HEADERS)); String payload = message.toString(); + if ("true".equals(dynamicProperties.get(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_TEMPLATED))) { + try { + JSONParser jsonParser = new JSONParser(); + JSONObject jsonPayload = (JSONObject) jsonParser.parse(payload); + + List matchList = new ArrayList<>(); + Pattern regex = Pattern.compile("\\{(.*?)\\}"); + Matcher regexMatcher = regex.matcher(url); + + while (regexMatcher.find()) {//Finds Matching Pattern in String + matchList.add(regexMatcher.group(1));//Fetching Group from String + } + + for(String str:matchList) { + if (jsonPayload.containsKey(str)) { + url = url.replace("{" + str + "}", jsonPayload.get(str).toString()); + } + } + if (log.isDebugEnabled()) { + log.debug("Modified url: " + url); + } + } catch (ParseException e) { + log.error("Unable to parse request body to Json.", e); + } + } + try { executorService.submit(new HTTPSender(url, payload, headers, httpClient)); } catch (RejectedExecutionException e) { diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/HTTPEventAdapterFactory.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/HTTPEventAdapterFactory.java index 0aad85489a..e5450ebfc0 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/HTTPEventAdapterFactory.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/HTTPEventAdapterFactory.java @@ -76,6 +76,12 @@ public class HTTPEventAdapterFactory extends OutputEventAdapterFactory { urlProp.setHint(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_HINT)); urlProp.setRequired(true); + Property urlTemplateProp = new Property(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_TEMPLATED); + urlTemplateProp.setDisplayName(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_TEMPLATED)); + urlTemplateProp.setHint(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_TEMPLATED_HINT)); + urlTemplateProp.setRequired(true); + urlTemplateProp.setOptions(new String[]{"true", "false"}); + Property usernameProp = new Property(HTTPEventAdapterConstants.ADAPTER_USERNAME); usernameProp.setDisplayName(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_USERNAME)); usernameProp.setHint(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_USERNAME_HINT)); @@ -94,6 +100,7 @@ public class HTTPEventAdapterFactory extends OutputEventAdapterFactory { headersProp.setRequired(false); dynamicPropertyList.add(urlProp); + dynamicPropertyList.add(urlTemplateProp); dynamicPropertyList.add(usernameProp); dynamicPropertyList.add(passwordProp); dynamicPropertyList.add(headersProp); diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/util/HTTPEventAdapterConstants.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/util/HTTPEventAdapterConstants.java index 212afe7367..296f51aa28 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/util/HTTPEventAdapterConstants.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/java/org/wso2/carbon/device/mgt/output/adapter/http/util/HTTPEventAdapterConstants.java @@ -21,6 +21,8 @@ public class HTTPEventAdapterConstants { public static final String ADAPTER_TYPE_HTTP = "oauth-http"; public static final String ADAPTER_MESSAGE_URL = "http.url"; + public static final String ADAPTER_MESSAGE_URL_TEMPLATED = "http.url.templated"; + public static final String ADAPTER_MESSAGE_URL_TEMPLATED_HINT = "http.url.templated.hint"; public static final String ADAPTER_MESSAGE_URL_HINT = "http.url.hint"; public static final String ADAPTER_CONF_DCR_URL = "dcrUrl"; public static final String ADAPTER_CONF_TOKEN_URL = "tokenUrl"; diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/resources/org/wso2/carbon/device/mgt/output/adapter/http/i18n/Resources.properties b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/resources/org/wso2/carbon/device/mgt/output/adapter/http/i18n/Resources.properties index 0ceb2d520a..a31fefd0ec 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/resources/org/wso2/carbon/device/mgt/output/adapter/http/i18n/Resources.properties +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.http/src/main/resources/org/wso2/carbon/device/mgt/output/adapter/http/i18n/Resources.properties @@ -18,6 +18,8 @@ http.url=URL http.url.hint=The target HTTP/HTTPS URL, e.g. "http://yourhost:8080/service (URL will auto format for tenants)" +http.url.templated=URL Templated +http.url.templated.hint=The target HTTP/HTTPS URL has template value(s) to fill from event. e.g. "http://yourhost:8080/service/{variable1}/{variable2}" variable1 & variable2 should be available in the event and event should be in json format username=Username http.username.hint=Username to obtain oauth token. Leave empty to use default. password=Password