Added IoT plugins feature and refactored IoT base.

revert-dabc3590
ayyoob 9 years ago
parent 0bf219c93a
commit c05191dac5

@ -3,15 +3,14 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>device-mgt-iot-androidsense</artifactId> <artifactId>androidsense-plugin</artifactId>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<version>2.0.4-SNAPSHOT</version> <version>2.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.device.mgt.iot.androidsense.service.impl</artifactId> <artifactId>org.wso2.carbon.device.mgt.iot.androidsense.service.impl</artifactId>
<version>2.0.4-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<name>WSO2 Carbon - Android Sense API</name> <name>WSO2 Carbon - Android Sense API</name>
<description>WSO2 Carbon - Android Sense Service-API Implementation</description> <description>WSO2 Carbon - Android Sense Service-API Implementation</description>
@ -98,7 +97,6 @@
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId> <artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId>
<version>1.1.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>

@ -141,6 +141,22 @@ public class AndroidSenseMQTTConnector extends MQTTTransportHandler {
String sensorName = null; String sensorName = null;
PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
DeviceManagementProviderService deviceManagementProviderService =
(DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null);
if (deviceManagementProviderService != null) {
DeviceIdentifier identifier = new DeviceIdentifier(deviceId, AndroidSenseConstants.DEVICE_TYPE);
Device device = deviceManagementProviderService.getDevice(identifier);
if (device != null) {
String owner = device.getEnrolmentInfo().getOwner();
ctx.setUsername(owner);
ctx.setTenantDomain(MultitenantUtils.getTenantDomain(owner), true);
} else {
return;
}
} else {
return;
}
ctx.setTenantDomain("carbon.super", true); ctx.setTenantDomain("carbon.super", true);
for (SensorData sensor : sensorData) { for (SensorData sensor : sensorData) {
if (sensor.key.equals("battery")) { if (sensor.key.equals("battery")) {
@ -254,6 +270,8 @@ public class AndroidSenseMQTTConnector extends MQTTTransportHandler {
} }
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
throw new TransportHandlerException("Invalid message format " + mqttMessage.toString()); throw new TransportHandlerException("Invalid message format " + mqttMessage.toString());
} catch (DeviceManagementException e) {
throw new TransportHandlerException("Invalid device id " + deviceId);
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }

@ -32,6 +32,7 @@ import java.util.List;
public class ArduinoManagerService implements DeviceManagementService { public class ArduinoManagerService implements DeviceManagementService {
private DeviceManager deviceManager; private DeviceManager deviceManager;
private final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super";
@Override @Override
public String getType() { public String getType() {
@ -40,7 +41,7 @@ public class ArduinoManagerService implements DeviceManagementService {
@Override @Override
public String getProviderTenantDomain() { public String getProviderTenantDomain() {
return "carbon.super"; return DEVICE_TYPE_PROVIDER_DOMAIN;
} }
@Override @Override

@ -21,15 +21,14 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>device-mgt-iot-arduino</artifactId> <artifactId>arduino-plugin</artifactId>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<version>2.0.4-SNAPSHOT</version> <version>2.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.device.mgt.iot.arduino.service.impl</artifactId> <artifactId>org.wso2.carbon.device.mgt.iot.arduino.service.impl</artifactId>
<version>2.0.4-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<name>WSO2 Carbon - IoT Server Arduino Service API</name> <name>WSO2 Carbon - IoT Server Arduino Service API</name>
<description>WSO2 Carbon - Arduino Service API Implementation</description> <description>WSO2 Carbon - Arduino Service API Implementation</description>
@ -53,11 +52,6 @@
<artifactId>org.wso2.carbon.device.mgt.extensions</artifactId> <artifactId>org.wso2.carbon.device.mgt.extensions</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.apimgt.application.extension</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.jwt.client.extension</artifactId> <artifactId>org.wso2.carbon.device.mgt.jwt.client.extension</artifactId>
@ -90,7 +84,6 @@
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId> <artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId>
<version>1.1.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
@ -162,11 +155,6 @@
<artifactId>jsr311-api</artifactId> <artifactId>jsr311-api</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.apimgt.annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.apimgt.webapp.publisher</artifactId> <artifactId>org.wso2.carbon.apimgt.webapp.publisher</artifactId>

@ -30,6 +30,8 @@ import java.util.List;
public class DigitalDisplayManagerService implements DeviceManagementService{ public class DigitalDisplayManagerService implements DeviceManagementService{
private DeviceManager deviceManager; private DeviceManager deviceManager;
private final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super";
@Override @Override
public String getType() { public String getType() {
return DigitalDisplayConstants.DEVICE_TYPE; return DigitalDisplayConstants.DEVICE_TYPE;
@ -37,7 +39,7 @@ public class DigitalDisplayManagerService implements DeviceManagementService{
@Override @Override
public String getProviderTenantDomain() { public String getProviderTenantDomain() {
return "carbon.super"; return DEVICE_TYPE_PROVIDER_DOMAIN;
} }
@Override @Override

@ -4,15 +4,14 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>device-mgt-iot-digitaldisplay</artifactId> <artifactId>digital-display-plugin</artifactId>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<version>2.0.4-SNAPSHOT</version> <version>2.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.device.mgt.iot.digitaldisplay.service.impl</artifactId> <artifactId>org.wso2.carbon.device.mgt.iot.digitaldisplay.service.impl</artifactId>
<version>2.0.4-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<name>WSO2 Carbon - IoT Server DigitalDisplay API</name> <name>WSO2 Carbon - IoT Server DigitalDisplay API</name>
<description>WSO2 Carbon - Digital Display Service API Implementation</description> <description>WSO2 Carbon - Digital Display Service API Implementation</description>
@ -49,23 +48,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.apimgt.application.extension</artifactId> <artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.jwt.client.extension</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.apimgt.application.extension</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.analytics</artifactId>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>

@ -29,9 +29,10 @@ import org.wso2.carbon.device.mgt.iot.droneanalyzer.plugin.constants.DroneConsta
import java.util.List; import java.util.List;
public class DroneAnalyzerManagerService implements DeviceManagementService { public class DroneAnalyzerManagerService implements DeviceManagementService {
private DeviceManager deviceManager; private DeviceManager deviceManager;
private final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super";
@Override @Override
public String getType() { public String getType() {
return DroneConstants.DEVICE_TYPE; return DroneConstants.DEVICE_TYPE;
@ -95,7 +96,7 @@ public class DroneAnalyzerManagerService implements DeviceManagementService {
@Override @Override
public String getProviderTenantDomain() { public String getProviderTenantDomain() {
return "carbon.super"; return DEVICE_TYPE_PROVIDER_DOMAIN;
} }
@Override @Override

@ -1,14 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent> <parent>
<artifactId>device-mgt-iot-droneanalyzer</artifactId> <artifactId>drone-analyzer-plugin</artifactId>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<version>2.0.4-SNAPSHOT</version> <version>2.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.device.mgt.iot.droneanalyzer.service.impl</artifactId> <artifactId>org.wso2.carbon.device.mgt.iot.droneanalyzer.service.impl</artifactId>
<version>2.0.4-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<name>WSO2 Carbon - IoT Server Drone Analyzer API</name> <name>WSO2 Carbon - IoT Server Drone Analyzer API</name>
<url>http://maven.apache.org</url> <url>http://maven.apache.org</url>
@ -35,7 +34,6 @@
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId> <artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId>
<version>1.1.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
@ -229,11 +227,6 @@
<artifactId>org.wso2.carbon.device.mgt.extensions</artifactId> <artifactId>org.wso2.carbon.device.mgt.extensions</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.apimgt.application.extension</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.jwt.client.extension</artifactId> <artifactId>org.wso2.carbon.device.mgt.jwt.client.extension</artifactId>

@ -30,7 +30,6 @@ import java.io.File;
public class DroneAnalyzerServiceUtils { public class DroneAnalyzerServiceUtils {
private static final String SUPER_TENANT = "carbon.super";
private static org.apache.commons.logging.Log log = LogFactory.getLog(DroneAnalyzerServiceUtils.class); private static org.apache.commons.logging.Log log = LogFactory.getLog(DroneAnalyzerServiceUtils.class);
public static void sendCommandViaXMPP(String deviceOwner, String deviceId, String resource, public static void sendCommandViaXMPP(String deviceOwner, String deviceId, String resource,

@ -1,42 +0,0 @@
/*
* 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.iot.api;
public class DeviceUsageDTO {
private String time;
private String value;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

@ -20,12 +20,7 @@ package org.wso2.carbon.device.mgt.iot.api;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -33,56 +28,20 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager; import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord; import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord;
import org.wso2.carbon.device.mgt.iot.util.APIUtil;
import javax.jws.WebService; import javax.ws.rs.*;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@WebService public class DevicesManagerService {
private static Log log = LogFactory.getLog(DevicesManagerService.class);
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
private PrivilegedCarbonContext ctx;
private DeviceManagementProviderService getServiceProvider() { public class DevicesManagerService {
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
PrivilegedCarbonContext.startTenantFlow();
ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
ctx.setTenantDomain(tenantDomain, true);
if (log.isDebugEnabled()) {
log.debug("Getting thread local carbon context for tenant domain: " + tenantDomain);
}
return (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null);
}
private void endTenantFlow() { private static final Log log = LogFactory.getLog(DevicesManagerService.class);
PrivilegedCarbonContext.endTenantFlow();
ctx = null;
if (log.isDebugEnabled()) {
log.debug("Tenant flow ended");
}
}
private Device[] getActiveDevices(List<Device> devices){ private Device[] getActiveDevices(List<Device> devices) {
List<Device> activeDevices = new ArrayList<>(); List<Device> activeDevices = new ArrayList<>();
if (devices != null) { if (devices != null) {
for (Device device : devices) { for (Device device : devices) {
@ -94,29 +53,27 @@ import java.util.List;
return activeDevices.toArray(new Device[activeDevices.size()]); return activeDevices.toArray(new Device[activeDevices.size()]);
} }
@Path("/device/user/{username}/all") @Path("/devices/users/{userName}")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Device[] getDevicesOfUser(@PathParam("username") String username) { public Response getDevicesOfUser(@PathParam("userName") String userName) {
try { try {
List<Device> devices = this.getServiceProvider().getDevicesOfUser(username); List<Device> devices = APIUtil.getDeviceManagementService()
return this.getActiveDevices(devices); .getDevicesOfUser(userName);
Device[] devicesArr = this.getActiveDevices(devices);
return Response.status(Response.Status.OK).entity(devicesArr).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/user/{username}/all/count") @Path("/devices/users/{userName}/count")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public int getDeviceCount(@PathParam("username") String username){ public Response getDeviceCount(@PathParam("userName") String userName) {
try { try {
List<Device> devices = this.getServiceProvider().getDevicesOfUser(username); List<Device> devices = APIUtil.getDeviceManagementService()
.getDevicesOfUser(userName);
if (devices != null) { if (devices != null) {
List<Device> activeDevices = new ArrayList<>(); List<Device> activeDevices = new ArrayList<>();
for (Device device : devices) { for (Device device : devices) {
@ -124,191 +81,182 @@ import java.util.List;
activeDevices.add(device); activeDevices.add(device);
} }
} }
return activeDevices.size(); return Response.status(Response.Status.OK).entity(activeDevices.size()).build();
} }
return 0; return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return 0;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}") @Path("/devices/{deviceType}/{identifier}")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Device getDevice(@PathParam("type") String type, @PathParam("identifier") String identifier){ public Response getDevice(@PathParam("deviceType") String deviceType,
@PathParam("identifier") String identifier) {
try{ try {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(identifier); deviceIdentifier.setId(identifier);
deviceIdentifier.setType(type); deviceIdentifier.setType(deviceType);
return this.getServiceProvider().getDevice(deviceIdentifier); Device device = APIUtil.getDeviceManagementService().getDevice(
deviceIdentifier);
if (device != null) {
return Response.status(Response.Status.OK).entity(device).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/all") @Path("/devices/types")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public DeviceType[] getDeviceTypes(){ public Response getDeviceTypes() {
try{ try {
List<DeviceType> deviceTypes = this.getServiceProvider().getAvailableDeviceTypes(); List<DeviceType> deviceTypes = APIUtil.getDeviceManagementService()
return deviceTypes.toArray(new DeviceType[deviceTypes.size()]); .getAvailableDeviceTypes();
DeviceType[] deviceTypesArr = deviceTypes.toArray(new DeviceType[deviceTypes.size()]);
return Response.status(Response.Status.OK).entity(deviceTypesArr).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/all") @Path("/devices/{deviceType}")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Device[] getAllDevices(@PathParam("type") String type){ public Response getAllDevices(@PathParam("deviceType") String deviceType) {
try{ try {
List<Device> devices = this.getServiceProvider().getAllDevices(type); List<Device> devices = APIUtil.getDeviceManagementService()
return this.getActiveDevices(devices); .getAllDevices(deviceType);
Device[] devicesArr = this.getActiveDevices(devices);
return Response.status(Response.Status.OK).entity(devicesArr).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/all") @Path("/devices")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Device[] getAllDevices(){ public Response getAllDevices() {
try{ try {
List<Device> devices = this.getServiceProvider().getAllDevices(); List<Device> devices = APIUtil.getDeviceManagementService()
return this.getActiveDevices(devices); .getAllDevices();
return Response.status(Response.Status.OK).entity(this.getActiveDevices(devices)).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/config") @Path("/devices/{deviceType}/config")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public TenantConfiguration getConfiguration(@PathParam("type") String type){ public Response getConfiguration(@PathParam("deviceType") String deviceType) {
try { try {
return this.getServiceProvider().getConfiguration(type); TenantConfiguration tenantConfiguration = APIUtil.getDeviceManagementService().getConfiguration(deviceType);
if (tenantConfiguration != null) {
return Response.status(Response.Status.OK).entity(tenantConfiguration).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/role/{role}/all") @Path("/devices/search/role")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Device[] getAllDevicesOfRole(@PathParam("role") String roleName){ public Response getAllDevicesOfRole(@QueryParam("roleName") String roleName) {
try{ try {
List<Device> devices = this.getServiceProvider().getAllDevicesOfRole(roleName); List<Device> devices = APIUtil.getDeviceManagementService()
return this.getActiveDevices(devices); .getAllDevicesOfRole(roleName);
Device[] devicesArr = this.getActiveDevices(devices);
return Response.status(Response.Status.OK).entity(devicesArr).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/name/{name}/all") @Path("/devices/search/name")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Device[] getDevicesByName(@PathParam("name") String name) { public Response getDevicesByName(@PathParam("deviceName") String deviceName) {
try{ try {
List<Device> devices = this.getServiceProvider().getDevicesByName(name); List<Device> devices = APIUtil.getDeviceManagementService()
return this.getActiveDevices(devices); .getDevicesByName(deviceName);
Device[] devicesArr = this.getActiveDevices(devices);
return Response.status(Response.Status.OK).entity(devicesArr).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}/status") @Path("/devices/{deviceType}/{identifier}/status")
@PUT @PUT
@Consumes("application/json") @Consumes("application/json")
@Produces("application/json") @Produces("application/json")
void updateDeviceEnrolmentInfo(@PathParam("type") String type, @PathParam("identifier") String identifier, public Response updateDeviceEnrolmentInfo(@PathParam("deviceType") String deviceType,
@PathParam("identifier") String identifier,
@FormParam("status") EnrolmentInfo.Status status) { @FormParam("status") EnrolmentInfo.Status status) {
DeviceManagementProviderService providerService = this.getServiceProvider(); try {
DeviceManagementProviderService providerService = APIUtil.getDeviceManagementService();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(type); deviceIdentifier.setType(deviceType);
deviceIdentifier.setId(identifier); deviceIdentifier.setId(identifier);
try {
Device device = providerService.getDevice(deviceIdentifier); Device device = providerService.getDevice(deviceIdentifier);
providerService.updateDeviceEnrolmentInfo(device, status); providerService.updateDeviceEnrolmentInfo(device, status);
return Response.status(Response.Status.NO_CONTENT).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/status/{status}/all") @Path("/devices/search/status")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Device[] getDevicesByStatus(@PathParam("status") EnrolmentInfo.Status status) { public Response getDevicesByStatus(@QueryParam("status") EnrolmentInfo.Status status) {
try{ try {
List<Device> devices = this.getServiceProvider().getDevicesByStatus(status); List<Device> devices = APIUtil.getDeviceManagementService()
return this.getActiveDevices(devices); .getDevicesByStatus(status);
Device[] devicesArr = this.getActiveDevices(devices);
return Response.status(Response.Status.OK).entity(devicesArr).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/license") @Path("/devices/{deviceType}/license")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public License getLicense(@PathParam("type") String type, @QueryParam("languageCode") String languageCode) { public Response getLicense(@PathParam("deviceType") String deviceType,
try{ @QueryParam("languageCode") String languageCode) {
return this.getServiceProvider().getLicense(type, languageCode); try {
License license = APIUtil.getDeviceManagementService().getLicense(
deviceType, languageCode);
if (license != null) {
return Response.status(Response.Status.OK).entity(license).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/license") @Path("/devices/{deviceType}/license")
@POST @POST
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public void addLicense(@PathParam("type") String type, @FormParam("provider") String provider, public Response addLicense(@PathParam("deviceType") String deviceType,
@FormParam("name") String name, @FormParam("version") String version, @FormParam("provider") String provider, @FormParam("name") String name,
@FormParam("language") String language, @FormParam("validFrom") Date validFrom, @FormParam("version") String version, @FormParam("language") String language,
@FormParam("validTo") Date validTo, @FormParam("text") String text) { @FormParam("validFrom") Date validFrom, @FormParam("validTo") Date validTo,
try{ @FormParam("text") String text) {
try {
License license = new License(); License license = new License();
license.setProvider(provider); license.setProvider(provider);
license.setName(name); license.setName(name);
@ -317,20 +265,21 @@ import java.util.List;
license.setValidFrom(validFrom); license.setValidFrom(validFrom);
license.setValidTo(validTo); license.setValidTo(validTo);
license.setText(text); license.setText(text);
this.getServiceProvider().addLicense(type, license); APIUtil.getDeviceManagementService().addLicense(deviceType, license);
return Response.status(Response.Status.NO_CONTENT).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}") @Path("/devices/{deviceType}/{identifier}/enrollment")
@PUT @PUT
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
boolean modifyEnrollment(@PathParam("type") String type, @PathParam("identifier") String identifier, public Response modifyEnrollment(@PathParam("deviceType") String deviceType,
@FormParam("name") String name, @FormParam("description") String description, @PathParam("identifier") String identifier,
@FormParam("name") String name,
@FormParam("description") String description,
@FormParam("groupId") int groupId,
@FormParam("enrollmentId") int enrollmentId, @FormParam("enrollmentId") int enrollmentId,
@FormParam("dateOfEnrolment") long dateOfEnrolment, @FormParam("dateOfEnrolment") long dateOfEnrolment,
@FormParam("dateOfLastUpdate") long dateOfLastUpdate, @FormParam("dateOfLastUpdate") long dateOfLastUpdate,
@ -347,27 +296,30 @@ import java.util.List;
enrolmentInfo.setOwner(owner); enrolmentInfo.setOwner(owner);
Device device = new Device(); Device device = new Device();
device.setType(type); device.setType(deviceType);
device.setDeviceIdentifier(identifier); device.setDeviceIdentifier(identifier);
device.setName(name); device.setName(name);
device.setDescription(description); device.setDescription(description);
device.setEnrolmentInfo(enrolmentInfo); device.setEnrolmentInfo(enrolmentInfo);
try { try {
return this.getServiceProvider().modifyEnrollment(device); boolean isModified = APIUtil.getDeviceManagementService()
.modifyEnrollment(device);
if (isModified) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device") @Path("/devices/{deviceType}/{identifier}/enrollment")
@POST @POST
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
boolean enrollDevice(@FormParam("type") String type, @FormParam("identifier") String identifier, public Response enrollDevice(@PathParam("deviceType") String deviceType, @PathParam("identifier") String identifier,
@FormParam("name") String name, @FormParam("description") String description, @FormParam("name") String name, @FormParam("description") String description,
@FormParam("groupId") int groupId,
@FormParam("enrollmentId") int enrollmentId, @FormParam("enrollmentId") int enrollmentId,
@FormParam("dateOfEnrolment") long dateOfEnrolment, @FormParam("dateOfEnrolment") long dateOfEnrolment,
@FormParam("dateOfLastUpdate") long dateOfLastUpdate, @FormParam("dateOfLastUpdate") long dateOfLastUpdate,
@ -384,196 +336,231 @@ import java.util.List;
enrolmentInfo.setOwner(owner); enrolmentInfo.setOwner(owner);
Device device = new Device(); Device device = new Device();
device.setType(type); device.setType(deviceType);
device.setDeviceIdentifier(identifier); device.setDeviceIdentifier(identifier);
device.setName(name); device.setName(name);
device.setDescription(description); device.setDescription(description);
device.setEnrolmentInfo(enrolmentInfo); device.setEnrolmentInfo(enrolmentInfo);
try { try {
return this.getServiceProvider().enrollDevice(device); boolean isModified = APIUtil.getDeviceManagementService().enrollDevice(
device);
if (isModified) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/tenantconfiguration") @Path("/devices/tenant/configuration")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public TenantConfiguration getConfiguration(){ public Response getTenantConfiguration() {
try { try {
return this.getServiceProvider().getConfiguration(); TenantConfiguration tenantConfiguration = APIUtil.getDeviceManagementService().getConfiguration();
if (tenantConfiguration != null) {
return Response.status(Response.Status.OK).entity(tenantConfiguration).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/tenantconfiguration") @Path("/devices/tenant/configuration")
@POST @POST
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean saveConfiguration(@FormParam("tenantConfiguration") TenantConfiguration tenantConfiguration){ public Response saveTenantConfiguration(@FormParam("tenantConfiguration") TenantConfiguration tenantConfiguration) {
try { try {
return this.getServiceProvider().saveConfiguration(tenantConfiguration); boolean isSaved = APIUtil.getDeviceManagementService()
.saveConfiguration(tenantConfiguration);
if (isSaved) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}") @Path("/devices/{deviceType}/{identifier}")
@DELETE @DELETE
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean disenrollDevice(@PathParam("type") String type, @PathParam("identifier") String identifier){ public Response disenrollDevice(@PathParam("deviceType") String deviceType,
@PathParam("identifier") String identifier) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(type); deviceIdentifier.setType(deviceType);
deviceIdentifier.setId(identifier); deviceIdentifier.setId(identifier);
try { try {
return this.getServiceProvider().disenrollDevice(deviceIdentifier); boolean isDisEnrolled = APIUtil.getDeviceManagementService()
.disenrollDevice(deviceIdentifier);
if (isDisEnrolled) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}/enrolled") @Path("/devices/{deviceType}/{identifier}/enrollment")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean isEnrolled(@PathParam("type") String type, @PathParam("identifier") String identifier){ public Response isEnrolled(@PathParam("deviceType") String deviceType,
@PathParam("identifier") String identifier) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(type); deviceIdentifier.setType(deviceType);
deviceIdentifier.setId(identifier); deviceIdentifier.setId(identifier);
try { try {
return this.getServiceProvider().isEnrolled(deviceIdentifier); boolean isEnrolled = APIUtil.getDeviceManagementService().isEnrolled(
deviceIdentifier);
if (isEnrolled) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}/active")
@Path("/devices/{deviceType}/{identifier}/status")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean isActive(@PathParam("type") String type, @PathParam("identifier") String identifier){ public Response isActive(@PathParam("deviceType") String deviceType,
@PathParam("identifier") String identifier) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(type); deviceIdentifier.setType(deviceType);
deviceIdentifier.setId(identifier); deviceIdentifier.setId(identifier);
try { try {
return this.getServiceProvider().isActive(deviceIdentifier); boolean isActive = APIUtil.getDeviceManagementService().isActive(
deviceIdentifier);
if (isActive) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}/active") @Path("/devices/{deviceType}/{identifier}/status")
@PUT @PUT
@Consumes("application/json") @Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean setActive(@PathParam("type") String type, @PathParam("identifier") String identifier, public Response changeDeviceStatus(@PathParam("deviceType") String deviceType,
@FormParam("status") boolean status){ @PathParam("identifier") String identifier,
@FormParam("status") boolean status) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(type); deviceIdentifier.setType(deviceType);
deviceIdentifier.setId(identifier); deviceIdentifier.setId(identifier);
try { try {
return this.getServiceProvider().setActive(deviceIdentifier, status); boolean isActivated = APIUtil.getDeviceManagementService().setActive(
deviceIdentifier, status);
if (isActivated) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}/ownership") @Path("/devices/{deviceType}/{identifier}/ownership")
@PUT @PUT
@Consumes("application/json") @Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean setOwnership(@PathParam("type") String type, @PathParam("identifier") String identifier, public Response setOwnership(@PathParam("deviceType") String deviceType,
@FormParam("ownership") String ownership){ @PathParam("identifier") String identifier,
@FormParam("ownership") String ownership) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(type); deviceIdentifier.setType(deviceType);
deviceIdentifier.setId(identifier); deviceIdentifier.setId(identifier);
try { try {
return this.getServiceProvider().setOwnership(deviceIdentifier, ownership); boolean isOwnershipChanged = APIUtil.getDeviceManagementService()
.setOwnership(deviceIdentifier, ownership);
if (isOwnershipChanged) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}/status") @Path("/devices/{deviceType}/{identifier}/enrollment/status")
@PUT @PUT
@Consumes("application/json") @Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean setStatus(@PathParam("type") String type, @PathParam("identifier") String identifier, public Response setStatus(@PathParam("deviceType") String deviceType,
@FormParam("owner") String owner, @FormParam("status") EnrolmentInfo.Status status){ @PathParam("identifier") String identifier, @FormParam("owner") String owner,
@FormParam("status") EnrolmentInfo.Status status) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(type); deviceIdentifier.setType(deviceType);
deviceIdentifier.setId(identifier); deviceIdentifier.setId(identifier);
try { try {
return this.getServiceProvider().setStatus(deviceIdentifier, owner, status); boolean isStatusChanged = APIUtil.getDeviceManagementService()
.setStatus(deviceIdentifier, owner, status);
if (isStatusChanged) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} finally {
this.endTenantFlow();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}/sensor/{sensorName}") @Path("/devices/{deviceType}/{identifier}/sensors/{sensorName}")
@POST @POST
@Consumes("application/json") @Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean setSensorValue(@PathParam("type") String type, @PathParam("identifier") String deviceId, public Response setSensorValue(@PathParam("deviceType") String deviceType,
@PathParam("identifier") String deviceId,
@PathParam("sensorName") String sensorName, @PathParam("sensorName") String sensorName,
@HeaderParam("sensorValue") String sensorValue){ @FormParam("sensorValue") String sensorValue) {
boolean isValueSet = SensorDataManager.getInstance().setSensorRecord(deviceId, sensorName,
try { sensorValue, Calendar.getInstance().getTimeInMillis());
return SensorDataManager.getInstance().setSensorRecord(deviceId, sensorName, sensorValue, Calendar if (isValueSet) {
.getInstance().getTimeInMillis()); return Response.status(Response.Status.NO_CONTENT).build();
} finally { } else {
this.endTenantFlow(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} }
} }
@Path("/device/type/{type}/identifier/{identifier}/sensor/{sensorName}") @Path("/devices/{deviceType}/{identifier}/sensors/{sensorName}")
@GET @GET
@Consumes("application/json") @Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public SensorRecord getSensorValue(@PathParam("type") String type, @PathParam("identifier") String deviceId, public Response getSensorValue(@PathParam("deviceType") String deviceType,
@PathParam("sensorName") String sensorName, @HeaderParam("defaultValue") String defaultValue){ @PathParam("identifier") String deviceId,
@PathParam("sensorName") String sensorName,
@QueryParam("defaultValue") String defaultValue) {
try { try {
return SensorDataManager.getInstance().getSensorRecord(deviceId, sensorName); SensorRecord sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId, sensorName);
if (sensorRecord != null) {
return Response.status(Response.Status.OK).entity(sensorRecord).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (DeviceControllerException e) { } catch (DeviceControllerException e) {
log.error("Error on reading sensor value: " + e.getMessage()); log.error("Error on reading sensor value: " + e.getMessage());
if(defaultValue != null){ if (defaultValue != null) {
return new SensorRecord(defaultValue, Calendar.getInstance().getTimeInMillis()); SensorRecord sensorRecord = new SensorRecord(defaultValue,
}else{ Calendar.getInstance().getTimeInMillis());
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.OK).entity(sensorRecord).build();
return null; } else {
} return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally { }
this.endTenantFlow();
} }
} }

@ -21,21 +21,16 @@ package org.wso2.carbon.device.mgt.iot.api;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser; import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.iot.util.APIUtil;
import org.wso2.carbon.device.mgt.iot.util.ResponsePayload; import org.wso2.carbon.device.mgt.iot.util.ResponsePayload;
import javax.jws.WebService;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam; import javax.ws.rs.FormParam;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -45,12 +40,10 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@WebService
public class GroupManagerService { public class GroupManagerService {
private static final String DEFAULT_ADMIN_ROLE = "admin"; private static final String DEFAULT_ADMIN_ROLE = "admin";
@ -69,46 +62,21 @@ public class GroupManagerService {
private static Log log = LogFactory.getLog(GroupManagerService.class); private static Log log = LogFactory.getLog(GroupManagerService.class);
@Context //injected response proxy supporting multiple threads @Path("/groups")
private HttpServletResponse response;
private PrivilegedCarbonContext ctx;
private GroupManagementProviderService getServiceProvider() {
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
String username = CarbonContext.getThreadLocalCarbonContext().getUsername();
PrivilegedCarbonContext.startTenantFlow();
ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
ctx.setTenantDomain(tenantDomain, true);
if (log.isDebugEnabled()) {
log.debug("Getting thread local carbon context for tenant domain: " + tenantDomain);
}
return (GroupManagementProviderService) ctx.getOSGiService(GroupManagementProviderService.class, null);
}
private void endTenantFlow() {
PrivilegedCarbonContext.endTenantFlow();
ctx = null;
if (log.isDebugEnabled()) {
log.debug("Tenant flow ended");
}
}
@Path("/group")
@POST @POST
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Response createGroup(@FormParam("name") String name, public Response createGroup(@FormParam("groupName") String groupName,
@FormParam("username") String username, @FormParam("userName") String username,
@FormParam("description") String description) { @FormParam("description") String description) {
DeviceGroup group = new DeviceGroup(); DeviceGroup group = new DeviceGroup();
group.setName(name); group.setName(username);
group.setDescription(description); group.setDescription(description);
group.setOwner(username); group.setOwner(username);
group.setDateOfCreation(new Date().getTime()); group.setDateOfCreation(new Date().getTime());
group.setDateOfLastUpdate(new Date().getTime()); group.setDateOfLastUpdate(new Date().getTime());
boolean isAdded = false; boolean isAdded = false;
try { try {
GroupManagementProviderService groupManagementService = this.getServiceProvider(); GroupManagementProviderService groupManagementService = APIUtil.getGroupManagementProviderService();
int groupId = groupManagementService.createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); int groupId = groupManagementService.createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS);
if (groupId == -2) { if (groupId == -2) {
ResponsePayload responsePayload = new ResponsePayload(); ResponsePayload responsePayload = new ResponsePayload();
@ -134,451 +102,374 @@ public class GroupManagerService {
} }
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
} finally {
this.endTenantFlow();
} }
} }
@Path("/group/id/{groupId}") @Path("/groups/{groupId}")
@PUT @PUT
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean updateGroup(@PathParam("groupId") int groupId, @FormParam("name") String name, public Response updateGroup(@PathParam("groupId") int groupId, @FormParam("groupName") String groupName,
@FormParam("username") String username, @FormParam("userName") String userName,
@FormParam("description") String description) { @FormParam("description") String description) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/modify")) {
response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
return false;
}
try { try {
GroupManagementProviderService groupManagementService = this.getServiceProvider(); GroupManagementProviderService groupManagementService = APIUtil.getGroupManagementProviderService();
DeviceGroup group = groupManagementService.getGroup(groupId); DeviceGroup group = groupManagementService.getGroup(groupId);
group.setName(name); group.setName(groupName);
group.setDescription(description); group.setDescription(description);
group.setOwner(username); group.setOwner(userName);
group.setDateOfLastUpdate(new Date().getTime()); group.setDateOfLastUpdate(new Date().getTime());
response.setStatus(Response.Status.OK.getStatusCode()); Response.status(Response.Status.OK.getStatusCode());
groupManagementService.updateGroup(group); groupManagementService.updateGroup(group);
return true; return Response.status(Response.Status.NO_CONTENT).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
return false; return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
} }
@Path("/group/id/{groupId}") @Path("/groups/{groupId}")
@DELETE @DELETE
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean deleteGroup(@PathParam("groupId") int groupId, public Response deleteGroup(@PathParam("groupId") int groupId, @QueryParam("userName") String userName) {
@QueryParam("username") String username) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/delete")) { if (!checkAuthorize(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/delete")) {
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return Response.status(Response.Status.FORBIDDEN).build();
return false;
} }
boolean isDeleted = false;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); boolean isDeleted = APIUtil.getGroupManagementProviderService().deleteGroup(
isDeleted = this.getServiceProvider().deleteGroup(groupId); groupId);
if (isDeleted) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage());
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return isDeleted;
} }
@Path("/group/id/{groupId}") @Path("/groups/{groupId}")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public DeviceGroup getGroup(@PathParam("groupId") int groupId, public Response getGroup(@PathParam("groupId") int groupId) {
@FormParam("username") String username) {
DeviceGroup deviceGroup = null;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); DeviceGroup deviceGroup = APIUtil.getGroupManagementProviderService().getGroup(
deviceGroup = this.getServiceProvider().getGroup(groupId); groupId);
if (deviceGroup != null) {
return Response.status(Response.Status.OK).entity(deviceGroup).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return deviceGroup;
} }
@Path("/group/name/{groupName}") @Path("/groups/search")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public DeviceGroup[] findGroups(@PathParam("groupName") String groupName, public Response findGroups(@QueryParam("groupName") String groupName,
@FormParam("username") String username) { @QueryParam("userName") String userName) {
DeviceGroup[] deviceGroups = null;
try { try {
List<DeviceGroup> groups = this.getServiceProvider().findInGroups(groupName, username); List<DeviceGroup> groups = APIUtil.getGroupManagementProviderService()
deviceGroups = new DeviceGroup[groups.size()]; .findInGroups(groupName, userName);
response.setStatus(Response.Status.OK.getStatusCode()); DeviceGroup[] deviceGroups = new DeviceGroup[groups.size()];
groups.toArray(deviceGroups); groups.toArray(deviceGroups);
return Response.status(Response.Status.OK).entity(deviceGroups).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return deviceGroups;
} }
@Path("/group/user/{username}/all") @Path("/groups")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public DeviceGroup[] getGroups(@PathParam("username") String username, public Response getGroups(@QueryParam("userName") String userName,
@QueryParam("permission") String permission) { @QueryParam("permission") String permission) {
DeviceGroup[] deviceGroups = null;
try { try {
GroupManagementProviderService groupManagementService = this.getServiceProvider(); GroupManagementProviderService groupManagementService = APIUtil.getGroupManagementProviderService();
List<DeviceGroup> groups; List<DeviceGroup> groups;
if (permission != null) { if (permission != null) {
groups = groupManagementService.getGroups(username, permission); groups = groupManagementService.getGroups(userName, permission);
} else { } else {
groups = groupManagementService.getGroups(username); groups = groupManagementService.getGroups(userName);
} }
deviceGroups = new DeviceGroup[groups.size()]; DeviceGroup[] deviceGroups = new DeviceGroup[groups.size()];
response.setStatus(Response.Status.OK.getStatusCode());
groups.toArray(deviceGroups); groups.toArray(deviceGroups);
return Response.status(Response.Status.OK).entity(deviceGroups).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return deviceGroups;
} }
@Path("/group/user/{username}/all/count") @Path("/groups/count")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public int getGroupCount(@PathParam("username") String username) { public Response getGroupCount(@QueryParam("userName") String userName) {
int count = -1;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); int count = APIUtil.getGroupManagementProviderService().getGroupCount(userName);
count = this.getServiceProvider().getGroupCount(username); return Response.status(Response.Status.OK).entity(count).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return count;
} }
@Path("/group/id/{groupId}/share") @Path("/groups/{groupId}/share")
@POST @PUT
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean shareGroup(@FormParam("username") String username, public Response shareGroup(@FormParam("userName") String userName,
@FormParam("shareUser") String shareUser, @FormParam("shareUser") String shareUser, @PathParam("groupId") int groupId,
@PathParam("groupId") int groupId, @FormParam("roleName") String sharingRole) {
@FormParam("role") String sharingRole) { if (!checkAuthorize(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")) { return Response.status(Response.Status.FORBIDDEN).build();
response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
return false;
} }
boolean isShared = false;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); boolean isShared = APIUtil.getGroupManagementProviderService().shareGroup(
isShared = this.getServiceProvider().shareGroup(shareUser, groupId, sharingRole); shareUser, groupId, sharingRole);
if (isShared) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return isShared;
} }
@Path("/group/id/{groupId}/unshare") @Path("/groups/{groupId}/unshare")
@POST @PUT
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean unShareGroup(@FormParam("username") String username, public Response unShareGroup(@FormParam("userName") String userName,
@FormParam("unShareUser") String unShareUser, @FormParam("unShareUser") String unShareUser,
@PathParam("groupId") int groupId, @PathParam("groupId") int groupId,
@FormParam("role") String sharingRole) { @FormParam("roleName") String sharingRole) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")) { if (!checkAuthorize(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")) {
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return Response.status(Response.Status.FORBIDDEN).build();
return false;
} }
boolean isUnShared = false;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); boolean isUnShared = APIUtil.getGroupManagementProviderService().unshareGroup(
isUnShared = this.getServiceProvider().unshareGroup(unShareUser, groupId, sharingRole); unShareUser, groupId, sharingRole);
if (isUnShared) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return isUnShared;
} }
@Path("/group/id/{groupId}/role") @Path("/groups/{groupId}/share/roles/{roleName}/permissions")
@POST @PUT
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean addSharing(@FormParam("username") String username, public Response addSharing(@QueryParam("userName") String userName, @PathParam("groupId") int groupId,
@PathParam("groupId") int groupId, @PathParam("roleName") String roleName,
@FormParam("role") String roleName,
@FormParam("permissions") String[] permissions) { @FormParam("permissions") String[] permissions) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")) { if (!checkAuthorize(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")) {
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return Response.status(Response.Status.FORBIDDEN).build();
return false;
} }
boolean isAdded = false;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); boolean isAdded = APIUtil.getGroupManagementProviderService().addGroupSharingRole(
isAdded = this.getServiceProvider().addGroupSharingRole(username, groupId, roleName, permissions); userName, groupId, roleName, permissions);
if (isAdded) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return isAdded;
} }
@Path("/group/id/{groupId}/role/{role}")
@DELETE @DELETE
@Consumes("application/json") @Path("/groups/{groupId}/share/roles/{roleName}/permissions")
@Produces("application/json") @Produces("application/json")
public boolean removeSharing(@QueryParam("username") String username, public Response removeSharing(@QueryParam("userName") String userName, @PathParam("groupId") int groupId,
@PathParam("groupId") int groupId, @PathParam("roleName") String roleName) {
@PathParam("role") String roleName) { if (!checkAuthorize(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")) { return Response.status(Response.Status.FORBIDDEN).build();
response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
}
boolean isRemoved = false;
try {
response.setStatus(Response.Status.OK.getStatusCode());
isRemoved = this.getServiceProvider().removeGroupSharingRole(groupId, roleName);
} catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e);
} finally {
this.endTenantFlow();
}
return isRemoved;
} }
@Path("/group/id/{groupId}/role/all")
@GET
@Consumes("application/json")
@Produces("application/json")
public String[] getRoles(@PathParam("groupId") int groupId) {
String[] rolesArray = null;
try { try {
List<String> roles = this.getServiceProvider().getRoles(groupId); boolean isRemoved = APIUtil.getGroupManagementProviderService().removeGroupSharingRole(
rolesArray = new String[roles.size()]; groupId, roleName);
response.setStatus(Response.Status.OK.getStatusCode()); if (isRemoved) {
roles.toArray(rolesArray); return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return rolesArray;
} }
@Path("/group/id/{groupId}/{user}/role/all")
@GET @GET
@Consumes("application/json") @Path("/groups/{groupId}/share/roles")
@Produces("application/json") @Produces("application/json")
public String[] getRoles(@PathParam("user") String user, @PathParam("groupId") int groupId) { public Response getRoles(@PathParam("groupId") int groupId, @QueryParam("userName") String userName) {
String[] rolesArray = null;
try { try {
List<String> roles = this.getServiceProvider().getRoles(user, groupId); List<String> roles;
rolesArray = new String[roles.size()]; if (userName != null && !userName.isEmpty()) {
response.setStatus(Response.Status.OK.getStatusCode()); roles = APIUtil.getGroupManagementProviderService().getRoles(userName,
groupId);
} else {
roles = APIUtil.getGroupManagementProviderService().getRoles(groupId);
}
String[] rolesArray = new String[roles.size()];
roles.toArray(rolesArray); roles.toArray(rolesArray);
return Response.status(Response.Status.OK).entity(rolesArray).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return rolesArray;
} }
@Path("/group/id/{groupId}/user/all")
@GET @GET
@Consumes("application/json") @Path("/groups/{groupId}/users")
@Produces("application/json") @Produces("application/json")
public GroupUser[] getUsers(@PathParam("groupId") int groupId) { public Response getUsers(@PathParam("groupId") int groupId) {
GroupUser[] usersArray = null;
try { try {
List<GroupUser> users = this.getServiceProvider().getUsers(groupId); List<GroupUser> users = APIUtil.getGroupManagementProviderService().getUsers(
usersArray = new GroupUser[users.size()]; groupId);
response.setStatus(Response.Status.OK.getStatusCode()); GroupUser[] usersArray = new GroupUser[users.size()];
users.toArray(usersArray); users.toArray(usersArray);
return Response.status(Response.Status.OK).entity(usersArray).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return usersArray;
} }
@Path("/group/id/{groupId}/device/all")
@GET @GET
@Consumes("application/json") @Path("/groups/{groupId}/devices/all")
@Produces("application/json") @Produces("application/json")
public Device[] getDevices(@PathParam("groupId") int groupId) { public Response getDevices(@PathParam("groupId") int groupId) {
Device[] deviceArray = null;
try { try {
List<Device> devices = this.getServiceProvider().getDevices(groupId); List<Device> devices = APIUtil.getGroupManagementProviderService().getDevices(
deviceArray = new Device[devices.size()]; groupId);
response.setStatus(Response.Status.OK.getStatusCode()); Device[] deviceArray = new Device[devices.size()];
devices.toArray(deviceArray); devices.toArray(deviceArray);
return Response.status(Response.Status.OK).entity(deviceArray).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
}
return deviceArray;
}
@Path("/group/id/{groupId}/device/count")
@GET
@Consumes("application/json")
@Produces("application/json")
public int getDeviceCount(@PathParam("groupId") int groupId) {
try {
return this.getServiceProvider().getDeviceCount(groupId);
} catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e);
return -1;
} finally {
this.endTenantFlow();
} }
} }
@Path("/group/id/{groupId}/device")
@GET @GET
@Consumes("application/json") @Path("/groups/{groupId}/devices/count")
@Produces("application/json") @Produces("application/json")
public List<Device> getDevices(@PathParam("groupId") int groupId, public Response getDeviceCount(@PathParam("groupId") int groupId) {
@QueryParam("index") int index,
@QueryParam("limit") int limit) {
try { try {
PaginationRequest request = new PaginationRequest(index, limit); int count = APIUtil.getGroupManagementProviderService().getDeviceCount(groupId);
return this.getServiceProvider().getDevices(groupId, request); return Response.status(Response.Status.OK).entity(count).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} finally {
this.endTenantFlow();
} }
} }
@Path("/group/id/{groupId}/device/assign")
@PUT @PUT
@Consumes("application/json") @Path("/groups/{groupId}/devices/{deviceType}/{deviceId}")
@Produces("application/json") @Produces("application/json")
public boolean addDevice(@PathParam("groupId") int groupId, public Response addDevice(@PathParam("groupId") int groupId, @PathParam("deviceId") String deviceId,
@FormParam("deviceId") String deviceId, @PathParam("deviceType") String deviceType,
@FormParam("deviceType") String deviceType, @FormParam("userName") String userName) {
@FormParam("username") String username) { if (!checkAuthorize(getCurrentUserName(), groupId,
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/add_devices")) { "/permission/device-mgt/admin/groups/add_devices")) {
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return Response.status(Response.Status.FORBIDDEN).build();
return false;
} }
boolean isAdded = false;
try { try {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
response.setStatus(Response.Status.OK.getStatusCode()); boolean isAdded = APIUtil.getGroupManagementProviderService().addDevice(
isAdded = this.getServiceProvider().addDevice(deviceIdentifier, groupId); deviceIdentifier, groupId);
if (isAdded) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return isAdded;
} }
@Path("/group/id/{groupId}/device/assign")
@DELETE @DELETE
@Consumes("application/json") @Path("/groups/{groupId}/devices/{deviceType}/{deviceId}")
@Produces("application/json") @Produces("application/json")
public boolean removeDevice(@PathParam("groupId") int groupId, public Response removeDevice(@PathParam("groupId") int groupId, @PathParam("deviceId") String deviceId,
@FormParam("deviceId") String deviceId, @PathParam("deviceType") String deviceType) {
@FormParam("deviceType") String deviceType, if (!checkAuthorize(getCurrentUserName(), groupId,
@FormParam("username") String username) { "/permission/device-mgt/admin/groups/remove_devices")) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/remove_devices")) { return Response.status(Response.Status.FORBIDDEN).build();
response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
return false;
} }
boolean isRemoved = false;
try { try {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
response.setStatus(Response.Status.OK.getStatusCode()); boolean isRemoved = APIUtil.getGroupManagementProviderService().removeDevice(
isRemoved = this.getServiceProvider().removeDevice(deviceIdentifier, groupId); deviceIdentifier, groupId);
if (isRemoved) {
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return isRemoved;
} }
@Path("/group/id/{groupId}/user/{username}/permissions")
@GET @GET
@Consumes("application/json") @Path("/groups/{groupId}/users/{userName}/permissions")
@Produces("application/json") @Produces("application/json")
public String[] getPermissions(@PathParam("username") String username, public Response getPermissions(@PathParam("userName") String userName,
@PathParam("groupId") int groupId) { @PathParam("groupId") int groupId) {
String[] permissions = null;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); String[] permissions = APIUtil.getGroupManagementProviderService()
permissions = this.getServiceProvider().getPermissions(username, groupId); .getPermissions(userName, groupId);
return Response.status(Response.Status.OK).entity(permissions).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
this.endTenantFlow();
} }
return permissions;
} }
@Path("/group/id/{groupId}/user/{username}/authorized")
@GET @GET
@Consumes("application/json") @Path("/groups/{groupId}/users/{userName}/authorized")
@Produces("application/json") @Produces("application/json")
public boolean isAuthorized(@PathParam("username") String username, public Response isAuthorized(@PathParam("userName") String userName, @PathParam("groupId") int groupId,
@PathParam("groupId") int groupId,
@QueryParam("permission") String permission) { @QueryParam("permission") String permission) {
boolean isAuthorized = false; boolean isAuthorized = checkAuthorize(userName, groupId, permission);
if (isAuthorized) {
return Response.status(Response.Status.OK).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
private boolean checkAuthorize(String userName, int groupId, String permission) {
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); return APIUtil.getGroupManagementProviderService().isAuthorized(userName, groupId, permission);
isAuthorized = this.getServiceProvider().isAuthorized(username, groupId, permission);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getMessage(), e);
log.error(e.getErrorMessage(), e); return false;
} finally {
this.endTenantFlow();
} }
return isAuthorized; }
private String getCurrentUserName() {
return PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
} }
} }

@ -24,6 +24,7 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.iot.util.APIUtil;
import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
@ -40,190 +41,108 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List; import java.util.List;
@WebService
public class PolicyManagementService { public class PolicyManagementService {
private static Log log = LogFactory.getLog(PolicyManagementService.class); private static final Log log = LogFactory.getLog(PolicyManagementService.class);
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
private PrivilegedCarbonContext ctx;
private PolicyManagerService getPolicyServiceProvider() throws DeviceManagementException {
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
PrivilegedCarbonContext.startTenantFlow();
ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
ctx.setTenantDomain(tenantDomain, true);
if (log.isDebugEnabled()) {
log.debug("Getting thread local carbon context for tenant domain: " + tenantDomain);
}
PolicyManagerService policyManagerService = (PolicyManagerService) ctx.getOSGiService(
PolicyManagerService.class, null);
if (policyManagerService == null) {
String msg = "Policy Management service not initialized";
log.error(msg);
throw new DeviceManagementException(msg);
}
return policyManagerService;
}
private void endTenantFlow() {
PrivilegedCarbonContext.endTenantFlow();
ctx = null;
if (log.isDebugEnabled()) {
log.debug("Tenant flow ended");
}
}
@POST @POST
@Path("/inactive-policy") @Path("/policies/inactive")
@Produces("application/json") @Produces("application/json")
public boolean addPolicy(Policy policy) { public Response addInactivePolicy(@FormParam("policy") Policy policy) {
return addPolicy(policy);
try {
PolicyManagerService policyManagerService = getPolicyServiceProvider();
PolicyAdministratorPoint pap = policyManagerService.getPAP();
pap.addPolicy(policy);
response.setStatus(Response.Status.CREATED.getStatusCode());
if (log.isDebugEnabled()) {
log.debug("Policy has been added successfully.");
}
return true;
} catch (PolicyManagementException e) {
String error = "Policy Management related exception.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
}
} }
@POST @POST
@Path("/active-policy") @Path("/policies/active")
@Produces("application/json") @Produces("application/json")
public boolean addActivePolicy(Policy policy) { public Response addActivePolicy(@FormParam("policy") Policy policy) {
policy.setActive(true); policy.setActive(true);
return addPolicy(policy);
}
private Response addPolicy(Policy policy) {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
PolicyAdministratorPoint pap = policyManagerService.getPAP(); PolicyAdministratorPoint pap = policyManagerService.getPAP();
pap.addPolicy(policy); pap.addPolicy(policy);
response.setStatus(Response.Status.CREATED.getStatusCode());
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy has been added successfully."); log.debug("Policy has been added successfully.");
} }
return true; return Response.status(Response.Status.NO_CONTENT).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Policy Management related exception."; String error = "Policy Management related exception.";
log.error(error, e); log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
} }
} }
@GET @GET
@Path("/policies")
@Produces("application/json") @Produces("application/json")
public Policy[] getAllPolicies() { public Response getAllPolicies() {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
PolicyAdministratorPoint policyAdministratorPoint = policyManagerService.getPAP(); PolicyAdministratorPoint policyAdministratorPoint = policyManagerService.getPAP();
List<Policy> policies = policyAdministratorPoint.getPolicies(); List<Policy> policies = policyAdministratorPoint.getPolicies();
return policyAdministratorPoint.getPolicies().toArray(new Policy[policies.size()]); Policy[] policyArr = policyAdministratorPoint.getPolicies().toArray(new Policy[policies.size()]);
return Response.status(Response.Status.OK).entity(policyArr).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Policy Management related exception"; String error = "Policy Management related exception";
log.error(error, e); log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return null;
} finally {
this.endTenantFlow();
} }
} }
@GET @GET
@Path("/policies/{policyId}")
@Produces("application/json") @Produces("application/json")
@Path("/{id}") public Response getPolicy(@PathParam("policyId") int policyId) {
public Policy getPolicy(@PathParam("id") int policyId) {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
PolicyAdministratorPoint policyAdministratorPoint = policyManagerService.getPAP(); PolicyAdministratorPoint policyAdministratorPoint = policyManagerService.getPAP();
Policy policy = policyAdministratorPoint.getPolicy(policyId); Policy policy = policyAdministratorPoint.getPolicy(policyId);
if (policy != null) { if (policy != null) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Sending policy for ID " + policyId); log.debug("Sending policy for ID " + policyId);
} }
return policy; return Response.status(Response.Status.OK).entity(policy).build();
} else { } else {
log.error("Policy for ID " + policyId + " not found."); log.error("Policy for ID " + policyId + " not found.");
response.setStatus(Response.Status.NOT_FOUND.getStatusCode()); return Response.status(Response.Status.NOT_FOUND).build();
return null;
} }
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Policy Management related exception"; String error = "Policy Management related exception";
log.error(error, e); log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return null;
} finally {
this.endTenantFlow();
} }
} }
@GET @GET
@Path("/count") @Path("/policies/inactive/count")
public int getPolicyCount() { @Produces("application/json")
public Response getPolicyCount() {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
PolicyAdministratorPoint policyAdministratorPoint = policyManagerService.getPAP(); PolicyAdministratorPoint policyAdministratorPoint = policyManagerService.getPAP();
return policyAdministratorPoint.getPolicyCount(); int policyCount = policyAdministratorPoint.getPolicyCount();
return Response.status(Response.Status.OK).entity(policyCount).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Policy Management related exception"; String error = "Policy Management related exception";
log.error(error, e); log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return -1;
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return -1;
} finally {
this.endTenantFlow();
} }
} }
@PUT @PUT
@Path("/{id}") @Path("/policies/{policyId}")
@Produces("application/json") @Produces("application/json")
public boolean updatePolicy(Policy policy, @PathParam("id") int policyId) { public Response updatePolicy(@FormParam("policy") Policy policy, @PathParam("policyId") int policyId) {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
PolicyAdministratorPoint pap = policyManagerService.getPAP(); PolicyAdministratorPoint pap = policyManagerService.getPAP();
Policy previousPolicy = pap.getPolicy(policyId); Policy previousPolicy = pap.getPolicy(policyId);
policy.setProfile(pap.getProfile(previousPolicy.getProfileId())); policy.setProfile(pap.getProfile(previousPolicy.getProfileId()));
@ -232,64 +151,49 @@ public class PolicyManagementService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy with ID " + policyId + " has been updated successfully."); log.debug("Policy with ID " + policyId + " has been updated successfully.");
} }
return true; return Response.status(Response.Status.NO_CONTENT).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Policy Management related exception"; String error = "Policy Management related exception";
log.error(error, e); log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
} }
} }
@PUT @PUT
@Path("/priorities") @Path("/policies/priorities")
@Consumes("application/json") @Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean updatePolicyPriorities(List<Policy> priorityUpdatedPolicies) { public Response updatePolicyPriorities(@FormParam("policies") List<Policy> priorityUpdatedPolicies) {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
PolicyAdministratorPoint pap = policyManagerService.getPAP(); PolicyAdministratorPoint pap = policyManagerService.getPAP();
boolean policiesUpdated = pap.updatePolicyPriorities(priorityUpdatedPolicies); boolean policiesUpdated = pap.updatePolicyPriorities(priorityUpdatedPolicies);
if (policiesUpdated) { if (policiesUpdated) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy Priorities successfully updated."); log.debug("Policy Priorities successfully updated.");
} }
return true; return Response.status(Response.Status.NO_CONTENT).build();
} else { } else {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy priorities did not update. Bad Request."); log.debug("Policy priorities did not update. Bad Request.");
} }
response.setStatus(Response.Status.BAD_REQUEST.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} }
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Exception in updating policy priorities."; String error = "Exception in updating policy priorities.";
log.error(error, e); log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
} }
} }
@DELETE @DELETE
@Path("/{id}") @Path("/policies/{policyId}")
@Produces("application/json") @Produces("application/json")
public boolean deletePolicy(@PathParam("id") int policyId) { public Response deletePolicy(@PathParam("policyId") int policyId) {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
PolicyAdministratorPoint pap = policyManagerService.getPAP(); PolicyAdministratorPoint pap = policyManagerService.getPAP();
Policy policy = pap.getPolicy(policyId); Policy policy = pap.getPolicy(policyId);
boolean policyDeleted = pap.deletePolicy(policy); boolean policyDeleted = pap.deletePolicy(policy);
@ -297,210 +201,155 @@ public class PolicyManagementService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy by id:" + policyId + " has been successfully deleted."); log.debug("Policy by id:" + policyId + " has been successfully deleted.");
} }
return true; return Response.status(Response.Status.OK).build();
} else { } else {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy by id:" + policyId + " does not exist."); log.debug("Policy by id:" + policyId + " does not exist.");
} }
response.setStatus(Response.Status.NOT_FOUND.getStatusCode()); return Response.status(Response.Status.NOT_FOUND).build();
return false;
} }
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Exception in deleting policy by id:" + policyId; String error = "Exception in deleting policy by id:" + policyId;
log.error(error, e); log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
} }
} }
@PUT @PUT
@Path("/policies/{policyId}/activate")
@Produces("application/json") @Produces("application/json")
@Path("/activate/{id}") public Response activatePolicy(@PathParam("policyId") int policyId) {
public boolean activatePolicy(@PathParam("id") int policyId) {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
PolicyAdministratorPoint pap = policyManagerService.getPAP(); PolicyAdministratorPoint pap = policyManagerService.getPAP();
pap.activatePolicy(policyId); pap.activatePolicy(policyId);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy by id:" + policyId + " has been successfully activated."); log.debug("Policy by id:" + policyId + " has been successfully activated.");
} }
return true; return Response.status(Response.Status.NO_CONTENT).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Exception in activating policy by id:" + policyId; String error = "Exception in activating policy by id:" + policyId;
log.error(error, e); log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return false;
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
} }
} }
@PUT @PUT
@Path("/policies/{policyId}/inactivate")
@Produces("application/json") @Produces("application/json")
@Path("/inactivate/{id}") public Response inactivatePolicy(@PathParam("policyId") int policyId) {
public boolean inactivatePolicy(@PathParam("id") int policyId) {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
PolicyAdministratorPoint pap = policyManagerService.getPAP(); PolicyAdministratorPoint pap = policyManagerService.getPAP();
pap.inactivatePolicy(policyId); pap.inactivatePolicy(policyId);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy by id:" + policyId + " has been successfully inactivated."); log.debug("Policy by id:" + policyId + " has been successfully inactivated.");
} }
return true; return Response.status(Response.Status.NO_CONTENT).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Exception in inactivating policy by id:" + policyId; String error = "Exception in inactivating policy by id:" + policyId;
log.error(error, e); log.error(error, e);
return false; return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
} }
} }
@PUT @PUT
@Path("/policies/publish-changes")
@Produces("application/json") @Produces("application/json")
@Path("/apply-changes") public Response publishChanges() {
public boolean applyChanges() {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
PolicyAdministratorPoint pap = policyManagerService.getPAP(); PolicyAdministratorPoint pap = policyManagerService.getPAP();
pap.publishChanges(); pap.publishChanges();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Changes have been successfully updated."); log.debug("Changes have been successfully updated.");
} }
return true; return Response.status(Response.Status.NO_CONTENT).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Exception in applying changes."; String error = "Exception in applying changes.";
log.error(error, e); log.error(error, e);
return false; return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
} }
} }
@GET @PUT
@Path("/start-task/{milliseconds}") @Path("/policies/monitor-task/start")
public boolean startTaskService(@PathParam("milliseconds") int monitoringFrequency) { @Produces("application/json")
public Response startTaskService(@FormParam("milliseconds") int monitoringFrequency) {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
TaskScheduleService taskScheduleService = policyManagerService.getTaskScheduleService(); TaskScheduleService taskScheduleService = policyManagerService.getTaskScheduleService();
taskScheduleService.startTask(monitoringFrequency); taskScheduleService.startTask(monitoringFrequency);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy monitoring service started successfully."); log.debug("Policy monitoring service started successfully.");
} }
return true; return Response.status(Response.Status.NO_CONTENT).build();
} catch (PolicyMonitoringTaskException e) { } catch (PolicyMonitoringTaskException e) {
String error = "Policy Management related exception."; String error = "Policy Management related exception.";
log.error(error, e); log.error(error, e);
return false; return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
} }
} }
@GET @PUT
@Path("/update-task/{milliseconds}") @Path("/policies/monitor-task/update")
public boolean updateTaskService(@PathParam("milliseconds") int monitoringFrequency) { @Produces("application/json")
public Response updateTaskService(@FormParam("milliseconds") int monitoringFrequency) {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();;
TaskScheduleService taskScheduleService = policyManagerService.getTaskScheduleService(); TaskScheduleService taskScheduleService = policyManagerService.getTaskScheduleService();
taskScheduleService.updateTask(monitoringFrequency); taskScheduleService.updateTask(monitoringFrequency);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy monitoring service updated successfully."); log.debug("Policy monitoring service updated successfully.");
} }
return true; return Response.status(Response.Status.NO_CONTENT).build();
} catch (PolicyMonitoringTaskException e) { } catch (PolicyMonitoringTaskException e) {
String error = "Policy Management related exception."; String error = "Policy Management related exception.";
log.error(error, e); log.error(error, e);
return false; return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
} }
} }
@GET @PUT
@Path("/stop-task") @Path("/policies/monitor-task/stop")
public boolean stopTaskService() { @Produces("application/json")
public Response stopTaskService() {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
TaskScheduleService taskScheduleService = policyManagerService.getTaskScheduleService(); TaskScheduleService taskScheduleService = policyManagerService.getTaskScheduleService();
taskScheduleService.stopTask(); taskScheduleService.stopTask();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy monitoring service stopped successfully."); log.debug("Policy monitoring service stopped successfully.");
} }
return true; return Response.status(Response.Status.NO_CONTENT).build();
} catch (PolicyMonitoringTaskException e) { } catch (PolicyMonitoringTaskException e) {
String error = "Policy Management related exception."; String error = "Policy Management related exception.";
log.error(error, e); log.error(error, e);
return false; return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
} finally {
this.endTenantFlow();
} }
} }
@GET @GET
@Path("/{type}/{id}") @Path("/policies/compliance/{deviceType}/{deviceId}")
public ComplianceData getComplianceDataOfDevice(@PathParam("id") String deviceId, @Produces("application/json")
@PathParam("type") String deviceType) { public Response getComplianceDataOfDevice(@PathParam("deviceId") String deviceId,
@PathParam("deviceType") String deviceType) {
try { try {
PolicyManagerService policyManagerService = getPolicyServiceProvider(); PolicyManagerService policyManagerService = APIUtil.getPolicyManagerService();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(deviceType); deviceIdentifier.setType(deviceType);
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
return policyManagerService.getDeviceCompliance(deviceIdentifier); ComplianceData complianceData = policyManagerService.getDeviceCompliance(deviceIdentifier);
return Response.status(Response.Status.OK).entity(complianceData).build();
} catch (PolicyComplianceException e) { } catch (PolicyComplianceException e) {
String error = "Error occurred while getting the compliance data."; String error = "Error occurred while getting the compliance data.";
log.error(error, e); log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return null;
} catch (DeviceManagementException e) {
String error = "Error occurred while invoking Policy Management Service.";
log.error(error, e);
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return null;
} finally {
this.endTenantFlow();
} }
} }

@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
/** /**
* This class provides utility functions used by REST-API. * This class provides utility functions used by REST-API.
@ -53,4 +54,16 @@ public class APIUtil {
} }
return deviceManagementProviderService; return deviceManagementProviderService;
} }
public static PolicyManagerService getPolicyManagerService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
PolicyManagerService policyManagerService =
(PolicyManagerService) ctx.getOSGiService(PolicyManagerService.class, null);
if (policyManagerService == null) {
String msg = "Policy management service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
return policyManagerService;
}
} }

@ -84,7 +84,8 @@
org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.device.mgt.common.*,
org.eclipse.paho.client.mqttv3.*;version="${eclipse.paho.version}", org.eclipse.paho.client.mqttv3.*;version="${eclipse.paho.version}",
javax.ws.rs.core, javax.ws.rs.core,
javax.xml javax.xml;resolution:=optional,
javax.net.ssl;resolution:=optional
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
!org.wso2.carbon.device.mgt.iot.internal, !org.wso2.carbon.device.mgt.iot.internal,

@ -33,7 +33,6 @@ import org.apache.http.util.EntityUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
import org.wso2.carbon.device.mgt.iot.exception.IoTException;
import org.wso2.carbon.device.mgt.iot.util.IoTUtil; import org.wso2.carbon.device.mgt.iot.util.IoTUtil;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
@ -140,7 +139,7 @@ public class XmppServerClient {
EntityUtils.consume(httpResponse.getEntity()); EntityUtils.consume(httpResponse.getEntity());
return true; return true;
} }
} catch (IOException | IoTException e) { } catch (IOException e) {
String errorMsg = "Error occured whilst trying a 'POST' at : " + xmppUsersAPIEndpoint; String errorMsg = "Error occured whilst trying a 'POST' at : " + xmppUsersAPIEndpoint;
log.error(errorMsg); log.error(errorMsg);
throw new DeviceControllerException(errorMsg, e); throw new DeviceControllerException(errorMsg, e);
@ -199,7 +198,7 @@ public class XmppServerClient {
return false; return false;
} }
} catch (IOException | IoTException e) { } catch (IOException e) {
String errorMsg = "Error occured whilst trying a 'GET' at : " + xmppCheckUserAPIEndpoint; String errorMsg = "Error occured whilst trying a 'GET' at : " + xmppCheckUserAPIEndpoint;
log.error(errorMsg); log.error(errorMsg);
throw new DeviceControllerException(errorMsg, e); throw new DeviceControllerException(errorMsg, e);
@ -266,7 +265,7 @@ public class XmppServerClient {
xmppSessions = new JSONObject(response).getJSONArray("session"); xmppSessions = new JSONObject(response).getJSONArray("session");
return xmppSessions; return xmppSessions;
} catch (IOException | IoTException e) { } catch (IOException e) {
String errorMsg = "Error occured whilst trying a 'GET' at : " + xmppSessionsAPIEndpoint; String errorMsg = "Error occured whilst trying a 'GET' at : " + xmppSessionsAPIEndpoint;
log.error(errorMsg); log.error(errorMsg);
throw new DeviceControllerException(errorMsg, e); throw new DeviceControllerException(errorMsg, e);

@ -22,18 +22,13 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.wso2.carbon.base.ServerConfiguration; import org.wso2.carbon.base.ServerConfiguration;
import org.wso2.carbon.device.mgt.iot.exception.IoTException; import org.wso2.carbon.device.mgt.iot.exception.IoTException;
import org.wso2.carbon.device.mgt.iot.internal.IoTCommonDataHolder;
import org.wso2.carbon.utils.NetworkUtils; import org.wso2.carbon.utils.NetworkUtils;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -43,49 +38,33 @@ import java.net.SocketException;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.KeyStoreException; import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
public class IoTUtil { public class IoTUtil {
public static final String HOST_NAME = "HostName"; public static final String HOST_NAME = "HostName";
private static final Log log = LogFactory.getLog(IoTUtil.class); private static final Log log = LogFactory.getLog(IoTUtil.class);
private static final String HTTPS_PROTOCOL = "https";
/** /**
* Return a http client instance * Return a http client instance
*
* @param port - server port
* @param protocol- service endpoint protocol http/https * @param protocol- service endpoint protocol http/https
* @return * @return
*/ */
public static HttpClient getHttpClient(int port, String protocol) public static HttpClient getHttpClient(int port, String protocol)
throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
KeyManagementException { HttpClient httpclient;
SchemeRegistry registry = new SchemeRegistry(); if (HTTPS_PROTOCOL.equals(protocol)) {
SSLContextBuilder builder = new SSLContextBuilder();
if ("https".equals(protocol)) { builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
System.setProperty("javax.net.ssl.trustStrore", IoTCommonDataHolder.getInstance().getTrustStoreLocation()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
System.setProperty("javax.net.ssl.trustStorePassword", httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
IoTCommonDataHolder.getInstance().getTrustStorePassword());
if (port >= 0) {
registry.register(new Scheme("https", port, SSLSocketFactory.getSocketFactory()));
} else { } else {
registry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); httpclient = HttpClients.createDefault();
} }
} else if ("http".equals(protocol)) { return httpclient;
if (port >= 0) {
registry.register(new Scheme("http", port, PlainSocketFactory.getSocketFactory()));
} else {
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
}
}
HttpParams params = new BasicHttpParams();
PoolingClientConnectionManager tcm = new PoolingClientConnectionManager(registry);
HttpClient client = new DefaultHttpClient(tcm, params);
return client;
} }
public static String getResponseString(HttpResponse httpResponse) throws IoTException { public static String getResponseString(HttpResponse httpResponse) throws IOException {
BufferedReader br = null; BufferedReader br = null;
try { try {
br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
@ -95,9 +74,6 @@ public class IoTUtil {
response += readLine; response += readLine;
} }
return response; return response;
} catch (IOException e) {
throw new IoTException("Error while reading the response from the remote. "
+ e.getMessage(), e);
} finally { } finally {
EntityUtils.consumeQuietly(httpResponse.getEntity()); EntityUtils.consumeQuietly(httpResponse.getEntity());
if (br != null) { if (br != null) {

@ -33,6 +33,7 @@ import java.util.List;
public class RaspberrypiManagerService implements DeviceManagementService { public class RaspberrypiManagerService implements DeviceManagementService {
private DeviceManager deviceManager; private DeviceManager deviceManager;
private final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super";
@Override @Override
public String getType() { public String getType() {
@ -41,12 +42,12 @@ public class RaspberrypiManagerService implements DeviceManagementService {
@Override @Override
public String getProviderTenantDomain() { public String getProviderTenantDomain() {
return "carbon.super"; return DEVICE_TYPE_PROVIDER_DOMAIN;
} }
@Override @Override
public boolean isSharedWithAllTenants() { public boolean isSharedWithAllTenants() {
return false; return true;
} }
@Override @Override

@ -21,16 +21,15 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>device-mgt-iot-raspberrypi</artifactId> <artifactId>raspberrypi-plugin</artifactId>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<version>2.0.4-SNAPSHOT</version> <version>2.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl</artifactId> <artifactId>org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl</artifactId>
<version>2.0.4-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<name>WSO2 Carbon - IoT Server RaspberryPi API</name> <name>WSO2 Carbon - IoT Server RaspberryPi API</name>
<description>WSO2 Carbon - RaspberryPi Service API Implementation</description> <description>WSO2 Carbon - RaspberryPi Service API Implementation</description>
@ -120,7 +119,6 @@
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId> <artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId>
<version>1.1.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>

@ -45,8 +45,6 @@ import java.util.concurrent.Future;
public class RaspberrypiServiceUtils { public class RaspberrypiServiceUtils {
private static final Log log = LogFactory.getLog(RaspberrypiServiceUtils.class); private static final Log log = LogFactory.getLog(RaspberrypiServiceUtils.class);
//TODO; replace this tenant domain
private static final String SUPER_TENANT = "carbon.super";
private static final String TEMPERATURE_STREAM_DEFINITION = "org.wso2.iot.devices.temperature"; private static final String TEMPERATURE_STREAM_DEFINITION = "org.wso2.iot.devices.temperature";
public static String sendCommandViaHTTP(final String deviceHTTPEndpoint, String urlContext, public static String sendCommandViaHTTP(final String deviceHTTPEndpoint, String urlContext,

@ -32,6 +32,8 @@ import java.util.List;
public class VirtualFireAlarmManagerService implements DeviceManagementService{ public class VirtualFireAlarmManagerService implements DeviceManagementService{
private DeviceManager deviceManager; private DeviceManager deviceManager;
private final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super";
@Override @Override
public String getType() { public String getType() {
return VirtualFireAlarmConstants.DEVICE_TYPE; return VirtualFireAlarmConstants.DEVICE_TYPE;
@ -40,7 +42,7 @@ public class VirtualFireAlarmManagerService implements DeviceManagementService{
@Override @Override
public String getProviderTenantDomain() { public String getProviderTenantDomain() {
return "carbon.super"; return DEVICE_TYPE_PROVIDER_DOMAIN;
} }
@Override @Override

@ -21,15 +21,14 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>device-mgt-iot-virtualfirealarm</artifactId> <artifactId>virtual-fire-alarm-plugin</artifactId>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<version>2.0.4-SNAPSHOT</version> <version>2.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl</artifactId> <artifactId>org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl</artifactId>
<version>2.0.4-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<name>WSO2 Carbon - IoT Server VirtualFireAlarm API</name> <name>WSO2 Carbon - IoT Server VirtualFireAlarm API</name>
<description>WSO2 Carbon - Virtual FireAlarm Service Management API Implementation</description> <description>WSO2 Carbon - Virtual FireAlarm Service Management API Implementation</description>
@ -57,7 +56,6 @@
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId> <artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId>
<version>1.1.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>

@ -57,23 +57,14 @@
org.osgi.service.component, org.osgi.service.component,
org.apache.commons.logging, org.apache.commons.logging,
javax.xml.bind.*, javax.xml.bind.*,
javax.naming,
javax.sql, javax.sql,
javax.xml.bind.annotation.*,
javax.xml.parsers.*;resolution:=optional,
org.w3c.dom,
org.wso2.carbon.core,
org.wso2.carbon.context, org.wso2.carbon.context,
org.wso2.carbon.utils.*,
org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.device.mgt.common.*,
org.wso2.carbon.device.mgt.mobile.*, org.wso2.carbon.device.mgt.mobile.*,
org.wso2.carbon.ndatasource.core, org.wso2.carbon.ndatasource.core,
org.wso2.carbon.policy.mgt.common.*, org.wso2.carbon.policy.mgt.common.*,
org.wso2.carbon.policy.mgt.core.*,
org.wso2.carbon.registry.core, org.wso2.carbon.registry.core,
org.wso2.carbon.registry.core.exceptions,
org.wso2.carbon.registry.core.service, org.wso2.carbon.registry.core.service,
org.wso2.carbon.registry.core.session,
org.wso2.carbon.registry.api, org.wso2.carbon.registry.api,
org.wso2.carbon.device.mgt.extensions.license.mgt.registry, org.wso2.carbon.device.mgt.extensions.license.mgt.registry,
com.google.gson.* com.google.gson.*

@ -36,12 +36,23 @@ public class AndroidDeviceManagementService implements DeviceManagementService {
private DeviceManager deviceManager; private DeviceManager deviceManager;
public static final String DEVICE_TYPE_ANDROID = "android"; public static final String DEVICE_TYPE_ANDROID = "android";
private static final String SUPER_TENANT_DOMAIN = "carbon.super";
@Override @Override
public String getType() { public String getType() {
return AndroidDeviceManagementService.DEVICE_TYPE_ANDROID; return AndroidDeviceManagementService.DEVICE_TYPE_ANDROID;
} }
@Override
public String getProviderTenantDomain() {
return SUPER_TENANT_DOMAIN;
}
@Override
public boolean isSharedWithAllTenants() {
return false;
}
@Override @Override
public void init() throws DeviceManagementException { public void init() throws DeviceManagementException {
this.deviceManager = new AndroidDeviceManager(); this.deviceManager = new AndroidDeviceManager();

@ -34,12 +34,23 @@ public class AndroidDeviceManagementService implements DeviceManagementService {
private DeviceManager deviceManager; private DeviceManager deviceManager;
public static final String DEVICE_TYPE_ANDROID = "android"; public static final String DEVICE_TYPE_ANDROID = "android";
private static final String SUPER_TENANT_DOMAIN = "carbon.super";
@Override @Override
public String getType() { public String getType() {
return AndroidDeviceManagementService.DEVICE_TYPE_ANDROID; return AndroidDeviceManagementService.DEVICE_TYPE_ANDROID;
} }
@Override
public String getProviderTenantDomain() {
return SUPER_TENANT_DOMAIN;
}
@Override
public boolean isSharedWithAllTenants() {
return true;
}
@Override @Override
public void init() throws DeviceManagementException { public void init() throws DeviceManagementException {
this.deviceManager = new AndroidDeviceManager(); this.deviceManager = new AndroidDeviceManager();

@ -36,12 +36,23 @@ public class WindowsDeviceManagementService implements DeviceManagementService {
private DeviceManager deviceManager; private DeviceManager deviceManager;
public static final String DEVICE_TYPE_WINDOWS = "windows"; public static final String DEVICE_TYPE_WINDOWS = "windows";
private static final String SUPER_TENANT_DOMAIN = "carbon.super";
@Override @Override
public String getType() { public String getType() {
return WindowsDeviceManagementService.DEVICE_TYPE_WINDOWS; return WindowsDeviceManagementService.DEVICE_TYPE_WINDOWS;
} }
@Override
public String getProviderTenantDomain() {
return SUPER_TENANT_DOMAIN;
}
@Override
public boolean isSharedWithAllTenants() {
return true;
}
@Override @Override
public void init() throws DeviceManagementException { public void init() throws DeviceManagementException {
this.deviceManager = new WindowsDeviceManager(); this.deviceManager = new WindowsDeviceManager();

@ -57,26 +57,15 @@
org.osgi.service.component, org.osgi.service.component,
org.apache.commons.logging, org.apache.commons.logging,
javax.xml.bind.*, javax.xml.bind.*,
javax.naming,
javax.sql, javax.sql,
javax.xml.bind.annotation.*,
javax.xml.parsers.*;resolution:=optional,
org.w3c.dom,
org.wso2.carbon.core,
org.wso2.carbon.context,
org.wso2.carbon.utils.*,
org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.device.mgt.common.*,
org.wso2.carbon.device.mgt.mobile.*, org.wso2.carbon.device.mgt.mobile.*,
org.wso2.carbon.ndatasource.core, org.wso2.carbon.ndatasource.core,
org.wso2.carbon.policy.mgt.common.*, org.wso2.carbon.policy.mgt.common.*,
org.wso2.carbon.policy.mgt.core.*,
org.wso2.carbon.registry.core, org.wso2.carbon.registry.core,
org.wso2.carbon.registry.core.exceptions,
org.wso2.carbon.registry.core.service, org.wso2.carbon.registry.core.service,
org.wso2.carbon.registry.core.session,
org.wso2.carbon.registry.api, org.wso2.carbon.registry.api,
org.wso2.carbon.device.mgt.extensions.license.mgt.registry, org.wso2.carbon.device.mgt.extensions.license.mgt.registry
com.google.gson.*
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
!org.wso2.carbon.device.mgt.mobile.windows.internal, !org.wso2.carbon.device.mgt.mobile.windows.internal,

@ -36,12 +36,23 @@ public class WindowsDeviceManagementService implements DeviceManagementService {
private DeviceManager deviceManager; private DeviceManager deviceManager;
public static final String DEVICE_TYPE_WINDOWS = "windows"; public static final String DEVICE_TYPE_WINDOWS = "windows";
private final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super";
@Override @Override
public String getType() { public String getType() {
return WindowsDeviceManagementService.DEVICE_TYPE_WINDOWS; return WindowsDeviceManagementService.DEVICE_TYPE_WINDOWS;
} }
@Override
public String getProviderTenantDomain() {
return DEVICE_TYPE_PROVIDER_DOMAIN;
}
@Override
public boolean isSharedWithAllTenants() {
return true;
}
@Override @Override
public void init() throws DeviceManagementException { public void init() throws DeviceManagementException {
this.deviceManager = new WindowsDeviceManager(); this.deviceManager = new WindowsDeviceManager();

Loading…
Cancel
Save