diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ConfigurationManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ConfigurationManagementService.java index 3dae98bf7f0..ab067e0e197 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ConfigurationManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ConfigurationManagementService.java @@ -22,6 +22,7 @@ import io.swagger.annotations.*; import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @@ -45,7 +46,6 @@ public interface ConfigurationManagementService { httpMethod = "GET", value = "Get the general platform configurations.", notes = "Get the general platform level configuration details.", - response = PlatformConfiguration.class, tags = "Configuration Management") @ApiResponses(value = { @ApiResponse( @@ -74,7 +74,9 @@ public interface ConfigurationManagementService { message = "Not Acceptable.\n The requested media type is not supported"), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n Server error occurred while fetching the general platform configuration.") + message = "Internal Server ErrorResponse. \n Server error occurred while fetching the general " + + "platform configuration.", + response = ErrorResponse.class) }) @Permission(scope = "configuration-view", permissions = {"/permission/admin/device-mgt/admin/platform-configs/view"}) @@ -115,16 +117,14 @@ public interface ConfigurationManagementService { @ApiResponse( code = 400, message = "Bad Request. \n Invalid request or validation error."), - @ApiResponse( - code = 404, - message = "Not Found. \n Resource to be deleted does not exist."), @ApiResponse( code = 415, message = "Unsupported media type. \n The entity of the request was in a not supported format."), @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while modifying general platform configuration.") + "Server error occurred while modifying general platform configuration.", + response = ErrorResponse.class) }) @Permission(scope = "configuration-modify", permissions = {"/permission/admin/device-mgt/admin/platform-configs/modify"}) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 7e12f306e7e..46708a16e73 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -30,12 +30,12 @@ import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.search.SearchContext; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.policy.mgt.common.Policy; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.Date; import java.util.List; /** @@ -138,7 +138,6 @@ public interface DeviceManagementService { value = "Retrieve devices information from the supplied device identifier.", notes = "This will return device information such as CPU usage, memory usage etc for supplied device " + "identifier.", - response = DeviceInfo.class, tags = "Device Management") @ApiResponses( value = { @@ -172,13 +171,17 @@ public interface DeviceManagementService { @ApiResponse( code = 400, message = "Bad Request. \n Invalid request or validation error."), + @ApiResponse( + code = 404, + message = "Not Found. \n No device is found under the provided type and id."), @ApiResponse( code = 406, message = "Not Acceptable. \n The requested media type is not supported."), @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while retrieving information of the list of the devices submitted.") + "Server error occurred while retrieving information of the list of the devices submitted.", + response = ErrorResponse.class) }) @Permission(scope = "device-info", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) Response getDeviceInfo( @@ -270,7 +273,6 @@ public interface DeviceManagementService { httpMethod = "GET", value = "Get information of the requested device.", notes = "Returns information of the requested device.", - response = Device.class, tags = "Device Management") @ApiResponses( value = { @@ -301,7 +303,8 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while retrieving information requested device.") + "Server error occurred while retrieving information requested device.", + response = ErrorResponse.class) }) @Permission(scope = "device-view", permissions = { "/permission/admin/device-mgt/admin/devices/view", @@ -332,7 +335,6 @@ public interface DeviceManagementService { value = "Get the device location of a given device and a device type.", notes = "This will return the device location including latitude and longitude as well the " + "physical address.", - response = DeviceLocation.class, tags = "Device Management") @ApiResponses( value = { @@ -346,10 +348,11 @@ public interface DeviceManagementService { "Empty body because the client already has the latest version of the requested resource."), @ApiResponse( code = 404, - message = "Location details are not available for the given device."), + message = "Not Found. \n No device is found under the provided type and id."), @ApiResponse( code = 500, - message = "ErrorResponse occurred while getting the device location.") + message = "ErrorResponse occurred while getting the device location.", + response = ErrorResponse.class) }) @Permission(scope = "device-info", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) Response getDeviceLocation( @@ -379,8 +382,6 @@ public interface DeviceManagementService { value = "Get the device location of a given devices and a device type.", notes = "This will return the device locations including latitude and longitude as well the " + "physical address of the given devices.", - response = DeviceLocation.class, - responseContainer = "List", tags = "Device Management") @ApiResponses( value = { @@ -398,7 +399,8 @@ public interface DeviceManagementService { message = "Location details are not available for the given devices."), @ApiResponse( code = 500, - message = "ErrorResponse occurred while getting the device location.") + message = "ErrorResponse occurred while getting the device location.", + response = ErrorResponse.class) }) @Permission(scope = "device-info", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) Response getDeviceLocations( @@ -422,8 +424,6 @@ public interface DeviceManagementService { notes = "WSO2 EMM features enable you to carry out many operations on a given device platform. " + "Using this REST API you can get the features that can be carried out on a preferred device type," + " such as iOS, Android or Windows.", - response = Feature.class, - responseContainer = "List", tags = "Device Management") @ApiResponses( value = { @@ -458,7 +458,8 @@ public interface DeviceManagementService { "Empty body because the client already has the latest version of the requested resource."), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid request or validation error."), + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), @ApiResponse( code = 404, message = "Not Found. \n Device of which the feature list is requested, is not found."), @@ -468,7 +469,8 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while retrieving feature list of the device.") + "Server error occurred while retrieving feature list of the device.", + response = ErrorResponse.class) }) @Permission(scope = "device-search", permissions = {"/permission/admin/device-mgt/admin/devices/view", "/permission/admin/device-mgt/user/devices/view"}) @@ -497,8 +499,6 @@ public interface DeviceManagementService { httpMethod = "POST", value = "Advanced search for devices.", notes = "Carry out an advanced search of devices.", - response = DeviceWrapper.class, - responseContainer = "List", tags = "Device Management") @ApiResponses( value = { @@ -526,7 +526,8 @@ public interface DeviceManagementService { "Empty body because the client already has the latest version of the requested resource."), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid request or validation error."), + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), @ApiResponse( code = 406, message = "Not Acceptable.\n The requested media type is not supported"), @@ -536,7 +537,8 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while enrolling the device.") + "Server error occurred while enrolling the device.", + response = ErrorResponse.class) }) @Permission(scope = "device-search", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) Response searchDevices( @@ -562,9 +564,7 @@ public interface DeviceManagementService { produces = MediaType.APPLICATION_JSON, httpMethod = "GET", value = "Getting installed application details of a device.", - responseContainer = "List", notes = "Get the list of applications that a device has subscribed.", - response = Application.class, tags = "Device Management") @ApiResponses( value = { @@ -599,7 +599,8 @@ public interface DeviceManagementService { "Empty body because the client already has the latest version of the requested resource."), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid request or validation error."), + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), @ApiResponse( code = 404, message = "Not Found. \n Device of which the application list is requested, is not found."), @@ -609,7 +610,8 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while retrieving installed application list of the device.") + "Server error occurred while retrieving installed application list of the device.", + response = ErrorResponse.class) }) @Permission(scope = "operation-view", permissions = { "/permission/admin/device-mgt/admin/devices/view", @@ -651,8 +653,6 @@ public interface DeviceManagementService { notes = "You will carry out many operations on a device. In a situation where you wish to view the all" + " the operations carried out on a device it is not feasible to show all the details on one page" + " therefore the details are paginated.", - response = Operation.class, - responseContainer = "List", tags = "Device Management") @ApiResponses( value = { @@ -687,7 +687,8 @@ public interface DeviceManagementService { "Empty body because the client already has the latest version of the requested resource."), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid request or validation error."), + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), @ApiResponse( code = 404, message = "Not Found. \n Device of which the operation list is requested, is not found."), @@ -697,7 +698,8 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while retrieving operation list scheduled for the device.") + "Server error occurred while retrieving operation list scheduled for the device.", + response = ErrorResponse.class) }) @Permission(scope = "operation-view", permissions = { "/permission/admin/device-mgt/admin/devices/view", @@ -739,7 +741,6 @@ public interface DeviceManagementService { notes = "When a device registers with WSO2 EMM a policy is enforced on the device. Initially the EMM " + "filters the policies based on the Platform (device type), filters based on the device ownership" + " type , filters based on the user role or name and finally the policy is enforced on the device.", - response = Policy.class, tags = "Device Management") @ApiResponses( value = { @@ -773,7 +774,8 @@ public interface DeviceManagementService { "Empty body because the client already has the latest version of the requested resource."), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid request or validation error."), + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), @ApiResponse( code = 404, message = "Not Found. \n Device of which the effective policy is requested, is not found."), @@ -783,7 +785,8 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while retrieving the effective policy calculated for the device.") + "Server error occurred while retrieving the effective policy calculated for the device.", + response = ErrorResponse.class) }) Response getEffectivePolicyOfDevice( @ApiParam( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ConfigurationServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ConfigurationServiceImpl.java index 177f87c9d08..4a2126f6aae 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ConfigurationServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ConfigurationServiceImpl.java @@ -23,7 +23,9 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.ConfigurationManagementService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.MDMAppConstants; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; @@ -63,7 +65,8 @@ public class ConfigurationServiceImpl implements ConfigurationManagementService } catch (ConfigurationManagementException | PolicyManagementException e) { msg = "ErrorResponse occurred while retrieving the configurations."; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -80,7 +83,8 @@ public class ConfigurationServiceImpl implements ConfigurationManagementService } catch (ConfigurationManagementException e) { String msg = "ErrorResponse occurred while updating the configuration."; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 81f5bedb3cb..75e886cb92d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -36,8 +36,10 @@ import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; @@ -98,7 +100,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } catch (DeviceManagementException e) { String msg = "Error occurred while fetching all enrolled devices"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -116,10 +119,15 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { deviceIdentifier.setType(type); informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); deviceInfo = informationManager.getDeviceInfo(deviceIdentifier); + if (deviceInfo == null) { + return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is " + + "found upon the give type '" + type + "' and id '" + id + "'").build(); + } } catch (DeviceDetailsMgtException e) { String msg = "Error occurred while getting the device information."; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(deviceInfo).build(); } @@ -162,7 +170,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } catch (DeviceManagementException e) { String msg = "Error occurred while fetching the device information."; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } if (device == null) { return Response.status(Response.Status.NOT_FOUND).entity("Requested device of type '" + type + @@ -194,7 +203,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { String msg = "Error occurred while getting the last updated location of the '" + type + "' device, " + "which carries the id '" + id + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(deviceLocation).build(); } @@ -211,7 +221,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } catch (DeviceDetailsMgtException e) { String msg = "Error occurred while getting the device location."; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(deviceLocations).build(); } @@ -229,16 +240,18 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { RequestValidationUtil.validateDeviceIdentifier(type, id); dms = DeviceMgtAPIUtils.getDeviceManagementService(); - features = dms.getFeatureManager(type).getFeatures(); + FeatureManager fm = dms.getFeatureManager(type); + if (fm == null) { + return Response.status(Response.Status.NOT_FOUND).entity("No feature manager is registered with " + + "the given type '" + type + "'").build(); + } + features = fm.getFeatures(); } catch (DeviceManagementException e) { String msg = "Error occurred while retrieving the list of features of '" + type + "' device, which " + "carries the id '" + id + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - if (features == null) { - return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + - "the provided type and id").build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(features).build(); } @@ -256,7 +269,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } catch (SearchMgtException e) { String msg = "Error occurred while searching for devices that matches the provided selection criteria"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } if (devices == null) { return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + @@ -283,13 +297,14 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { applications = amc.getApplicationListForDevice(new DeviceIdentifier(id, type)); if (applications == null) { return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon" + - "the provided type and id").build(); + " the provided type and id").build(); } } catch (ApplicationManagementException e) { String msg = "Error occurred while fetching the apps of the '" + type + "' device, which carries " + "the id '" + id + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(applications).build(); } @@ -318,7 +333,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { String msg = "Error occurred while fetching the operations for the '" + type + "' device, which " + "carries the id '" + id + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(operations).build(); } @@ -343,7 +359,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { String msg = "Error occurred while retrieving the current policy associated with the '" + type + "' device, which carries the id '" + id + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index 53ec73b8418..dfb8bc19dfa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -71,7 +71,7 @@ public class DeviceMgtAPIUtils { "frequency.").build() ); } - return Integer.parseInt(entry.getValue().toString()); + return (int) (Double.parseDouble(entry.getValue().toString()) + 0.5d); } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/IllegalTransactionStateException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/IllegalTransactionStateException.java index 3d007fbf77e..1cc0594ba7d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/IllegalTransactionStateException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/IllegalTransactionStateException.java @@ -42,5 +42,4 @@ public class IllegalTransactionStateException extends RuntimeException { super(cause); } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/UnAuthorizedDeviceAccessException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/UnAuthorizedDeviceAccessException.java new file mode 100644 index 00000000000..7a1846001cc --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/UnAuthorizedDeviceAccessException.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.common; + +public class UnauthorizedDeviceAccessException extends RuntimeException { + + private static final long serialVersionUID = -3151289331929070297L; + + public UnauthorizedDeviceAccessException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public UnauthorizedDeviceAccessException(String message, Throwable cause) { + super(message, cause); + } + + public UnauthorizedDeviceAccessException(String msg) { + super(msg); + } + + public UnauthorizedDeviceAccessException() { + super(); + } + + public UnauthorizedDeviceAccessException(Throwable cause) { + super(cause); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index 61eb2669fee..a33df19a415 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -264,6 +264,13 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManagementDAOFactory.openConnection(); device = deviceDAO.getDevice(deviceId, tenantId); + if (device == null) { + if (log.isDebugEnabled()) { + log.debug("No device is found upon the device identifier '" + deviceId.getId() + + "' and type '" + deviceId.getType() + "'. Therefore returning null"); + } + return null; + } return applicationDAO.getInstalledApplications(device.getId()); } catch (DeviceManagementDAOException e) { throw new ApplicationManagementException("Error occurred while fetching the Application List of '" + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java index 3b392cc8200..c951f13944e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java @@ -112,20 +112,20 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { @Override public DeviceInfo getDeviceInformation(int deviceId) throws DeviceDetailsMgtDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - DeviceInfo deviceInfo = new DeviceInfo(); + DeviceInfo deviceInfo = null; try { conn = this.getConnection(); - String sql = "SELECT * FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?"; + String sql = "SELECT * FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); rs = stmt.executeQuery(); - while (rs.next()) { + if (rs.next()) { + deviceInfo = new DeviceInfo(); deviceInfo.setDeviceId(rs.getInt("DEVICE_ID")); // deviceInfo.setIMEI(rs.getString("IMEI")); // deviceInfo.setIMSI(rs.getString("IMSI")); @@ -148,7 +148,6 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { deviceInfo.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); } - deviceInfo.setDeviceId(deviceId); return deviceInfo; } catch (SQLException e) { throw new DeviceDetailsMgtDAOException("Error occurred while fetching the details of the registered devices.", e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index e1033cc3ee0..0532f21ddaa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -19,6 +19,8 @@ package org.wso2.carbon.device.mgt.core.device.details.mgt.impl; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; @@ -38,6 +40,7 @@ import java.util.Map; public class DeviceInformationManagerImpl implements DeviceInformationManager { private DeviceDetailsDAO deviceDetailsDAO; + private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class); public DeviceInformationManagerImpl() { this.deviceDetailsDAO = DeviceManagementDAOFactory.getDeviceDetailsDAO(); @@ -72,13 +75,22 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } @Override - public DeviceInfo getDeviceInfo( - DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException { - + public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { + Device device; + try { + device = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider().getDevice(deviceId); + if (device == null) { + if (log.isDebugEnabled()) { + log.debug("No device is found upon the device identifier '" + deviceId.getId() + + "' and type '" + deviceId.getType() + "'. Therefore returning null"); + } + return null; + } + } catch (DeviceManagementException e) { + throw new DeviceDetailsMgtException("Exception occurred while retrieving the device.", e); + } try { - Device device = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider().getDevice(deviceIdentifier); - DeviceManagementDAOFactory.openConnection(); DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId()); deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(device.getId())); @@ -86,8 +98,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } catch (SQLException e) { throw new DeviceDetailsMgtException("SQL error occurred while retrieving device from database.", e); - } catch (DeviceManagementException e) { - throw new DeviceDetailsMgtException("Exception occurred while retrieving the device.", e); } catch (DeviceDetailsMgtDAOException e) { throw new DeviceDetailsMgtException("Exception occurred while retrieving device details.", e); } finally { @@ -114,7 +124,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } DeviceManagementDAOFactory.openConnection(); - for(Integer id : deviceIds) { + for (Integer id : deviceIds) { DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(id); deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(id)); deviceInfos.add(deviceInfo); @@ -157,18 +167,25 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } @Override - public DeviceLocation getDeviceLocation(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException { + public DeviceLocation getDeviceLocation(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { + Device device; + try { + device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId); + if (device == null) { + if (log.isDebugEnabled()) { + log.debug("No device is found upon the device identifier '" + deviceId.getId() + + "' and type '" + deviceId.getType() + "'. Therefore returning null"); + } + return null; + } + } catch (DeviceManagementException e) { + throw new DeviceDetailsMgtException("Exception occurred while retrieving the device.", e); + } try { - Device device = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider().getDevice(deviceIdentifier); - DeviceManagementDAOFactory.openConnection(); return deviceDetailsDAO.getDeviceLocation(device.getId()); - } catch (SQLException e) { throw new DeviceDetailsMgtException("SQL error occurred while retrieving device from database.", e); - } catch (DeviceManagementException e) { - throw new DeviceDetailsMgtException("Exception occurred while retrieving the device.", e); } catch (DeviceDetailsMgtDAOException e) { throw new DeviceDetailsMgtException("Exception occurred while retrieving device location.", e); } finally { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index c4bb97bc229..3d60f9f78a7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -185,49 +185,49 @@ public class OperationManagerImpl implements OperationManager { } @Override - public List getOperations( - DeviceIdentifier deviceId) throws OperationManagementException { + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { int enrolmentId; - List operations = new ArrayList<>(); + List operations = null; try { boolean isUserAuthorized = DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(). isUserAuthorized(deviceId, DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS); - if (isUserAuthorized) { + if (!isUserAuthorized) { + throw new UnauthorizedDeviceAccessException("User '" + getUser() + "' is not authorized to " + + "fetch operations on device '" + deviceId.getId() + "'"); + } + try { try { - try { - DeviceManagementDAOFactory.openConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - if (enrolmentId < 0) { - return null; - } - OperationManagementDAOFactory.openConnection(); - List operationList = - operationDAO.getOperationsForDevice(enrolmentId); - - for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { - Operation operation = OperationDAOUtil.convertOperation(dtoOperation); - operations.add(operation); - } - } catch (OperationManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); - } catch (DeviceManagementDAOException e) { - throw new OperationManagementException("Error occurred while retrieving metadata of '" + - deviceId.getType() + "' device carrying the identifier '" + - deviceId.getId() + "'"); - } catch (SQLException e) { - throw new OperationManagementException( - "Error occurred while opening a connection to the data source", e); + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); } finally { - OperationManagementDAOFactory.closeConnection(); + DeviceManagementDAOFactory.closeConnection(); } - } else { - log.info("User : " + getUser() + " is not authorized to fetch operations on device : " + deviceId.getId()); + if (enrolmentId < 0) { + return null; + } + OperationManagementDAOFactory.openConnection(); + List operationList = + operationDAO.getOperationsForDevice(enrolmentId); + + operations = new ArrayList<>(); + for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { + Operation operation = OperationDAOUtil.convertOperation(dtoOperation); + operations.add(operation); + } + } catch (OperationManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the list of " + + "operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); + } catch (DeviceManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving metadata of '" + + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "'"); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + OperationManagementDAOFactory.closeConnection(); } } catch (DeviceAccessAuthorizationException e) { throw new OperationManagementException("Error occurred while authorizing access to the devices for user : " + @@ -757,7 +757,7 @@ public class OperationManagerImpl implements OperationManager { // This parses the operation id from activity id (ex : ACTIVITY_23) and converts to the integer. int operationId = Integer.parseInt( activity.replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, "")); - if (operationId == 0){ + if (operationId == 0) { throw new IllegalArgumentException("Operation ID cannot be null or zero (0)."); } try { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 45258a58510..778de88dad0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -607,7 +607,7 @@ public class PolicyManagerImpl implements PolicyManager { policy.setPolicyCriterias(policyDAO.getPolicyCriteria(policy.getId())); List deviceGroupWrappers = policyDAO.getDeviceGroupsOfPolicy(policy.getId()); - if(!deviceGroupWrappers.isEmpty()){ + if (!deviceGroupWrappers.isEmpty()) { deviceGroupWrappers = this.getDeviceGroupNames(deviceGroupWrappers); } policy.setDeviceGroups(deviceGroupWrappers); @@ -977,17 +977,26 @@ public class PolicyManagerImpl implements PolicyManager { } @Override - public Policy getAppliedPolicyToDevice( - DeviceIdentifier deviceIdentifier) throws PolicyManagementException { + public Policy getAppliedPolicyToDevice(DeviceIdentifier deviceId) throws PolicyManagementException { Policy policy; + DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); + Device device; + try { + device = service.getDevice(deviceId); + if (device == null) { + if (log.isDebugEnabled()) { + log.debug("No device is found upon the device identifier '" + deviceId.getId() + + "' and type '" + deviceId.getType() + "'. Therefore returning null"); + } + return null; + } + } catch (DeviceManagementException e) { + throw new PolicyManagementException("Error occurred while getting device id.", e); + } try { - DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); - Device device = service.getDevice(deviceIdentifier); //int policyId = policyDAO.getAppliedPolicyId(device.getId()); PolicyManagementDAOFactory.openConnection(); policy = policyDAO.getAppliedPolicy(device.getId(), device.getEnrolmentInfo().getId()); - } catch (DeviceManagementException e) { - throw new PolicyManagementException("Error occurred while getting device id.", e); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting policy id or policy.", e); } catch (SQLException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index b233ed85528..c05700009d0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -212,7 +212,7 @@ public class PolicyManagerUtil { throw new PolicyManagementException("Invalid value, i.e. '" + cEntry.getValue() + "', is configured as the monitoring frequency"); } - monitoringFrequency = Integer.parseInt(cEntry.getValue().toString()); + monitoringFrequency = (int) (Double.parseDouble(cEntry.getValue().toString()) + 0.5d); } } }