Add improvments to oauth-http publisher to work with url templates

revert-dabc3590
charitha 7 years ago
parent 69b1102ee2
commit 5f4bada92a

@ -19,24 +19,24 @@
<eventPublisher name="WSO2IoT-DeviceOperation-Publisher" xmlns="http://wso2.org/carbon/eventpublisher">
<from streamName="org.wso2.iot.operation" version="1.0.0"/>
<mapping customMapping="enable" type="json">
<mapping customMapping="enable" type="text">
<inline>
{
"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}}"
}
}
</inline>
</mapping>
<to eventAdapterType="oauth-http">
<property name="http.client.method">HttpPost</property>
<property name="http.url">https://localhost:9443/api/device-mgt/v1.0/devices/android/operations</property>
<property name="http.url">https://localhost:9443/api/device-mgt/v1.0/devices/{deviceType}/operations</property>
<property name="http.url.templated">true</property>
</to>
</eventPublisher>

@ -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"
}
],

@ -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<String> 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) {

@ -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);

@ -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";

@ -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

Loading…
Cancel
Save