From 0fdff580170e6742d58e49fedd200f51d4bcf81a Mon Sep 17 00:00:00 2001 From: shamalka Date: Sat, 7 Dec 2019 00:19:21 +0530 Subject: [PATCH] Remove Response references from Android Service --- .../api/impl/DeviceManagementAPIImpl.java | 4 +- .../impl/DeviceManagementAdminAPIImpl.java | 17 +++++- .../api/impl/EventReceiverAPIImpl.java | 4 +- .../pom.xml | 4 ++ .../android/common/spi/AndroidService.java | 9 ++-- .../pom.xml | 2 + .../android/core/impl/AndroidServiceImpl.java | 52 ++++++++----------- 7 files changed, 54 insertions(+), 38 deletions(-) diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java index a6a8e00468..79db4baacc 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java @@ -146,7 +146,9 @@ public class DeviceManagementAPIImpl implements DeviceManagementAPI { } try { AndroidService androidService = AndroidAPIUtils.getAndroidService(); - return androidService.enrollDevice(androidDevice); + Message message = androidService.enrollDevice(androidDevice); + return Response.status(Integer.parseInt(message.getResponseCode())) + .entity(message.getResponseMessage()).build(); } catch (DeviceManagementException e) { String msg = "Error occurred while enrolling the android, which carries the id '" + androidDevice.getDeviceIdentifier() + "'"; diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAdminAPIImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAdminAPIImpl.java index 756c6d24e5..7791ee3449 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAdminAPIImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAdminAPIImpl.java @@ -89,6 +89,7 @@ import org.wso2.carbon.device.mgt.mobile.android.common.bean.wrapper.WifiBeanWra import org.wso2.carbon.device.mgt.mobile.android.common.bean.wrapper.WipeDataBeanWrapper; import org.wso2.carbon.device.mgt.mobile.android.common.exception.AndroidDeviceMgtPluginException; import org.wso2.carbon.device.mgt.mobile.android.common.exception.BadRequestExceptionDup; +import org.wso2.carbon.device.mgt.mobile.android.common.exception.UnexpectedServerErrorExceptionDup; import org.wso2.carbon.device.mgt.mobile.android.common.spi.AndroidService; import org.wso2.carbon.device.mgt.mobile.android.core.util.AndroidAPIUtils; import org.wso2.carbon.device.mgt.mobile.android.core.util.AndroidDeviceUtils; @@ -1047,8 +1048,22 @@ public class DeviceManagementAdminAPIImpl implements DeviceManagementAdminAPI { try{ AndroidService androidService = AndroidAPIUtils.getAndroidService(); - Response response = androidService.sendApplicationConfiguration(applicationRestrictionBeanWrapper); + ProfileOperation operation = androidService.sendApplicationConfiguration(applicationRestrictionBeanWrapper); + Response response = AndroidAPIUtils.getOperationResponse(applicationRestrictionBeanWrapper.getDeviceIDs(), + operation); return Response.status(Response.Status.CREATED).entity(response).build(); + } catch (InvalidDeviceException e) { + String errorMessage = "Invalid Device Identifiers found."; + log.error(errorMessage, e); + return Response.status(Response.Status.BAD_REQUEST).entity( + new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_BAD_REQUEST) + .setMessage(errorMessage).build()).build(); + } catch (OperationManagementException e) { + String errorMessage = "Issue in retrieving operation management service instance"; + log.error(errorMessage, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( + new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + .setMessage(errorMessage).build()).build(); } catch (BadRequestExceptionDup e){ String msg = "Invalid request"; log.error(msg, e); diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/EventReceiverAPIImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/EventReceiverAPIImpl.java index 172a028091..0495c547ba 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/EventReceiverAPIImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/EventReceiverAPIImpl.java @@ -137,8 +137,8 @@ public class EventReceiverAPIImpl implements EventReceiverAPI { @HeaderParam("If-Modified-Since") String ifModifiedSince) { try{ AndroidService androidService = AndroidAPIUtils.getAndroidService(); - Response response = androidService.retrieveAlerts(deviceId, from, to, type, ifModifiedSince); - return response; + List deviceStates = androidService.retrieveAlerts(deviceId, from, to, type, ifModifiedSince); + return Response.status(Response.Status.OK).entity(deviceStates).build(); } catch (BadRequestExceptionDup e){ String msg = "Invalid request"; log.error(msg, e); diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/pom.xml b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/pom.xml index 2f9db1994b..ef36c1b9d9 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/pom.xml +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/pom.xml @@ -117,5 +117,9 @@ org.codehaus.jackson jackson-core-asl + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/spi/AndroidService.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/spi/AndroidService.java index 18d16ce318..fac389e18f 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/spi/AndroidService.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/spi/AndroidService.java @@ -28,9 +28,10 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; - +import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.mobile.android.common.Message; import org.wso2.carbon.device.mgt.mobile.android.common.bean.AndroidPlatformConfiguration; +import org.wso2.carbon.device.mgt.mobile.android.common.bean.DeviceState; import org.wso2.carbon.device.mgt.mobile.android.common.bean.EnterpriseTokenUrl; import org.wso2.carbon.device.mgt.mobile.android.common.bean.GoogleAppSyncResponse; import org.wso2.carbon.device.mgt.mobile.android.common.bean.wrapper.*; @@ -380,7 +381,7 @@ public interface AndroidService { * @return * @throws {@link AndroidDeviceMgtPluginException} */ - Response sendApplicationConfiguration( + ProfileOperation sendApplicationConfiguration( ApplicationRestrictionBeanWrapper applicationRestrictionBeanWrapper) throws AndroidDeviceMgtPluginException; @@ -427,7 +428,7 @@ public interface AndroidService { * @return {@link Response} * @throws {@link DeviceManagementException} */ - Response enrollDevice(AndroidDevice androidDevice) throws DeviceManagementException, AndroidDeviceMgtPluginException; + Message enrollDevice(AndroidDevice androidDevice) throws DeviceManagementException, AndroidDeviceMgtPluginException; /** * Method to check if a device is enrolled @@ -480,6 +481,6 @@ public interface AndroidService { * @return {@link Response} * @throws {@link AndroidDeviceMgtPluginException} */ - Response retrieveAlerts(String deviceId, long from, long to, String type, String ifModifiedSince) + List retrieveAlerts(String deviceId, long from, long to, String type, String ifModifiedSince) throws AndroidDeviceMgtPluginException; } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/pom.xml b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/pom.xml index 342234c45b..620d525f42 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/pom.xml +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/pom.xml @@ -81,6 +81,8 @@ org.wso2.carbon.analytics.datasource.commons.*, org.wso2.carbon.base, com.google.gson.* + org.wso2.carbon.device.application.mgt.common.* + org.wso2.carbon.user.core. !org.wso2.carbon.device.mgt.mobile.android.core.internal, diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java index cbd7e6b26d..a0802448b9 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java @@ -17,6 +17,7 @@ package org.wso2.carbon.device.mgt.mobile.android.core.impl; +import com.google.api.client.http.HttpStatusCodes; import com.google.api.services.androidenterprise.model.ProductsListResponse; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; @@ -63,7 +64,7 @@ import org.wso2.carbon.device.mgt.mobile.android.core.util.AndroidEnterpriseUtil import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; -import javax.ws.rs.core.Response; + import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; @@ -222,13 +223,13 @@ public class AndroidServiceImpl implements AndroidService { if (device != null) { String status = String.valueOf(device.getEnrolmentInfo().getStatus()); Message responseMessage = new Message(); - responseMessage.setResponseCode(Response.Status.OK.toString()); + responseMessage.setResponseCode(String.valueOf(HttpStatusCodes.STATUS_CODE_OK)); responseMessage .setResponseMessage("Status of android device that carries the id '" + id + "' is " + status); return responseMessage; } else { Message responseMessage = new Message(); - responseMessage.setResponseCode(Response.Status.NOT_FOUND.toString()); + responseMessage.setResponseCode(String.valueOf(HttpStatusCodes.STATUS_CODE_NOT_FOUND)); responseMessage.setResponseMessage("No Android device is found upon the id '" + id + "'"); return responseMessage; } @@ -824,7 +825,7 @@ public class AndroidServiceImpl implements AndroidService { } @Override - public Response sendApplicationConfiguration(ApplicationRestrictionBeanWrapper applicationRestrictionBeanWrapper) + public ProfileOperation sendApplicationConfiguration(ApplicationRestrictionBeanWrapper applicationRestrictionBeanWrapper) throws AndroidDeviceMgtPluginException { try { if (applicationRestrictionBeanWrapper == null || applicationRestrictionBeanWrapper.getOperation() == null) { @@ -837,16 +838,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.REMOTE_APP_CONFIG); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(applicationRestriction.toJSON()); - return AndroidAPIUtils.getOperationResponse(applicationRestrictionBeanWrapper.getDeviceIDs(), - operation); - } catch (InvalidDeviceException e) { - String errorMessage = "Invalid Device Identifiers found."; - log.error(errorMessage, e); - throw new BadRequestExceptionDup(errorMessage, e); - } catch (OperationManagementException e) { - String errorMessage = "Issue in retrieving operation management service instance"; - log.error(errorMessage, e); - throw new UnexpectedServerErrorExceptionDup(errorMessage); + return operation; } catch (BadRequestExceptionDup e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); @@ -975,7 +967,7 @@ public class AndroidServiceImpl implements AndroidService { } @Override - public Response enrollDevice(AndroidDevice androidDevice) + public Message enrollDevice(AndroidDevice androidDevice) throws DeviceManagementException, AndroidDeviceMgtPluginException { try { String token = null; @@ -1069,21 +1061,21 @@ public class AndroidServiceImpl implements AndroidService { } Message responseMessage = new Message(); - responseMessage.setResponseCode(Response.Status.OK.toString()); + responseMessage.setResponseCode(String.valueOf(HttpStatusCodes.STATUS_CODE_OK)); if (token == null) { responseMessage.setResponseMessage("Android device, which carries the id '" + androidDevice.getDeviceIdentifier() + "' has successfully been enrolled"); } else { responseMessage.setResponseMessage("Google response token" + token); } - return Response.status(Response.Status.OK).entity(responseMessage).build(); + return responseMessage; } else { Message responseMessage = new Message(); - responseMessage.setResponseCode(Response.Status.INTERNAL_SERVER_ERROR.toString()); + responseMessage.setResponseCode(String.valueOf(HttpStatusCodes.STATUS_CODE_SERVER_ERROR)); responseMessage.setResponseMessage("Failed to enroll '" + device.getType() + "' device, which carries the id '" + androidDevice.getDeviceIdentifier() + "'"); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(responseMessage).build(); + return responseMessage; } } catch (PolicyManagementException | InvalidDeviceException | OperationManagementException e) { String msg = "Error occurred while enforcing default enrollment policy upon android " + @@ -1174,11 +1166,11 @@ public class AndroidServiceImpl implements AndroidService { } @Override - public Response retrieveAlerts(String deviceId, - long from, - long to, - String type, - String ifModifiedSince) throws AndroidDeviceMgtPluginException { + public List retrieveAlerts(String deviceId, + long from, + long to, + String type, + String ifModifiedSince) throws AndroidDeviceMgtPluginException { if (from != 0l && to != 0l && deviceId != null){ return retrieveAlertFromDate(deviceId, from, to); } else if (deviceId != null && type != null) { @@ -1194,7 +1186,7 @@ public class AndroidServiceImpl implements AndroidService { } } - private Response retrieveAlert(String deviceId) throws NotFoundExceptionDup, UnexpectedServerErrorExceptionDup { + private List retrieveAlert(String deviceId) throws NotFoundExceptionDup, UnexpectedServerErrorExceptionDup { if (log.isDebugEnabled()) { log.debug("Retrieving events for given device Identifier."); } @@ -1207,7 +1199,7 @@ public class AndroidServiceImpl implements AndroidService { "published for Device: " + deviceId + "."; throw new NotFoundExceptionDup(errorMessage); } else { - return Response.status(Response.Status.OK).entity(deviceStates).build(); + return deviceStates; } } catch (AnalyticsException e) { String msg = "Error occurred while getting published events for specific device: " + deviceId + "."; @@ -1216,7 +1208,7 @@ public class AndroidServiceImpl implements AndroidService { } } - private Response retrieveAlertFromDate(String deviceId, long from, long to) throws NotFoundExceptionDup, UnexpectedServerErrorExceptionDup { + private List retrieveAlertFromDate(String deviceId, long from, long to) throws NotFoundExceptionDup, UnexpectedServerErrorExceptionDup { String fromDate = String.valueOf(from); String toDate = String.valueOf(to); if (log.isDebugEnabled()) { @@ -1233,7 +1225,7 @@ public class AndroidServiceImpl implements AndroidService { throw new NotFoundExceptionDup(errorMessage); } else { - return Response.status(Response.Status.OK).entity(deviceStates).build(); + return deviceStates; } } catch (AnalyticsException e) { String msg = "Error occurred while getting published events for specific " + @@ -1243,7 +1235,7 @@ public class AndroidServiceImpl implements AndroidService { } } - private Response retrieveAlertByType(String deviceId, String type) throws NotFoundExceptionDup, UnexpectedServerErrorExceptionDup { + private List retrieveAlertByType(String deviceId, String type) throws NotFoundExceptionDup, UnexpectedServerErrorExceptionDup { if (log.isDebugEnabled()) { log.debug("Retrieving events for given device identifier and type."); } @@ -1257,7 +1249,7 @@ public class AndroidServiceImpl implements AndroidService { throw new NotFoundExceptionDup(errorMessage); } else { - return Response.status(Response.Status.OK).entity(deviceStates).build(); + return deviceStates; } } catch (AnalyticsException e) { String msg = "Error occurred while getting published events for specific " +