few changes after tenant changes

application-manager-new
ayyoob 9 years ago
parent 61af9dc01f
commit 1796db2807

@ -77,11 +77,6 @@
<groupId>javax.ws.rs</groupId> <groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId> <artifactId>jsr311-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>commons-httpclient.wso2</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>
</dependencies> </dependencies>

@ -15,8 +15,6 @@
*/ */
package org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl; package org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl;
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.device.mgt.iot.sample.android.sense.service.impl.util.DeviceJSON; import org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl.util.DeviceJSON;
@ -27,11 +25,13 @@ import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class AndroidSenseControllerService { public class AndroidSenseControllerService {
private static Log log = LogFactory.getLog(AndroidSenseControllerService.class); private static Log log = LogFactory.getLog(AndroidSenseControllerService.class);
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
/* Service to push all the sensor data collected by the Android /* Service to push all the sensor data collected by the Android
Called by the Android device */ Called by the Android device */
@ -57,7 +57,8 @@ public class AndroidSenseControllerService {
// temperature, "TEMPERATURE"); // temperature, "TEMPERATURE");
if (!result) { if (!result) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
//return result; //return result;
// } catch (UnauthorizedException e) { // } catch (UnauthorizedException e) {

@ -15,8 +15,6 @@
*/ */
package org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl; package org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl;
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.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
@ -42,7 +40,13 @@ import java.util.Date;
public class AndroidSenseManagerService { public class AndroidSenseManagerService {
private static Log log = LogFactory.getLog(AndroidSenseManagerService.class); private static Log log = LogFactory.getLog(AndroidSenseManagerService.class);
//TODO; replace this tenant domain
private final String SUPER_TENANT = "carbon.super";
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
@Path("/device") @Path("/device")
@PUT @PUT
@ -50,7 +54,7 @@ public class AndroidSenseManagerService {
@FormParam("owner") String owner) { @FormParam("owner") String owner) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
@ -74,19 +78,17 @@ public class AndroidSenseManagerService {
device.setType(AndroidSenseConstants.DEVICE_TYPE); device.setType(AndroidSenseConstants.DEVICE_TYPE);
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
if (added) { if (added) {
Response.status(HttpStatus.SC_OK).build(); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return added; return added;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -95,20 +97,22 @@ public class AndroidSenseManagerService {
public void removeDevice(@PathParam("device_id") String deviceId, public void removeDevice(@PathParam("device_id") String deviceId,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
try { try {
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier);
if (removed) { if (removed) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} finally {
deviceManagement.endTenantFlow();
} }
@ -120,7 +124,7 @@ public class AndroidSenseManagerService {
@FormParam("name") String name, @FormParam("name") String name,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
@ -128,8 +132,6 @@ public class AndroidSenseManagerService {
try { try {
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
device.setDeviceIdentifier(deviceId); device.setDeviceIdentifier(deviceId);
// device.setDeviceTypeId(deviceTypeId);
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.setName(name); device.setName(name);
@ -139,16 +141,18 @@ public class AndroidSenseManagerService {
if (updated) { if (updated) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return updated; return updated;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -159,7 +163,7 @@ public class AndroidSenseManagerService {
@Produces("application/json") @Produces("application/json")
public Device getDevice(@PathParam("device_id") String deviceId) { public Device getDevice(@PathParam("device_id") String deviceId) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
@ -168,9 +172,11 @@ public class AndroidSenseManagerService {
return deviceManagement.getDeviceManagementService().getDevice( return deviceManagement.getDeviceManagementService().getDevice(
deviceIdentifier); deviceIdentifier);
} catch (DeviceManagementException ex) { } catch (DeviceManagementException e) {
log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return null; return null;
} finally {
deviceManagement.endTenantFlow();
} }
} }

@ -79,13 +79,6 @@
<artifactId>jsr311-api</artifactId> <artifactId>jsr311-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>commons-httpclient.wso2</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>
</dependencies> </dependencies>

@ -16,7 +16,6 @@
package org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl; package org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl;
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.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
@ -41,19 +40,24 @@ public class ArduinoManagerService {
private static Log log = LogFactory.getLog(ArduinoManagerService.class); private static Log log = LogFactory.getLog(ArduinoManagerService.class);
//TODO; replace this tenant domain
private final String SUPER_TENANT = "carbon.super";
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
@Path("/device/register") @Path("/device/register")
@PUT @PUT
public boolean register(@QueryParam("deviceId") String deviceId, public boolean register(@QueryParam("deviceId") String deviceId,
@QueryParam("name") String name, @QueryParam("owner") String owner) { @QueryParam("name") String name, @QueryParam("owner") String owner) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE); deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
try { try {
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
Response.status(HttpStatus.SC_CONFLICT).build(); response.setStatus(Response.Status.CONFLICT.getStatusCode());
return false; return false;
} }
@ -70,19 +74,17 @@ public class ArduinoManagerService {
device.setEnrolmentInfo(enrolmentInfo); device.setEnrolmentInfo(enrolmentInfo);
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
if (added) { if (added) {
Response.status(HttpStatus.SC_OK).build(); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return added; return added;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -91,22 +93,23 @@ public class ArduinoManagerService {
public void removeDevice(@PathParam("device_id") String deviceId, public void removeDevice(@PathParam("device_id") String deviceId,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE); deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
try { try {
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier);
if (removed) { if (removed) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} finally {
deviceManagement.endTenantFlow();
} }
@ -118,7 +121,7 @@ public class ArduinoManagerService {
@QueryParam("name") String name, @QueryParam("name") String name,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
@ -137,16 +140,18 @@ public class ArduinoManagerService {
if (updated) { if (updated) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return updated; return updated;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -157,7 +162,7 @@ public class ArduinoManagerService {
@Produces("application/json") @Produces("application/json")
public Device getDevice(@PathParam("device_id") String deviceId) { public Device getDevice(@PathParam("device_id") String deviceId) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE); deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
@ -166,9 +171,11 @@ public class ArduinoManagerService {
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
return device; return device;
} catch (DeviceManagementException ex) { } catch (DeviceManagementException e) {
log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return null; return null;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -237,7 +244,7 @@ public class ArduinoManagerService {
ZipUtil ziputil = new ZipUtil(); ZipUtil ziputil = new ZipUtil();
ZipArchive zipFile = null; ZipArchive zipFile = null;
zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, token, refreshToken); zipFile = ziputil.downloadSketch(owner,SUPER_TENANT, sketchType, deviceId, token, refreshToken);
zipFile.setDeviceId(deviceId); zipFile.setDeviceId(deviceId);
return zipFile; return zipFile;
} }

@ -78,10 +78,6 @@
<artifactId>jsr311-api</artifactId> <artifactId>jsr311-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>commons-httpclient.wso2</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>
</dependencies> </dependencies>

@ -15,8 +15,6 @@
*/ */
package org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl; package org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl;
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.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
@ -48,20 +46,23 @@ import java.util.UUID;
public class DigitalDisplayManagerService { public class DigitalDisplayManagerService {
private static Log log = LogFactory.getLog(DigitalDisplayManagerService.class); private static Log log = LogFactory.getLog(DigitalDisplayManagerService.class);
//TODO; replace this tenant domain
private final String SUPER_TENANT = "carbon.super";
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
@Path("/device/register") @Path("/device/register")
@PUT @PUT
public boolean register(@QueryParam("deviceId") String deviceId, public boolean register(@QueryParam("deviceId") String deviceId,
@QueryParam("name") String name, @QueryParam("owner") String owner) { @QueryParam("name") String name, @QueryParam("owner") String owner) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE); deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE);
try { try {
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
Response.status(HttpStatus.SC_CONFLICT).build(); response.setStatus(Response.Status.CONFLICT.getStatusCode());
return false; return false;
} }
@ -78,19 +79,19 @@ public class DigitalDisplayManagerService {
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
if (added) { if (added) {
Response.status(HttpStatus.SC_OK).build(); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return added; return added;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -99,7 +100,7 @@ public class DigitalDisplayManagerService {
public void removeDevice(@PathParam("device_id") String deviceId, public void removeDevice(@PathParam("device_id") String deviceId,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE); deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE);
@ -107,15 +108,16 @@ public class DigitalDisplayManagerService {
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice( boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(
deviceIdentifier); deviceIdentifier);
if (removed) { if (removed) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} finally {
deviceManagement.endTenantFlow();
} }
@ -127,7 +129,7 @@ public class DigitalDisplayManagerService {
@QueryParam("name") String name, @QueryParam("name") String name,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
@ -147,16 +149,18 @@ public class DigitalDisplayManagerService {
if (updated) { if (updated) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return updated; return updated;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -167,7 +171,7 @@ public class DigitalDisplayManagerService {
@Produces("application/json") @Produces("application/json")
public Device getDevice(@PathParam("device_id") String deviceId) { public Device getDevice(@PathParam("device_id") String deviceId) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE); deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE);
@ -176,9 +180,11 @@ public class DigitalDisplayManagerService {
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
return device; return device;
} catch (DeviceManagementException ex) { } catch (DeviceManagementException e) {
log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return null; return null;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -215,7 +221,7 @@ public class DigitalDisplayManagerService {
ZipUtil ziputil = new ZipUtil(); ZipUtil ziputil = new ZipUtil();
ZipArchive zipFile = null; ZipArchive zipFile = null;
try { try {
zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, zipFile = ziputil.downloadSketch(owner, SUPER_TENANT,sketchType, deviceId,
token, refreshToken); token, refreshToken);
} catch (DeviceManagementException ex) { } catch (DeviceManagementException ex) {
return Response.status(500).entity("Error occurred while creating zip file").build(); return Response.status(500).entity("Error occurred while creating zip file").build();

@ -16,7 +16,6 @@
package org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl; package org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl;
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.apache.http.HttpResponse; import org.apache.http.HttpResponse;
@ -46,6 +45,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -61,6 +61,11 @@ import java.util.concurrent.Future;
public class FireAlarmControllerService { public class FireAlarmControllerService {
private static Log log = LogFactory.getLog(FireAlarmControllerService.class); private static Log log = LogFactory.getLog(FireAlarmControllerService.class);
//TODO; replace this tenant domain
private final String SUPER_TENANT = "carbon.super";
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
private static final String URL_PREFIX = "http://"; private static final String URL_PREFIX = "http://";
private static final String BULB_CONTEXT = "/BULB/"; private static final String BULB_CONTEXT = "/BULB/";
@ -89,7 +94,7 @@ public class FireAlarmControllerService {
deviceToIpMap.put(deviceId, deviceIP); deviceToIpMap.put(deviceId, deviceIP);
result = "Device-IP Registered"; result = "Device-IP Registered";
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug(result); log.debug(result);
@ -111,15 +116,14 @@ public class FireAlarmControllerService {
try { try {
DeviceValidator deviceValidator = new DeviceValidator(); DeviceValidator deviceValidator = new DeviceValidator();
if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId, if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId,
FireAlarmConstants FireAlarmConstants.DEVICE_TYPE))) {
.DEVICE_TYPE))) { response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
response.setStatus(HttpStatus.SC_UNAUTHORIZED);
return; return;
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error("DeviceValidation Failed for deviceId: " + deviceId + " of user: " + owner); log.error("DeviceValidation Failed for deviceId: " + deviceId + " of user: " + owner);
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return; return;
} }
@ -128,13 +132,13 @@ public class FireAlarmControllerService {
if (!switchToState.equals(FireAlarmConstants.STATE_ON) && !switchToState.equals( if (!switchToState.equals(FireAlarmConstants.STATE_ON) && !switchToState.equals(
FireAlarmConstants.STATE_OFF)) { FireAlarmConstants.STATE_OFF)) {
log.error("The requested state change shoud be either - 'ON' or 'OFF'"); log.error("The requested state change shoud be either - 'ON' or 'OFF'");
response.setStatus(HttpStatus.SC_BAD_REQUEST); response.setStatus(Response.Status.BAD_REQUEST.getStatusCode());
return; return;
} }
String deviceIP = deviceToIpMap.get(deviceId); String deviceIP = deviceToIpMap.get(deviceId);
if (deviceIP == null) { if (deviceIP == null) {
response.setStatus(HttpStatus.SC_PRECONDITION_FAILED); response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode());
return; return;
} }
@ -161,7 +165,7 @@ public class FireAlarmControllerService {
if (protocolString == null) { if (protocolString == null) {
sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true); sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true);
} else { } else {
response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
return; return;
} }
break; break;
@ -169,11 +173,11 @@ public class FireAlarmControllerService {
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error("Failed to send command '" + callUrlPattern + "' to: " + deviceIP + " via" + log.error("Failed to send command '" + callUrlPattern + "' to: " + deviceIP + " via" +
" " + protocol); " " + protocol);
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return; return;
} }
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} }
@ -187,15 +191,15 @@ public class FireAlarmControllerService {
DeviceValidator deviceValidator = new DeviceValidator(); DeviceValidator deviceValidator = new DeviceValidator();
try { try {
if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId, if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId,
FireAlarmConstants FireAlarmConstants
.DEVICE_TYPE))) { .DEVICE_TYPE))) {
response.setStatus(HttpStatus.SC_UNAUTHORIZED); response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
return "Unauthorized Access"; return "Unauthorized Access";
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
replyMsg = e.getErrorMessage(); replyMsg = e.getErrorMessage();
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return replyMsg; return replyMsg;
} }
@ -203,7 +207,7 @@ public class FireAlarmControllerService {
if (deviceIp == null) { if (deviceIp == null) {
replyMsg = "IP not registered for device: " + deviceId + " of owner: " + owner; replyMsg = "IP not registered for device: " + deviceId + " of owner: " + owner;
response.setStatus(HttpStatus.SC_PRECONDITION_FAILED); response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode());
return replyMsg; return replyMsg;
} }
@ -231,18 +235,18 @@ public class FireAlarmControllerService {
replyMsg = sendCommandViaHTTP(deviceIp, 80, SONAR_CONTEXT, false); replyMsg = sendCommandViaHTTP(deviceIp, 80, SONAR_CONTEXT, false);
} else { } else {
replyMsg = "Requested protocol '" + protocol + "' is not supported"; replyMsg = "Requested protocol '" + protocol + "' is not supported";
response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
return replyMsg; return replyMsg;
} }
break; break;
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
replyMsg = e.getErrorMessage(); replyMsg = e.getErrorMessage();
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return replyMsg; return replyMsg;
} }
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
replyMsg = "The current sonar reading of the device is " + replyMsg; replyMsg = "The current sonar reading of the device is " + replyMsg;
return replyMsg; return replyMsg;
} }
@ -258,15 +262,15 @@ public class FireAlarmControllerService {
DeviceValidator deviceValidator = new DeviceValidator(); DeviceValidator deviceValidator = new DeviceValidator();
try { try {
if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId, if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId,
FireAlarmConstants FireAlarmConstants
.DEVICE_TYPE))) { .DEVICE_TYPE))) {
response.setStatus(HttpStatus.SC_UNAUTHORIZED); response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
return "Unauthorized Access"; return "Unauthorized Access";
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
replyMsg = e.getErrorMessage(); replyMsg = e.getErrorMessage();
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return replyMsg; return replyMsg;
} }
@ -274,7 +278,7 @@ public class FireAlarmControllerService {
if (deviceIp == null) { if (deviceIp == null) {
replyMsg = "IP not registered for device: " + deviceId + " of owner: " + owner; replyMsg = "IP not registered for device: " + deviceId + " of owner: " + owner;
response.setStatus(HttpStatus.SC_PRECONDITION_FAILED); response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode());
return replyMsg; return replyMsg;
} }
@ -303,18 +307,18 @@ public class FireAlarmControllerService {
replyMsg = sendCommandViaHTTP(deviceIp, 80, TEMPERATURE_CONTEXT, false); replyMsg = sendCommandViaHTTP(deviceIp, 80, TEMPERATURE_CONTEXT, false);
} else { } else {
replyMsg = "Requested protocol '" + protocol + "' is not supported"; replyMsg = "Requested protocol '" + protocol + "' is not supported";
response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
return replyMsg; return replyMsg;
} }
break; break;
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
replyMsg = e.getErrorMessage(); replyMsg = e.getErrorMessage();
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return replyMsg; return replyMsg;
} }
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
replyMsg = "The current temperature of the device is " + replyMsg; replyMsg = "The current temperature of the device is " + replyMsg;
return replyMsg; return replyMsg;
} }
@ -385,14 +389,14 @@ public class FireAlarmControllerService {
"Unregistered IP: Temperature Data Received from an un-registered IP " + "Unregistered IP: Temperature Data Received from an un-registered IP " +
deviceIp + deviceIp +
" for device ID - " + deviceId); " for device ID - " + deviceId);
response.setStatus(HttpStatus.SC_PRECONDITION_FAILED); response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode());
return; return;
} else if (!registeredIp.equals(deviceIp)) { } else if (!registeredIp.equals(deviceIp)) {
log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " +
deviceId + deviceId +
" is already registered under some other IP. Re-registration " + " is already registered under some other IP. Re-registration " +
"required"); "required");
response.setStatus(HttpStatus.SC_CONFLICT); response.setStatus(Response.Status.CONFLICT.getStatusCode());
return; return;
} }
@ -408,10 +412,10 @@ public class FireAlarmControllerService {
if (!result) { if (!result) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} }
} catch (UnauthorizedException e) { } catch (UnauthorizedException e) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error("Data Push Attempt Failed for BAM Publisher: " + e.getMessage()); log.error("Data Push Attempt Failed for BAM Publisher: " + e.getMessage());
} }
@ -427,10 +431,10 @@ public class FireAlarmControllerService {
if (!result) { if (!result) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} }
} catch (UnauthorizedException e) { } catch (UnauthorizedException e) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error("Data Push Attempt Failed for CEP Publisher: " + e.getMessage()); log.error("Data Push Attempt Failed for CEP Publisher: " + e.getMessage());
} }
} }
@ -465,7 +469,7 @@ public class FireAlarmControllerService {
temperature, "TEMPERATURE"); temperature, "TEMPERATURE");
if (!result) { if (!result) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error("Error whilst pushing temperature: " + sensorValues); log.error("Error whilst pushing temperature: " + sensorValues);
return; return;
} }
@ -478,7 +482,7 @@ public class FireAlarmControllerService {
"BULB"); "BULB");
if (!result) { if (!result) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error("Error whilst pushing Bulb data: " + sensorValues); log.error("Error whilst pushing Bulb data: " + sensorValues);
return; return;
} }
@ -491,7 +495,7 @@ public class FireAlarmControllerService {
"SONAR"); "SONAR");
if (!result) { if (!result) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error("Error whilst pushing Sonar data: " + sensorValues); log.error("Error whilst pushing Sonar data: " + sensorValues);
} }
@ -503,13 +507,13 @@ public class FireAlarmControllerService {
System.currentTimeMillis(), "DeviceData", System.currentTimeMillis(), "DeviceData",
dataMsg.value, dataMsg.reply); dataMsg.value, dataMsg.reply);
if (!result) { if (!result) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error("Error whilst pushing sensor data: " + sensorValues); log.error("Error whilst pushing sensor data: " + sensorValues);
} }
} }
} catch (UnauthorizedException e) { } catch (UnauthorizedException e) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error("Data Push Attempt Failed at Publisher: " + e.getMessage()); log.error("Data Push Attempt Failed at Publisher: " + e.getMessage());
} }
} }
@ -544,7 +548,9 @@ public class FireAlarmControllerService {
String scriptPath = CarbonUtils.getCarbonHome() + seperator + scriptsFolder + seperator String scriptPath = CarbonUtils.getCarbonHome() + seperator + scriptsFolder + seperator
+ "xmpp_client.py "; + "xmpp_client.py ";
scriptArguments = "-j " + xmppAdminUserLogin + " -p " + xmppAdminPass + " -c " + clientToConnect + " -r " + resource + " -s " + state; scriptArguments =
"-j " + xmppAdminUserLogin + " -p " + xmppAdminPass + " -c " + clientToConnect +
" -r " + resource + " -s " + state;
command = "python " + scriptPath + scriptArguments; command = "python " + scriptPath + scriptArguments;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {

@ -16,7 +16,7 @@
package org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl; package org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl;
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.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
@ -59,19 +59,24 @@ public class FireAlarmManagerService {
private static Log log = LogFactory.getLog(FireAlarmManagerService.class); private static Log log = LogFactory.getLog(FireAlarmManagerService.class);
//TODO; replace this tenant domain
private final String SUPER_TENANT = "carbon.super";
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
@Path("/device/register") @Path("/device/register")
@PUT @PUT
public boolean register(@QueryParam("deviceId") String deviceId, public boolean register(@QueryParam("deviceId") String deviceId,
@QueryParam("name") String name, @QueryParam("owner") String owner) { @QueryParam("name") String name, @QueryParam("owner") String owner) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE); deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE);
try { try {
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
Response.status(HttpStatus.SC_CONFLICT).build(); response.setStatus(Response.Status.CONFLICT.getStatusCode());
return false; return false;
} }
@ -91,15 +96,17 @@ public class FireAlarmManagerService {
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
if (added) { if (added) {
Response.status(HttpStatus.SC_OK).build(); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return added; return added;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -108,7 +115,7 @@ public class FireAlarmManagerService {
public void removeDevice(@PathParam("device_id") String deviceId, public void removeDevice(@PathParam("device_id") String deviceId,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE); deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE);
@ -116,15 +123,16 @@ public class FireAlarmManagerService {
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice( boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(
deviceIdentifier); deviceIdentifier);
if (removed) { if (removed) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -135,7 +143,7 @@ public class FireAlarmManagerService {
@QueryParam("name") String name, @QueryParam("name") String name,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
@ -155,16 +163,18 @@ public class FireAlarmManagerService {
device); device);
if (updated) { if (updated) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return updated; return updated;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -175,7 +185,7 @@ public class FireAlarmManagerService {
@Produces("application/json") @Produces("application/json")
public Device getDevice(@PathParam("device_id") String deviceId) { public Device getDevice(@PathParam("device_id") String deviceId) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE); deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE);
@ -183,9 +193,11 @@ public class FireAlarmManagerService {
try { try {
return deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); return deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
} catch (DeviceManagementException ex) { } catch (DeviceManagementException e) {
log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return null; return null;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -196,7 +208,7 @@ public class FireAlarmManagerService {
@Produces("application/json") @Produces("application/json")
public Device[] getFirealarmDevices(@PathParam("username") String username) { public Device[] getFirealarmDevices(@PathParam("username") String username) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
try { try {
List<Device> userDevices = List<Device> userDevices =
@ -214,9 +226,11 @@ public class FireAlarmManagerService {
} }
return userDevicesforFirealarm.toArray(new Device[]{}); return userDevicesforFirealarm.toArray(new Device[]{});
} catch (DeviceManagementException ex) { } catch (DeviceManagementException e) {
log.error("Error occurred while retrieving devices for " + username); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return null; return null;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -332,7 +346,7 @@ public class FireAlarmManagerService {
ZipUtil ziputil = new ZipUtil(); ZipUtil ziputil = new ZipUtil();
ZipArchive zipFile = null; ZipArchive zipFile = null;
zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, accessToken, refreshToken); zipFile = ziputil.downloadSketch(owner,SUPER_TENANT, sketchType, deviceId, accessToken, refreshToken);
zipFile.setDeviceId(deviceId); zipFile.setDeviceId(deviceId);
return zipFile; return zipFile;
} }

@ -78,11 +78,6 @@
<artifactId>jsr311-api</artifactId> <artifactId>jsr311-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>commons-httpclient.wso2</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>
</dependencies> </dependencies>

@ -15,8 +15,6 @@
*/ */
package org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl; package org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl;
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.device.mgt.iot.common.datastore.impl.DataStreamDefinitions; import org.wso2.carbon.device.mgt.iot.common.datastore.impl.DataStreamDefinitions;
@ -32,10 +30,14 @@ import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class RaspberrypiControllerService { public class RaspberrypiControllerService {
private static Log log = LogFactory.getLog(RaspberrypiControllerService.class); private static Log log = LogFactory.getLog(RaspberrypiControllerService.class);
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
/* Service to push all the sensor data collected by the Arduino /* Service to push all the sensor data collected by the Arduino
Called by the Arduino device */ Called by the Arduino device */
@ -61,11 +63,11 @@ public class RaspberrypiControllerService {
DataStreamDefinitions.StreamTypeLabel.TEMPERATURE); DataStreamDefinitions.StreamTypeLabel.TEMPERATURE);
if (!result) { if (!result) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} }
} catch (UnauthorizedException e) { } catch (UnauthorizedException e) {
response.setStatus(HttpStatus.SC_UNAUTHORIZED); response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
} }
} }

@ -16,7 +16,6 @@
package org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl; package org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl;
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.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
@ -49,19 +48,24 @@ public class RaspberrypiManagerService {
private static Log log = LogFactory.getLog(RaspberrypiManagerService.class); private static Log log = LogFactory.getLog(RaspberrypiManagerService.class);
//TODO; replace this tenant domain
private final String SUPER_TENANT = "carbon.super";
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
@Path("/device/register") @Path("/device/register")
@PUT @PUT
public boolean register(@QueryParam("deviceId") String deviceId, public boolean register(@QueryParam("deviceId") String deviceId,
@QueryParam("name") String name, @QueryParam("owner") String owner) { @QueryParam("name") String name, @QueryParam("owner") String owner) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE); deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
try { try {
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
Response.status(HttpStatus.SC_CONFLICT).build(); response.setStatus(Response.Status.CONFLICT.getStatusCode());
return false; return false;
} }
@ -77,13 +81,9 @@ public class RaspberrypiManagerService {
enrolmentInfo.setOwner(owner); enrolmentInfo.setOwner(owner);
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
if (added) { if (added) {
Response.status(HttpStatus.SC_OK).build(); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return added; return added;
@ -98,18 +98,16 @@ public class RaspberrypiManagerService {
public void removeDevice(@PathParam("device_id") String deviceId, public void removeDevice(@PathParam("device_id") String deviceId,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE); deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
try { try {
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier);
if (removed) { if (removed) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); log.error(e.getErrorMessage());
@ -125,7 +123,7 @@ public class RaspberrypiManagerService {
@QueryParam("name") String name, @QueryParam("name") String name,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
@ -144,10 +142,10 @@ public class RaspberrypiManagerService {
if (updated) { if (updated) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return updated; return updated;
@ -164,7 +162,7 @@ public class RaspberrypiManagerService {
@Produces("application/json") @Produces("application/json")
public Device getDevice(@PathParam("device_id") String deviceId) { public Device getDevice(@PathParam("device_id") String deviceId) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE); deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
@ -211,7 +209,7 @@ public class RaspberrypiManagerService {
ZipUtil ziputil = new ZipUtil(); ZipUtil ziputil = new ZipUtil();
ZipArchive zipFile = null; ZipArchive zipFile = null;
try { try {
zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, zipFile = ziputil.downloadSketch(owner,SUPER_TENANT, sketchType, deviceId,
token,refreshToken); token,refreshToken);
} catch (DeviceManagementException ex) { } catch (DeviceManagementException ex) {
return Response.status(500).entity("Error occurred while creating zip file").build(); return Response.status(500).entity("Error occurred while creating zip file").build();

@ -77,11 +77,6 @@
<groupId>javax.ws.rs</groupId> <groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId> <artifactId>jsr311-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>commons-httpclient.wso2</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>
</dependencies> </dependencies>

@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl;
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.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients; import org.apache.http.impl.nio.client.HttpAsyncClients;
@ -35,7 +34,6 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Future;
public class SensebotControllerService { public class SensebotControllerService {
@ -340,7 +338,7 @@ public class SensebotControllerService {
log.debug(urlString); log.debug(urlString);
} }
HttpGet request = new HttpGet(urlString); HttpGet request = new HttpGet(urlString);
Future<HttpResponse> future = httpclient.execute(request, null); httpclient.execute(request, null);
} }
} }

@ -16,7 +16,6 @@
package org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl; package org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl;
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.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
@ -41,19 +40,24 @@ public class SensebotManagerService {
private static Log log = LogFactory.getLog(SensebotManagerService.class); private static Log log = LogFactory.getLog(SensebotManagerService.class);
//TODO; replace this tenant domain
private final String SUPER_TENANT = "carbon.super";
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response;
@Path("/device/register") @Path("/device/register")
@PUT @PUT
public boolean register(@QueryParam("deviceId") String deviceId, public boolean register(@QueryParam("deviceId") String deviceId,
@QueryParam("name") String name, @QueryParam("owner") String owner) { @QueryParam("name") String name, @QueryParam("owner") String owner) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(SensebotConstants.DEVICE_TYPE); deviceIdentifier.setType(SensebotConstants.DEVICE_TYPE);
try { try {
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
Response.status(HttpStatus.SC_CONFLICT).build(); response.setStatus(Response.Status.CONFLICT.getStatusCode());
return false; return false;
} }
@ -71,19 +75,17 @@ public class SensebotManagerService {
device.setEnrolmentInfo(enrolmentInfo); device.setEnrolmentInfo(enrolmentInfo);
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
if (added) { if (added) {
Response.status(HttpStatus.SC_OK).build(); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return added; return added;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -92,22 +94,22 @@ public class SensebotManagerService {
public void removeDevice(@PathParam("device_id") String deviceId, public void removeDevice(@PathParam("device_id") String deviceId,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(SensebotConstants.DEVICE_TYPE); deviceIdentifier.setType(SensebotConstants.DEVICE_TYPE);
try { try {
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier);
if (removed) { if (removed) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} finally {
deviceManagement.endTenantFlow();
} }
@ -119,7 +121,7 @@ public class SensebotManagerService {
@QueryParam("name") String name, @QueryParam("name") String name,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
@ -127,10 +129,7 @@ public class SensebotManagerService {
try { try {
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
device.setDeviceIdentifier(deviceId); device.setDeviceIdentifier(deviceId);
// device.setDeviceTypeId(deviceTypeId);
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.setName(name); device.setName(name);
device.setType(SensebotConstants.DEVICE_TYPE); device.setType(SensebotConstants.DEVICE_TYPE);
@ -138,16 +137,16 @@ public class SensebotManagerService {
if (updated) { if (updated) {
response.setStatus(HttpStatus.SC_OK); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
return updated; return updated;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error(e.getErrorMessage()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@ -158,7 +157,7 @@ public class SensebotManagerService {
@Produces("application/json") @Produces("application/json")
public Device getDevice(@PathParam("device_id") String deviceId) { public Device getDevice(@PathParam("device_id") String deviceId) {
DeviceManagement deviceManagement = new DeviceManagement(); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(SensebotConstants.DEVICE_TYPE); deviceIdentifier.setType(SensebotConstants.DEVICE_TYPE);
@ -167,11 +166,15 @@ public class SensebotManagerService {
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
return device; return device;
} catch (DeviceManagementException ex) { } catch (DeviceManagementException e) {
log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error("Error occurred while retrieving device with Id " + deviceId + "\n");
return null; return null;
} finally {
deviceManagement.endTenantFlow();
} }
} }
@Path("/device/{sketch_type}/download") @Path("/device/{sketch_type}/download")
@ -205,7 +208,7 @@ public class SensebotManagerService {
ZipUtil ziputil = new ZipUtil(); ZipUtil ziputil = new ZipUtil();
ZipArchive zipFile = null; ZipArchive zipFile = null;
try { try {
zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, zipFile = ziputil.downloadSketch(owner,SUPER_TENANT, sketchType, deviceId,
token,refreshToken); token,refreshToken);
} catch (DeviceManagementException ex) { } catch (DeviceManagementException ex) {
return Response.status(500).entity("Error occurred while creating zip file").build(); return Response.status(500).entity("Error occurred while creating zip file").build();

Loading…
Cancel
Save