From c45400afd099c9c6704724eb7ee57ce4d2653e10 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Thu, 16 Jun 2016 07:57:40 +0530 Subject: [PATCH] fixing error messages not thrown properly. --- .../exception/BadRequestException.java | 10 ++-- .../android/exception/NotFoundException.java | 33 +++++++++++++ .../DeviceManagementAdminServiceImpl.java | 47 ++++++++++++------- .../impl/EventReceiverServiceImpl.java | 34 +++++++------- .../android/util/AndroidAPIUtils.java | 10 ++-- 5 files changed, 92 insertions(+), 42 deletions(-) create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/NotFoundException.java diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/BadRequestException.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/BadRequestException.java index 53ea4e99e1..741f24b313 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/BadRequestException.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/BadRequestException.java @@ -18,6 +18,7 @@ package org.wso2.carbon.mdm.services.android.exception; +import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; import org.wso2.carbon.mdm.services.android.util.Message; import javax.ws.rs.WebApplicationException; @@ -29,13 +30,8 @@ import javax.ws.rs.core.Response; */ public class BadRequestException extends WebApplicationException { - public BadRequestException(Message message, MediaType mediaType) { - super(Response.status(Response.Status.BAD_REQUEST).entity(message). - type(mediaType).build()); + public BadRequestException(ErrorResponse error) { + super(Response.status(Response.Status.BAD_REQUEST).entity(error).build()); } - public BadRequestException(Message message) { - super(Response.status(Response.Status.BAD_REQUEST).entity(message).build()); - } - } \ No newline at end of file diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/NotFoundException.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/NotFoundException.java new file mode 100644 index 0000000000..bda2219bf5 --- /dev/null +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/NotFoundException.java @@ -0,0 +1,33 @@ +/* + * 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.mdm.services.android.exception; + +import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +public class NotFoundException extends WebApplicationException { + + private static final long serialVersionUID = 147943572342342340L; + + public NotFoundException(ErrorResponse error) { + super(Response.status(Response.Status.NOT_FOUND).entity(error).build()); + } + +} diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementAdminServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementAdminServiceImpl.java index bd09d30cc5..468882aa9a 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementAdminServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementAdminServiceImpl.java @@ -27,6 +27,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.mdm.services.android.bean.*; import org.wso2.carbon.mdm.services.android.bean.wrapper.*; +import org.wso2.carbon.mdm.services.android.exception.BadRequestException; import org.wso2.carbon.mdm.services.android.exception.UnexpectedServerErrorException; import org.wso2.carbon.mdm.services.android.services.DeviceManagementAdminService; import org.wso2.carbon.mdm.services.android.util.AndroidAPIUtils; @@ -59,7 +60,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (deviceLockBeanWrapper == null || deviceLockBeanWrapper.getOperation() == null) { String errorMessage = "Lock bean is empty."; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } DeviceLock lock = deviceLockBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -172,7 +174,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (cameraBeanWrapper == null || cameraBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the configure camera operation is incorrect."; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } Camera camera = cameraBeanWrapper.getOperation(); CommandOperation operation = new CommandOperation(); @@ -256,7 +259,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (wipeDataBeanWrapper == null || wipeDataBeanWrapper.getOperation() == null) { String errorMessage = "WipeData bean is empty."; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } WipeData wipeData = wipeDataBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -395,7 +399,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe null) { String errorMessage = "The payload of the application installing operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } ApplicationInstallation applicationInstallation = applicationInstallationBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -429,7 +434,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (applicationUpdateBeanWrapper == null || applicationUpdateBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the application update operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } ApplicationUpdate applicationUpdate = applicationUpdateBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -465,7 +471,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe applicationUninstallationBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the application uninstalling operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } ApplicationUninstallation applicationUninstallation = applicationUninstallationBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -500,14 +507,14 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (blacklistApplicationsBeanWrapper == null || blacklistApplicationsBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the blacklisting apps operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } BlacklistApplications blacklistApplications = blacklistApplicationsBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); operation.setCode(AndroidConstants.OperationCodes.BLACKLIST_APPLICATIONS); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(blacklistApplications.toJSON()); - return AndroidAPIUtils.getOperationResponse(blacklistApplicationsBeanWrapper.getDeviceIDs(), operation); @@ -536,7 +543,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (upgradeFirmwareBeanWrapper == null || upgradeFirmwareBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the upgrade firmware operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } UpgradeFirmware upgradeFirmware = upgradeFirmwareBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -569,7 +577,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (vpnBeanWrapper == null || vpnBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the VPN operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } Vpn vpn = vpnBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -603,7 +612,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (notificationBeanWrapper == null || notificationBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the notification operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } Notification notification = notificationBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -638,7 +648,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (wifiBeanWrapper == null || wifiBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the wifi operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } Wifi wifi = wifiBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -674,7 +685,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (encryptionBeanWrapper == null || encryptionBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the device encryption operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } DeviceEncryption deviceEncryption = encryptionBeanWrapper.getOperation(); CommandOperation operation = new CommandOperation(); @@ -709,7 +721,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (lockCodeBeanWrapper == null || lockCodeBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the change lock code operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } LockCode lockCode = lockCodeBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -744,7 +757,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (passwordPolicyBeanWrapper == null || passwordPolicyBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the change password policy operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } PasscodePolicy passcodePolicy = passwordPolicyBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); @@ -782,7 +796,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe if (webClipBeanWrapper == null || webClipBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the add webclip operation is incorrect"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } WebClip webClip = webClipBeanWrapper.getOperation(); ProfileOperation operation = new ProfileOperation(); diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/EventReceiverServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/EventReceiverServiceImpl.java index d80b3606c5..cdbcab310f 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/EventReceiverServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/EventReceiverServiceImpl.java @@ -25,6 +25,8 @@ import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublish import org.wso2.carbon.mdm.services.android.bean.DeviceState; import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; import org.wso2.carbon.mdm.services.android.bean.wrapper.EventBeanWrapper; +import org.wso2.carbon.mdm.services.android.exception.BadRequestException; +import org.wso2.carbon.mdm.services.android.exception.NotFoundException; import org.wso2.carbon.mdm.services.android.exception.UnexpectedServerErrorException; import org.wso2.carbon.mdm.services.android.services.EventReceiverService; import org.wso2.carbon.mdm.services.android.util.AndroidAPIUtils; @@ -57,9 +59,9 @@ public class EventReceiverServiceImpl implements EventReceiverService { message.setResponseCode("Event is published successfully."); return Response.status(Response.Status.CREATED).entity(message).build(); } else { - message.setResponseCode("Error occurred while publishing the event."); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR). - entity(message).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage("Error occurred while " + + "publishing the event.").build()); } } catch (DataPublisherConfigurationException e) { String msg = "Error occurred while getting the Data publisher Service instance."; @@ -82,8 +84,10 @@ public class EventReceiverServiceImpl implements EventReceiverService { } else if (deviceId != null) { return retrieveAlert(deviceId); } else { - return Response.status(Response.Status.BAD_REQUEST).entity("Request must contain the device identifier. " + - "Optionally, both from and to value should be present to get alerts between times.").build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request must contain " + + "the device identifier. Optionally, both from and to value should be present to get " + + "alerts between times.").build()); } } @@ -91,14 +95,14 @@ public class EventReceiverServiceImpl implements EventReceiverService { if (log.isDebugEnabled()) { log.debug("Retrieving events for given device Identifier."); } - Message message = new Message(); String query = "deviceIdentifier:" + deviceId; List deviceStates; try { deviceStates = AndroidAPIUtils.getAllEventsForDevice(EVENT_STREAM_DEFINITION, query); if (deviceStates == null) { - message.setResponseCode("No any alerts are published for Device: " + deviceId + "."); - return Response.status(Response.Status.NOT_FOUND).entity(message).build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No any alerts are " + + "published for Device: " + deviceId + ".").build()); } else { return Response.status(Response.Status.OK).entity(deviceStates).build(); } @@ -116,16 +120,15 @@ public class EventReceiverServiceImpl implements EventReceiverService { if (log.isDebugEnabled()) { log.debug("Retrieving events for given device Identifier and time period."); } - Message message = new Message(); String query = "deviceIdentifier:" + deviceId + " AND _timestamp: [" + fromDate + " TO " + toDate + "]"; List deviceStates; try { deviceStates = AndroidAPIUtils.getAllEventsForDevice(EVENT_STREAM_DEFINITION, query); if (deviceStates == null) { - message. - setResponseCode("No any alerts are published on given date for given Device: " + deviceId + "."); - return Response.status(Response.Status.NOT_FOUND).entity(message).build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No any alerts are " + + "published on given date for given Device: " + deviceId + ".").build()); } else { return Response.status(Response.Status.OK).entity(deviceStates).build(); @@ -143,15 +146,14 @@ public class EventReceiverServiceImpl implements EventReceiverService { if (log.isDebugEnabled()) { log.debug("Retrieving events for given device identifier and type."); } - Message message = new Message(); String query = "deviceIdentifier:" + deviceId + " AND type:" + type; List deviceStates; try { deviceStates = AndroidAPIUtils.getAllEventsForDevice(EVENT_STREAM_DEFINITION, query); if (deviceStates == null) { - message.setResponseCode("No any alerts are published for given Device: " + - "" + deviceId + "and given specific Type."); - return Response.status(Response.Status.NOT_FOUND).entity(message).build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No any alerts are " + + "published for given Device: '" + deviceId + "' and given specific Type.").build()); } else { return Response.status(Response.Status.OK).entity(deviceStates).build(); diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java index 3635bae738..c69d8973f5 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java @@ -48,6 +48,8 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManag import org.wso2.carbon.device.mgt.core.search.mgt.impl.Utils; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.mdm.services.android.bean.DeviceState; +import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; +import org.wso2.carbon.mdm.services.android.exception.BadRequestException; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; @@ -120,7 +122,8 @@ public class AndroidAPIUtils { if (deviceIDs == null || deviceIDs.size() == 0) { String errorMessage = "Device identifier list is empty"; log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } AndroidDeviceUtils deviceUtils = new AndroidDeviceUtils(); DeviceIDHolder deviceIDHolder = deviceUtils.validateDeviceIdentifiers(deviceIDs); @@ -140,8 +143,9 @@ public class AndroidAPIUtils { // } // } if (!deviceIDHolder.getErrorDeviceIdList().isEmpty()) { - return javax.ws.rs.core.Response.status(Response.Status.BAD_REQUEST).entity(deviceUtils. - convertErrorMapIntoErrorMessage(deviceIDHolder.getErrorDeviceIdList())).build(); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(deviceUtils. + convertErrorMapIntoErrorMessage(deviceIDHolder.getErrorDeviceIdList())).build()); } return Response.status(Response.Status.CREATED).entity(activity).build(); }