diff --git a/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/pom.xml b/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/pom.xml index 86f9f130..5626c9b6 100644 --- a/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/pom.xml +++ b/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/pom.xml @@ -77,11 +77,6 @@ javax.ws.rs jsr311-api - - - commons-httpclient.wso2 - commons-httpclient - diff --git a/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseControllerService.java b/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseControllerService.java index 71e79e94..828367f3 100644 --- a/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseControllerService.java +++ b/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseControllerService.java @@ -15,8 +15,6 @@ */ 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.LogFactory; 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.core.Context; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; public class AndroidSenseControllerService { 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 Called by the Android device */ @@ -57,7 +57,8 @@ public class AndroidSenseControllerService { // temperature, "TEMPERATURE"); if (!result) { - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); + } //return result; // } catch (UnauthorizedException e) { diff --git a/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseManagerService.java b/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseManagerService.java index 63b3a127..f1aaa122 100644 --- a/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseManagerService.java +++ b/modules/distribution/src/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseManagerService.java @@ -15,8 +15,6 @@ */ 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.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -42,7 +40,13 @@ import java.util.Date; public class AndroidSenseManagerService { + + 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") @PUT @@ -50,7 +54,7 @@ public class AndroidSenseManagerService { @FormParam("owner") String owner) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); @@ -74,19 +78,17 @@ public class AndroidSenseManagerService { device.setType(AndroidSenseConstants.DEVICE_TYPE); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); if (added) { - Response.status(HttpStatus.SC_OK).build(); - - + response.setStatus(Response.Status.OK.getStatusCode()); } else { - Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); - - + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return added; } catch (DeviceManagementException e) { - log.error(e.getErrorMessage()); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return false; + } finally { + deviceManagement.endTenantFlow(); } } @@ -95,20 +97,22 @@ public class AndroidSenseManagerService { public void removeDevice(@PathParam("device_id") String deviceId, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); try { boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); if (removed) { - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } } 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, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); @@ -128,8 +132,6 @@ public class AndroidSenseManagerService { try { Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); device.setDeviceIdentifier(deviceId); - - // device.setDeviceTypeId(deviceTypeId); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.setName(name); @@ -139,16 +141,18 @@ public class AndroidSenseManagerService { if (updated) { - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return updated; } catch (DeviceManagementException e) { - log.error(e.getErrorMessage()); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return false; + } finally { + deviceManagement.endTenantFlow(); } } @@ -159,7 +163,7 @@ public class AndroidSenseManagerService { @Produces("application/json") public Device getDevice(@PathParam("device_id") String deviceId) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); @@ -168,9 +172,11 @@ public class AndroidSenseManagerService { return deviceManagement.getDeviceManagementService().getDevice( deviceIdentifier); - } catch (DeviceManagementException ex) { - log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex); + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return null; + } finally { + deviceManagement.endTenantFlow(); } } diff --git a/modules/distribution/src/samples/arduino/src/org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl/pom.xml b/modules/distribution/src/samples/arduino/src/org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl/pom.xml index fce2f87b..802b3b46 100644 --- a/modules/distribution/src/samples/arduino/src/org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl/pom.xml +++ b/modules/distribution/src/samples/arduino/src/org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl/pom.xml @@ -79,13 +79,6 @@ jsr311-api - - commons-httpclient.wso2 - commons-httpclient - - - - diff --git a/modules/distribution/src/samples/arduino/src/org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/arduino/service/impl/ArduinoManagerService.java b/modules/distribution/src/samples/arduino/src/org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/arduino/service/impl/ArduinoManagerService.java index 73ed8d84..06512681 100644 --- a/modules/distribution/src/samples/arduino/src/org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/arduino/service/impl/ArduinoManagerService.java +++ b/modules/distribution/src/samples/arduino/src/org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/arduino/service/impl/ArduinoManagerService.java @@ -16,7 +16,6 @@ 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.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -41,19 +40,24 @@ public class ArduinoManagerService { 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") @PUT public boolean register(@QueryParam("deviceId") String deviceId, @QueryParam("name") String name, @QueryParam("owner") String owner) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE); try { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { - Response.status(HttpStatus.SC_CONFLICT).build(); + response.setStatus(Response.Status.CONFLICT.getStatusCode()); return false; } @@ -70,19 +74,17 @@ public class ArduinoManagerService { device.setEnrolmentInfo(enrolmentInfo); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); if (added) { - Response.status(HttpStatus.SC_OK).build(); - - + response.setStatus(Response.Status.OK.getStatusCode()); } else { - Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); - - + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return added; } catch (DeviceManagementException e) { - log.error(e.getErrorMessage()); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return false; + } finally { + deviceManagement.endTenantFlow(); } } @@ -91,22 +93,23 @@ public class ArduinoManagerService { public void removeDevice(@PathParam("device_id") String deviceId, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE); try { boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); if (removed) { - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } } 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, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); @@ -137,16 +140,18 @@ public class ArduinoManagerService { if (updated) { - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return updated; } catch (DeviceManagementException e) { - log.error(e.getErrorMessage()); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return false; + } finally { + deviceManagement.endTenantFlow(); } } @@ -157,7 +162,7 @@ public class ArduinoManagerService { @Produces("application/json") public Device getDevice(@PathParam("device_id") String deviceId) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE); @@ -166,9 +171,11 @@ public class ArduinoManagerService { Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); return device; - } catch (DeviceManagementException ex) { - log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex); + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return null; + } finally { + deviceManagement.endTenantFlow(); } } @@ -237,7 +244,7 @@ public class ArduinoManagerService { ZipUtil ziputil = new ZipUtil(); ZipArchive zipFile = null; - zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, token, refreshToken); + zipFile = ziputil.downloadSketch(owner,SUPER_TENANT, sketchType, deviceId, token, refreshToken); zipFile.setDeviceId(deviceId); return zipFile; } diff --git a/modules/distribution/src/samples/digital_display/src/org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl/pom.xml b/modules/distribution/src/samples/digital_display/src/org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl/pom.xml index 7a442aa7..4f6c5a8c 100644 --- a/modules/distribution/src/samples/digital_display/src/org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl/pom.xml +++ b/modules/distribution/src/samples/digital_display/src/org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl/pom.xml @@ -78,10 +78,6 @@ jsr311-api - - commons-httpclient.wso2 - commons-httpclient - diff --git a/modules/distribution/src/samples/digital_display/src/org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/digitaldisplay/service/impl/DigitalDisplayManagerService.java b/modules/distribution/src/samples/digital_display/src/org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/digitaldisplay/service/impl/DigitalDisplayManagerService.java index 8a98ee39..f9f679b4 100644 --- a/modules/distribution/src/samples/digital_display/src/org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/digitaldisplay/service/impl/DigitalDisplayManagerService.java +++ b/modules/distribution/src/samples/digital_display/src/org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/digitaldisplay/service/impl/DigitalDisplayManagerService.java @@ -15,8 +15,6 @@ */ 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.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -48,20 +46,23 @@ import java.util.UUID; public class DigitalDisplayManagerService { 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") @PUT public boolean register(@QueryParam("deviceId") String deviceId, @QueryParam("name") String name, @QueryParam("owner") String owner) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE); try { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { - Response.status(HttpStatus.SC_CONFLICT).build(); + response.setStatus(Response.Status.CONFLICT.getStatusCode()); return false; } @@ -78,19 +79,19 @@ public class DigitalDisplayManagerService { enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); if (added) { - Response.status(HttpStatus.SC_OK).build(); - - + response.setStatus(Response.Status.OK.getStatusCode()); } else { - Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return added; } catch (DeviceManagementException e) { - log.error(e.getErrorMessage()); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return false; + } finally { + deviceManagement.endTenantFlow(); } } @@ -99,7 +100,7 @@ public class DigitalDisplayManagerService { public void removeDevice(@PathParam("device_id") String deviceId, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE); @@ -107,15 +108,16 @@ public class DigitalDisplayManagerService { boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice( deviceIdentifier); if (removed) { - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } } 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, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); @@ -147,16 +149,18 @@ public class DigitalDisplayManagerService { if (updated) { - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return updated; } catch (DeviceManagementException e) { - log.error(e.getErrorMessage()); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return false; + } finally { + deviceManagement.endTenantFlow(); } } @@ -167,7 +171,7 @@ public class DigitalDisplayManagerService { @Produces("application/json") public Device getDevice(@PathParam("device_id") String deviceId) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE); @@ -176,9 +180,11 @@ public class DigitalDisplayManagerService { Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); return device; - } catch (DeviceManagementException ex) { - log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex); + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return null; + } finally { + deviceManagement.endTenantFlow(); } } @@ -215,7 +221,7 @@ public class DigitalDisplayManagerService { ZipUtil ziputil = new ZipUtil(); ZipArchive zipFile = null; try { - zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, + zipFile = ziputil.downloadSketch(owner, SUPER_TENANT,sketchType, deviceId, token, refreshToken); } catch (DeviceManagementException ex) { return Response.status(500).entity("Error occurred while creating zip file").build(); diff --git a/modules/distribution/src/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmControllerService.java b/modules/distribution/src/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmControllerService.java index 21132285..72247904 100644 --- a/modules/distribution/src/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmControllerService.java +++ b/modules/distribution/src/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmControllerService.java @@ -16,7 +16,6 @@ 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.LogFactory; import org.apache.http.HttpResponse; @@ -46,6 +45,7 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -61,6 +61,11 @@ import java.util.concurrent.Future; public class FireAlarmControllerService { 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 BULB_CONTEXT = "/BULB/"; @@ -89,7 +94,7 @@ public class FireAlarmControllerService { deviceToIpMap.put(deviceId, deviceIP); result = "Device-IP Registered"; - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); if (log.isDebugEnabled()) { log.debug(result); @@ -111,15 +116,14 @@ public class FireAlarmControllerService { try { DeviceValidator deviceValidator = new DeviceValidator(); - if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId, - FireAlarmConstants - .DEVICE_TYPE))) { - response.setStatus(HttpStatus.SC_UNAUTHORIZED); + if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, + FireAlarmConstants.DEVICE_TYPE))) { + response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); return; } } catch (DeviceManagementException e) { 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; } @@ -128,13 +132,13 @@ public class FireAlarmControllerService { if (!switchToState.equals(FireAlarmConstants.STATE_ON) && !switchToState.equals( FireAlarmConstants.STATE_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; } String deviceIP = deviceToIpMap.get(deviceId); if (deviceIP == null) { - response.setStatus(HttpStatus.SC_PRECONDITION_FAILED); + response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); return; } @@ -161,7 +165,7 @@ public class FireAlarmControllerService { if (protocolString == null) { sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true); } else { - response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); return; } break; @@ -169,11 +173,11 @@ public class FireAlarmControllerService { } catch (DeviceManagementException e) { log.error("Failed to send command '" + callUrlPattern + "' to: " + deviceIP + " via" + " " + protocol); - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return; } - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); } @@ -187,15 +191,15 @@ public class FireAlarmControllerService { DeviceValidator deviceValidator = new DeviceValidator(); try { - if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId, - FireAlarmConstants - .DEVICE_TYPE))) { - response.setStatus(HttpStatus.SC_UNAUTHORIZED); + if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, + FireAlarmConstants + .DEVICE_TYPE))) { + response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); return "Unauthorized Access"; } } catch (DeviceManagementException e) { replyMsg = e.getErrorMessage(); - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return replyMsg; } @@ -203,7 +207,7 @@ public class FireAlarmControllerService { if (deviceIp == null) { 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; } @@ -231,18 +235,18 @@ public class FireAlarmControllerService { replyMsg = sendCommandViaHTTP(deviceIp, 80, SONAR_CONTEXT, false); } else { replyMsg = "Requested protocol '" + protocol + "' is not supported"; - response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); return replyMsg; } break; } } catch (DeviceManagementException e) { replyMsg = e.getErrorMessage(); - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return replyMsg; } - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); replyMsg = "The current sonar reading of the device is " + replyMsg; return replyMsg; } @@ -258,15 +262,15 @@ public class FireAlarmControllerService { DeviceValidator deviceValidator = new DeviceValidator(); try { - if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId, - FireAlarmConstants - .DEVICE_TYPE))) { - response.setStatus(HttpStatus.SC_UNAUTHORIZED); + if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, + FireAlarmConstants + .DEVICE_TYPE))) { + response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); return "Unauthorized Access"; } } catch (DeviceManagementException e) { replyMsg = e.getErrorMessage(); - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return replyMsg; } @@ -274,7 +278,7 @@ public class FireAlarmControllerService { if (deviceIp == null) { 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; } @@ -303,18 +307,18 @@ public class FireAlarmControllerService { replyMsg = sendCommandViaHTTP(deviceIp, 80, TEMPERATURE_CONTEXT, false); } else { replyMsg = "Requested protocol '" + protocol + "' is not supported"; - response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); return replyMsg; } break; } } catch (DeviceManagementException e) { replyMsg = e.getErrorMessage(); - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return replyMsg; } - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); replyMsg = "The current temperature of the device is " + replyMsg; return replyMsg; } @@ -385,14 +389,14 @@ public class FireAlarmControllerService { "Unregistered IP: Temperature Data Received from an un-registered IP " + deviceIp + " for device ID - " + deviceId); - response.setStatus(HttpStatus.SC_PRECONDITION_FAILED); + response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); return; } else if (!registeredIp.equals(deviceIp)) { log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + deviceId + " is already registered under some other IP. Re-registration " + "required"); - response.setStatus(HttpStatus.SC_CONFLICT); + response.setStatus(Response.Status.CONFLICT.getStatusCode()); return; } @@ -408,10 +412,10 @@ public class FireAlarmControllerService { if (!result) { - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } } 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()); } @@ -427,10 +431,10 @@ public class FireAlarmControllerService { if (!result) { - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } } 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()); } } @@ -465,7 +469,7 @@ public class FireAlarmControllerService { temperature, "TEMPERATURE"); if (!result) { - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error("Error whilst pushing temperature: " + sensorValues); return; } @@ -478,7 +482,7 @@ public class FireAlarmControllerService { "BULB"); 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); return; } @@ -491,7 +495,7 @@ public class FireAlarmControllerService { "SONAR"); 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); } @@ -503,13 +507,13 @@ public class FireAlarmControllerService { System.currentTimeMillis(), "DeviceData", dataMsg.value, dataMsg.reply); 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); } } } 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()); } } @@ -544,10 +548,12 @@ public class FireAlarmControllerService { String scriptPath = CarbonUtils.getCarbonHome() + seperator + scriptsFolder + seperator + "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; - if (log.isDebugEnabled()){ + if (log.isDebugEnabled()) { log.debug("Connecting to XMPP Server via Admin credentials: " + xmppAdminUserLogin); log.debug("Trying to contact xmpp device account: " + clientToConnect); log.debug("Arguments used for the scripts: '" + scriptArguments + "'"); diff --git a/modules/distribution/src/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmManagerService.java b/modules/distribution/src/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmManagerService.java index fe7d0426..a2931272 100644 --- a/modules/distribution/src/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmManagerService.java +++ b/modules/distribution/src/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmManagerService.java @@ -16,7 +16,7 @@ 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.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -59,19 +59,24 @@ public class FireAlarmManagerService { 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") @PUT public boolean register(@QueryParam("deviceId") String deviceId, @QueryParam("name") String name, @QueryParam("owner") String owner) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE); try { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { - Response.status(HttpStatus.SC_CONFLICT).build(); + response.setStatus(Response.Status.CONFLICT.getStatusCode()); return false; } @@ -91,15 +96,17 @@ public class FireAlarmManagerService { boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); if (added) { - Response.status(HttpStatus.SC_OK).build(); + response.setStatus(Response.Status.OK.getStatusCode()); } else { - Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return added; } catch (DeviceManagementException e) { - log.error(e.getErrorMessage()); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return false; + } finally { + deviceManagement.endTenantFlow(); } } @@ -108,7 +115,7 @@ public class FireAlarmManagerService { public void removeDevice(@PathParam("device_id") String deviceId, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE); @@ -116,15 +123,16 @@ public class FireAlarmManagerService { boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice( deviceIdentifier); if (removed) { - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } } 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, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); @@ -155,16 +163,18 @@ public class FireAlarmManagerService { device); if (updated) { - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return updated; } catch (DeviceManagementException e) { - log.error(e.getErrorMessage()); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return false; + } finally { + deviceManagement.endTenantFlow(); } } @@ -175,7 +185,7 @@ public class FireAlarmManagerService { @Produces("application/json") public Device getDevice(@PathParam("device_id") String deviceId) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE); @@ -183,9 +193,11 @@ public class FireAlarmManagerService { try { return deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); - } catch (DeviceManagementException ex) { - log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex); + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return null; + } finally { + deviceManagement.endTenantFlow(); } } @@ -196,7 +208,7 @@ public class FireAlarmManagerService { @Produces("application/json") public Device[] getFirealarmDevices(@PathParam("username") String username) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); try { List userDevices = @@ -214,9 +226,11 @@ public class FireAlarmManagerService { } return userDevicesforFirealarm.toArray(new Device[]{}); - } catch (DeviceManagementException ex) { - log.error("Error occurred while retrieving devices for " + username); + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return null; + } finally { + deviceManagement.endTenantFlow(); } } @@ -332,7 +346,7 @@ public class FireAlarmManagerService { ZipUtil ziputil = new ZipUtil(); ZipArchive zipFile = null; - zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, accessToken, refreshToken); + zipFile = ziputil.downloadSketch(owner,SUPER_TENANT, sketchType, deviceId, accessToken, refreshToken); zipFile.setDeviceId(deviceId); return zipFile; } diff --git a/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/pom.xml b/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/pom.xml index eb03683d..122df7fe 100644 --- a/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/pom.xml +++ b/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/pom.xml @@ -78,11 +78,6 @@ jsr311-api - - commons-httpclient.wso2 - commons-httpclient - - diff --git a/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/raspberrypi/service/impl/RaspberrypiControllerService.java b/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/raspberrypi/service/impl/RaspberrypiControllerService.java index 7a3029ce..134b281c 100644 --- a/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/raspberrypi/service/impl/RaspberrypiControllerService.java +++ b/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/raspberrypi/service/impl/RaspberrypiControllerService.java @@ -15,8 +15,6 @@ */ 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.LogFactory; 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.core.Context; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; public class RaspberrypiControllerService { 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 Called by the Arduino device */ @@ -61,11 +63,11 @@ public class RaspberrypiControllerService { DataStreamDefinitions.StreamTypeLabel.TEMPERATURE); if (!result) { - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } } catch (UnauthorizedException e) { - response.setStatus(HttpStatus.SC_UNAUTHORIZED); + response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); } } diff --git a/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/raspberrypi/service/impl/RaspberrypiManagerService.java b/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/raspberrypi/service/impl/RaspberrypiManagerService.java index 68dbdef7..b37ef4d4 100644 --- a/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/raspberrypi/service/impl/RaspberrypiManagerService.java +++ b/modules/distribution/src/samples/raspberrypi/src/org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/raspberrypi/service/impl/RaspberrypiManagerService.java @@ -16,7 +16,6 @@ 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.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -49,19 +48,24 @@ public class RaspberrypiManagerService { 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") @PUT public boolean register(@QueryParam("deviceId") String deviceId, @QueryParam("name") String name, @QueryParam("owner") String owner) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE); try { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { - Response.status(HttpStatus.SC_CONFLICT).build(); + response.setStatus(Response.Status.CONFLICT.getStatusCode()); return false; } @@ -77,13 +81,9 @@ public class RaspberrypiManagerService { enrolmentInfo.setOwner(owner); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); if (added) { - Response.status(HttpStatus.SC_OK).build(); - - + response.setStatus(Response.Status.OK.getStatusCode()); } else { - Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); - - + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return added; @@ -98,18 +98,16 @@ public class RaspberrypiManagerService { public void removeDevice(@PathParam("device_id") String deviceId, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE); try { boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); if (removed) { - response.setStatus(HttpStatus.SC_OK); - + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); - + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } } catch (DeviceManagementException e) { log.error(e.getErrorMessage()); @@ -125,7 +123,7 @@ public class RaspberrypiManagerService { @QueryParam("name") String name, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); @@ -144,10 +142,10 @@ public class RaspberrypiManagerService { if (updated) { - response.setStatus(HttpStatus.SC_OK); + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return updated; @@ -164,7 +162,7 @@ public class RaspberrypiManagerService { @Produces("application/json") public Device getDevice(@PathParam("device_id") String deviceId) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE); @@ -211,7 +209,7 @@ public class RaspberrypiManagerService { ZipUtil ziputil = new ZipUtil(); ZipArchive zipFile = null; try { - zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, + zipFile = ziputil.downloadSketch(owner,SUPER_TENANT, sketchType, deviceId, token,refreshToken); } catch (DeviceManagementException ex) { return Response.status(500).entity("Error occurred while creating zip file").build(); diff --git a/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/pom.xml b/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/pom.xml index 12abd2ff..662bc07d 100644 --- a/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/pom.xml +++ b/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/pom.xml @@ -77,11 +77,6 @@ javax.ws.rs jsr311-api - - - commons-httpclient.wso2 - commons-httpclient - diff --git a/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/sensebot/service/impl/SensebotControllerService.java b/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/sensebot/service/impl/SensebotControllerService.java index 30d90b7b..544aa4b2 100644 --- a/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/sensebot/service/impl/SensebotControllerService.java +++ b/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/sensebot/service/impl/SensebotControllerService.java @@ -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.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; 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 java.util.HashMap; import java.util.Map; -import java.util.concurrent.Future; public class SensebotControllerService { @@ -340,7 +338,7 @@ public class SensebotControllerService { log.debug(urlString); } HttpGet request = new HttpGet(urlString); - Future future = httpclient.execute(request, null); + httpclient.execute(request, null); } } diff --git a/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/sensebot/service/impl/SensebotManagerService.java b/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/sensebot/service/impl/SensebotManagerService.java index 7c465c29..e3591289 100644 --- a/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/sensebot/service/impl/SensebotManagerService.java +++ b/modules/distribution/src/samples/sensebot/src/org.wso2.carbon.device.mgt.iot.sample.sensebot.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/sensebot/service/impl/SensebotManagerService.java @@ -16,7 +16,6 @@ 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.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -41,19 +40,24 @@ public class SensebotManagerService { 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") @PUT public boolean register(@QueryParam("deviceId") String deviceId, @QueryParam("name") String name, @QueryParam("owner") String owner) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(SensebotConstants.DEVICE_TYPE); try { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { - Response.status(HttpStatus.SC_CONFLICT).build(); + response.setStatus(Response.Status.CONFLICT.getStatusCode()); return false; } @@ -71,19 +75,17 @@ public class SensebotManagerService { device.setEnrolmentInfo(enrolmentInfo); boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); if (added) { - Response.status(HttpStatus.SC_OK).build(); - - + response.setStatus(Response.Status.OK.getStatusCode()); } else { - Response.status(HttpStatus.SC_EXPECTATION_FAILED).build(); - - + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return added; } catch (DeviceManagementException e) { - log.error(e.getErrorMessage()); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return false; + } finally { + deviceManagement.endTenantFlow(); } } @@ -92,22 +94,22 @@ public class SensebotManagerService { public void removeDevice(@PathParam("device_id") String deviceId, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(SensebotConstants.DEVICE_TYPE); try { boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); if (removed) { - response.setStatus(HttpStatus.SC_OK); - + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } } 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, @Context HttpServletResponse response) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); @@ -127,10 +129,7 @@ public class SensebotManagerService { try { Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); device.setDeviceIdentifier(deviceId); - - // device.setDeviceTypeId(deviceTypeId); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); - device.setName(name); device.setType(SensebotConstants.DEVICE_TYPE); @@ -138,16 +137,16 @@ public class SensebotManagerService { if (updated) { - response.setStatus(HttpStatus.SC_OK); - + response.setStatus(Response.Status.OK.getStatusCode()); } else { - response.setStatus(HttpStatus.SC_EXPECTATION_FAILED); - + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } return updated; } catch (DeviceManagementException e) { - log.error(e.getErrorMessage()); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return false; + } finally { + deviceManagement.endTenantFlow(); } } @@ -158,7 +157,7 @@ public class SensebotManagerService { @Produces("application/json") public Device getDevice(@PathParam("device_id") String deviceId) { - DeviceManagement deviceManagement = new DeviceManagement(); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(SensebotConstants.DEVICE_TYPE); @@ -167,11 +166,15 @@ public class SensebotManagerService { Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); return device; - } catch (DeviceManagementException ex) { - log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex); + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + log.error("Error occurred while retrieving device with Id " + deviceId + "\n"); return null; + } finally { + deviceManagement.endTenantFlow(); } + } @Path("/device/{sketch_type}/download") @@ -205,7 +208,7 @@ public class SensebotManagerService { ZipUtil ziputil = new ZipUtil(); ZipArchive zipFile = null; try { - zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, + zipFile = ziputil.downloadSketch(owner,SUPER_TENANT, sketchType, deviceId, token,refreshToken); } catch (DeviceManagementException ex) { return Response.status(500).entity("Error occurred while creating zip file").build();