mirror of
https://repository.entgra.net/community/device-mgt-plugins.git
synced 2025-09-16 23:42:15 +00:00
commit
e634a38561
@ -1,72 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
~
|
||||
~ WSO2 Inc. licenses this file to you under the Apache License,
|
||||
~ Version 2.0 (the "License"); you may not use this file except
|
||||
~ in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing,
|
||||
~ software distributed under the License is distributed on an
|
||||
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
~ KIND, either express or implied. See the License for the
|
||||
~ specific language governing permissions and limitations
|
||||
~ under the License.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<parent>
|
||||
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
|
||||
<artifactId>iot-analytics</artifactId>
|
||||
<version>3.0.3-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>org.wso2.carbon.iot.das.rest.api</artifactId>
|
||||
<name>WSO2 Carbon - IoT Server REST API</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-scr-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>generate-scr-descriptor</id>
|
||||
<goals>
|
||||
<goal>scr</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||
<Private-Package>
|
||||
</Private-Package>
|
||||
<Export-Package>
|
||||
</Export-Package>
|
||||
<Import-Package>
|
||||
</Import-Package>
|
||||
<DynamicImport-Package>*</DynamicImport-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@ -19,10 +19,8 @@
|
||||
|
||||
<eventReceiver name="android_sense_receiver" statistics="disable" trace="disable" xmlns="http://wso2.org/carbon/eventreceiver">
|
||||
<from eventAdapterType="oauth-mqtt">
|
||||
<property name="topic">carbon.super/android_sense/+/data</property>
|
||||
<property name="username">admin</property>
|
||||
<property name="password">admin</property>
|
||||
<property name="contentValidator">org.wso2.carbon.device.mgt.input.adapter.mqtt.util.MQTTContentValidator</property>
|
||||
<property name="topic">${tenant-domain}/android_sense/+/data</property>
|
||||
<property name="contentValidator">iot-mqtt</property>
|
||||
<property name="cleanSession">true</property>
|
||||
</from>
|
||||
<mapping customMapping="disable" type="json"/>
|
||||
|
||||
@ -56,7 +56,7 @@ import javax.ws.rs.core.Response;
|
||||
}
|
||||
),
|
||||
tags = {
|
||||
@Tag(name = "android_sense", description = "")
|
||||
@Tag(name = "android_sense,device_management", description = "")
|
||||
}
|
||||
)
|
||||
@Scopes(
|
||||
|
||||
@ -21,9 +21,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration
|
||||
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.constants.AndroidSenseConstants;
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
import org.wso2.carbon.utils.NetworkUtils;
|
||||
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
@ -176,18 +174,6 @@ public class APIUtil {
|
||||
return deviceAccessAuthorizationService;
|
||||
}
|
||||
|
||||
public static OutputEventAdapterService getOutputEventAdapterService() {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
OutputEventAdapterService outputEventAdapterService =
|
||||
(OutputEventAdapterService) ctx.getOSGiService(OutputEventAdapterService.class, null);
|
||||
if (outputEventAdapterService == null) {
|
||||
String msg = "Device Authorization service has not initialized.";
|
||||
log.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
return outputEventAdapterService;
|
||||
}
|
||||
|
||||
public static String getTenantDomainOftheUser() {
|
||||
PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
return threadLocalCarbonContext.getTenantDomain();
|
||||
|
||||
@ -21,6 +21,8 @@ var modalPopupContainer = modalPopup + " .modal-content";
|
||||
var modalPopupContent = modalPopup + " .modal-content";
|
||||
var body = "body";
|
||||
|
||||
var backendEndBasePath = "/api/device-mgt/v1.0";
|
||||
|
||||
/*
|
||||
* set popup maximum height function.
|
||||
*/
|
||||
@ -69,4 +71,32 @@ function attachEvents() {
|
||||
generateQRCode(modalPopupContent + " .qr-code");
|
||||
modalDialog.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function artifactUpload() {
|
||||
var contentType = "application/json";
|
||||
|
||||
var urix = backendEndBasePath + "/admin/devicetype/deploy/android_sense";
|
||||
var defaultStatusClasses = "fw fw-stack-1x";
|
||||
var content = $("#androidsense-statistic-response-template").find(".content");
|
||||
var title = content.find("#title");
|
||||
var statusIcon = content.find("#status-icon");
|
||||
var data = {}
|
||||
invokerUtil.post(urix, data, function (data) {
|
||||
title.html("Deploying statistic artifacts. Please wait...");
|
||||
statusIcon.attr("class", defaultStatusClasses + " fw-check");
|
||||
$(modalPopupContent).html(content.html());
|
||||
showPopup();
|
||||
setTimeout(function () {
|
||||
hidePopup();
|
||||
location.reload(true);
|
||||
}, 5000);
|
||||
|
||||
}, function (jqXHR) {
|
||||
title.html("Failed to deploy artifacts, Please contact administrator.");
|
||||
statusIcon.attr("class", defaultStatusClasses + " fw-error");
|
||||
$(modalPopupContent).html(content.html());
|
||||
showPopup();
|
||||
}, contentType);
|
||||
}
|
||||
|
||||
|
||||
@ -41,7 +41,9 @@
|
||||
<a href="#" class="download-link btn-operations"><i class="fw fw-mobile fw-inverse fw-lg add-margin-1x"></i> Enroll Device</a>
|
||||
<a href="{{hostName}}{{@unit.publicUri}}/asset/androidsense.apk" class="btn-operations"><i class="fw fw-download fw-inverse fw-lg add-margin-1x"></i> Download APK</a>
|
||||
<a href="javascript:toggleEmailInvite()" class="btn-operations"><i class="fw fw-mail fw-inverse fw-lg add-margin-1x"></i> Invite by Email</a>
|
||||
|
||||
{{#if displayStatus}}
|
||||
<a href="javascript:artifactUpload()" class="btn-operations"><i class="fw fw-upload fw-inverse fw-lg add-margin-1x"></i> Deploy Analytics Artifacts</a>
|
||||
{{/if}}
|
||||
<p class="doc-link">Click <a href="https://docs.wso2.com/display/IoTS300/Android+Sense"
|
||||
target="_blank">[ here ]</a> for latest instructions and
|
||||
troubleshooting.</p>
|
||||
@ -240,7 +242,26 @@
|
||||
</div>
|
||||
|
||||
{{unit "cdmf.unit.device.type.email.invite-modal" deviceTypeView="androidSense"}}
|
||||
|
||||
<div id="androidsense-statistic-response-template" style="display: none">
|
||||
<div class="content">
|
||||
<div class="row">
|
||||
<div class="col-lg-5 col-md-6 col-centered">
|
||||
<h3>
|
||||
<span class="fw-stack">
|
||||
<i class="fw fw-circle-outline fw-stack-2x"></i>
|
||||
<i id="status-icon" class="fw fw-error fw-stack-1x"></i>
|
||||
</span>
|
||||
<br>
|
||||
</h3>
|
||||
<h4>
|
||||
<span id="title"></span>
|
||||
<br>
|
||||
</h4>
|
||||
<span id="description"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!--<div class="content">-->
|
||||
<!--<div class="row">-->
|
||||
|
||||
@ -19,6 +19,20 @@
|
||||
function onRequest(context){
|
||||
var viewModel = {};
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
var url = devicemgtProps["httpsURL"] + "/api/device-mgt/v1.0/admin/devicetype/deploy/android_sense/status";
|
||||
serviceInvokers.XMLHttp.get(
|
||||
url, function (responsePayload) {
|
||||
var responseContent = responsePayload.status;
|
||||
new Log().error(responseContent);
|
||||
if ("204" == responsePayload.status) {
|
||||
viewModel["displayStatus"] = "Display";
|
||||
}
|
||||
},
|
||||
function (responsePayload) {
|
||||
//do nothing.
|
||||
}
|
||||
);
|
||||
viewModel["hostName"] = devicemgtProps["generalConfig"]["host"];
|
||||
viewModel["enrollmentURL"] = viewModel["hostName"] + context.unit.publicUri + "/asset/androidsense.apk";
|
||||
return viewModel;
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
-->
|
||||
<eventReceiver name="arduino_receiver" statistics="disable" trace="disable" xmlns="http://wso2.org/carbon/eventreceiver">
|
||||
<from eventAdapterType="oauth-http">
|
||||
<property name="contentValidator">org.wso2.carbon.device.mgt.input.adapter.http.util.HTTPContentValidator</property>
|
||||
<property name="contentValidator">iot-http</property>
|
||||
</from>
|
||||
<mapping customMapping="disable" type="json"/>
|
||||
<to streamName="org.wso2.iot.arduino" version="1.0.0"/>
|
||||
|
||||
@ -40,7 +40,7 @@ import javax.ws.rs.core.Response;
|
||||
}
|
||||
),
|
||||
tags = {
|
||||
@Tag(name = "arduino", description = "")
|
||||
@Tag(name = "arduino,device_management", description = "")
|
||||
}
|
||||
)
|
||||
@Scopes(
|
||||
|
||||
@ -179,7 +179,9 @@ public class ArduinoServiceImpl implements ArduinoService {
|
||||
@Produces("application/zip")
|
||||
public Response downloadSketch(@QueryParam("deviceName") String deviceName) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
||||
String username = APIUtil.getAuthenticatedUser() + "@" + PrivilegedCarbonContext
|
||||
.getThreadLocalCarbonContext().getTenantDomain();
|
||||
ZipArchive zipFile = createDownloadFile(username, deviceName);
|
||||
Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile()));
|
||||
response.status(Response.Status.OK);
|
||||
response.type("application/zip");
|
||||
@ -221,7 +223,8 @@ public class ArduinoServiceImpl implements ArduinoService {
|
||||
throw new DeviceManagementException(msg);
|
||||
}
|
||||
String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
||||
.getRealmConfiguration().getAdminUserName();
|
||||
.getRealmConfiguration().getAdminUserName() + "@" + PrivilegedCarbonContext
|
||||
.getThreadLocalCarbonContext().getTenantDomain();;
|
||||
if (apiApplicationKey == null) {
|
||||
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
||||
String[] tags = {ArduinoConstants.DEVICE_TYPE};
|
||||
|
||||
@ -20,10 +20,6 @@
|
||||
<param-name>doAuthentication</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>isSharedWithAllTenants</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
|
||||
<!--publish to apim-->
|
||||
<context-param>
|
||||
|
||||
@ -21,6 +21,8 @@ var modalPopupContainer = modalPopup + " .modal-content";
|
||||
var modalPopupContent = modalPopup + " .modal-content";
|
||||
var body = "body";
|
||||
|
||||
var backendEndBasePath = "/api/device-mgt/v1.0";
|
||||
|
||||
/*
|
||||
* set popup maximum height function.
|
||||
*/
|
||||
@ -181,4 +183,31 @@ function doAction(data) {
|
||||
hidePopup();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function artifactUpload() {
|
||||
var contentType = "application/json";
|
||||
|
||||
var urix = backendEndBasePath + "/admin/devicetype/deploy/arduino";
|
||||
var defaultStatusClasses = "fw fw-stack-1x";
|
||||
var content = $("#arduino-statistic-response-template").find(".content");
|
||||
var title = content.find("#title");
|
||||
var statusIcon = content.find("#status-icon");
|
||||
var data = {}
|
||||
invokerUtil.post(urix, data, function (data) {
|
||||
title.html("Deploying statistic artifacts. Please wait...");
|
||||
statusIcon.attr("class", defaultStatusClasses + " fw-check");
|
||||
$(modalPopupContent).html(content.html());
|
||||
showPopup();
|
||||
setTimeout(function () {
|
||||
hidePopup();
|
||||
location.reload(true);
|
||||
}, 5000);
|
||||
|
||||
}, function (jqXHR) {
|
||||
title.html("Failed to deploy artifacts, Please contact administrator.");
|
||||
statusIcon.attr("class", defaultStatusClasses + " fw-error");
|
||||
$(modalPopupContent).html(content.html());
|
||||
showPopup();
|
||||
}, contentType);
|
||||
}
|
||||
@ -66,6 +66,10 @@
|
||||
<a href="#" class="download-link btn-operations">
|
||||
<i class="fw fw-download add-margin-1x"></i>Download Sketch
|
||||
</a>
|
||||
{{#if displayStatus}}
|
||||
<a href="javascript:artifactUpload()" class="btn-operations"><i class="fw fw-upload fw-inverse fw-lg add-margin-1x"></i> Deploy Analytics Artifacts</a>
|
||||
{{/if}}
|
||||
|
||||
<p class="doc-link">Click <a href="https://docs.wso2.com/display/IoTS300/Arduino"
|
||||
target="_blank">here</a> for latest instructions and
|
||||
troubleshooting.</p>
|
||||
@ -291,6 +295,27 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="arduino-statistic-response-template" style="display: none">
|
||||
<div class="content">
|
||||
<div class="row">
|
||||
<div class="col-lg-5 col-md-6 col-centered">
|
||||
<h3>
|
||||
<span class="fw-stack">
|
||||
<i class="fw fw-circle-outline fw-stack-2x"></i>
|
||||
<i id="status-icon" class="fw fw-error fw-stack-1x"></i>
|
||||
</span>
|
||||
<br>
|
||||
</h3>
|
||||
<h4>
|
||||
<span id="title"></span>
|
||||
<br>
|
||||
</h4>
|
||||
<span id="description"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{#zone "topCss"}}
|
||||
{{css "css/styles.css"}}
|
||||
{{/zone}}
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
function onRequest(context){
|
||||
var viewModel = {};
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
var url = devicemgtProps["httpsURL"] + "/api/device-mgt/v1.0/admin/devicetype/deploy/arduino/status";
|
||||
serviceInvokers.XMLHttp.get(
|
||||
url, function (responsePayload) {
|
||||
var responseContent = responsePayload.status;
|
||||
new Log().error(responseContent);
|
||||
if ("204" == responsePayload.status) {
|
||||
viewModel["displayStatus"] = "Display";
|
||||
}
|
||||
},
|
||||
function (responsePayload) {
|
||||
//do nothing.
|
||||
}
|
||||
);
|
||||
return viewModel;
|
||||
}
|
||||
@ -18,10 +18,8 @@
|
||||
-->
|
||||
<eventReceiver name="raspberrypi_receiver" statistics="disable" trace="disable" xmlns="http://wso2.org/carbon/eventreceiver">
|
||||
<from eventAdapterType="oauth-mqtt">
|
||||
<property name="topic">carbon.super/raspberrypi/+/temperature</property>
|
||||
<property name="username">admin</property>
|
||||
<property name="password">admin</property>
|
||||
<property name="contentValidator">org.wso2.carbon.device.mgt.input.adapter.mqtt.util.MQTTContentValidator</property>
|
||||
<property name="topic">${tenant-domain}/raspberrypi/+/temperature</property>
|
||||
<property name="contentValidator">iot-mqtt</property>
|
||||
<property name="cleanSession">true</property>
|
||||
</from>
|
||||
<mapping customMapping="disable" type="json"/>
|
||||
|
||||
@ -39,7 +39,7 @@ import javax.ws.rs.core.Response;
|
||||
}
|
||||
),
|
||||
tags = {
|
||||
@Tag(name = "raspberrypi", description = "")
|
||||
@Tag(name = "raspberrypi,device_management", description = "")
|
||||
}
|
||||
)
|
||||
@Scopes(
|
||||
|
||||
@ -141,7 +141,9 @@ public class RaspberryPiServiceImpl implements RaspberryPiService {
|
||||
@Produces("application/zip")
|
||||
public Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
|
||||
String username = APIUtil.getAuthenticatedUser() + "@" + PrivilegedCarbonContext
|
||||
.getThreadLocalCarbonContext().getTenantDomain();
|
||||
ZipArchive zipFile = createDownloadFile(username, deviceName, sketchType);
|
||||
Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile()));
|
||||
response.status(Response.Status.OK);
|
||||
response.type("application/zip");
|
||||
@ -206,7 +208,8 @@ public class RaspberryPiServiceImpl implements RaspberryPiService {
|
||||
}
|
||||
if (apiApplicationKey == null) {
|
||||
String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
||||
.getRealmConfiguration().getAdminUserName();
|
||||
.getRealmConfiguration().getAdminUserName() + "@" + PrivilegedCarbonContext
|
||||
.getThreadLocalCarbonContext().getTenantDomain();
|
||||
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
||||
String[] tags = {RaspberrypiConstants.DEVICE_TYPE};
|
||||
apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
||||
|
||||
@ -17,7 +17,6 @@ import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorization
|
||||
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.dto.SensorRecord;
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -170,18 +169,6 @@ public class APIUtil {
|
||||
return deviceAccessAuthorizationService;
|
||||
}
|
||||
|
||||
public static OutputEventAdapterService getOutputEventAdapterService() {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
OutputEventAdapterService outputEventAdapterService =
|
||||
(OutputEventAdapterService) ctx.getOSGiService(OutputEventAdapterService.class, null);
|
||||
if (outputEventAdapterService == null) {
|
||||
String msg = "Device Authorization service has not initialized.";
|
||||
log.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
return outputEventAdapterService;
|
||||
}
|
||||
|
||||
public static PlatformConfigurationManagementService getTenantConfigurationManagementService() {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
PlatformConfigurationManagementService tenantConfigurationManagementService =
|
||||
|
||||
@ -21,6 +21,8 @@ var modalPopupContainer = modalPopup + " .modal-content";
|
||||
var modalPopupContent = modalPopup + " .modal-content";
|
||||
var body = "body";
|
||||
|
||||
var backendEndBasePath = "/api/device-mgt/v1.0";
|
||||
|
||||
/*
|
||||
* set popup maximum height function.
|
||||
*/
|
||||
@ -180,4 +182,31 @@ function doAction(data) {
|
||||
hidePopup();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function artifactUpload() {
|
||||
var contentType = "application/json";
|
||||
|
||||
var urix = backendEndBasePath + "/admin/devicetype/deploy/raspberrypi";
|
||||
var defaultStatusClasses = "fw fw-stack-1x";
|
||||
var content = $("#raspberrypi-statistic-response-template").find(".content");
|
||||
var title = content.find("#title");
|
||||
var statusIcon = content.find("#status-icon");
|
||||
var data = {}
|
||||
invokerUtil.post(urix, data, function (data) {
|
||||
title.html("Deploying statistic artifacts. Please wait...");
|
||||
statusIcon.attr("class", defaultStatusClasses + " fw-check");
|
||||
$(modalPopupContent).html(content.html());
|
||||
showPopup();
|
||||
setTimeout(function () {
|
||||
hidePopup();
|
||||
location.reload(true);
|
||||
}, 5000);
|
||||
|
||||
}, function (jqXHR) {
|
||||
title.html("Failed to deploy artifacts, Please contact administrator.");
|
||||
statusIcon.attr("class", defaultStatusClasses + " fw-error");
|
||||
$(modalPopupContent).html(content.html());
|
||||
showPopup();
|
||||
}, contentType);
|
||||
}
|
||||
@ -61,6 +61,10 @@
|
||||
<a href="#" class="download-link btn-operations">
|
||||
<i class="fw fw-download add-margin-1x"></i>Download Agent
|
||||
</a>
|
||||
{{#if displayStatus}}
|
||||
<a href="javascript:artifactUpload()" class="btn-operations"><i class="fw fw-upload fw-inverse fw-lg add-margin-1x"></i> Deploy Analytics Artifacts</a>
|
||||
{{/if}}
|
||||
|
||||
<p class="doc-link">Click <a href="https://docs.wso2.com/display/IoTS300/Raspberry+Pi"
|
||||
target="_blank">here</a> for latest instructions and
|
||||
troubleshooting.</p>
|
||||
@ -275,6 +279,27 @@
|
||||
</center>
|
||||
</div>
|
||||
|
||||
<div id="raspberrypi-statistic-response-template" style="display: none">
|
||||
<div class="content">
|
||||
<div class="row">
|
||||
<div class="col-lg-5 col-md-6 col-centered">
|
||||
<h3>
|
||||
<span class="fw-stack">
|
||||
<i class="fw fw-circle-outline fw-stack-2x"></i>
|
||||
<i id="status-icon" class="fw fw-error fw-stack-1x"></i>
|
||||
</span>
|
||||
<br>
|
||||
</h3>
|
||||
<h4>
|
||||
<span id="title"></span>
|
||||
<br>
|
||||
</h4>
|
||||
<span id="description"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{#zone "topCss"}}
|
||||
{{css "css/styles.css"}}
|
||||
{{/zone}}
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
function onRequest(context){
|
||||
var viewModel = {};
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
var url = devicemgtProps["httpsURL"] + "/api/device-mgt/v1.0/admin/devicetype/deploy/raspberrypi/status";
|
||||
serviceInvokers.XMLHttp.get(
|
||||
url, function (responsePayload) {
|
||||
var responseContent = responsePayload.status;
|
||||
new Log().error(responseContent);
|
||||
if ("204" == responsePayload.status) {
|
||||
viewModel["displayStatus"] = "Display";
|
||||
}
|
||||
},
|
||||
function (responsePayload) {
|
||||
//do nothing.
|
||||
}
|
||||
);
|
||||
return viewModel;
|
||||
}
|
||||
@ -18,10 +18,8 @@
|
||||
-->
|
||||
<eventReceiver name="virtualfirealarm_receiver" statistics="disable" trace="disable" xmlns="http://wso2.org/carbon/eventreceiver">
|
||||
<from eventAdapterType="oauth-mqtt">
|
||||
<property name="topic">carbon.super/virtual_firealarm/+/temperature</property>
|
||||
<property name="username">admin</property>
|
||||
<property name="password">admin</property>
|
||||
<property name="contentValidator">org.wso2.carbon.device.mgt.input.adapter.mqtt.util.MQTTContentValidator</property>
|
||||
<property name="topic">${tenant-domain}/virtual_firealarm/+/temperature</property>
|
||||
<property name="contentValidator">iot-mqtt</property>
|
||||
<property name="cleanSession">true</property>
|
||||
</from>
|
||||
<mapping customMapping="disable" type="json"/>
|
||||
|
||||
@ -45,7 +45,7 @@ import javax.ws.rs.core.Response;
|
||||
}
|
||||
),
|
||||
tags = {
|
||||
@Tag(name = "virtual_firealarm", description = "")
|
||||
@Tag(name = "virtual_firealarm,device_management", description = "")
|
||||
}
|
||||
)
|
||||
@Scopes(
|
||||
|
||||
@ -134,40 +134,6 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService {
|
||||
}
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("device/{deviceId}/policy")
|
||||
public Response updatePolicy(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol,
|
||||
@FormParam("policy") String policy) {
|
||||
String protocolString = protocol.toUpperCase();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending request to update-policy of device [" + deviceId + "] via " + protocolString);
|
||||
}
|
||||
try {
|
||||
if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(
|
||||
new DeviceIdentifier(deviceId, VirtualFireAlarmConstants.DEVICE_TYPE),
|
||||
DeviceGroupConstants.Permissions.DEFAULT_MANAGE_POLICIES_PERMISSIONS)) {
|
||||
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
||||
}
|
||||
String actualMessage = VirtualFireAlarmConstants.POLICY_CONTEXT + ":" + policy;
|
||||
Map<String, String> dynamicProperties = new HashMap<>();
|
||||
String publishTopic = APIUtil.getTenantDomainOftheUser() + "/"
|
||||
+ VirtualFireAlarmConstants.DEVICE_TYPE + "/" + deviceId;
|
||||
dynamicProperties.put(VirtualFireAlarmConstants.ADAPTER_TOPIC_PROPERTY, publishTopic);
|
||||
dynamicProperties.put(VirtualFireAlarmConstants.JID_PROPERTY_KEY,
|
||||
deviceId + "@" + XmppConfig.getInstance().getServerName());
|
||||
dynamicProperties.put(VirtualFireAlarmConstants.SUBJECT_PROPERTY_KEY, "POLICTY-REQUEST");
|
||||
dynamicProperties.put(VirtualFireAlarmConstants.MESSAGE_TYPE_PROPERTY_KEY,
|
||||
VirtualFireAlarmConstants.CHAT_PROPERTY_KEY);
|
||||
APIUtil.getOutputEventAdapterService().publish(VirtualFireAlarmConstants.XMPP_ADAPTER_NAME,
|
||||
dynamicProperties, actualMessage);
|
||||
return Response.ok().build();
|
||||
} catch (DeviceAccessAuthorizationException e) {
|
||||
log.error(e.getErrorMessage(), e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Path("device/stats/{deviceId}")
|
||||
@GET
|
||||
@Consumes("application/json")
|
||||
@ -206,7 +172,9 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService {
|
||||
public Response downloadSketch(@QueryParam("deviceName") String deviceName,
|
||||
@QueryParam("sketchType") String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
|
||||
String user = APIUtil.getAuthenticatedUser() + "@" + PrivilegedCarbonContext.getThreadLocalCarbonContext()
|
||||
.getTenantDomain();
|
||||
ZipArchive zipFile = createDownloadFile(user, deviceName, sketchType);
|
||||
Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile()));
|
||||
response.status(Response.Status.OK);
|
||||
response.type("application/zip");
|
||||
@ -276,7 +244,8 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService {
|
||||
if (apiApplicationKey == null) {
|
||||
String applicationUsername =
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration()
|
||||
.getAdminUserName();
|
||||
.getAdminUserName() + "@" + PrivilegedCarbonContext.getThreadLocalCarbonContext()
|
||||
.getTenantDomain();
|
||||
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
||||
String[] tags = {VirtualFireAlarmConstants.DEVICE_TYPE};
|
||||
apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
||||
|
||||
@ -16,7 +16,6 @@ import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorization
|
||||
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dto.SensorRecord;
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -169,18 +168,6 @@ public class APIUtil {
|
||||
return deviceAccessAuthorizationService;
|
||||
}
|
||||
|
||||
public static OutputEventAdapterService getOutputEventAdapterService() {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
OutputEventAdapterService outputEventAdapterService =
|
||||
(OutputEventAdapterService) ctx.getOSGiService(OutputEventAdapterService.class, null);
|
||||
if (outputEventAdapterService == null) {
|
||||
String msg = "Device Authorization service has not initialized.";
|
||||
log.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
return outputEventAdapterService;
|
||||
}
|
||||
|
||||
public static PlatformConfigurationManagementService getTenantConfigurationManagementService() {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
PlatformConfigurationManagementService tenantConfigurationManagementService =
|
||||
|
||||
@ -1,199 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
~
|
||||
~ WSO2 Inc. licenses this file to you under the Apache License,
|
||||
~ Version 2.0 (the "License"); you may not use this file except
|
||||
~ in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing,
|
||||
~ software distributed under the License is distributed on an
|
||||
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
~ KIND, either express or implied. See the License for the
|
||||
~ specific language governing permissions and limitations
|
||||
~ under the License.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>virtual-fire-alarm-plugin</artifactId>
|
||||
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
|
||||
<version>3.0.6-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>org.wso2.carbon.device.mgt.iot.virtualfirealarm.scep.api</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<name>WSO2 Carbon - IoT Server SCEP Server API</name>
|
||||
<description>WSO2 Carbon - Virtual FireAlarm SCEP Server API Implementation</description>
|
||||
<url>http://wso2.org</url>
|
||||
|
||||
<dependencies>
|
||||
<!-- CDM -->
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.core</artifactId>
|
||||
<scope>provided</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.axis2.wso2</groupId>
|
||||
<artifactId>axis2-client</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.certificate.mgt.core</artifactId>
|
||||
<scope>provided</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!--CXF -->
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-transports-http</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!--JAX-RS -->
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jackson</groupId>
|
||||
<artifactId>jackson-core-asl</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jackson</groupId>
|
||||
<artifactId>jackson-jaxrs</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-web-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.ws.rs</groupId>
|
||||
<artifactId>jsr311-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-httpclient.wso2</groupId>
|
||||
<artifactId>commons-httpclient</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.utils</artifactId>
|
||||
<scope>provided</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.bouncycastle.wso2</groupId>
|
||||
<artifactId>bcprov-jdk15on</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.user.api</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.queuing</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.base</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.axis2.wso2</groupId>
|
||||
<artifactId>axis2</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.igniterealtime.smack.wso2</groupId>
|
||||
<artifactId>smack</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.igniterealtime.smack.wso2</groupId>
|
||||
<artifactId>smackx</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>jaxen</groupId>
|
||||
<artifactId>jaxen</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>commons-fileupload.wso2</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.ant.wso2</groupId>
|
||||
<artifactId>ant</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.ant.wso2</groupId>
|
||||
<artifactId>ant</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>commons-httpclient.wso2</groupId>
|
||||
<artifactId>commons-httpclient</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.eclipse.equinox</groupId>
|
||||
<artifactId>javax.servlet</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.registry.api</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<source>${wso2.maven.compiler.source}</source>
|
||||
<target>${wso2.maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<configuration>
|
||||
<warName>virtual_firealarm_scep</warName>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@ -39,4 +39,4 @@ function onRequest(context) {
|
||||
+ "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token;
|
||||
}
|
||||
return {"device": device, "websocketEndpoint": websocketEndpoint};
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,6 +21,8 @@ var modalPopupContainer = modalPopup + " .modal-content";
|
||||
var modalPopupContent = modalPopup + " .modal-content";
|
||||
var body = "body";
|
||||
|
||||
var backendEndBasePath = "/api/device-mgt/v1.0";
|
||||
|
||||
/*
|
||||
* Set popup maximum height function.
|
||||
*/
|
||||
@ -132,4 +134,31 @@ function doAction(data) {
|
||||
hideAgentDownloadPopup();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function artifactUpload() {
|
||||
var contentType = "application/json";
|
||||
|
||||
var urix = backendEndBasePath + "/admin/devicetype/deploy/virtual_firealarm";
|
||||
var defaultStatusClasses = "fw fw-stack-1x";
|
||||
var content = $("#virtualfirealarm-statistic-response-template").find(".content");
|
||||
var title = content.find("#title");
|
||||
var statusIcon = content.find("#status-icon");
|
||||
var data = {}
|
||||
invokerUtil.post(urix, data, function (data) {
|
||||
title.html("Deploying statistic artifacts. Please wait...");
|
||||
statusIcon.attr("class", defaultStatusClasses + " fw-check");
|
||||
$(modalPopupContent).html(content.html());
|
||||
showPopup();
|
||||
setTimeout(function () {
|
||||
hidePopup();
|
||||
location.reload(true);
|
||||
}, 5000);
|
||||
|
||||
}, function (jqXHR) {
|
||||
title.html("Failed to deploy artifacts, Please contact administrator.");
|
||||
statusIcon.attr("class", defaultStatusClasses + " fw-error");
|
||||
$(modalPopupContent).html(content.html());
|
||||
showPopup();
|
||||
}, contentType);
|
||||
}
|
||||
@ -54,6 +54,10 @@
|
||||
</a>
|
||||
<a href="#" class="download-link btn-operations">
|
||||
<i class="fw fw-download"></i>Download Agent</a>
|
||||
{{#if displayStatus}}
|
||||
<a href="javascript:artifactUpload()" class="btn-operations"><i class="fw fw-upload fw-inverse fw-lg add-margin-1x"></i> Deploy Analytics Artifacts</a>
|
||||
{{/if}}
|
||||
|
||||
<p class="doc-link">Click <a href="https://docs.wso2.com/display/IoTS300/Virtual+Firealarm"
|
||||
target="_blank">[ here ]</a> for latest instructions and
|
||||
troubleshooting.</p>
|
||||
@ -260,6 +264,27 @@
|
||||
</center>
|
||||
</div>
|
||||
|
||||
<div id="virtualfirealarm-statistic-response-template" style="display: none">
|
||||
<div class="content">
|
||||
<div class="row">
|
||||
<div class="col-lg-5 col-md-6 col-centered">
|
||||
<h3>
|
||||
<span class="fw-stack">
|
||||
<i class="fw fw-circle-outline fw-stack-2x"></i>
|
||||
<i id="status-icon" class="fw fw-error fw-stack-1x"></i>
|
||||
</span>
|
||||
<br>
|
||||
</h3>
|
||||
<h4>
|
||||
<span id="title"></span>
|
||||
<br>
|
||||
</h4>
|
||||
<span id="description"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{#zone "topCss"}}
|
||||
{{css "css/styles.css"}}
|
||||
{{/zone}}
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
function onRequest(context){
|
||||
var viewModel = {};
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
var url = devicemgtProps["httpsURL"] + "/api/device-mgt/v1.0/admin/devicetype/deploy/virtual_firealarm/status";
|
||||
serviceInvokers.XMLHttp.get(
|
||||
url, function (responsePayload) {
|
||||
var responseContent = responsePayload.status;
|
||||
new Log().error(responseContent);
|
||||
if ("204" == responsePayload.status) {
|
||||
viewModel["displayStatus"] = "Display";
|
||||
}
|
||||
},
|
||||
function (responsePayload) {
|
||||
//do nothing.
|
||||
}
|
||||
);
|
||||
return viewModel;
|
||||
}
|
||||
@ -27,6 +27,7 @@
|
||||
<packaging>bundle</packaging>
|
||||
<name>WSO2 Carbon - App Manager WSO2 MDM REST Connector Component</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@ -51,6 +52,8 @@
|
||||
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||
<Private-Package>org.wso2.carbon.appmgt.mdm.restconnector.internal</Private-Package>
|
||||
<Import-Package>
|
||||
org.osgi.framework,
|
||||
org.osgi.service.component,
|
||||
org.wso2.carbon.appmgt.mobile.utils.*,
|
||||
org.wso2.carbon.appmgt.mobile.mdm.*,
|
||||
org.wso2.carbon.appmgt.mobile.interfaces.*,
|
||||
@ -62,7 +65,10 @@
|
||||
feign.auth,
|
||||
feign.codec,
|
||||
feign.gson,
|
||||
*;resolution:=optional
|
||||
org.json.simple.*,
|
||||
org.wso2.carbon.appmgt.mobile.beans,
|
||||
org.wso2.carbon.context,
|
||||
javax.net.ssl
|
||||
</Import-Package>
|
||||
<Export-Package>
|
||||
!org.wso2.carbon.appmgt.mdm.restconnector.internal,
|
||||
@ -70,8 +76,7 @@
|
||||
</Export-Package>
|
||||
<Embed-Dependency>
|
||||
jsr311-api,
|
||||
feign-jaxrs,
|
||||
org.wso2.carbon.device.mgt.common
|
||||
feign-jaxrs
|
||||
</Embed-Dependency>
|
||||
</instructions>
|
||||
</configuration>
|
||||
@ -80,38 +85,14 @@
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-io.wso2</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple.wso2</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.plist</groupId>
|
||||
<artifactId>dd-plist</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>org.apache.felix.scr</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.governance</groupId>
|
||||
<artifactId>org.wso2.carbon.governance.api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.appmgt</groupId>
|
||||
<artifactId>org.wso2.carbon.appmgt.mobile</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.identity.jwt.client.extension</artifactId>
|
||||
@ -136,21 +117,10 @@
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-jaxrs</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.ws.commons.axiom</groupId>
|
||||
<artifactId>axiom-api</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.ws.commons.axiom</groupId>
|
||||
<artifactId>axiom-impl</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.appmgt</groupId>
|
||||
<artifactId>org.wso2.carbon.appmgt.mobile</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
*/
|
||||
package org.wso2.carbon.appmgt.mdm.restconnector;
|
||||
|
||||
import feign.Client;
|
||||
import feign.Feign;
|
||||
import feign.gson.GsonDecoder;
|
||||
import feign.gson.GsonEncoder;
|
||||
@ -27,6 +28,7 @@ import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.OAuthRequestInterceptor;
|
||||
import org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.dto.Activity;
|
||||
import org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.dto.ApplicationManagementAdminService;
|
||||
import org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.dto.ApplicationWrapper;
|
||||
import org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.dto.DeviceManagementAdminService;
|
||||
@ -42,8 +44,15 @@ import org.wso2.carbon.appmgt.mobile.mdm.Device;
|
||||
import org.wso2.carbon.appmgt.mobile.utils.MobileApplicationException;
|
||||
import org.wso2.carbon.appmgt.mobile.utils.MobileConfigurations;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -62,12 +71,12 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
|
||||
public ApplicationOperationsImpl() {
|
||||
String authorizationConfigManagerServerURL = AuthorizationConfigurationManager.getInstance().getServerURL();
|
||||
OAuthRequestInterceptor oAuthRequestInterceptor = new OAuthRequestInterceptor();
|
||||
deviceManagementAdminService = Feign.builder()
|
||||
deviceManagementAdminService = Feign.builder().client(getSSLClient())
|
||||
.requestInterceptor(oAuthRequestInterceptor)
|
||||
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
|
||||
.target(DeviceManagementAdminService.class,
|
||||
authorizationConfigManagerServerURL + CDMF_SERVER_BASE_CONTEXT);
|
||||
applicationManagementAdminService = Feign.builder()
|
||||
applicationManagementAdminService = Feign.builder().client(getSSLClient())
|
||||
.requestInterceptor(oAuthRequestInterceptor)
|
||||
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
|
||||
.target(ApplicationManagementAdminService.class,
|
||||
@ -271,4 +280,37 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
|
||||
log.error(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private static Client getSSLClient() {
|
||||
return new Client.Default(getTrustedSSLSocketFactory(), new HostnameVerifier() {
|
||||
@Override
|
||||
public boolean verify(String s, SSLSession sslSession) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static SSLSocketFactory getTrustedSSLSocketFactory() {
|
||||
try {
|
||||
TrustManager[] trustAllCerts = new TrustManager[]{
|
||||
new X509TrustManager() {
|
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
public void checkClientTrusted(
|
||||
java.security.cert.X509Certificate[] certs, String authType) {
|
||||
}
|
||||
public void checkServerTrusted(
|
||||
java.security.cert.X509Certificate[] certs, String authType) {
|
||||
}
|
||||
}
|
||||
};
|
||||
SSLContext sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, trustAllCerts, new java.security.SecureRandom());
|
||||
return sc.getSocketFactory();
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -17,6 +17,7 @@
|
||||
*/
|
||||
package org.wso2.carbon.appmgt.mdm.restconnector.authorization.client;
|
||||
|
||||
import feign.Client;
|
||||
import feign.Feign;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
@ -33,6 +34,15 @@ import org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.dto.TokenIs
|
||||
import org.wso2.carbon.appmgt.mdm.restconnector.config.AuthorizationConfigurationManager;
|
||||
import org.wso2.carbon.appmgt.mdm.restconnector.internal.AuthorizationDataHolder;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* This is a request interceptor to add oauth token header.
|
||||
*/
|
||||
@ -54,7 +64,7 @@ public class OAuthRequestInterceptor implements RequestInterceptor {
|
||||
refreshTimeOffset = AuthorizationConfigurationManager.getInstance().getTokenRefreshTimeOffset();
|
||||
String username = AuthorizationConfigurationManager.getInstance().getUserName();
|
||||
String password = AuthorizationConfigurationManager.getInstance().getPassword();
|
||||
apiApplicationRegistrationService = Feign.builder().requestInterceptor(
|
||||
apiApplicationRegistrationService = Feign.builder().client(getSSLClient()).requestInterceptor(
|
||||
new BasicAuthRequestInterceptor(username, password))
|
||||
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
|
||||
.target(ApiApplicationRegistrationService.class,
|
||||
@ -82,7 +92,7 @@ public class OAuthRequestInterceptor implements RequestInterceptor {
|
||||
String consumerSecret = apiApplicationKey.getConsumerSecret();
|
||||
String username = AuthorizationConfigurationManager.getInstance().getUserName();
|
||||
String password = AuthorizationConfigurationManager.getInstance().getPassword();
|
||||
tokenIssuerService = Feign.builder().requestInterceptor(
|
||||
tokenIssuerService = Feign.builder().client(getSSLClient()).requestInterceptor(
|
||||
new BasicAuthRequestInterceptor(consumerKey, consumerSecret))
|
||||
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
|
||||
.target(TokenIssuerService.class, AuthorizationConfigurationManager.getInstance().getTokenApiURL());
|
||||
@ -98,4 +108,37 @@ public class OAuthRequestInterceptor implements RequestInterceptor {
|
||||
String headerValue = Constants.RestConstants.BEARER + tokenInfo.getAccess_token();
|
||||
template.header(Constants.RestConstants.AUTHORIZATION, headerValue);
|
||||
}
|
||||
|
||||
private static Client getSSLClient() {
|
||||
return new Client.Default(getTrustedSSLSocketFactory(), new HostnameVerifier() {
|
||||
@Override
|
||||
public boolean verify(String s, SSLSession sslSession) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static SSLSocketFactory getTrustedSSLSocketFactory() {
|
||||
try {
|
||||
TrustManager[] trustAllCerts = new TrustManager[]{
|
||||
new X509TrustManager() {
|
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
public void checkClientTrusted(
|
||||
java.security.cert.X509Certificate[] certs, String authType) {
|
||||
}
|
||||
public void checkServerTrusted(
|
||||
java.security.cert.X509Certificate[] certs, String authType) {
|
||||
}
|
||||
}
|
||||
};
|
||||
SSLContext sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, trustAllCerts, new java.security.SecureRandom());
|
||||
return sc.getSocketFactory();
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.dto;
|
||||
|
||||
|
||||
public class Activity {
|
||||
|
||||
public enum Type {
|
||||
CONFIG, MESSAGE, INFO, COMMAND, PROFILE, POLICY
|
||||
}
|
||||
private String activityId;
|
||||
private String code;
|
||||
private Type type;
|
||||
private String createdTimeStamp;
|
||||
|
||||
public String getActivityId() {
|
||||
return activityId;
|
||||
}
|
||||
|
||||
public void setActivityId(String activityId) {
|
||||
this.activityId = activityId;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getCreatedTimeStamp() {
|
||||
return createdTimeStamp;
|
||||
}
|
||||
|
||||
public void setCreatedTimeStamp(String createdTimeStamp) {
|
||||
this.createdTimeStamp = createdTimeStamp;
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,7 +17,6 @@
|
||||
*/
|
||||
package org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.dto;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.POST;
|
||||
|
||||
@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
/**
|
||||
* Number of Resources.
|
||||
*/
|
||||
public class BasePaginatedResult {
|
||||
@ApiModelProperty(value = "Number of total resources.", example = "2")
|
||||
@JsonProperty("count")
|
||||
private int count;
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
}
|
||||
}
|
||||
@ -17,10 +17,7 @@
|
||||
*/
|
||||
package org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.dto;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.Feature;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The DTO class of device.
|
||||
@ -37,15 +34,6 @@ public class Device implements Serializable {
|
||||
public Device() {
|
||||
}
|
||||
|
||||
public Device(String name, String type, String description, String deviceId, EnrolmentInfo enrolmentInfo,
|
||||
List<Feature> features, List<Property> properties) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.description = description;
|
||||
this.deviceIdentifier = deviceId;
|
||||
this.enrolmentInfo = enrolmentInfo;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
@ -130,22 +118,4 @@ public class Device implements Serializable {
|
||||
"]" +
|
||||
"]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof org.wso2.carbon.device.mgt.common.Device))
|
||||
return false;
|
||||
|
||||
org.wso2.carbon.device.mgt.common.Device device = (org.wso2.carbon.device.mgt.common.Device) o;
|
||||
|
||||
return getDeviceIdentifier().equals(device.getDeviceIdentifier());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getDeviceIdentifier().hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,17 +17,21 @@
|
||||
*/
|
||||
package org.wso2.carbon.appmgt.mdm.restconnector.authorization.client.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DeviceList extends BasePaginatedResult {
|
||||
public class DeviceList {
|
||||
private List<Device> devices = new ArrayList<>();
|
||||
private int count;
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
@ApiModelProperty(value = "List of devices returned")
|
||||
@JsonProperty("devices")
|
||||
public List<Device> getList() {
|
||||
return devices;
|
||||
}
|
||||
|
||||
@ -53,4 +53,4 @@ public class MDMComponent {
|
||||
log.debug("WSO2MDM MDM Component deactivated");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,6 +33,18 @@
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-scr-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>generate-scr-descriptor</id>
|
||||
<goals>
|
||||
<goal>scr</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
@ -45,12 +57,48 @@
|
||||
<instructions>
|
||||
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||
<Private-Package>
|
||||
org.wso2.carbon.device.mgt.input.adapter.extension.internal,
|
||||
org.wso2.carbon.device.mgt.input.adapter.extension.internal.*
|
||||
</Private-Package>
|
||||
<Export-Package>
|
||||
org.wso2.carbon.device.mgt.input.adapter.extension.*
|
||||
org.wso2.carbon.device.mgt.input.adapter.extension.*,
|
||||
!org.wso2.carbon.device.mgt.input.adapter.extension.internal,
|
||||
</Export-Package>
|
||||
<Import-Package>
|
||||
org.osgi.framework,
|
||||
org.osgi.service.component,
|
||||
com.jayway.jsonpath,
|
||||
org.apache.commons.logging,
|
||||
org.json.simple,
|
||||
org.json.simple.parser
|
||||
</Import-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple.wso2</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.jsonpath</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.osgi</groupId>
|
||||
<artifactId>org.eclipse.osgi</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.osgi</groupId>
|
||||
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -25,6 +25,11 @@ import java.util.Map;
|
||||
*/
|
||||
public interface ContentTransformer {
|
||||
|
||||
/**
|
||||
* This returns the type of ContentTransformer.
|
||||
*/
|
||||
String getType();
|
||||
|
||||
/**
|
||||
* This is used to transform the receiver content
|
||||
* @param message message to be format
|
||||
|
||||
@ -24,6 +24,13 @@ import java.util.Map;
|
||||
* This interface will be triggered to validate the stream content before publishing.
|
||||
*/
|
||||
public interface ContentValidator {
|
||||
|
||||
/**
|
||||
* this returns the unique name of ContentValidatorType.
|
||||
* @return
|
||||
*/
|
||||
String getType();
|
||||
|
||||
/**
|
||||
* @param dynamicParameter that message.
|
||||
* @return ContentInfo.
|
||||
|
||||
@ -0,0 +1,34 @@
|
||||
package org.wso2.carbon.device.mgt.input.adapter.extension;
|
||||
|
||||
/**
|
||||
* This hold the input adapter extension service.
|
||||
*/
|
||||
public interface InputAdapterExtensionService {
|
||||
|
||||
/**
|
||||
* return content validator for the given type.
|
||||
* @param type type of the content validator
|
||||
* @return content validator for the given type.
|
||||
*/
|
||||
ContentValidator getContentValidator(String type);
|
||||
|
||||
/**
|
||||
* return default content validator for the given type.
|
||||
* @return default content validator for the given type.
|
||||
*/
|
||||
ContentValidator getDefaultContentValidator();
|
||||
|
||||
/**
|
||||
* return content transformer for the given type.
|
||||
* @param type of the content transfomer
|
||||
* @return content transformer for the given type.
|
||||
*/
|
||||
ContentTransformer getContentTransformer(String type);
|
||||
|
||||
/**
|
||||
* return default content transformer for the given type.
|
||||
* @return default content transformer for the given type.
|
||||
*/
|
||||
ContentTransformer getDefaultContentTransformer();
|
||||
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package org.wso2.carbon.device.mgt.input.adapter.extension;
|
||||
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.internal.InputAdapterServiceDataHolder;
|
||||
|
||||
/**
|
||||
* This hold the input adapter extension service implementation.
|
||||
*/
|
||||
public class InputAdapterExtensionServiceImpl implements InputAdapterExtensionService {
|
||||
private static final String DEFAULT = "default";
|
||||
|
||||
|
||||
@Override
|
||||
public ContentValidator getContentValidator(String type) {
|
||||
return InputAdapterServiceDataHolder.getInstance().getContentValidatorMap().get(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContentValidator getDefaultContentValidator() {
|
||||
return InputAdapterServiceDataHolder.getInstance().getContentValidatorMap().get(DEFAULT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContentTransformer getContentTransformer(String type) {
|
||||
return InputAdapterServiceDataHolder.getInstance().getContentTransformerMap().get(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContentTransformer getDefaultContentTransformer() {
|
||||
return InputAdapterServiceDataHolder.getInstance().getContentTransformerMap().get(DEFAULT);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.input.adapter.extension.internal;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.osgi.service.component.ComponentContext;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentValidator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionServiceImpl;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.transformer.DefaultContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.validator.DefaultContentValidator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.validator.HTTPContentValidator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.validator.MQTTContentValidator;
|
||||
|
||||
/**
|
||||
* @scr.component name="input.adapter.extension.adapterService.component" immediate="true"
|
||||
* @scr.reference name="InputAdapterServiceComponent.service"
|
||||
* interface="org.wso2.carbon.device.mgt.input.adapter.extension.ContentValidator"
|
||||
* cardinality="0..n"
|
||||
* policy="dynamic"
|
||||
* bind="setContentValidator"
|
||||
* unbind="unsetContentValidator"
|
||||
* * @scr.reference name="InputAdapterServiceComponent.service"
|
||||
* interface="org.wso2.carbon.device.mgt.input.adapter.extension.ContentTransformer"
|
||||
* cardinality="0..n"
|
||||
* policy="dynamic"
|
||||
* bind="setContentTransformer"
|
||||
* unbind="unsetContentTransformer"
|
||||
*/
|
||||
public class InputAdapterServiceComponent {
|
||||
|
||||
private static final Log log = LogFactory.getLog(
|
||||
InputAdapterServiceComponent.class);
|
||||
|
||||
protected void activate(ComponentContext context) {
|
||||
try {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully deployed the input adapter extension service");
|
||||
}
|
||||
|
||||
InputAdapterServiceDataHolder.getInstance().addContentTransformer(new DefaultContentTransformer());
|
||||
InputAdapterServiceDataHolder.getInstance().addContentValidator(new DefaultContentValidator());
|
||||
InputAdapterServiceDataHolder.getInstance().addContentValidator(new HTTPContentValidator());
|
||||
InputAdapterServiceDataHolder.getInstance().addContentValidator(new MQTTContentValidator());
|
||||
|
||||
context.getBundleContext().registerService(InputAdapterExtensionService.class,
|
||||
new InputAdapterExtensionServiceImpl(), null);
|
||||
} catch (RuntimeException e) {
|
||||
log.error("Can not create the input adapter service ", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void setContentValidator(ContentValidator contentValidator) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Setting ContentValidator Service");
|
||||
}
|
||||
InputAdapterServiceDataHolder.getInstance().addContentValidator(contentValidator);
|
||||
}
|
||||
|
||||
protected void unsetContentValidator(ContentValidator contentValidator) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Un-setting ContentValidator Service");
|
||||
}
|
||||
}
|
||||
|
||||
protected void setContentTransformer(ContentTransformer contentTransformer) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Setting contentTransformer Service");
|
||||
}
|
||||
InputAdapterServiceDataHolder.getInstance().addContentTransformer(contentTransformer);
|
||||
}
|
||||
|
||||
protected void unsetContentValidator(ContentTransformer contentTransformer) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Un-setting ContentTransformer Service");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy
|
||||
* of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.input.adapter.extension.internal;
|
||||
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentValidator;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* common place to hold some OSGI service references.
|
||||
*/
|
||||
public class InputAdapterServiceDataHolder {
|
||||
|
||||
private static InputAdapterServiceDataHolder inputAdapterServiceDataHolder = new InputAdapterServiceDataHolder();
|
||||
private static Map<String, ContentValidator> contentValidatorMap = new HashMap<>();
|
||||
private static Map<String, ContentTransformer> contentTransformerMap = new HashMap<>();
|
||||
|
||||
private InputAdapterServiceDataHolder() {
|
||||
}
|
||||
|
||||
public static InputAdapterServiceDataHolder getInstance() {
|
||||
return inputAdapterServiceDataHolder;
|
||||
}
|
||||
|
||||
public Map<String, ContentValidator> getContentValidatorMap() {
|
||||
return contentValidatorMap;
|
||||
}
|
||||
|
||||
public void addContentValidator(ContentValidator contentValidator) {
|
||||
InputAdapterServiceDataHolder.contentValidatorMap.put(contentValidator.getType(), contentValidator);
|
||||
}
|
||||
|
||||
public Map<String, ContentTransformer> getContentTransformerMap() {
|
||||
return contentTransformerMap;
|
||||
}
|
||||
|
||||
public void addContentTransformer(ContentTransformer contentTransformer) {
|
||||
InputAdapterServiceDataHolder.contentTransformerMap.put(contentTransformer.getType(), contentTransformer);
|
||||
}
|
||||
}
|
||||
@ -16,14 +16,22 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.extension;
|
||||
package org.wso2.carbon.device.mgt.input.adapter.extension.transformer;
|
||||
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentTransformer;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This holds the default implementation of ContentTransformer
|
||||
*/
|
||||
public class DefaultContentTransformer implements ContentTransformer{
|
||||
public class DefaultContentTransformer implements ContentTransformer {
|
||||
private static final String DEFAULT_CONTENT_VALIDATOR = "default";
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return DEFAULT_CONTENT_VALIDATOR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object transform(Object message, Map<String, Object> dynamicProperties) {
|
||||
@ -16,7 +16,10 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.extension;
|
||||
package org.wso2.carbon.device.mgt.input.adapter.extension.validator;
|
||||
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentInfo;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentValidator;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@ -24,6 +27,12 @@ import java.util.Map;
|
||||
* This holds the default implementation of content validator interface.
|
||||
*/
|
||||
public class DefaultContentValidator implements ContentValidator {
|
||||
private static final String DEFAULT_TYPE = "default";
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return DEFAULT_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContentInfo validate(Object message, Map<String, Object> dynamicParams) {
|
||||
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.extension.validator;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentInfo;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentValidator;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class HTTPContentValidator implements ContentValidator {
|
||||
private static final Log log = LogFactory.getLog(HTTPContentValidator.class);
|
||||
private static String JSON_ARRAY_START_CHAR = "[";
|
||||
private static String CDMF_SCOPE_PREFIX = "cdmf";
|
||||
private static String CDMF_SCOPE_SEPERATOR = "/";
|
||||
private static String CDMF_HTTP_CONTENT_VALIDATOR = "iot-http";
|
||||
public static final String DEVICE_ID_JSON_PATH = "event.metaData.deviceId";
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return CDMF_HTTP_CONTENT_VALIDATOR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContentInfo validate(Object msgPayload, Map<String, Object> dynamicParams) {
|
||||
String deviceId = (String) dynamicParams.get("deviceId");
|
||||
String msg = (String) msgPayload;
|
||||
String deviceIdJsonPath = DEVICE_ID_JSON_PATH;
|
||||
boolean status;
|
||||
if (msg.startsWith(JSON_ARRAY_START_CHAR)) {
|
||||
status = processMultipleEvents(msg, deviceId, deviceIdJsonPath);
|
||||
} else {
|
||||
status = processSingleEvent(msg, deviceId, deviceIdJsonPath);
|
||||
}
|
||||
return new ContentInfo(status, msg);
|
||||
}
|
||||
|
||||
private boolean processSingleEvent(String msg, String deviceIdFromTopic, String deviceIdJsonPath) {
|
||||
Object res = JsonPath.read(msg, deviceIdJsonPath);
|
||||
String deviceIdFromContent = (res != null) ? res.toString() : "";
|
||||
if (deviceIdFromContent.equals(deviceIdFromTopic)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean processMultipleEvents(String msg, String deviceIdFromTopic, String deviceIdJsonPath) {
|
||||
try {
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
JSONArray jsonArray = (JSONArray) jsonParser.parse(msg);
|
||||
boolean status = false;
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
status = processSingleEvent(jsonArray.get(i).toString(), deviceIdFromTopic, deviceIdJsonPath);
|
||||
if (!status) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
} catch (ParseException e) {
|
||||
log.error("Invalid input " + msg, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -16,7 +16,7 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.mqtt.util;
|
||||
package org.wso2.carbon.device.mgt.input.adapter.extension.validator;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import org.apache.commons.logging.Log;
|
||||
@ -32,13 +32,23 @@ import java.util.Map;
|
||||
public class MQTTContentValidator implements ContentValidator {
|
||||
private static final String JSON_ARRAY_START_CHAR = "[";
|
||||
private static final Log log = LogFactory.getLog(MQTTContentValidator.class);
|
||||
private static final String CDMF_MQTT_CONTENT_VALIDATOR = "iot-mqtt";
|
||||
public static final String DEVICE_ID_JSON_PATH = "event.metaData.deviceId";
|
||||
public static final String DEVICE_TYPE_JSON_PATH = "event.metaData.deviceId";
|
||||
public static final String TOPIC = "topic";
|
||||
public static final int DEVICE_ID_TOPIC_HIERARCHY_INDEX = 2;
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContentInfo validate(Object msgPayload, Map<String, Object> dynamicParams) {
|
||||
String topic = (String) dynamicParams.get(MQTTEventAdapterConstants.TOPIC);
|
||||
String topic = (String) dynamicParams.get(TOPIC);
|
||||
String topics[] = topic.split("/");
|
||||
String deviceIdJsonPath = MQTTEventAdapterConstants.DEVICE_ID_JSON_PATH;
|
||||
int deviceIdInTopicHierarchyLevelIndex = MQTTEventAdapterConstants.DEVICE_ID_TOPIC_HIERARCHY_INDEX;
|
||||
String deviceIdJsonPath = DEVICE_ID_JSON_PATH;
|
||||
int deviceIdInTopicHierarchyLevelIndex = DEVICE_ID_TOPIC_HIERARCHY_INDEX;
|
||||
String deviceIdFromTopic = topics[deviceIdInTopicHierarchyLevelIndex];
|
||||
boolean status;
|
||||
String message = (String) msgPayload;
|
||||
@ -76,6 +76,22 @@
|
||||
<groupId>commons-pool.wso2</groupId>
|
||||
<artifactId>commons-pool</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-jaxrs</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-gson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.ws.rs</groupId>
|
||||
<artifactId>jsr311-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -117,7 +133,6 @@
|
||||
org.wso2.carbon.event.input.adapter.core,
|
||||
org.wso2.carbon.event.input.adapter.core.*,
|
||||
javax.xml.namespace; version=0.0.0,
|
||||
com.jayway.jsonpath.*,
|
||||
com.nimbusds.jose,
|
||||
com.nimbusds.jose.crypto,
|
||||
com.nimbusds.jwt,
|
||||
@ -130,9 +145,28 @@
|
||||
org.wso2.carbon.user.core.service,
|
||||
org.wso2.carbon.user.core.tenant,
|
||||
org.apache.commons.pool,
|
||||
org.apache.commons.pool.impl
|
||||
org.apache.commons.pool.impl,
|
||||
feign,
|
||||
feign.auth,
|
||||
feign.codec,
|
||||
feign.gson,
|
||||
org.wso2.carbon.device.mgt.input.adapter.extension,
|
||||
org.apache.axiom.util.base64,
|
||||
org.apache.axis2.*,
|
||||
org.apache.commons.httpclient.*,
|
||||
org.apache.commons.logging,
|
||||
org.apache.log4j,
|
||||
org.wso2.carbon.context,
|
||||
org.wso2.carbon.core.util,
|
||||
org.wso2.carbon.identity.oauth2.*,
|
||||
org.wso2.carbon.utils,
|
||||
org.wso2.carbon.utils.multitenancy,
|
||||
javax.net.ssl
|
||||
</Import-Package>
|
||||
<DynamicImport-Package>*</DynamicImport-Package>
|
||||
<Embed-Dependency>
|
||||
jsr311-api,
|
||||
feign-jaxrs
|
||||
</Embed-Dependency>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
@ -76,12 +76,12 @@ public class HTTPEventAdapterFactory extends InputEventAdapterFactory {
|
||||
propertyList.add(exposedTransportsProperty);
|
||||
|
||||
//Content Validator details
|
||||
Property contentValidator = new Property(HTTPEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME);
|
||||
Property contentValidator = new Property(HTTPEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE);
|
||||
contentValidator.setDisplayName(
|
||||
resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME));
|
||||
resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE));
|
||||
contentValidator.setRequired(false);
|
||||
contentValidator.setHint(
|
||||
resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME_HINT));
|
||||
resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE_HINT));
|
||||
contentValidator.setDefaultValue(HTTPEventAdapterConstants.DEFAULT);
|
||||
propertyList.add(contentValidator);
|
||||
|
||||
|
||||
@ -18,11 +18,13 @@ package org.wso2.carbon.device.mgt.input.adapter.http;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.DeviceAuthorizer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.internal.InputAdapterServiceDataHolder;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.oauth.OAuthAuthenticator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentInfo;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.DefaultContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.DefaultContentValidator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.transformer.DefaultContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.validator.DefaultContentValidator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.exception.HTTPContentInitializationException;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.jwt.JWTAuthenticator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.util.AuthenticationInfo;
|
||||
@ -58,6 +60,7 @@ public class HTTPMessageServlet extends HttpServlet {
|
||||
private String exposedTransports;
|
||||
private static JWTAuthenticator jwtAuthenticator;
|
||||
private static OAuthAuthenticator oAuthAuthenticator;
|
||||
private static DeviceAuthorizer deviceAuthorizer;
|
||||
|
||||
public HTTPMessageServlet(InputEventAdapterListener eventAdaptorListener, int tenantId,
|
||||
InputEventAdapterConfiguration eventAdapterConfiguration,
|
||||
@ -67,48 +70,29 @@ public class HTTPMessageServlet extends HttpServlet {
|
||||
this.exposedTransports = eventAdapterConfiguration.getProperties().get(
|
||||
HTTPEventAdapterConstants.EXPOSED_TRANSPORTS);
|
||||
|
||||
String className = eventAdapterConfiguration.getProperties().get(
|
||||
HTTPEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME);
|
||||
if (HTTPEventAdapterConstants.DEFAULT.equals(className)) {
|
||||
contentValidator = new DefaultContentValidator();
|
||||
String contentValidatorType = eventAdapterConfiguration.getProperties().get(
|
||||
HTTPEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE);
|
||||
if (contentValidatorType == null || HTTPEventAdapterConstants.DEFAULT.equals(contentValidatorType)) {
|
||||
contentValidator = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getDefaultContentValidator();
|
||||
} else {
|
||||
try {
|
||||
Class<? extends ContentValidator> contentValidatorClass = Class.forName(className)
|
||||
.asSubclass(ContentValidator.class);
|
||||
contentValidator = contentValidatorClass.newInstance();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new HTTPContentInitializationException(
|
||||
"Unable to find the class validator: " + className, e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new HTTPContentInitializationException(
|
||||
"Unable to create an instance of :" + className, e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new HTTPContentInitializationException("Access of the instance in not allowed.", e);
|
||||
}
|
||||
contentValidator = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getContentValidator(contentValidatorType);
|
||||
}
|
||||
|
||||
String contentTransformerClassName = eventAdapterConfiguration.getProperties().get(
|
||||
HTTPEventAdapterConstants.ADAPTER_CONF_CONTENT_TRANSFORMER_CLASSNAME);
|
||||
if (contentTransformerClassName != null && contentTransformerClassName.equals(HTTPEventAdapterConstants.DEFAULT)) {
|
||||
contentTransformer = new DefaultContentTransformer();
|
||||
} else if (contentTransformerClassName != null && !contentTransformerClassName.isEmpty()) {
|
||||
try {
|
||||
Class<? extends ContentTransformer> contentTransformerClass = Class.forName(contentTransformerClassName)
|
||||
.asSubclass(ContentTransformer.class);
|
||||
contentTransformer = contentTransformerClass.newInstance();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new HTTPContentInitializationException(
|
||||
"Unable to find the class transformer: " + contentTransformerClassName, e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new HTTPContentInitializationException(
|
||||
"Unable to create an instance of :" + contentTransformerClassName, e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new HTTPContentInitializationException("Access of the instance in not allowed.", e);
|
||||
}
|
||||
if (contentTransformerClassName == null || contentTransformerClassName.equals(HTTPEventAdapterConstants.DEFAULT)) {
|
||||
contentTransformer = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getDefaultContentTransformer();
|
||||
} else {
|
||||
contentTransformer = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getContentTransformer(contentValidatorType);
|
||||
}
|
||||
|
||||
jwtAuthenticator = new JWTAuthenticator();
|
||||
oAuthAuthenticator = new OAuthAuthenticator(globalProperties);
|
||||
deviceAuthorizer = new DeviceAuthorizer(globalProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -177,14 +161,24 @@ public class HTTPMessageServlet extends HttpServlet {
|
||||
paramMap.put(HTTPEventAdapterConstants.USERNAME_TAG, authenticationInfo.getUsername());
|
||||
paramMap.put(HTTPEventAdapterConstants.TENANT_DOMAIN_TAG, authenticationInfo.getTenantDomain());
|
||||
paramMap.put(HTTPEventAdapterConstants.SCOPE_TAG, authenticationInfo.getScopes());
|
||||
if (contentValidator != null && contentTransformer != null) {
|
||||
data = (String) contentTransformer.transform(data, paramMap);
|
||||
ContentInfo contentInfo = contentValidator.validate(data, paramMap);
|
||||
if (contentInfo != null && contentInfo.isValidContent()) {
|
||||
HTTPEventAdapter.executorService.submit(new HTTPRequestProcessor(eventAdaptorListener,
|
||||
(String) contentInfo.getMessage(), tenantId));
|
||||
}
|
||||
}
|
||||
String deviceId = (String) paramMap.get("deviceId");
|
||||
String deviceType = (String) paramMap.get("deviceType");
|
||||
if (deviceAuthorizer.isAuthorized(authenticationInfo, deviceId, deviceType)) {
|
||||
if (contentValidator != null && contentTransformer != null) {
|
||||
data = (String) contentTransformer.transform(data, paramMap);
|
||||
ContentInfo contentInfo = contentValidator.validate(data, paramMap);
|
||||
if (contentInfo != null && contentInfo.isValidContent()) {
|
||||
HTTPEventAdapter.executorService.submit(new HTTPRequestProcessor(eventAdaptorListener,
|
||||
(String) contentInfo
|
||||
.getMessage(),
|
||||
tenantId));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Unauthorized device with device id" + deviceId + " and device type" + deviceType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* you may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization;
|
||||
|
||||
import feign.Client;
|
||||
import feign.Feign;
|
||||
import feign.FeignException;
|
||||
import feign.gson.GsonDecoder;
|
||||
import feign.gson.GsonEncoder;
|
||||
import feign.jaxrs.JAXRSContract;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.OAuthRequestInterceptor;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto.AuthorizationRequest;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto.DeviceAccessAuthorizationAdminService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto.DeviceAuthorizationResult;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.util.AuthenticationInfo;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.util.PropertyUtils;
|
||||
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterException;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This authorizer crossvalidates the request with device id and device type.
|
||||
*/
|
||||
public class DeviceAuthorizer {
|
||||
|
||||
private static DeviceAccessAuthorizationAdminService deviceAccessAuthorizationAdminService;
|
||||
private static final String CDMF_SERVER_BASE_CONTEXT = "/api/device-mgt/v1.0";
|
||||
private static final String DEVICE_MGT_SERVER_URL = "deviceMgtServerUrl";
|
||||
private static Log logger = LogFactory.getLog(DeviceAuthorizer.class);
|
||||
|
||||
public DeviceAuthorizer(Map<String, String> globalProperties) {
|
||||
try {
|
||||
deviceAccessAuthorizationAdminService = Feign.builder().client(getSSLClient())
|
||||
.requestInterceptor(new OAuthRequestInterceptor(globalProperties))
|
||||
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
|
||||
.target(DeviceAccessAuthorizationAdminService.class, getDeviceMgtServerUrl(globalProperties)
|
||||
+ CDMF_SERVER_BASE_CONTEXT);
|
||||
} catch (InputEventAdapterException e) {
|
||||
logger.error("Invalid value for deviceMgtServerUrl in globalProperties.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isAuthorized(AuthenticationInfo authenticationInfo, String deviceId, String deviceType) {
|
||||
|
||||
if (deviceId != null && !deviceId.isEmpty() && deviceType != null && !deviceType.isEmpty()) {
|
||||
|
||||
AuthorizationRequest authorizationRequest = new AuthorizationRequest();
|
||||
authorizationRequest.setTenantDomain(authenticationInfo.getTenantDomain());
|
||||
authorizationRequest.setUsername(authenticationInfo.getUsername());
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(deviceType);
|
||||
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
|
||||
deviceIdentifiers.add(deviceIdentifier);
|
||||
authorizationRequest.setDeviceIdentifiers(deviceIdentifiers);
|
||||
try {
|
||||
DeviceAuthorizationResult deviceAuthorizationResult =
|
||||
deviceAccessAuthorizationAdminService.isAuthorized(authorizationRequest);
|
||||
List<DeviceIdentifier> devices = deviceAuthorizationResult.getAuthorizedDevices();
|
||||
if (devices != null && devices.size() > 0) {
|
||||
DeviceIdentifier authorizedDevice = devices.get(0);
|
||||
if (authorizedDevice.getId().equals(deviceId) && authorizedDevice.getType().equals(deviceType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (FeignException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getDeviceMgtServerUrl(Map<String, String> properties) throws InputEventAdapterException {
|
||||
String deviceMgtServerUrl = PropertyUtils.replaceProperty(properties.get(DEVICE_MGT_SERVER_URL));
|
||||
if (deviceMgtServerUrl == null || deviceMgtServerUrl.isEmpty()) {
|
||||
logger.error("deviceMgtServerUrl can't be empty ");
|
||||
}
|
||||
return deviceMgtServerUrl;
|
||||
}
|
||||
|
||||
private static Client getSSLClient() {
|
||||
return new Client.Default(getTrustedSSLSocketFactory(), new HostnameVerifier() {
|
||||
@Override
|
||||
public boolean verify(String s, SSLSession sslSession) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static SSLSocketFactory getTrustedSSLSocketFactory() {
|
||||
try {
|
||||
TrustManager[] trustAllCerts = new TrustManager[]{
|
||||
new X509TrustManager() {
|
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
public void checkClientTrusted(
|
||||
java.security.cert.X509Certificate[] certs, String authType) {
|
||||
}
|
||||
public void checkServerTrusted(
|
||||
java.security.cert.X509Certificate[] certs, String authType) {
|
||||
}
|
||||
}
|
||||
};
|
||||
SSLContext sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, trustAllCerts, new java.security.SecureRandom());
|
||||
return sc.getSocketFactory();
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,203 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client;
|
||||
|
||||
import feign.Client;
|
||||
import feign.Feign;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import feign.auth.BasicAuthRequestInterceptor;
|
||||
import feign.gson.GsonDecoder;
|
||||
import feign.gson.GsonEncoder;
|
||||
import feign.jaxrs.JAXRSContract;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto.AccessTokenInfo;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto.ApiApplicationKey;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto.ApiApplicationRegistrationService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto.ApiRegistrationProfile;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto.TokenIssuerService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.util.PropertyUtils;
|
||||
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterException;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This is a request interceptor to add oauth token header.
|
||||
*/
|
||||
public class OAuthRequestInterceptor implements RequestInterceptor {
|
||||
|
||||
private AccessTokenInfo tokenInfo;
|
||||
private long refreshTimeOffset;
|
||||
private static final String API_APPLICATION_REGISTRATION_CONTEXT = "/api-application-registration";
|
||||
private static final String DEVICE_MANAGEMENT_SERVICE_TAG[] = {"device_management"};
|
||||
private static final String APPLICATION_NAME = "websocket-app";
|
||||
private static final String PASSWORD_GRANT_TYPE = "password";
|
||||
private static final String REFRESH_GRANT_TYPE = "refresh_token";
|
||||
private static final String REQUIRED_SCOPE = "perm:authorization:verify";
|
||||
private ApiApplicationRegistrationService apiApplicationRegistrationService;
|
||||
private TokenIssuerService tokenIssuerService;
|
||||
|
||||
private static Log logger = LogFactory.getLog(OAuthRequestInterceptor.class);
|
||||
|
||||
private static final String CONNECTION_USERNAME = "username";
|
||||
private static final String CONNECTION_PASSWORD = "password";
|
||||
private static final String TOKEN_REFRESH_TIME_OFFSET = "tokenRefreshTimeOffset";
|
||||
private static final String TOKEN_SCOPES = "scopes";
|
||||
private static final String DEVICE_MGT_SERVER_URL = "deviceMgtServerUrl";
|
||||
private static final String TOKEN_ENDPOINT_CONTEXT = "tokenUrl";
|
||||
private static String username;
|
||||
private static String password;
|
||||
private static String tokenEndpoint;
|
||||
private static String deviceMgtServerUrl;
|
||||
private static String scopes;
|
||||
private static Map<String, String> globalProperties;
|
||||
|
||||
|
||||
/**
|
||||
* Creates an interceptor that authenticates all requests.
|
||||
*/
|
||||
public OAuthRequestInterceptor(Map<String, String> globalProperties) {
|
||||
this.globalProperties = globalProperties;
|
||||
try {
|
||||
deviceMgtServerUrl = getDeviceMgtServerUrl(globalProperties);
|
||||
refreshTimeOffset = getRefreshTimeOffset(globalProperties) * 1000;
|
||||
username = getUsername(globalProperties);
|
||||
password = getPassword(globalProperties);
|
||||
tokenEndpoint = getTokenEndpoint(globalProperties);
|
||||
apiApplicationRegistrationService = Feign.builder().client(getSSLClient()).requestInterceptor(
|
||||
new BasicAuthRequestInterceptor(username, password))
|
||||
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
|
||||
.target(ApiApplicationRegistrationService.class,
|
||||
deviceMgtServerUrl + API_APPLICATION_REGISTRATION_CONTEXT);
|
||||
} catch (InputEventAdapterException e) {
|
||||
logger.error("Invalid url: deviceMgtServerUrl" + deviceMgtServerUrl + " or tokenEndpoint:" + tokenEndpoint,
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
if (tokenInfo == null) {
|
||||
//had to do on demand initialization due to start up error.
|
||||
ApiRegistrationProfile apiRegistrationProfile = new ApiRegistrationProfile();
|
||||
apiRegistrationProfile.setApplicationName(APPLICATION_NAME);
|
||||
apiRegistrationProfile.setIsAllowedToAllDomains(false);
|
||||
apiRegistrationProfile.setIsMappingAnExistingOAuthApp(false);
|
||||
apiRegistrationProfile.setTags(DEVICE_MANAGEMENT_SERVICE_TAG);
|
||||
ApiApplicationKey apiApplicationKey = apiApplicationRegistrationService.register(apiRegistrationProfile);
|
||||
String consumerKey = apiApplicationKey.getConsumerKey();
|
||||
String consumerSecret = apiApplicationKey.getConsumerSecret();
|
||||
tokenIssuerService = Feign.builder().client(getSSLClient()).requestInterceptor(
|
||||
new BasicAuthRequestInterceptor(consumerKey, consumerSecret))
|
||||
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
|
||||
.target(TokenIssuerService.class, tokenEndpoint);
|
||||
tokenInfo = tokenIssuerService.getToken(PASSWORD_GRANT_TYPE, username, password, REQUIRED_SCOPE);
|
||||
tokenInfo.setExpires_in(System.currentTimeMillis() + (tokenInfo.getExpires_in() * 1000));
|
||||
}
|
||||
synchronized(this) {
|
||||
if (System.currentTimeMillis() + refreshTimeOffset > tokenInfo.getExpires_in()) {
|
||||
tokenInfo = tokenIssuerService.getToken(REFRESH_GRANT_TYPE, tokenInfo.getRefresh_token());
|
||||
tokenInfo.setExpires_in(System.currentTimeMillis() + tokenInfo.getExpires_in());
|
||||
}
|
||||
}
|
||||
String headerValue = "Bearer " + tokenInfo.getAccess_token();
|
||||
template.header("Authorization", headerValue);
|
||||
}
|
||||
|
||||
private String getUsername(Map<String, String> globalProperties) {
|
||||
String username = globalProperties.get(CONNECTION_USERNAME);
|
||||
if (username == null || username.isEmpty()) {
|
||||
logger.error("username can't be empty ");
|
||||
}
|
||||
return username;
|
||||
}
|
||||
|
||||
private String getPassword(Map<String, String> globalProperties) {
|
||||
String password = globalProperties.get(CONNECTION_PASSWORD);;
|
||||
if (password == null || password.isEmpty()) {
|
||||
logger.error("password can't be empty ");
|
||||
}
|
||||
return password;
|
||||
}
|
||||
|
||||
private String getDeviceMgtServerUrl(Map<String, String> globalProperties) throws InputEventAdapterException {
|
||||
String deviceMgtServerUrl = globalProperties.get(DEVICE_MGT_SERVER_URL);
|
||||
if (deviceMgtServerUrl == null || deviceMgtServerUrl.isEmpty()) {
|
||||
logger.error("deviceMgtServerUrl can't be empty ");
|
||||
}
|
||||
return PropertyUtils.replaceProperty(deviceMgtServerUrl);
|
||||
}
|
||||
|
||||
private String getTokenEndpoint(Map<String, String> globalProperties) throws InputEventAdapterException {
|
||||
String tokenEndpoint = globalProperties.get(TOKEN_ENDPOINT_CONTEXT);
|
||||
if ( tokenEndpoint.isEmpty()) {
|
||||
logger.error("tokenEndpoint can't be empty ");
|
||||
}
|
||||
return PropertyUtils.replaceProperty(tokenEndpoint);
|
||||
}
|
||||
|
||||
private long getRefreshTimeOffset(Map<String, String> globalProperties) {
|
||||
long refreshTimeOffset = 100;
|
||||
try {
|
||||
refreshTimeOffset = Long.parseLong(globalProperties.get(TOKEN_REFRESH_TIME_OFFSET));
|
||||
} catch (NumberFormatException e) {
|
||||
logger.error("refreshTimeOffset should be a number", e);
|
||||
}
|
||||
return refreshTimeOffset;
|
||||
}
|
||||
|
||||
private static Client getSSLClient() {
|
||||
return new Client.Default(getTrustedSSLSocketFactory(), new HostnameVerifier() {
|
||||
@Override
|
||||
public boolean verify(String s, SSLSession sslSession) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static SSLSocketFactory getTrustedSSLSocketFactory() {
|
||||
try {
|
||||
TrustManager[] trustAllCerts = new TrustManager[]{
|
||||
new X509TrustManager() {
|
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
public void checkClientTrusted(
|
||||
java.security.cert.X509Certificate[] certs, String authType) {
|
||||
}
|
||||
public void checkServerTrusted(
|
||||
java.security.cert.X509Certificate[] certs, String authType) {
|
||||
}
|
||||
}
|
||||
};
|
||||
SSLContext sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, trustAllCerts, new java.security.SecureRandom());
|
||||
return sc.getSocketFactory();
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
/**
|
||||
* This hold access token info that returned from the api call
|
||||
*/
|
||||
public class AccessTokenInfo {
|
||||
public String token_type;
|
||||
public long expires_in;
|
||||
public String refresh_token;
|
||||
public String access_token;
|
||||
|
||||
public String getToken_type() {
|
||||
return token_type;
|
||||
}
|
||||
|
||||
public void setToken_type(String token_type) {
|
||||
this.token_type = token_type;
|
||||
}
|
||||
|
||||
public long getExpires_in() {
|
||||
return expires_in;
|
||||
}
|
||||
|
||||
public void setExpires_in(long expires_in) {
|
||||
this.expires_in = expires_in;
|
||||
}
|
||||
|
||||
public String getRefresh_token() {
|
||||
return refresh_token;
|
||||
}
|
||||
|
||||
public void setRefresh_token(String refresh_token) {
|
||||
this.refresh_token = refresh_token;
|
||||
}
|
||||
|
||||
public String getAccess_token() {
|
||||
return access_token;
|
||||
}
|
||||
|
||||
public void setAccess_token(String access_token) {
|
||||
this.access_token = access_token;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
/**
|
||||
* This holds api application consumer key and secret.
|
||||
*/
|
||||
public class ApiApplicationKey {
|
||||
private String client_id;
|
||||
private String client_secret;
|
||||
|
||||
public String getConsumerKey() {
|
||||
return this.client_id;
|
||||
}
|
||||
|
||||
public void setClient_id(String consumerKey) {
|
||||
this.client_id = consumerKey;
|
||||
}
|
||||
|
||||
public String getConsumerSecret() {
|
||||
return this.client_secret;
|
||||
}
|
||||
|
||||
public void setClient_secret(String consumerSecret) {
|
||||
this.client_secret = consumerSecret;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
/**
|
||||
* This is the application registration service that exposed for apimApplicationRegistration
|
||||
*/
|
||||
|
||||
@Path("/register")
|
||||
public interface ApiApplicationRegistrationService {
|
||||
|
||||
/**
|
||||
* This method is used to register api application
|
||||
*
|
||||
* @param registrationProfile contains the necessary attributes that are needed in order to register an app.
|
||||
*/
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
ApiApplicationKey register(ApiRegistrationProfile registrationProfile);
|
||||
}
|
||||
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
|
||||
/**
|
||||
* This class represents the data that are required to register
|
||||
* the oauth application.
|
||||
*/
|
||||
public class ApiRegistrationProfile {
|
||||
|
||||
public String applicationName;
|
||||
public String tags[];
|
||||
public boolean isAllowedToAllDomains;
|
||||
public String consumerKey;
|
||||
public String consumerSecret;
|
||||
public boolean isMappingAnExistingOAuthApp;
|
||||
|
||||
public String getApplicationName() {
|
||||
return applicationName;
|
||||
}
|
||||
|
||||
public void setApplicationName(String applicationName) {
|
||||
this.applicationName = applicationName;
|
||||
}
|
||||
|
||||
public String[] getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(String[] tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public boolean isAllowedToAllDomains() {
|
||||
return isAllowedToAllDomains;
|
||||
}
|
||||
|
||||
public void setIsAllowedToAllDomains(boolean isAllowedToAllDomains) {
|
||||
this.isAllowedToAllDomains = isAllowedToAllDomains;
|
||||
}
|
||||
|
||||
public boolean isMappingAnExistingOAuthApp() {
|
||||
return isMappingAnExistingOAuthApp;
|
||||
}
|
||||
|
||||
public void setIsMappingAnExistingOAuthApp(boolean isMappingAnExistingOAuthApp) {
|
||||
this.isMappingAnExistingOAuthApp = isMappingAnExistingOAuthApp;
|
||||
}
|
||||
|
||||
public String getConsumerKey() {
|
||||
return consumerKey;
|
||||
}
|
||||
|
||||
public void setConsumerKey(String consumerKey) {
|
||||
this.consumerKey = consumerKey;
|
||||
}
|
||||
|
||||
public String getConsumerSecret() {
|
||||
return consumerSecret;
|
||||
}
|
||||
|
||||
public void setConsumerSecret(String consumerSecret) {
|
||||
this.consumerSecret = consumerSecret;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* DTO of the authorization request
|
||||
*/
|
||||
public class AuthorizationRequest {
|
||||
|
||||
String tenantDomain;
|
||||
String username;
|
||||
List<DeviceIdentifier> deviceIdentifiers;
|
||||
List<String> permissions;
|
||||
|
||||
public String getTenantDomain() {
|
||||
return tenantDomain;
|
||||
}
|
||||
|
||||
public void setTenantDomain(String tenantDomain) {
|
||||
this.tenantDomain = tenantDomain;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public List<DeviceIdentifier> getDeviceIdentifiers() {
|
||||
return deviceIdentifiers;
|
||||
}
|
||||
|
||||
public void setDeviceIdentifiers(List<DeviceIdentifier> deviceIdentifiers) {
|
||||
this.deviceIdentifiers = deviceIdentifiers;
|
||||
}
|
||||
|
||||
public List<String> getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public void setPermissions(List<String> permissions) {
|
||||
this.permissions = permissions;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Path("/admin/authorization")
|
||||
/**
|
||||
* This interface provided the definition of the device - user access verification service.
|
||||
*/
|
||||
public interface DeviceAccessAuthorizationAdminService {
|
||||
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
DeviceAuthorizationResult isAuthorized(AuthorizationRequest authorizationRequest);
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* you may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents a DeviceAuthorizationResult including a list of authorized devices and a list of unauthorized devices.
|
||||
*/
|
||||
public class DeviceAuthorizationResult {
|
||||
|
||||
private List<DeviceIdentifier> authorizedDevices = new ArrayList<>();
|
||||
private List<DeviceIdentifier> unauthorizedDevices = new ArrayList<>();
|
||||
|
||||
public List<DeviceIdentifier> getAuthorizedDevices() {
|
||||
return authorizedDevices;
|
||||
}
|
||||
|
||||
public void setAuthorizedDevices(List<DeviceIdentifier> authorizedDevices) {
|
||||
this.authorizedDevices = authorizedDevices;
|
||||
}
|
||||
|
||||
public void setUnauthorizedDevices(
|
||||
List<DeviceIdentifier> unauthorizedDevices) {
|
||||
this.unauthorizedDevices = unauthorizedDevices;
|
||||
}
|
||||
|
||||
public void addAuthorizedDevice(DeviceIdentifier deviceIdentifier) {
|
||||
authorizedDevices.add(deviceIdentifier);
|
||||
}
|
||||
|
||||
public List<DeviceIdentifier> getUnauthorizedDevices() {
|
||||
return unauthorizedDevices;
|
||||
}
|
||||
|
||||
public void addUnauthorizedDevice(DeviceIdentifier deviceIdentifier) {
|
||||
unauthorizedDevices.add(deviceIdentifier);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* you may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* DTO of the device identifier
|
||||
*/
|
||||
public class DeviceIdentifier implements Serializable{
|
||||
|
||||
private String id;
|
||||
private String type;
|
||||
|
||||
public DeviceIdentifier() {}
|
||||
|
||||
public DeviceIdentifier(String id, String type) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type.toLowerCase();
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
/**
|
||||
* This class represents an OAuth application populated with necessary data.
|
||||
*/
|
||||
public class OAuthApplicationInfo {
|
||||
|
||||
public String client_id;
|
||||
public String client_name;
|
||||
public String callback_url;
|
||||
public String client_secret;
|
||||
|
||||
public String getClient_id() {
|
||||
return client_id;
|
||||
}
|
||||
|
||||
public void setClient_id(String client_id) {
|
||||
this.client_id = client_id;
|
||||
}
|
||||
|
||||
public String getClient_name() {
|
||||
return client_name;
|
||||
}
|
||||
|
||||
public void setClient_name(String client_name) {
|
||||
this.client_name = client_name;
|
||||
}
|
||||
|
||||
public String getCallback_url() {
|
||||
return callback_url;
|
||||
}
|
||||
|
||||
public void setCallback_url(String callback_url) {
|
||||
this.callback_url = callback_url;
|
||||
}
|
||||
|
||||
public String getClient_secret() {
|
||||
return client_secret;
|
||||
}
|
||||
|
||||
public void setClient_secret(String client_secret) {
|
||||
this.client_secret = client_secret;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
/**
|
||||
* This holds the data related to registration.
|
||||
*/
|
||||
public class RegisterInfo {
|
||||
|
||||
private boolean isRegistered;
|
||||
private String msg;
|
||||
|
||||
public boolean isRegistered() {
|
||||
return isRegistered;
|
||||
}
|
||||
|
||||
public void setIsRegistered(boolean isRegistered) {
|
||||
this.isRegistered = isRegistered;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
|
||||
/**
|
||||
* This class represents the data that are required to register
|
||||
* the oauth application.
|
||||
*/
|
||||
public class RegistrationProfile {
|
||||
|
||||
public String callbackUrl;
|
||||
public String clientName;
|
||||
public String tokenScope;
|
||||
public String owner;
|
||||
public String grantType;
|
||||
public String applicationType;
|
||||
|
||||
public String getCallbackUrl() {
|
||||
return callbackUrl;
|
||||
}
|
||||
|
||||
public void setCallbackUrl(String callBackUrl) {
|
||||
this.callbackUrl = callBackUrl;
|
||||
}
|
||||
|
||||
public String getClientName() {
|
||||
return clientName;
|
||||
}
|
||||
|
||||
public void setClientName(String clientName) {
|
||||
this.clientName = clientName;
|
||||
}
|
||||
|
||||
public String getTokenScope() {
|
||||
return tokenScope;
|
||||
}
|
||||
|
||||
public void setTokenScope(String tokenScope) {
|
||||
this.tokenScope = tokenScope;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(String owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public String getGrantType() {
|
||||
return grantType;
|
||||
}
|
||||
|
||||
public void setGrantType(String grantType) {
|
||||
this.grantType = grantType;
|
||||
}
|
||||
|
||||
public String getApplicationType() {
|
||||
return applicationType;
|
||||
}
|
||||
|
||||
public void setApplicationType(String applicationType) {
|
||||
this.applicationType = applicationType;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.authorization.client.dto;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
/**
|
||||
* This hold the api defintion that is used as a contract with netflix feign.
|
||||
*/
|
||||
public interface TokenIssuerService {
|
||||
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
AccessTokenInfo getToken(@QueryParam("grant_type") String grant, @QueryParam("username") String username,
|
||||
@QueryParam("password") String password);
|
||||
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
AccessTokenInfo getToken(@QueryParam("grant_type") String grant, @QueryParam("username") String username,
|
||||
@QueryParam("password") String password, @QueryParam("scope") String scopes);
|
||||
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
AccessTokenInfo getToken(@QueryParam("grant_type") String grant, @QueryParam("refresh_token") String refreshToken);
|
||||
}
|
||||
@ -21,6 +21,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.osgi.service.component.ComponentContext;
|
||||
import org.osgi.service.http.HttpService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.http.HTTPEventAdapterFactory;
|
||||
import org.wso2.carbon.event.input.adapter.core.InputEventAdapterFactory;
|
||||
import org.wso2.carbon.user.core.service.RealmService;
|
||||
@ -29,9 +30,19 @@ import org.wso2.carbon.user.core.service.RealmService;
|
||||
* @scr.component name="input.iot.http.AdapterService.component" immediate="true"
|
||||
* @scr.reference name="user.realmservice.default"
|
||||
* interface="org.wso2.carbon.user.core.service.RealmService" cardinality="1..1"
|
||||
* policy="dynamic" bind="setRealmService" unbind="unsetRealmService"
|
||||
* policy="dynamic"
|
||||
* bind="setRealmService"
|
||||
* unbind="unsetRealmService"
|
||||
* @scr.reference name="http.service" interface="org.osgi.service.http.HttpService"
|
||||
* cardinality="1..1" policy="dynamic" bind="setHttpService" unbind="unsetHttpService"
|
||||
* cardinality="1..1"
|
||||
* policy="dynamic"
|
||||
* bind="setHttpService"
|
||||
* unbind="unsetHttpService"
|
||||
* @scr.reference name="input.extension.service" interface="org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionService"
|
||||
* cardinality="1..1"
|
||||
* policy="dynamic"
|
||||
* bind="setInputAdapterExtensionService"
|
||||
* unbind="unsetInputAdapterExtensionService"
|
||||
*/
|
||||
public class InputAdapterServiceComponent {
|
||||
|
||||
@ -67,4 +78,12 @@ public class InputAdapterServiceComponent {
|
||||
InputAdapterServiceDataHolder.registerHTTPService(null);
|
||||
}
|
||||
|
||||
protected void setInputAdapterExtensionService(InputAdapterExtensionService inputAdapterExtensionService) {
|
||||
InputAdapterServiceDataHolder.setInputAdapterExtensionService(inputAdapterExtensionService);
|
||||
}
|
||||
|
||||
protected void unsetInputAdapterExtensionService(InputAdapterExtensionService inputAdapterExtensionService) {
|
||||
InputAdapterServiceDataHolder.setInputAdapterExtensionService(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.internal;
|
||||
|
||||
import org.osgi.service.http.HttpService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionService;
|
||||
import org.wso2.carbon.user.core.service.RealmService;
|
||||
|
||||
/**
|
||||
@ -24,6 +25,7 @@ public final class InputAdapterServiceDataHolder {
|
||||
|
||||
private static RealmService realmService;
|
||||
private static HttpService httpService;
|
||||
private static InputAdapterExtensionService inputAdapterExtensionService;
|
||||
|
||||
private InputAdapterServiceDataHolder() {
|
||||
}
|
||||
@ -46,5 +48,12 @@ public final class InputAdapterServiceDataHolder {
|
||||
return httpService;
|
||||
}
|
||||
|
||||
public static void setInputAdapterExtensionService(InputAdapterExtensionService inputAdapterExtensionService) {
|
||||
InputAdapterServiceDataHolder.inputAdapterExtensionService = inputAdapterExtensionService;
|
||||
}
|
||||
|
||||
public static InputAdapterExtensionService getInputAdapterExtensionService() {
|
||||
return inputAdapterExtensionService;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,99 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.util;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentInfo;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentValidator;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class HTTPContentValidator implements ContentValidator {
|
||||
private static final Log log = LogFactory.getLog(HTTPContentValidator.class);
|
||||
private static String JSON_ARRAY_START_CHAR = "[";
|
||||
private static String CDMF_SCOPE_PREFIX = "cdmf";
|
||||
private static String CDMF_SCOPE_SEPERATOR = "/";
|
||||
|
||||
@Override
|
||||
public ContentInfo validate(Object msgPayload, Map<String, Object> dynamicParams) {
|
||||
String deviceId = (String) dynamicParams.get("deviceId");
|
||||
String deviceType = (String) dynamicParams.get("deviceType");
|
||||
String msg = (String) msgPayload;
|
||||
String deviceIdJsonPath = HTTPEventAdapterConstants.DEVICE_ID_JSON_PATH;
|
||||
boolean status;
|
||||
if (status = isValidDevice(deviceId, deviceType, dynamicParams)) {
|
||||
if (msg.startsWith(JSON_ARRAY_START_CHAR)) {
|
||||
status = processMultipleEvents(msg, deviceId, deviceIdJsonPath);
|
||||
} else {
|
||||
status = processSingleEvent(msg, deviceId, deviceIdJsonPath);
|
||||
}
|
||||
}
|
||||
return new ContentInfo(status, msg);
|
||||
}
|
||||
|
||||
private boolean processSingleEvent(String msg, String deviceIdFromTopic, String deviceIdJsonPath) {
|
||||
Object res = JsonPath.read(msg, deviceIdJsonPath);
|
||||
String deviceIdFromContent = (res != null) ? res.toString() : "";
|
||||
if (deviceIdFromContent.equals(deviceIdFromTopic)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean processMultipleEvents(String msg, String deviceIdFromTopic, String deviceIdJsonPath) {
|
||||
try {
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
JSONArray jsonArray = (JSONArray) jsonParser.parse(msg);
|
||||
boolean status = false;
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
status = processSingleEvent(jsonArray.get(i).toString(), deviceIdFromTopic, deviceIdJsonPath);
|
||||
if (!status) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
} catch (ParseException e) {
|
||||
log.error("Invalid input " + msg, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidDevice(String deviceId, String deviceType, Map<String, Object> dynamicParams) {
|
||||
List<String> scopes = (List<String>) dynamicParams.get(HTTPEventAdapterConstants.SCOPE_TAG);
|
||||
if (scopes != null) {
|
||||
for (String scope : scopes) {
|
||||
if (scope.startsWith(CDMF_SCOPE_PREFIX)) {
|
||||
String deviceIdInfo[] = scope.split(CDMF_SCOPE_SEPERATOR);
|
||||
if (deviceIdInfo.length == 3) {
|
||||
if (deviceId.equals(deviceIdInfo[2]) && deviceType.equals(deviceIdInfo[1])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -48,27 +48,17 @@ public final class HTTPEventAdapterConstants {
|
||||
public static final int DEFAULT_HTTP_PORT = 9763;
|
||||
public static final int DEFAULT_HTTPS_PORT = 9443;
|
||||
public static final String MAXIMUM_TOTAL_HTTP_CONNECTION = "maximumTotalHttpConnection";
|
||||
public static final String MAXIMUM_TOTAL_HTTP_CONNECTION_HINT = "maximumTotalHttpConnection.hint";
|
||||
public static final String MAXIMUM_HTTP_CONNECTION_PER_HOST = "maximumHttpConnectionPerHost";
|
||||
public static final String MAXIMUM_HTTP_CONNECTION_PER_HOST_HINT = "maximumHttpConnectionPerHost.hint";
|
||||
public static final String TOKEN_VALIDATION_ENDPOINT_URL = "keymanagerUrl";
|
||||
public static final String TOKEN_VALIDATION_ENDPOINT_URL = "tokenValidationUrl";
|
||||
public static final String TOKEN_VALIDATION_POST_FIX = "/services/OAuth2TokenValidationService";
|
||||
public static final String USERNAME = "username";
|
||||
public static final String USERNAME_HINT = "username.hint";
|
||||
public static final String PASSWORD = "password";
|
||||
public static final String PASSWORD_HINT = "password.hint";
|
||||
public static final String DEFAULT_STRING = "default";
|
||||
public static final String MAX_HTTP_CONNECTION = "2";
|
||||
public static final String MAX_TOTAL_HTTP_CONNECTION = "100";
|
||||
public static final String TENANT_DOMAIN_TAG = "tenantDomain";
|
||||
public static final String USERNAME_TAG = "username";
|
||||
public static final String SCOPE_TAG = "scopes";
|
||||
public static final String PAYLOAD_TAG = "payload";
|
||||
public static final String DEVICE_ID_JSON_PATH = "event.metaData.deviceId";
|
||||
public static final String ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME = "contentValidator";
|
||||
public static final String ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME_HINT = "contentValidator.hint";
|
||||
public static final String ADAPTER_CONF_CONTENT_VALIDATOR_TYPE = "contentValidator";
|
||||
public static final String ADAPTER_CONF_CONTENT_VALIDATOR_TYPE_HINT = "contentValidator.hint";
|
||||
public static final String DEFAULT = "default";
|
||||
public static final String HTTP_CONTENT_VALIDATION_DEFAULT_PARAMETERS = "";
|
||||
public static final String ADAPTER_CONF_CONTENT_TRANSFORMER_CLASSNAME = "contentTransformer";
|
||||
public static final String ADAPTER_CONF_CONTENT_TRANSFORMER_CLASSNAME_HINT = "contentTransformer.hint";
|
||||
}
|
||||
|
||||
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.http.util;
|
||||
|
||||
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterException;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PropertyUtils {
|
||||
|
||||
//This method is only used if the mb features are within DAS.
|
||||
public static String replaceProperty(String urlWithPlaceholders) throws InputEventAdapterException {
|
||||
String regex = "\\$\\{(.*?)\\}";
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matchPattern = pattern.matcher(urlWithPlaceholders);
|
||||
while (matchPattern.find()) {
|
||||
String sysPropertyName = matchPattern.group(1);
|
||||
String sysPropertyValue = System.getProperty(sysPropertyName);
|
||||
if (sysPropertyValue != null && !sysPropertyName.isEmpty()) {
|
||||
urlWithPlaceholders = urlWithPlaceholders.replaceAll("\\$\\{(" + sysPropertyName + ")\\}", sysPropertyValue);
|
||||
} else {
|
||||
throw new InputEventAdapterException("System property - " + sysPropertyName
|
||||
+ " is not defined, hence cannot resolve : " + urlWithPlaceholders);
|
||||
}
|
||||
}
|
||||
return urlWithPlaceholders;
|
||||
}
|
||||
}
|
||||
@ -23,6 +23,6 @@ http.usage.tips_mid2=/endpoints/<event_receiver_name></i></br></br>For oth
|
||||
http.usage.tips_mid3=/endpoints/t/<tenant_domain>/<event_receiver_name></i></br> <i>https://localhost:
|
||||
http.usage.tips_postfix=/endpoints/t/<tenant_domain>/<event_receiver_name></i>
|
||||
contentValidator=contentValidator
|
||||
contentValidator.hint=Class Name of the content Validation or 'default' to set default class, required to implement (if required)
|
||||
contentValidator.hint=Type of the content Validation or 'default' to set default impl, required to implement (if required)
|
||||
contentTransformer=contentTransformer
|
||||
contentTransformer.hint=Class Name of the content transformer or 'default' to set default class, required to implement (if required)
|
||||
contentTransformer.hint=Type of the content transformer or 'default' to set default type, required to implement (if required)
|
||||
@ -32,6 +32,10 @@
|
||||
<url>http://wso2.org</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-codec.wso2</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.analytics-common</groupId>
|
||||
<artifactId>org.wso2.carbon.event.input.adapter.core</artifactId>
|
||||
@ -72,6 +76,10 @@
|
||||
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.input.adapter.extension</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.identity.jwt.client.extension</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -124,9 +132,20 @@
|
||||
org.apache.http.client.methods;version="${httpclient.version.range}",
|
||||
org.apache.http.impl.client;version="${httpclient.version.range}",
|
||||
org.json.simple.*,
|
||||
com.jayway.jsonpath.*
|
||||
com.jayway.jsonpath.*,
|
||||
org.wso2.carbon.identity.jwt.client.extension.*,
|
||||
javax.net.ssl,
|
||||
org.apache.commons.codec.binary,
|
||||
org.apache.commons.logging,
|
||||
org.apache.http.entity,
|
||||
org.osgi.framework,
|
||||
org.osgi.service.component,
|
||||
org.osgi.service.http,
|
||||
org.wso2.carbon.context,
|
||||
org.wso2.carbon.core,
|
||||
org.wso2.carbon.device.mgt.input.adapter.extension,
|
||||
org.wso2.carbon.user.api
|
||||
</Import-Package>
|
||||
<DynamicImport-Package>*</DynamicImport-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
@ -42,7 +42,6 @@ public class MQTTEventAdapter implements InputEventAdapter {
|
||||
private MQTTAdapterListener mqttAdapterListener;
|
||||
private MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration;
|
||||
|
||||
|
||||
public MQTTEventAdapter(InputEventAdapterConfiguration eventAdapterConfiguration,
|
||||
Map<String, String> globalProperties) {
|
||||
this.eventAdapterConfiguration = eventAdapterConfiguration;
|
||||
@ -53,7 +52,6 @@ public class MQTTEventAdapter implements InputEventAdapter {
|
||||
public void init(InputEventAdapterListener eventAdapterListener) throws InputEventAdapterException {
|
||||
this.eventAdapterListener = eventAdapterListener;
|
||||
try {
|
||||
|
||||
mqttBrokerConnectionConfiguration = new MQTTBrokerConnectionConfiguration(eventAdapterConfiguration
|
||||
,globalProperties);
|
||||
mqttAdapterListener = new MQTTAdapterListener(mqttBrokerConnectionConfiguration
|
||||
|
||||
@ -65,12 +65,12 @@ public class MQTTEventAdapterFactory extends InputEventAdapterFactory {
|
||||
|
||||
|
||||
//Content Validator details
|
||||
Property contentValidator = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME);
|
||||
Property contentValidator = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE);
|
||||
contentValidator.setDisplayName(
|
||||
resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME));
|
||||
resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE));
|
||||
contentValidator.setRequired(false);
|
||||
contentValidator.setHint(
|
||||
resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME_HINT));
|
||||
resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE_HINT));
|
||||
contentValidator.setDefaultValue(MQTTEventAdapterConstants.DEFAULT);
|
||||
propertyList.add(contentValidator);
|
||||
|
||||
@ -78,7 +78,7 @@ public class MQTTEventAdapterFactory extends InputEventAdapterFactory {
|
||||
Property userName = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME);
|
||||
userName.setDisplayName(
|
||||
resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME));
|
||||
userName.setRequired(true);
|
||||
userName.setRequired(false);
|
||||
userName.setHint(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME_HINT));
|
||||
propertyList.add(userName);
|
||||
|
||||
@ -86,7 +86,7 @@ public class MQTTEventAdapterFactory extends InputEventAdapterFactory {
|
||||
Property password = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD);
|
||||
userName.setDisplayName(
|
||||
resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD));
|
||||
userName.setRequired(true);
|
||||
userName.setRequired(false);
|
||||
userName.setHint(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD_HINT));
|
||||
propertyList.add(password);
|
||||
|
||||
@ -108,12 +108,12 @@ public class MQTTEventAdapterFactory extends InputEventAdapterFactory {
|
||||
propertyList.add(clearSession);
|
||||
|
||||
//Content Transformer details
|
||||
Property contentTransformer = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_TRANSFORMER_CLASSNAME);
|
||||
Property contentTransformer = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_TRANSFORMER_TYPE);
|
||||
contentTransformer.setDisplayName(
|
||||
resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_TRANSFORMER_CLASSNAME));
|
||||
resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_TRANSFORMER_TYPE));
|
||||
contentTransformer.setRequired(false);
|
||||
contentTransformer.setHint(
|
||||
resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_TRANSFORMER_CLASSNAME_HINT));
|
||||
resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_TRANSFORMER_TYPE_HINT));
|
||||
contentTransformer.setDefaultValue(MQTTEventAdapterConstants.DEFAULT);
|
||||
propertyList.add(contentTransformer);
|
||||
|
||||
|
||||
@ -21,11 +21,23 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.osgi.service.component.ComponentContext;
|
||||
import org.osgi.service.http.HttpService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.mqtt.MQTTEventAdapterFactory;
|
||||
import org.wso2.carbon.event.input.adapter.core.InputEventAdapterFactory;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
|
||||
/**
|
||||
* @scr.component name="input.iot.mqtt.AdapterService.component" immediate="true"
|
||||
* @scr.reference name="input.extension.service" interface="org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionService"
|
||||
* cardinality="1..1"
|
||||
* policy="dynamic"
|
||||
* bind="setInputAdapterExtensionService"
|
||||
* unbind="unsetInputAdapterExtensionService"
|
||||
* @scr.reference name="jwt.client.service" interface="org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService"
|
||||
* cardinality="1..1"
|
||||
* policy="dynamic"
|
||||
* bind="setJWTClientManagerService"
|
||||
* unbind="unsetJWTClientManagerService"
|
||||
*/
|
||||
public class InputAdapterServiceComponent {
|
||||
|
||||
@ -52,4 +64,20 @@ public class InputAdapterServiceComponent {
|
||||
InputAdapterServiceDataHolder.registerHTTPService(null);
|
||||
}
|
||||
|
||||
protected void setInputAdapterExtensionService(InputAdapterExtensionService inputAdapterExtensionService) {
|
||||
InputAdapterServiceDataHolder.setInputAdapterExtensionService(inputAdapterExtensionService);
|
||||
}
|
||||
|
||||
protected void unsetInputAdapterExtensionService(InputAdapterExtensionService inputAdapterExtensionService) {
|
||||
InputAdapterServiceDataHolder.setInputAdapterExtensionService(null);
|
||||
}
|
||||
|
||||
protected void setJWTClientManagerService(JWTClientManagerService jwtClientManagerService) {
|
||||
InputAdapterServiceDataHolder.setJwtClientManagerService(jwtClientManagerService);
|
||||
}
|
||||
|
||||
protected void unsetJWTClientManagerService(JWTClientManagerService jwtClientManagerService) {
|
||||
InputAdapterServiceDataHolder.setJwtClientManagerService(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -15,6 +15,8 @@
|
||||
package org.wso2.carbon.device.mgt.input.adapter.mqtt.internal;
|
||||
|
||||
import org.osgi.service.http.HttpService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionService;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
|
||||
/**
|
||||
* common place to hold some OSGI service references.
|
||||
@ -22,6 +24,8 @@ import org.osgi.service.http.HttpService;
|
||||
public final class InputAdapterServiceDataHolder {
|
||||
|
||||
private static HttpService httpService;
|
||||
private static InputAdapterExtensionService inputAdapterExtensionService;
|
||||
private static JWTClientManagerService jwtClientManagerService;
|
||||
|
||||
private InputAdapterServiceDataHolder() {
|
||||
}
|
||||
@ -35,5 +39,20 @@ public final class InputAdapterServiceDataHolder {
|
||||
return httpService;
|
||||
}
|
||||
|
||||
public static void setInputAdapterExtensionService(InputAdapterExtensionService inputAdapterExtensionService) {
|
||||
InputAdapterServiceDataHolder.inputAdapterExtensionService = inputAdapterExtensionService;
|
||||
}
|
||||
|
||||
public static InputAdapterExtensionService getInputAdapterExtensionService() {
|
||||
return inputAdapterExtensionService;
|
||||
}
|
||||
|
||||
public static JWTClientManagerService getJwtClientManagerService() {
|
||||
return jwtClientManagerService;
|
||||
}
|
||||
|
||||
public static void setJwtClientManagerService(
|
||||
JWTClientManagerService jwtClientManagerService) {
|
||||
InputAdapterServiceDataHolder.jwtClientManagerService = jwtClientManagerService;
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,14 +21,11 @@ import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
|
||||
import org.json.simple.JSONObject;
|
||||
@ -39,11 +36,13 @@ import org.wso2.carbon.core.ServerStatus;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentInfo;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentValidator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.DefaultContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.DefaultContentValidator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.mqtt.internal.InputAdapterServiceDataHolder;
|
||||
import org.wso2.carbon.event.input.adapter.core.InputEventAdapterListener;
|
||||
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterRuntimeException;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.mqtt.exception.MQTTContentInitializationException;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
@ -51,9 +50,7 @@ import java.net.URL;
|
||||
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;
|
||||
|
||||
public class MQTTAdapterListener implements MqttCallback, Runnable {
|
||||
@ -83,7 +80,7 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
|
||||
this.mqttBrokerConnectionConfiguration = mqttBrokerConnectionConfiguration;
|
||||
this.cleanSession = mqttBrokerConnectionConfiguration.isCleanSession();
|
||||
int keepAlive = mqttBrokerConnectionConfiguration.getKeepAlive();
|
||||
this.topic = topic;
|
||||
this.topic = PropertyUtils.replaceTenantDomainProperty(topic);
|
||||
this.eventAdapterListener = inputEventAdapterListener;
|
||||
this.tenantId = tenantId;
|
||||
|
||||
@ -102,43 +99,23 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
|
||||
|
||||
// Set this wrapper as the callback handler
|
||||
mqttClient.setCallback(this);
|
||||
String contentValidatorClassName = this.mqttBrokerConnectionConfiguration.getContentValidatorClassName();
|
||||
String contentValidatorType = this.mqttBrokerConnectionConfiguration.getContentValidatorType();
|
||||
|
||||
if (contentValidatorClassName != null && contentValidatorClassName.equals(MQTTEventAdapterConstants.DEFAULT)) {
|
||||
contentValidator = new DefaultContentValidator();
|
||||
} else if (contentValidatorClassName != null && !contentValidatorClassName.isEmpty()) {
|
||||
try {
|
||||
Class<? extends ContentValidator> contentValidatorClass = Class.forName(contentValidatorClassName)
|
||||
.asSubclass(ContentValidator.class);
|
||||
contentValidator = contentValidatorClass.newInstance();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new MQTTContentInitializationException(
|
||||
"Unable to find the class validator: " + contentValidatorClassName, e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new MQTTContentInitializationException(
|
||||
"Unable to create an instance of :" + contentValidatorClassName, e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new MQTTContentInitializationException("Access of the instance in not allowed.", e);
|
||||
}
|
||||
if (contentValidatorType == null || contentValidatorType.equals(MQTTEventAdapterConstants.DEFAULT)) {
|
||||
contentValidator = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getDefaultContentValidator();
|
||||
} else {
|
||||
contentValidator = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getContentValidator(contentValidatorType);
|
||||
}
|
||||
|
||||
String contentTransformerClassName = this.mqttBrokerConnectionConfiguration.getContentTransformerClassName();
|
||||
if (contentTransformerClassName != null && contentTransformerClassName.equals(MQTTEventAdapterConstants.DEFAULT)) {
|
||||
contentTransformer = new DefaultContentTransformer();
|
||||
} else if (contentTransformerClassName != null && !contentTransformerClassName.isEmpty()) {
|
||||
try {
|
||||
Class<? extends ContentTransformer> contentTransformerClass = Class.forName(contentTransformerClassName)
|
||||
.asSubclass(ContentTransformer.class);
|
||||
contentTransformer = contentTransformerClass.newInstance();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new MQTTContentInitializationException(
|
||||
"Unable to find the class transfoer: " + contentTransformerClassName, e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new MQTTContentInitializationException(
|
||||
"Unable to create an instance of :" + contentTransformerClassName, e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new MQTTContentInitializationException("Access of the instance in not allowed.", e);
|
||||
}
|
||||
String contentTransformerType = this.mqttBrokerConnectionConfiguration.getContentTransformerType();
|
||||
if (contentTransformerType == null || contentTransformerType.equals(MQTTEventAdapterConstants.DEFAULT)) {
|
||||
contentTransformer = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getDefaultContentTransformer();
|
||||
} else {
|
||||
contentTransformer = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getContentTransformer(contentTransformerType);
|
||||
}
|
||||
} catch (MqttException e) {
|
||||
log.error("Exception occurred while subscribing to MQTT broker at "
|
||||
@ -165,8 +142,16 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
|
||||
registrationProfile.setGrantType(MQTTEventAdapterConstants.GRANT_TYPE);
|
||||
registrationProfile.setOwner(username);
|
||||
registrationProfile.setTokenScope(MQTTEventAdapterConstants.TOKEN_SCOPE);
|
||||
registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX
|
||||
+ mqttBrokerConnectionConfiguration.getAdapterName() + "_" + tenantId);
|
||||
if (!mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) {
|
||||
registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX
|
||||
+ mqttBrokerConnectionConfiguration.getAdapterName() +
|
||||
"_" + tenantId);
|
||||
registrationProfile.setIsSaasApp(false);
|
||||
} else {
|
||||
registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX
|
||||
+ mqttBrokerConnectionConfiguration.getAdapterName());
|
||||
registrationProfile.setIsSaasApp(true);
|
||||
}
|
||||
String jsonString = registrationProfile.toJSON();
|
||||
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
|
||||
postMethod.setEntity(requestEntity);
|
||||
@ -192,8 +177,10 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
log.error("Invalid dcrUrl : " + dcrUrlString);
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | IOException e) {
|
||||
log.error("Failed to create an https connection.", e);
|
||||
} catch (JWTClientException | UserStoreException e) {
|
||||
log.error("Failed to create an oauth token with jwt grant type.", e);
|
||||
} catch (NoSuchAlgorithmException |KeyManagementException |KeyStoreException | IOException e) {
|
||||
log.error("Failed to create a http connection.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -287,34 +274,26 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
|
||||
}
|
||||
|
||||
private String getToken(String clientId, String clientSecret)
|
||||
throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, ParseException {
|
||||
URL tokenEndpoint = new URL(mqttBrokerConnectionConfiguration.getTokenUrl());
|
||||
HttpClient httpClient = MQTTUtil.getHttpClient(tokenEndpoint.getProtocol());
|
||||
HttpPost postMethod = new HttpPost(tokenEndpoint.toString());
|
||||
throws UserStoreException, JWTClientException {
|
||||
PrivilegedCarbonContext.startTenantFlow();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
||||
try {
|
||||
String scopes = mqttBrokerConnectionConfiguration.getBrokerScopes();
|
||||
String username = mqttBrokerConnectionConfiguration.getUsername();
|
||||
if (mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) {
|
||||
username = PrivilegedCarbonContext.getThreadLocalCarbonContext()
|
||||
.getUserRealm().getRealmConfiguration().getAdminUserName() + "@" + PrivilegedCarbonContext
|
||||
.getThreadLocalCarbonContext().getTenantDomain(true);
|
||||
}
|
||||
|
||||
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
||||
nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.GRANT_TYPE_PARAM_NAME,
|
||||
MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE));
|
||||
nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE_USERNAME,
|
||||
mqttBrokerConnectionConfiguration.getUsername()));
|
||||
nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE_PASSWORD,
|
||||
mqttBrokerConnectionConfiguration.getPassword()));
|
||||
String scopes = mqttBrokerConnectionConfiguration.getBrokerScopes();
|
||||
if (scopes != null && !scopes.isEmpty()) {
|
||||
nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE_SCOPES, scopes));
|
||||
JWTClientManagerService jwtClientManagerService =
|
||||
InputAdapterServiceDataHolder.getJwtClientManagerService();
|
||||
AccessTokenInfo accessTokenInfo = jwtClientManagerService.getJWTClient().getAccessToken(
|
||||
clientId, clientSecret, username, scopes);
|
||||
return accessTokenInfo.getAccessToken();
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
|
||||
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
||||
postMethod.addHeader("Authorization", "Basic " + getBase64Encode(clientId, clientSecret));
|
||||
postMethod.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
HttpResponse httpResponse = httpClient.execute(postMethod);
|
||||
String response = MQTTUtil.getResponseString(httpResponse);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(response);
|
||||
}
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
JSONObject jsonObject = (JSONObject) jsonParser.parse(response);
|
||||
return (String) jsonObject.get(MQTTEventAdapterConstants.ACCESS_TOKEN_GRANT_TYPE_PARAM_NAME);
|
||||
}
|
||||
|
||||
private String getBase64Encode(String key, String value) {
|
||||
|
||||
@ -34,10 +34,10 @@ public class MQTTBrokerConnectionConfiguration {
|
||||
private int keepAlive;
|
||||
private String brokerUrl;
|
||||
private String dcrUrl;
|
||||
private String tokenUrl;
|
||||
private String contentValidatorClassName;
|
||||
private String contentTransformerClassName;
|
||||
private String contentValidatorType;
|
||||
private String contentTransformerType;
|
||||
private String adapterName;
|
||||
private boolean globalCredentailSet;
|
||||
|
||||
public String getBrokerScopes() {
|
||||
return brokerScopes;
|
||||
@ -67,16 +67,16 @@ public class MQTTBrokerConnectionConfiguration {
|
||||
return keepAlive;
|
||||
}
|
||||
|
||||
public String getContentValidatorClassName() {
|
||||
return contentValidatorClassName;
|
||||
public String getContentValidatorType() {
|
||||
return contentValidatorType;
|
||||
}
|
||||
|
||||
public String getContentTransformerClassName() {
|
||||
return contentTransformerClassName;
|
||||
public String getContentTransformerType() {
|
||||
return contentTransformerType;
|
||||
}
|
||||
|
||||
public String getTokenUrl() {
|
||||
return tokenUrl;
|
||||
public boolean isGlobalCredentailSet() {
|
||||
return globalCredentailSet;
|
||||
}
|
||||
|
||||
public String getAdapterName() {
|
||||
@ -89,6 +89,11 @@ public class MQTTBrokerConnectionConfiguration {
|
||||
adapterName = eventAdapterConfiguration.getName();
|
||||
this.username = eventAdapterConfiguration.getProperties().get(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME);
|
||||
this.password = eventAdapterConfiguration.getProperties().get(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD);
|
||||
if ((username == null || username.isEmpty()) && (password == null || password.isEmpty())) {
|
||||
username = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME);
|
||||
password = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD);
|
||||
globalCredentailSet = true;
|
||||
}
|
||||
this.brokerScopes = eventAdapterConfiguration.getProperties().get(MQTTEventAdapterConstants.ADAPTER_CONF_SCOPES);
|
||||
if (brokerScopes == null) {
|
||||
this.brokerScopes = MQTTEventAdapterConstants.EMPTY_STRING;
|
||||
@ -100,10 +105,8 @@ public class MQTTBrokerConnectionConfiguration {
|
||||
this.brokerUrl = PropertyUtils.replaceMqttProperty(url);
|
||||
this.dcrUrl = PropertyUtils
|
||||
.replaceMqttProperty(globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_DCR_URL));
|
||||
this.tokenUrl = PropertyUtils
|
||||
.replaceMqttProperty(globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_TOKEN_URL));
|
||||
this.contentValidatorClassName = eventAdapterConfiguration.getProperties()
|
||||
.get(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME);
|
||||
this.contentValidatorType = eventAdapterConfiguration.getProperties()
|
||||
.get(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE);
|
||||
this.cleanSession = Boolean.parseBoolean(eventAdapterConfiguration.getProperties()
|
||||
.get(MQTTEventAdapterConstants.ADAPTER_CONF_CLEAN_SESSION));
|
||||
//If global properties are available those will be assigned else constant values will be assigned
|
||||
@ -112,7 +115,7 @@ public class MQTTBrokerConnectionConfiguration {
|
||||
} else {
|
||||
keepAlive = MQTTEventAdapterConstants.ADAPTER_CONF_DEFAULT_KEEP_ALIVE;
|
||||
}
|
||||
this.contentTransformerClassName = eventAdapterConfiguration.getProperties()
|
||||
.get(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_TRANSFORMER_CLASSNAME);
|
||||
this.contentTransformerType = eventAdapterConfiguration.getProperties()
|
||||
.get(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_TRANSFORMER_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,10 +35,10 @@ public class MQTTEventAdapterConstants {
|
||||
public static final String ADAPTER_CONF_DCR_URL = "dcrUrl";
|
||||
public static final String ADAPTER_CONF_TOKEN_URL = "tokenUrl";
|
||||
public static final String ADAPTER_CONF_DCR_URL_HINT = "dcrUrl.hint";
|
||||
public static final String ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME = "contentValidator";
|
||||
public static final String ADAPTER_CONF_CONTENT_VALIDATOR_CLASSNAME_HINT = "contentValidator.hint";
|
||||
public static final String ADAPTER_CONF_CONTENT_TRANSFORMER_CLASSNAME = "contentTransformer";
|
||||
public static final String ADAPTER_CONF_CONTENT_TRANSFORMER_CLASSNAME_HINT = "contentTransformer.hint";
|
||||
public static final String ADAPTER_CONF_CONTENT_VALIDATOR_TYPE = "contentValidator";
|
||||
public static final String ADAPTER_CONF_CONTENT_VALIDATOR_TYPE_HINT = "contentValidator.hint";
|
||||
public static final String ADAPTER_CONF_CONTENT_TRANSFORMER_TYPE = "contentTransformer";
|
||||
public static final String ADAPTER_CONF_CONTENT_TRANSFORMER_TYPE_HINT = "contentTransformer.hint";
|
||||
public static final String ADAPTER_MESSAGE_TOPIC = "topic";
|
||||
public static final String ADAPTER_MESSAGE_TOPIC_HINT = "topic.hint";
|
||||
public static final String ADAPTER_CONF_CLIENTID = "clientId";
|
||||
@ -53,7 +53,7 @@ public class MQTTEventAdapterConstants {
|
||||
|
||||
public static final String EMPTY_STRING = "";
|
||||
public static final String GRANT_TYPE_PARAM_NAME = "grant_type";
|
||||
public static final String GRANT_TYPE = "password refresh_token";
|
||||
public static final String GRANT_TYPE = "password refresh_token urn:ietf:params:oauth:grant-type:jwt-bearer";
|
||||
public static final String TOKEN_SCOPE = "production";
|
||||
public static final String APPLICATION_NAME_PREFIX = "InputAdapter_";
|
||||
public static final String CLIENT_ID = "clientId";
|
||||
@ -63,15 +63,7 @@ public class MQTTEventAdapterConstants {
|
||||
public static final String MQTT_CONTENT_VALIDATION_DEFAULT_PARAMETERS = "";
|
||||
public static final String TOPIC = "topic";
|
||||
public static final String PAYLOAD = "payload";
|
||||
public static final String DEVICE_ID_JSON_PATH = "event.metaData.deviceId";
|
||||
public static final String DEVICE_TYPE_JSON_PATH = "event.metaData.deviceId";
|
||||
public static final int DEVICE_ID_TOPIC_HIERARCHY_INDEX = 2;
|
||||
public static final String AUTHORIZATION_HEADER_NAME = "Authorization";
|
||||
public static final String AUTHORIZATION_HEADER_VALUE_PREFIX = "Basic ";
|
||||
public static final String PASSWORD_GRANT_TYPE = "password";
|
||||
public static final String PASSWORD_GRANT_TYPE_USERNAME = "username";
|
||||
public static final String PASSWORD_GRANT_TYPE_PASSWORD = "password";
|
||||
public static final String PASSWORD_GRANT_TYPE_SCOPES = "scopes";
|
||||
public static final String ACCESS_TOKEN_GRANT_TYPE_PARAM_NAME = "access_token";
|
||||
|
||||
}
|
||||
|
||||
@ -18,12 +18,14 @@
|
||||
|
||||
package org.wso2.carbon.device.mgt.input.adapter.mqtt.util;
|
||||
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterException;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class PropertyUtils {
|
||||
private static final String TENANT_DOMAIN_PROPERTY = "\\$\\{tenant-domain\\}";
|
||||
|
||||
//This method is only used if the mb features are within DAS.
|
||||
static String replaceMqttProperty(String urlWithPlaceholders) throws InputEventAdapterException {
|
||||
@ -42,4 +44,10 @@ class PropertyUtils {
|
||||
}
|
||||
return urlWithPlaceholders;
|
||||
}
|
||||
|
||||
public static String replaceTenantDomainProperty (String urlWithPlaceholders) {
|
||||
urlWithPlaceholders = urlWithPlaceholders.replaceAll(TENANT_DOMAIN_PROPERTY, PrivilegedCarbonContext
|
||||
.getThreadLocalCarbonContext().getTenantDomain(true));
|
||||
return urlWithPlaceholders;
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ public class RegistrationProfile {
|
||||
private String owner;
|
||||
private String grantType;
|
||||
private String applicationType;
|
||||
private boolean isSaasApp;
|
||||
|
||||
private static final String TAG = RegistrationProfile.class.getSimpleName();
|
||||
|
||||
@ -63,11 +64,19 @@ public class RegistrationProfile {
|
||||
this.applicationType = applicationType;
|
||||
}
|
||||
|
||||
public String toJSON() {
|
||||
public boolean isSaasApp() {
|
||||
return isSaasApp;
|
||||
}
|
||||
|
||||
public void setIsSaasApp(boolean isSaasApp) {
|
||||
this.isSaasApp = isSaasApp;
|
||||
}
|
||||
|
||||
public String toJSON() {
|
||||
String jsonString =
|
||||
"{\"callbackUrl\": \"" + callbackUrl + "\",\"clientName\": \"" + clientName + "\", \"tokenScope\": " +
|
||||
"\"" + tokenScope + "\", \"owner\": \"" + owner + "\"," + "\"grantType\": \"" + grantType +
|
||||
"\", \"saasApp\" :false }\n";
|
||||
"\", \"saasApp\" :" + isSaasApp + " }\n";
|
||||
return jsonString;
|
||||
}
|
||||
}
|
||||
@ -27,14 +27,12 @@ password=Password
|
||||
password.hint=Password of the user for the broker (if required)
|
||||
scopes=Scopes
|
||||
scopes.hint=Scopes required to connect to broker (if required)
|
||||
dcrUrl=dcrUrl
|
||||
dcrUrl.hint=dynamic client registration endpoint URL to create application (if required) eg: https://localhost:9443/dynamic-client-web/register
|
||||
contentValidator=contentValidation
|
||||
contentValidator.hint=Class Name of the content Validation or 'default' to set default class, required to implement (if required)
|
||||
contentValidator.hint=Type of the content Validation or 'default' to set default type, required to implement (if required)
|
||||
url.hint=MQTT broker url tcp://localhost:1883
|
||||
cleanSession=Clean Session
|
||||
cleanSession.hint=Persist topic subscriptions and ack positions across client sessions
|
||||
keepAlive=Keep Alive (In seconds)
|
||||
events.duplicated.in.cluster=Is events duplicated in cluster
|
||||
contentTransformer=contentTransformer
|
||||
contentTransformer.hint=Class Name of the content transformer or 'default' to set default class, required to implement (if required)
|
||||
contentTransformer.hint=Type of the content transformer or 'default' to set default type, required to implement (if required)
|
||||
@ -116,9 +116,16 @@
|
||||
org.jivesoftware.smack.*,
|
||||
org.apache.log4j,
|
||||
org.wso2.carbon.base,
|
||||
org.wso2.carbon.core.util
|
||||
org.wso2.carbon.core.util,
|
||||
org.apache.commons.logging,
|
||||
org.osgi.framework,
|
||||
org.osgi.service.component,
|
||||
org.osgi.service.http,
|
||||
org.wso2.carbon.context,
|
||||
org.wso2.carbon.core,
|
||||
org.wso2.carbon.device.mgt.input.adapter.extension,
|
||||
org.wso2.carbon.user.core.service
|
||||
</Import-Package>
|
||||
<DynamicImport-Package>*</DynamicImport-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
@ -21,6 +21,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.osgi.service.component.ComponentContext;
|
||||
import org.osgi.service.http.HttpService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.xmpp.XMPPEventAdapterFactory;
|
||||
import org.wso2.carbon.event.input.adapter.core.InputEventAdapterFactory;
|
||||
import org.wso2.carbon.user.core.service.RealmService;
|
||||
@ -30,6 +31,11 @@ import org.wso2.carbon.user.core.service.RealmService;
|
||||
* @scr.reference name="user.realmservice.default"
|
||||
* interface="org.wso2.carbon.user.core.service.RealmService" cardinality="1..1"
|
||||
* policy="dynamic" bind="setRealmService" unbind="unsetRealmService"
|
||||
* @scr.reference name="input.extension.service" interface="org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionService"
|
||||
* cardinality="1..1"
|
||||
* policy="dynamic"
|
||||
* bind="setInputAdapterExtensionService"
|
||||
* unbind="unsetInputAdapterExtensionService"
|
||||
*/
|
||||
public class InputAdapterServiceComponent {
|
||||
|
||||
@ -64,4 +70,12 @@ public class InputAdapterServiceComponent {
|
||||
InputAdapterServiceDataHolder.registerHTTPService(null);
|
||||
}
|
||||
|
||||
protected void setInputAdapterExtensionService(InputAdapterExtensionService inputAdapterExtensionService) {
|
||||
InputAdapterServiceDataHolder.setInputAdapterExtensionService(inputAdapterExtensionService);
|
||||
}
|
||||
|
||||
protected void unsetInputAdapterExtensionService(InputAdapterExtensionService inputAdapterExtensionService) {
|
||||
InputAdapterServiceDataHolder.setInputAdapterExtensionService(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
package org.wso2.carbon.device.mgt.input.adapter.xmpp.internal;
|
||||
|
||||
import org.osgi.service.http.HttpService;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.InputAdapterExtensionService;
|
||||
import org.wso2.carbon.user.core.service.RealmService;
|
||||
|
||||
/**
|
||||
@ -24,6 +25,7 @@ public final class InputAdapterServiceDataHolder {
|
||||
|
||||
private static RealmService realmService;
|
||||
private static HttpService httpService;
|
||||
private static InputAdapterExtensionService inputAdapterExtensionService;
|
||||
|
||||
private InputAdapterServiceDataHolder() {
|
||||
}
|
||||
@ -46,5 +48,12 @@ public final class InputAdapterServiceDataHolder {
|
||||
return httpService;
|
||||
}
|
||||
|
||||
public static void setInputAdapterExtensionService(InputAdapterExtensionService inputAdapterExtensionService) {
|
||||
InputAdapterServiceDataHolder.inputAdapterExtensionService = inputAdapterExtensionService;
|
||||
}
|
||||
|
||||
public static InputAdapterExtensionService getInputAdapterExtensionService() {
|
||||
return inputAdapterExtensionService;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -21,7 +21,6 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.jivesoftware.smack.ConnectionConfiguration;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.ReconnectionManager;
|
||||
import org.jivesoftware.smack.SmackConfiguration;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
@ -36,9 +35,10 @@ import org.wso2.carbon.core.ServerStatus;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentInfo;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.ContentValidator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.DefaultContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.DefaultContentValidator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.transformer.DefaultContentTransformer;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.extension.validator.DefaultContentValidator;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.xmpp.exception.XMPPContentInitializationException;
|
||||
import org.wso2.carbon.device.mgt.input.adapter.xmpp.internal.InputAdapterServiceDataHolder;
|
||||
import org.wso2.carbon.event.input.adapter.core.InputEventAdapterListener;
|
||||
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterRuntimeException;
|
||||
import java.util.HashMap;
|
||||
@ -58,55 +58,27 @@ public class XMPPAdapterListener implements Runnable {
|
||||
|
||||
private InputEventAdapterListener eventAdapterListener = null;
|
||||
|
||||
|
||||
public XMPPAdapterListener(XMPPServerConnectionConfiguration xmppServerConnectionConfiguration,
|
||||
InputEventAdapterListener inputEventAdapterListener, int tenantId) {
|
||||
this.xmppServerConnectionConfiguration = xmppServerConnectionConfiguration;
|
||||
this.eventAdapterListener = inputEventAdapterListener;
|
||||
this.tenantId = tenantId;
|
||||
|
||||
try {
|
||||
String contentValidatorClassName = this.xmppServerConnectionConfiguration.getContentValidatorClassName();
|
||||
if (contentValidatorClassName != null && contentValidatorClassName.equals(XMPPEventAdapterConstants.DEFAULT)) {
|
||||
contentValidator = new DefaultContentValidator();
|
||||
} else if (contentValidatorClassName != null && !contentValidatorClassName.isEmpty()) {
|
||||
try {
|
||||
Class<? extends ContentValidator> contentValidatorClass = Class.forName(contentValidatorClassName)
|
||||
.asSubclass(ContentValidator.class);
|
||||
contentValidator = contentValidatorClass.newInstance();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new XMPPContentInitializationException(
|
||||
"Unable to find the class validator: " + contentValidatorClassName, e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new XMPPContentInitializationException(
|
||||
"Unable to create an instance of :" + contentValidatorClassName, e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new XMPPContentInitializationException("Access of the instance in not allowed.", e);
|
||||
}
|
||||
}
|
||||
String contentValidatorType = this.xmppServerConnectionConfiguration.getContentValidatorClassName();
|
||||
if (contentValidatorType == null || contentValidatorType.equals(XMPPEventAdapterConstants.DEFAULT)) {
|
||||
contentValidator = InputAdapterServiceDataHolder.getInputAdapterExtensionService().getDefaultContentValidator();
|
||||
} else {
|
||||
contentValidator = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getContentValidator(contentValidatorType);
|
||||
}
|
||||
|
||||
String contentTransformerClassName = this.xmppServerConnectionConfiguration.getContentTransformerClassName();
|
||||
if (contentTransformerClassName != null && contentTransformerClassName.equals(XMPPEventAdapterConstants.DEFAULT)) {
|
||||
contentTransformer = new DefaultContentTransformer();
|
||||
} else if (contentTransformerClassName != null && !contentTransformerClassName.isEmpty()) {
|
||||
try {
|
||||
Class<? extends ContentTransformer> contentTransformerClass = Class.forName(contentTransformerClassName)
|
||||
.asSubclass(ContentTransformer.class);
|
||||
contentTransformer = contentTransformerClass.newInstance();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new XMPPContentInitializationException(
|
||||
"Unable to find the class transformer: " + contentTransformerClassName, e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new XMPPContentInitializationException(
|
||||
"Unable to create an instance of :" + contentTransformerClassName, e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new XMPPContentInitializationException("Access of the instance in not allowed.", e);
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
log.error("Exception occurred while subscribing to MQTT broker at "
|
||||
+ xmppServerConnectionConfiguration.getHost());
|
||||
throw new InputEventAdapterRuntimeException(e);
|
||||
String contentTransformerType = this.xmppServerConnectionConfiguration.getContentTransformerClassName();
|
||||
if (contentTransformer == null || contentTransformerType.equals(XMPPEventAdapterConstants.DEFAULT)) {
|
||||
this.contentTransformer = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getDefaultContentTransformer();
|
||||
} else {
|
||||
this.contentTransformer = InputAdapterServiceDataHolder.getInputAdapterExtensionService()
|
||||
.getContentTransformer(contentTransformerType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -29,8 +29,8 @@ timeoutInterval.hint=used by listeners to the server and for reconnection schedu
|
||||
resource=Resource
|
||||
resource.hint=specific to the XMPP-Account to which the login is made to.
|
||||
contentValidator=contentValidation
|
||||
contentValidator.hint=Class Name of the content Validation or 'default' to set default class, required to implement (if required)
|
||||
contentValidator.hint=Type of the content Validation or 'default' to set default type, required to implement (if required)
|
||||
jid=jid
|
||||
jid.hint=JID - XMPP Account Name.
|
||||
contentTransformer=contentTransformer
|
||||
contentTransformer.hint=Class Name of the content transformer or 'default' to set default class, required to implement (if required)
|
||||
contentTransformer.hint=Type of the content transformer or 'default' to set default type, required to implement (if required)
|
||||
@ -33,6 +33,10 @@
|
||||
<url>http://wso2.org</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-codec.wso2</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.analytics-common</groupId>
|
||||
<artifactId>org.wso2.carbon.event.output.adapter.core</artifactId>
|
||||
@ -61,6 +65,10 @@
|
||||
<groupId>com.googlecode.json-simple.wso2</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.identity.jwt.client.extension</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
@ -121,7 +129,9 @@
|
||||
org.apache.commons.codec.binary,
|
||||
org.apache.http.client.entity,
|
||||
org.apache.http.message,
|
||||
org.apache.commons.ssl
|
||||
org.apache.commons.ssl,
|
||||
org.wso2.carbon.identity.jwt.client.extension.*,
|
||||
org.wso2.carbon.user.api
|
||||
</Import-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
|
||||
@ -122,7 +122,7 @@ public class MQTTEventAdapter implements OutputEventAdapter {
|
||||
String clientId = eventAdapterConfiguration.getStaticProperties().get(
|
||||
MQTTEventAdapterConstants.ADAPTER_CONF_CLIENTID);
|
||||
qos = eventAdapterConfiguration.getStaticProperties().get(MQTTEventAdapterConstants.ADAPTER_MESSAGE_QOS);
|
||||
mqttAdapterPublisher = new MQTTAdapterPublisher(mqttBrokerConnectionConfiguration, clientId);
|
||||
mqttAdapterPublisher = new MQTTAdapterPublisher(mqttBrokerConnectionConfiguration, clientId, tenantId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -56,19 +56,20 @@ public class MQTTEventAdapterFactory extends OutputEventAdapterFactory {
|
||||
//Broker Username
|
||||
Property userName = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME);
|
||||
userName.setDisplayName(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME));
|
||||
userName.setRequired(true);
|
||||
userName.setRequired(false);
|
||||
userName.setHint(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME_HINT));
|
||||
|
||||
//Broker Password
|
||||
Property password = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD);
|
||||
password.setDisplayName(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD));
|
||||
password.setRequired(true);
|
||||
password.setRequired(false);
|
||||
password.setHint(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD_HINT));
|
||||
|
||||
//Broker Connection Scopes
|
||||
Property scopes = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_SCOPES);
|
||||
scopes.setDisplayName(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_SCOPES));
|
||||
scopes.setRequired(true);
|
||||
scopes.setRequired(false);
|
||||
scopes.setDefaultValue("default");
|
||||
scopes.setHint(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_SCOPES_HINT));
|
||||
|
||||
// set clientId
|
||||
|
||||
@ -22,10 +22,15 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.osgi.service.component.ComponentContext;
|
||||
import org.wso2.carbon.device.mgt.output.adapter.mqtt.MQTTEventAdapterFactory;
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterFactory;
|
||||
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
|
||||
/**
|
||||
* @scr.component component.name="output.Mqtt.AdapterService.component" immediate="true"
|
||||
* @scr.reference name="jwt.client.service" interface="org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService"
|
||||
* cardinality="1..1"
|
||||
* policy="dynamic"
|
||||
* bind="setJWTClientManagerService"
|
||||
* unbind="unsetJWTClientManagerService"
|
||||
*/
|
||||
public class MQTTEventAdapterServiceComponent {
|
||||
|
||||
@ -48,4 +53,12 @@ public class MQTTEventAdapterServiceComponent {
|
||||
}
|
||||
}
|
||||
|
||||
protected void setJWTClientManagerService(JWTClientManagerService jwtClientManagerService) {
|
||||
OutputAdapterServiceDataHolder.setJwtClientManagerService(jwtClientManagerService);
|
||||
}
|
||||
|
||||
protected void unsetJWTClientManagerService(JWTClientManagerService jwtClientManagerService) {
|
||||
OutputAdapterServiceDataHolder.setJwtClientManagerService(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy
|
||||
* of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.output.adapter.mqtt.internal;
|
||||
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
|
||||
/**
|
||||
* common place to hold some OSGI service references.
|
||||
*/
|
||||
public final class OutputAdapterServiceDataHolder {
|
||||
|
||||
private static JWTClientManagerService jwtClientManagerService;
|
||||
|
||||
public static JWTClientManagerService getJwtClientManagerService() {
|
||||
return jwtClientManagerService;
|
||||
}
|
||||
|
||||
public static void setJwtClientManagerService(
|
||||
JWTClientManagerService jwtClientManagerService) {
|
||||
OutputAdapterServiceDataHolder.jwtClientManagerService = jwtClientManagerService;
|
||||
}
|
||||
}
|
||||
@ -21,14 +21,11 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.commons.ssl.Base64;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
@ -38,9 +35,14 @@ import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.output.adapter.mqtt.internal.OutputAdapterServiceDataHolder;
|
||||
import org.wso2.carbon.event.output.adapter.core.exception.ConnectionUnavailableException;
|
||||
import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException;
|
||||
import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterRuntimeException;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
@ -48,8 +50,6 @@ import java.net.URL;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* MQTT publisher related configuration initialization and publishing capabilties are implemented here.
|
||||
@ -60,8 +60,11 @@ public class MQTTAdapterPublisher {
|
||||
private MqttClient mqttClient;
|
||||
private MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration;
|
||||
String clientId;
|
||||
int tenantId;
|
||||
|
||||
public MQTTAdapterPublisher(MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration, String clientId) {
|
||||
public MQTTAdapterPublisher(MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration, String clientId
|
||||
, int tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
if (clientId == null || clientId.trim().isEmpty()) {
|
||||
this.clientId = MqttClient.generateClientId();
|
||||
}
|
||||
@ -146,7 +149,6 @@ public class MQTTAdapterPublisher {
|
||||
String password = this.mqttBrokerConnectionConfiguration.getPassword();
|
||||
String dcrUrlString = this.mqttBrokerConnectionConfiguration.getDcrUrl();
|
||||
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
||||
if (dcrUrlString != null && !dcrUrlString.isEmpty()) {
|
||||
try {
|
||||
URL dcrUrl = new URL(dcrUrlString);
|
||||
@ -157,8 +159,16 @@ public class MQTTAdapterPublisher {
|
||||
registrationProfile.setGrantType(MQTTEventAdapterConstants.GRANT_TYPE);
|
||||
registrationProfile.setOwner(username);
|
||||
registrationProfile.setTokenScope(MQTTEventAdapterConstants.TOKEN_SCOPE);
|
||||
registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX
|
||||
+ mqttBrokerConnectionConfiguration.getAdapterName() + "_" + tenantId);
|
||||
if (!mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) {
|
||||
registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX
|
||||
+ mqttBrokerConnectionConfiguration.getAdapterName() +
|
||||
"_" + tenantId);
|
||||
registrationProfile.setIsSaasApp(false);
|
||||
} else {
|
||||
registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX
|
||||
+ mqttBrokerConnectionConfiguration.getAdapterName());
|
||||
registrationProfile.setIsSaasApp(true);
|
||||
}
|
||||
String jsonString = registrationProfile.toJSON();
|
||||
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
|
||||
postMethod.setEntity(requestEntity);
|
||||
@ -186,40 +196,34 @@ public class MQTTAdapterPublisher {
|
||||
throw new OutputEventAdapterRuntimeException("Invalid dcrUrl : " + dcrUrlString);
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | IOException e) {
|
||||
throw new OutputEventAdapterRuntimeException("Failed to create an https connection.", e);
|
||||
} catch (JWTClientException | UserStoreException e) {
|
||||
log.error("Failed to create an oauth token with jwt grant type.", e);
|
||||
}
|
||||
}
|
||||
throw new OutputEventAdapterRuntimeException("Invalid configuration for mqtt publisher");
|
||||
}
|
||||
|
||||
private String getToken(String clientId, String clientSecret)
|
||||
throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, ParseException {
|
||||
URL tokenEndpoint = new URL(mqttBrokerConnectionConfiguration.getTokenUrl());
|
||||
HttpClient httpClient = MQTTUtil.getHttpClient(tokenEndpoint.getProtocol());
|
||||
HttpPost postMethod = new HttpPost(tokenEndpoint.toString());
|
||||
throws UserStoreException, JWTClientException {
|
||||
PrivilegedCarbonContext.startTenantFlow();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
||||
try {
|
||||
String scopes = mqttBrokerConnectionConfiguration.getScopes();
|
||||
String username = mqttBrokerConnectionConfiguration.getUsername();
|
||||
if (mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) {
|
||||
username = PrivilegedCarbonContext.getThreadLocalCarbonContext()
|
||||
.getUserRealm().getRealmConfiguration().getAdminUserName() + "@" + PrivilegedCarbonContext
|
||||
.getThreadLocalCarbonContext().getTenantDomain(true);
|
||||
}
|
||||
|
||||
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
||||
nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.GRANT_TYPE_PARAM_NAME,
|
||||
MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE));
|
||||
nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE_USERNAME,
|
||||
mqttBrokerConnectionConfiguration.getUsername()));
|
||||
nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE_PASSWORD,
|
||||
mqttBrokerConnectionConfiguration.getPassword()));
|
||||
String scopes = mqttBrokerConnectionConfiguration.getScopes();
|
||||
if (scopes != null && !scopes.isEmpty()) {
|
||||
nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE_SCOPES, scopes));
|
||||
JWTClientManagerService jwtClientManagerService =
|
||||
OutputAdapterServiceDataHolder.getJwtClientManagerService();
|
||||
AccessTokenInfo accessTokenInfo = jwtClientManagerService.getJWTClient().getAccessToken(
|
||||
clientId, clientSecret, username, scopes);
|
||||
return accessTokenInfo.getAccessToken();
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
|
||||
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
||||
postMethod.addHeader("Authorization", "Basic " + getBase64Encode(clientId, clientSecret));
|
||||
postMethod.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
HttpResponse httpResponse = httpClient.execute(postMethod);
|
||||
String response = MQTTUtil.getResponseString(httpResponse);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(response);
|
||||
}
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
JSONObject jsonObject = (JSONObject) jsonParser.parse(response);
|
||||
return (String) jsonObject.get(MQTTEventAdapterConstants.ACCESS_TOKEN_GRANT_TYPE_PARAM_NAME);
|
||||
}
|
||||
|
||||
private String getBase64Encode(String key, String value) {
|
||||
|
||||
@ -33,6 +33,8 @@ public class MQTTBrokerConnectionConfiguration {
|
||||
private String tokenUrl;
|
||||
private boolean cleanSession = true;
|
||||
private int keepAlive;
|
||||
private boolean globalCredentailSet;
|
||||
|
||||
public String getTokenUrl() {
|
||||
return tokenUrl;
|
||||
}
|
||||
@ -69,11 +71,20 @@ public class MQTTBrokerConnectionConfiguration {
|
||||
return adapterName;
|
||||
}
|
||||
|
||||
public boolean isGlobalCredentailSet() {
|
||||
return globalCredentailSet;
|
||||
}
|
||||
|
||||
public MQTTBrokerConnectionConfiguration(OutputEventAdapterConfiguration eventAdapterConfiguration,
|
||||
Map<String, String> globalProperties) {
|
||||
adapterName = eventAdapterConfiguration.getName();
|
||||
this.username = eventAdapterConfiguration.getStaticProperties().get(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME);
|
||||
this.password = eventAdapterConfiguration.getStaticProperties().get(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD);
|
||||
if ((username == null || username.isEmpty()) && (password == null || password.isEmpty())) {
|
||||
username = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME);
|
||||
password = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD);
|
||||
globalCredentailSet = true;
|
||||
}
|
||||
String url = eventAdapterConfiguration .getStaticProperties().get(MQTTEventAdapterConstants.ADAPTER_CONF_URL);
|
||||
if (url == null || url.isEmpty()) {
|
||||
url = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_URL);
|
||||
|
||||
@ -58,7 +58,7 @@ public final class MQTTEventAdapterConstants {
|
||||
|
||||
public static final String DEFAULT_CALLBACK = "";
|
||||
public static final String DEFAULT_PASSWORD = "";
|
||||
public static final String GRANT_TYPE = "password";
|
||||
public static final String GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer";
|
||||
public static final String TOKEN_SCOPE = "production";
|
||||
public static final String APPLICATION_NAME_PREFIX = "OutputAdapter_";
|
||||
public static final String CLIENT_ID = "clientId";
|
||||
|
||||
@ -21,10 +21,10 @@ package org.wso2.carbon.device.mgt.output.adapter.mqtt.util;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class PropertyUtils {
|
||||
public class PropertyUtils {
|
||||
|
||||
//This method is only used if the mb features are within DAS.
|
||||
static String replaceMqttProperty(String urlWithPlaceholders) {
|
||||
public static String replaceMqttProperty(String urlWithPlaceholders) {
|
||||
String regex = "\\$\\{(.*?)\\}";
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matchPattern = pattern.matcher(urlWithPlaceholders);
|
||||
|
||||
@ -12,6 +12,7 @@ public class RegistrationProfile {
|
||||
private String owner;
|
||||
private String grantType;
|
||||
private String applicationType;
|
||||
private boolean isSaasApp;
|
||||
|
||||
private static final String TAG = RegistrationProfile.class.getSimpleName();
|
||||
|
||||
@ -63,11 +64,19 @@ public class RegistrationProfile {
|
||||
this.applicationType = applicationType;
|
||||
}
|
||||
|
||||
public String toJSON() {
|
||||
public boolean isSaasApp() {
|
||||
return isSaasApp;
|
||||
}
|
||||
|
||||
public void setIsSaasApp(boolean isSaasApp) {
|
||||
this.isSaasApp = isSaasApp;
|
||||
}
|
||||
|
||||
public String toJSON() {
|
||||
String jsonString =
|
||||
"{\"callbackUrl\": \"" + callbackUrl + "\",\"clientName\": \"" + clientName + "\", \"tokenScope\": " +
|
||||
"\"" + tokenScope + "\", \"owner\": \"" + owner + "\"," + "\"grantType\": \"" + grantType +
|
||||
"\", \"saasApp\" :false }\n";
|
||||
"\", \"saasApp\" : " + isSaasApp + " }\n";
|
||||
return jsonString;
|
||||
}
|
||||
}
|
||||
@ -169,7 +169,8 @@
|
||||
feign.auth,
|
||||
feign.codec,
|
||||
feign.gson,
|
||||
javax.cache
|
||||
javax.cache,
|
||||
javax.net.ssl
|
||||
</Import-Package>
|
||||
<DynamicImport-Package>*</DynamicImport-Package>
|
||||
<Embed-Dependency>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user