From ed5d611c3516c40729c5ca5b310f192174098f6e Mon Sep 17 00:00:00 2001 From: prabathabey Date: Wed, 29 Jun 2016 18:05:09 +0530 Subject: [PATCH] Code cleanup --- .../service/api/DeviceManagementService.java | 14 ++--- .../api/NotificationManagementService.java | 3 +- .../service/api/PolicyManagementService.java | 10 ++-- .../ApplicationManagementAdminService.java | 4 +- .../admin/DeviceManagementAdminService.java | 2 +- .../api/admin/UserManagementAdminService.java | 2 +- .../impl/DeviceManagementServiceImpl.java | 2 +- .../NotificationManagementServiceImpl.java | 21 +++---- .../impl/PolicyManagementServiceImpl.java | 2 +- .../impl/RoleManagementServiceImpl.java | 2 +- .../impl/UserManagementServiceImpl.java | 2 +- ...ApplicationManagementAdminServiceImpl.java | 33 ++++++----- .../DeviceManagementAdminServiceImpl.java | 19 ++----- .../CredentialManagementResponseBuilder.java | 56 +++++++++---------- 14 files changed, 81 insertions(+), 91 deletions(-) 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 f82a9737b6..29e36efd39 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 @@ -87,7 +87,7 @@ public interface DeviceManagementService { 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 device list.", + message = "Internal Server Error. \n Server error occurred while fetching the device list.", response = ErrorResponse.class) }) @Permission(scope = "device-list", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) @@ -180,7 +180,7 @@ public interface DeviceManagementService { response = ErrorResponse.class), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while retrieving information requested device.", response = ErrorResponse.class) }) @@ -259,7 +259,7 @@ public interface DeviceManagementService { message = "Not Acceptable. \n The requested media type is not supported."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while retrieving feature list of the device.", response = ErrorResponse.class) }) @@ -331,7 +331,7 @@ public interface DeviceManagementService { message = "Unsupported media type. \n The entity of the request was in a not supported format."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while enrolling the device.", response = ErrorResponse.class) }) @@ -405,7 +405,7 @@ public interface DeviceManagementService { message = "Not Acceptable. \n The requested media type is not supported."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while retrieving installed application list of the device.", response = ErrorResponse.class) }) @@ -494,7 +494,7 @@ public interface DeviceManagementService { message = "Not Acceptable. \n The requested media type is not supported."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while retrieving operation list scheduled for the device.", response = ErrorResponse.class) }) @@ -582,7 +582,7 @@ public interface DeviceManagementService { message = "Not Acceptable. \n The requested media type is not supported."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while retrieving the effective policy calculated for the device.", response = ErrorResponse.class) }) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java index 6a21547885..87134f05c0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java @@ -85,7 +85,8 @@ public interface NotificationManagementService { 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 notification list.", + message = "Internal Server Error. " + + "\n Server error occurred while fetching the notification list.", response = ErrorResponse.class) }) @Permission(scope = "device-notification-view", permissions = { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java index 05515fb655..468c14f42f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java @@ -90,7 +90,7 @@ public interface PolicyManagementService { message = "Unsupported media type. \n The entity of the request was in a not supported format."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while adding a new policy.", response = ErrorResponse.class) }) @@ -142,7 +142,7 @@ public interface PolicyManagementService { message = "Not Acceptable.\n The requested media type is not supported"), @ApiResponse( code = 500, - message = ("Internal Server ErrorResponse. \n Server error occurred while fetching " + + message = ("Internal Server Error. \n Server error occurred while fetching " + "policies."), response = ErrorResponse.class) }) @@ -205,7 +205,7 @@ public interface PolicyManagementService { 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 " + + message = "Internal Server Error. \n Server error occurred while fetching the " + "policy.", response = ErrorResponse.class) }) @@ -265,7 +265,7 @@ public interface PolicyManagementService { message = "Unsupported media type. \n The entity of the request was in a not supported format."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while updating the policy.", response = ErrorResponse.class) }) @@ -307,7 +307,7 @@ public interface PolicyManagementService { message = "Unsupported media type. \n The entity of the request was in a not supported format."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while bulk removing policies.", response = ErrorResponse.class) }) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/ApplicationManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/ApplicationManagementAdminService.java index 23b6cba5ab..49f15dccb1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/ApplicationManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/ApplicationManagementAdminService.java @@ -68,7 +68,7 @@ public interface ApplicationManagementAdminService { message = "Unsupported media type. \n The entity of the request was in a not supported format."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while bulk issuing application installation operations upon " + "a given set of devices.", response = ErrorResponse.class) @@ -106,7 +106,7 @@ public interface ApplicationManagementAdminService { message = "Unsupported media type. \n The entity of the request was in a not supported format."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while bulk issuing application un-installation operations upon " + "a given set of devices.", response = ErrorResponse.class) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java index 2396de53f1..89b8d9185b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java @@ -80,7 +80,7 @@ public interface DeviceManagementAdminService { 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 device list.", + message = "Internal Server Error. \n Server error occurred while fetching the device list.", response = ErrorResponse.class) }) Response getDevicesByName( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java index fb8610ce43..66ca21d1fc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java @@ -60,7 +60,7 @@ public interface UserManagementAdminService { message = "Unsupported media type. \n The entity of the request was in a not supported format."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n " + + message = "Internal Server Error. \n " + "Server error occurred while updating credentials of the user.", response = ErrorResponse.class) }) 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 97a6c549ff..edac09a140 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 @@ -265,7 +265,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { RequestValidationUtil.validateDeviceIdentifier(type, id); dms = DeviceMgtAPIUtils.getDeviceManagementService(); - result = dms.getOperations(new DeviceIdentifier(id, type),request); + result = dms.getOperations(new DeviceIdentifier(id, type), request); operationsList.setList((List) result.getData()); operationsList.setCount(result.getRecordsTotal()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java index 8d7a2a72b1..2d1171f7a5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java @@ -22,11 +22,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; -import org.wso2.carbon.device.mgt.jaxrs.NotificationContext; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.beans.NotificationList; import org.wso2.carbon.device.mgt.jaxrs.service.api.NotificationManagementService; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.ws.rs.*; @@ -49,6 +48,7 @@ public class NotificationManagementServiceImpl implements NotificationManagement @QueryParam("offset") int offset, @QueryParam("limit") int limit) { String msg; List notifications; + NotificationList notificationList = new NotificationList(); try { if (status != null) { RequestValidationUtil.validateNotificationStatus(status); @@ -59,17 +59,14 @@ public class NotificationManagementServiceImpl implements NotificationManagement notifications = DeviceMgtAPIUtils.getNotificationManagementService().getAllNotifications(); } - if (notifications == null || notifications.size() == 0) { - throw new NotFoundException( - new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No notification is " + - "available to be retrieved.").build()); - } - return Response.status(Response.Status.OK).entity(notifications).build(); + notificationList.setCount(notifications.size()); + notificationList.setNotifications(notifications); + return Response.status(Response.Status.OK).entity(notificationList).build(); } catch (NotificationManagementException e) { - msg = "Error occurred while retrieving notification info"; + msg = "Error occurred while retrieving notification list"; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index ffd87ad598..455dc0f9ea 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -298,7 +298,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } if (isPolicyDeActivated) { return Response.status(Response.Status.OK).entity("Selected policies have been successfully " + - "deactivated").build(); + "deactivated").build(); } else { return Response.status(Response.Status.NOT_FOUND).entity( new ErrorResponse.ErrorResponseBuilder().setMessage("Selected policies have " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java index 7d5da44c86..4e788411e3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java @@ -209,7 +209,7 @@ public class RoleManagementServiceImpl implements RoleManagementService { //TODO fix what's returned in the entity return Response.created(new URI(API_BASE_PATH + "/" + roleInfo.getRoleName())).entity( "Role '" + roleInfo.getRoleName() + "' has " + - "successfully been added").build(); + "successfully been added").build(); } catch (UserStoreException e) { String msg = "Error occurred while adding role '" + roleInfo.getRoleName() + "'"; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index 3799b68d31..d95f7d6afd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -110,7 +110,7 @@ public class UserManagementServiceImpl implements UserManagementService { } return Response.status(Response.Status.NOT_FOUND).entity( new ErrorResponse.ErrorResponseBuilder().setMessage( - "User doesn't exist.").build()).build(); + "User doesn't exist.").build()).build(); } BasicUserInfo user = this.getBasicUserInfo(username); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java index 3aa0e8f3c9..59f32534c9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java @@ -29,9 +29,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.exception.UnknownApplicationTypeException; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.ApplicationManagementAdminService; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException; 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.device.mgt.jaxrs.util.MDMAndroidOperationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.MDMIOSOperationUtil; @@ -82,21 +80,22 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem applicationWrapper.getDeviceIdentifiers().size() > 0) { activity = appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers()); } else { - throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage( - "No application installation criteria i.e. user/role/device is given").build()); + return Response.status(Response.Status.BAD_REQUEST).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage( + "No application installation criteria i.e. user/role/device is given").build()).build(); } } return Response.status(Response.Status.ACCEPTED).entity(activity).build(); } catch (ApplicationManagementException e) { - String msg = "ErrorResponse occurred while processing application installation request"; + String msg = "Error occurred while processing application installation request"; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } catch (UnknownApplicationTypeException e) { String msg = "The type of application requested to be installed is not supported"; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } } @@ -130,22 +129,22 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem applicationWrapper.getDeviceIdentifiers().size() > 0) { activity = appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers()); } else { - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage( - "No application un-installation criteria i.e. user/role/device is given").build()); + return Response.status(Response.Status.BAD_REQUEST).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage( + "No application un-installation criteria i.e. user/role/device is given").build()).build(); } } return Response.status(Response.Status.ACCEPTED).entity(activity).build(); } catch (ApplicationManagementException e) { - String msg = "ErrorResponse occurred while processing application un-installation request"; + String msg = "Error occurred while processing application un-installation request"; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } catch (UnknownApplicationTypeException e) { String msg = "The type of application requested to be un-installed is not supported"; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java index 589127b211..4956583627 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java @@ -28,8 +28,6 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.admin.DeviceManagementAdminService; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.ws.rs.*; @@ -55,21 +53,16 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe try { int currentTenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); if (MultitenantConstants.SUPER_TENANT_ID != currentTenantId) { - throw new UnauthorizedAccessException( - new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage( - "Current logged in user is not authorized to perform this operation").build()); + return Response.status(Response.Status.UNAUTHORIZED).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage( + "Current logged in user is not authorized to perform this operation").build()).build(); } PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(DeviceMgtAPIUtils.getTenantId(tenantDomain)); List devices = DeviceMgtAPIUtils.getDeviceManagementService(). - getDevicesByNameAndType(name, type, offset, limit); - if (devices == null || devices.size() == 0) { - throw new NotFoundException( - new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No device, which carries" + - " the name '" + name + "', is currently enrolled in the system").build()); - } + getDevicesByNameAndType(name, type, offset, limit); // setting up paginated result DeviceList deviceList = new DeviceList(); @@ -80,8 +73,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (DeviceManagementException e) { String msg = "Error occurred at server side while fetching device list."; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } finally { PrivilegedCarbonContext.endTenantFlow(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/CredentialManagementResponseBuilder.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/CredentialManagementResponseBuilder.java index 6fe5706243..09371c3c5d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/CredentialManagementResponseBuilder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/CredentialManagementResponseBuilder.java @@ -21,11 +21,9 @@ package org.wso2.carbon.device.mgt.jaxrs.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; -import org.wso2.carbon.device.mgt.jaxrs.beans.PasswordResetWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException; +import org.wso2.carbon.device.mgt.jaxrs.beans.PasswordResetWrapper; 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.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreManager; @@ -44,7 +42,8 @@ public class CredentialManagementResponseBuilder { /** * Builds the response to change the password of a user - * @param username - Username of the user. + * + * @param username - Username of the user. * @param credentials - User credentials * @return Response Object */ @@ -52,38 +51,39 @@ public class CredentialManagementResponseBuilder { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (!userStoreManager.isExistingUser(username)) { - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("No user found with the username " - + username).build()); + return Response.status(Response.Status.NOT_FOUND).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage("No user found with the username '" + + username + "'").build()).build(); } RequestValidationUtil.validateCredentials(credentials); if (!validateCredential(credentials.getNewPassword())) { String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration() .getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMsg).build()); + return Response.status(Response.Status.BAD_REQUEST).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(errorMsg).build()).build(); } userStoreManager.updateCredential(username, credentials.getNewPassword(), - credentials.getOldPassword()); + credentials.getOldPassword()); return Response.status(Response.Status.OK).entity("UserImpl password by username: " + username + " was successfully changed.").build(); } catch (UserStoreException e) { log.error(e.getMessage(), e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(e.getMessage()).build()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(e.getMessage()).build()).build(); } catch (UnsupportedEncodingException e) { String msg = "Could not change the password of the user: " + username + ". The Character Encoding is not supported."; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } } /** * Builds the response to reset the password of a user - * @param username - Username of the user. + * + * @param username - Username of the user. * @param credentials - User credentials * @return Response Object */ @@ -91,35 +91,35 @@ public class CredentialManagementResponseBuilder { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (!userStoreManager.isExistingUser(username)) { - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("No user found with the username " - + username).build()); + return Response.status(Response.Status.BAD_REQUEST).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage("No user found with the username " + + username).build()).build(); } if (credentials == null || credentials.getNewPassword() == null) { - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Password cannot be empty." - + username).build()); + return Response.status(Response.Status.BAD_REQUEST).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage("Password cannot be empty." + + username).build()).build(); } if (!validateCredential(credentials.getNewPassword())) { String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration() .getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMsg).build()); + return Response.status(Response.Status.BAD_REQUEST).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(errorMsg).build()).build(); } userStoreManager.updateCredentialByAdmin(username, credentials.getNewPassword()); return Response.status(Response.Status.OK).entity("UserImpl password by username: " + username + " was successfully changed.").build(); } catch (UserStoreException e) { - String msg = "ErrorResponse occurred while updating the credentials of user '" + username + "'"; + String msg = "Error occurred while updating the credentials of user '" + username + "'"; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } catch (UnsupportedEncodingException e) { String msg = "Could not change the password of the user: " + username + ". The Character Encoding is not supported."; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } }