Code cleanup

revert-70aa11f8
prabathabey 9 years ago
parent f33ac36bfa
commit ed5d611c35

@ -87,7 +87,7 @@ public interface DeviceManagementService {
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, 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 = ErrorResponse.class)
}) })
@Permission(scope = "device-list", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) @Permission(scope = "device-list", permissions = {"/permission/admin/device-mgt/admin/devices/list"})
@ -180,7 +180,7 @@ public interface DeviceManagementService {
response = ErrorResponse.class), response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \n " +
"Server error occurred while retrieving information requested device.", "Server error occurred while retrieving information requested device.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
@ -259,7 +259,7 @@ public interface DeviceManagementService {
message = "Not Acceptable. \n The requested media type is not supported."), message = "Not Acceptable. \n The requested media type is not supported."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \n " +
"Server error occurred while retrieving feature list of the device.", "Server error occurred while retrieving feature list of the device.",
response = ErrorResponse.class) 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."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \n " +
"Server error occurred while enrolling the device.", "Server error occurred while enrolling the device.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
@ -405,7 +405,7 @@ public interface DeviceManagementService {
message = "Not Acceptable. \n The requested media type is not supported."), message = "Not Acceptable. \n The requested media type is not supported."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \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) response = ErrorResponse.class)
}) })
@ -494,7 +494,7 @@ public interface DeviceManagementService {
message = "Not Acceptable. \n The requested media type is not supported."), message = "Not Acceptable. \n The requested media type is not supported."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \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) response = ErrorResponse.class)
}) })
@ -582,7 +582,7 @@ public interface DeviceManagementService {
message = "Not Acceptable. \n The requested media type is not supported."), message = "Not Acceptable. \n The requested media type is not supported."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \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 = ErrorResponse.class)
}) })

@ -85,7 +85,8 @@ public interface NotificationManagementService {
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, 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) response = ErrorResponse.class)
}) })
@Permission(scope = "device-notification-view", permissions = { @Permission(scope = "device-notification-view", permissions = {

@ -90,7 +90,7 @@ public interface PolicyManagementService {
message = "Unsupported media type. \n The entity of the request was in a not supported format."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \n " +
"Server error occurred while adding a new policy.", "Server error occurred while adding a new policy.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
@ -142,7 +142,7 @@ public interface PolicyManagementService {
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = ("Internal Server ErrorResponse. \n Server error occurred while fetching " + message = ("Internal Server Error. \n Server error occurred while fetching " +
"policies."), "policies."),
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
@ -205,7 +205,7 @@ public interface PolicyManagementService {
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, 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.", "policy.",
response = ErrorResponse.class) 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."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \n " +
"Server error occurred while updating the policy.", "Server error occurred while updating the policy.",
response = ErrorResponse.class) 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."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \n " +
"Server error occurred while bulk removing policies.", "Server error occurred while bulk removing policies.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })

@ -68,7 +68,7 @@ public interface ApplicationManagementAdminService {
message = "Unsupported media type. \n The entity of the request was in a not supported format."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \n " +
"Server error occurred while bulk issuing application installation operations upon " + "Server error occurred while bulk issuing application installation operations upon " +
"a given set of devices.", "a given set of devices.",
response = ErrorResponse.class) 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."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \n " +
"Server error occurred while bulk issuing application un-installation operations upon " + "Server error occurred while bulk issuing application un-installation operations upon " +
"a given set of devices.", "a given set of devices.",
response = ErrorResponse.class) response = ErrorResponse.class)

@ -80,7 +80,7 @@ public interface DeviceManagementAdminService {
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, 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 = ErrorResponse.class)
}) })
Response getDevicesByName( Response getDevicesByName(

@ -60,7 +60,7 @@ public interface UserManagementAdminService {
message = "Unsupported media type. \n The entity of the request was in a not supported format."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server Error. \n " +
"Server error occurred while updating credentials of the user.", "Server error occurred while updating credentials of the user.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })

@ -265,7 +265,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
RequestValidationUtil.validateDeviceIdentifier(type, id); RequestValidationUtil.validateDeviceIdentifier(type, id);
dms = DeviceMgtAPIUtils.getDeviceManagementService(); dms = DeviceMgtAPIUtils.getDeviceManagementService();
result = dms.getOperations(new DeviceIdentifier(id, type),request); result = dms.getOperations(new DeviceIdentifier(id, type), request);
operationsList.setList((List<? extends Operation>) result.getData()); operationsList.setList((List<? extends Operation>) result.getData());
operationsList.setCount(result.getRecordsTotal()); operationsList.setCount(result.getRecordsTotal());

@ -22,11 +22,10 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.Notification;
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; 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.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.api.NotificationManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import javax.ws.rs.*; import javax.ws.rs.*;
@ -49,6 +48,7 @@ public class NotificationManagementServiceImpl implements NotificationManagement
@QueryParam("offset") int offset, @QueryParam("limit") int limit) { @QueryParam("offset") int offset, @QueryParam("limit") int limit) {
String msg; String msg;
List<Notification> notifications; List<Notification> notifications;
NotificationList notificationList = new NotificationList();
try { try {
if (status != null) { if (status != null) {
RequestValidationUtil.validateNotificationStatus(status); RequestValidationUtil.validateNotificationStatus(status);
@ -59,17 +59,14 @@ public class NotificationManagementServiceImpl implements NotificationManagement
notifications = DeviceMgtAPIUtils.getNotificationManagementService().getAllNotifications(); notifications = DeviceMgtAPIUtils.getNotificationManagementService().getAllNotifications();
} }
if (notifications == null || notifications.size() == 0) { notificationList.setCount(notifications.size());
throw new NotFoundException( notificationList.setNotifications(notifications);
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No notification is " + return Response.status(Response.Status.OK).entity(notificationList).build();
"available to be retrieved.").build());
}
return Response.status(Response.Status.OK).entity(notifications).build();
} catch (NotificationManagementException e) { } catch (NotificationManagementException e) {
msg = "Error occurred while retrieving notification info"; msg = "Error occurred while retrieving notification list";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }

@ -298,7 +298,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} }
if (isPolicyDeActivated) { if (isPolicyDeActivated) {
return Response.status(Response.Status.OK).entity("Selected policies have been successfully " + return Response.status(Response.Status.OK).entity("Selected policies have been successfully " +
"deactivated").build(); "deactivated").build();
} else { } else {
return Response.status(Response.Status.NOT_FOUND).entity( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("Selected policies have " + new ErrorResponse.ErrorResponseBuilder().setMessage("Selected policies have " +

@ -209,7 +209,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
//TODO fix what's returned in the entity //TODO fix what's returned in the entity
return Response.created(new URI(API_BASE_PATH + "/" + roleInfo.getRoleName())).entity( return Response.created(new URI(API_BASE_PATH + "/" + roleInfo.getRoleName())).entity(
"Role '" + roleInfo.getRoleName() + "' has " + "Role '" + roleInfo.getRoleName() + "' has " +
"successfully been added").build(); "successfully been added").build();
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Error occurred while adding role '" + roleInfo.getRoleName() + "'"; String msg = "Error occurred while adding role '" + roleInfo.getRoleName() + "'";
log.error(msg, e); log.error(msg, e);

@ -110,7 +110,7 @@ public class UserManagementServiceImpl implements UserManagementService {
} }
return Response.status(Response.Status.NOT_FOUND).entity( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage( new ErrorResponse.ErrorResponseBuilder().setMessage(
"User doesn't exist.").build()).build(); "User doesn't exist.").build()).build();
} }
BasicUserInfo user = this.getBasicUserInfo(username); BasicUserInfo user = this.getBasicUserInfo(username);

@ -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.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.exception.UnknownApplicationTypeException; 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.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.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.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.util.MDMAndroidOperationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.MDMAndroidOperationUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.MDMIOSOperationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.MDMIOSOperationUtil;
@ -82,21 +80,22 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem
applicationWrapper.getDeviceIdentifiers().size() > 0) { applicationWrapper.getDeviceIdentifiers().size() > 0) {
activity = appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers()); activity = appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers());
} else { } else {
throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage( return Response.status(Response.Status.BAD_REQUEST).entity(
"No application installation criteria i.e. user/role/device is given").build()); 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(); return Response.status(Response.Status.ACCEPTED).entity(activity).build();
} catch (ApplicationManagementException e) { } 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); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (UnknownApplicationTypeException e) { } catch (UnknownApplicationTypeException e) {
String msg = "The type of application requested to be installed is not supported"; String msg = "The type of application requested to be installed is not supported";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }
@ -130,22 +129,22 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem
applicationWrapper.getDeviceIdentifiers().size() > 0) { applicationWrapper.getDeviceIdentifiers().size() > 0) {
activity = appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers()); activity = appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers());
} else { } else {
throw new InputValidationException( return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage( new ErrorResponse.ErrorResponseBuilder().setMessage(
"No application un-installation criteria i.e. user/role/device is given").build()); "No application un-installation criteria i.e. user/role/device is given").build()).build();
} }
} }
return Response.status(Response.Status.ACCEPTED).entity(activity).build(); return Response.status(Response.Status.ACCEPTED).entity(activity).build();
} catch (ApplicationManagementException e) { } 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); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (UnknownApplicationTypeException e) { } catch (UnknownApplicationTypeException e) {
String msg = "The type of application requested to be un-installed is not supported"; String msg = "The type of application requested to be un-installed is not supported";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }

@ -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.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; 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.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 org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import javax.ws.rs.*; import javax.ws.rs.*;
@ -55,21 +53,16 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
try { try {
int currentTenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int currentTenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
if (MultitenantConstants.SUPER_TENANT_ID != currentTenantId) { if (MultitenantConstants.SUPER_TENANT_ID != currentTenantId) {
throw new UnauthorizedAccessException( return Response.status(Response.Status.UNAUTHORIZED).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage( new ErrorResponse.ErrorResponseBuilder().setMessage(
"Current logged in user is not authorized to perform this operation").build()); "Current logged in user is not authorized to perform this operation").build()).build();
} }
PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(DeviceMgtAPIUtils.getTenantId(tenantDomain)); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(DeviceMgtAPIUtils.getTenantId(tenantDomain));
List<Device> devices = DeviceMgtAPIUtils.getDeviceManagementService(). List<Device> devices = DeviceMgtAPIUtils.getDeviceManagementService().
getDevicesByNameAndType(name, type, offset, limit); 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());
}
// setting up paginated result // setting up paginated result
DeviceList deviceList = new DeviceList(); DeviceList deviceList = new DeviceList();
@ -80,8 +73,8 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred at server side while fetching device list."; String msg = "Error occurred at server side while fetching device list.";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }

@ -21,11 +21,9 @@ package org.wso2.carbon.device.mgt.jaxrs.util;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; 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.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.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.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager; import org.wso2.carbon.user.api.UserStoreManager;
@ -44,7 +42,8 @@ public class CredentialManagementResponseBuilder {
/** /**
* Builds the response to change the password of a user * 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 * @param credentials - User credentials
* @return Response Object * @return Response Object
*/ */
@ -52,38 +51,39 @@ public class CredentialManagementResponseBuilder {
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (!userStoreManager.isExistingUser(username)) { if (!userStoreManager.isExistingUser(username)) {
throw new InputValidationException( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("No user found with the username " new ErrorResponse.ErrorResponseBuilder().setMessage("No user found with the username '"
+ username).build()); + username + "'").build()).build();
} }
RequestValidationUtil.validateCredentials(credentials); RequestValidationUtil.validateCredentials(credentials);
if (!validateCredential(credentials.getNewPassword())) { if (!validateCredential(credentials.getNewPassword())) {
String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration() String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration()
.getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG); .getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG);
throw new InputValidationException( return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMsg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(errorMsg).build()).build();
} }
userStoreManager.updateCredential(username, credentials.getNewPassword(), userStoreManager.updateCredential(username, credentials.getNewPassword(),
credentials.getOldPassword()); credentials.getOldPassword());
return Response.status(Response.Status.OK).entity("UserImpl password by username: " + return Response.status(Response.Status.OK).entity("UserImpl password by username: " +
username + " was successfully changed.").build(); username + " was successfully changed.").build();
} catch (UserStoreException e) { } catch (UserStoreException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(e.getMessage()).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(e.getMessage()).build()).build();
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
String msg = "Could not change the password of the user: " + username + String msg = "Could not change the password of the user: " + username +
". The Character Encoding is not supported."; ". The Character Encoding is not supported.";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }
/** /**
* Builds the response to reset the password of a user * 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 * @param credentials - User credentials
* @return Response Object * @return Response Object
*/ */
@ -91,35 +91,35 @@ public class CredentialManagementResponseBuilder {
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (!userStoreManager.isExistingUser(username)) { if (!userStoreManager.isExistingUser(username)) {
throw new InputValidationException( return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("No user found with the username " new ErrorResponse.ErrorResponseBuilder().setMessage("No user found with the username "
+ username).build()); + username).build()).build();
} }
if (credentials == null || credentials.getNewPassword() == null) { if (credentials == null || credentials.getNewPassword() == null) {
throw new InputValidationException( return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Password cannot be empty." new ErrorResponse.ErrorResponseBuilder().setMessage("Password cannot be empty."
+ username).build()); + username).build()).build();
} }
if (!validateCredential(credentials.getNewPassword())) { if (!validateCredential(credentials.getNewPassword())) {
String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration() String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration()
.getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG); .getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG);
throw new InputValidationException( return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMsg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(errorMsg).build()).build();
} }
userStoreManager.updateCredentialByAdmin(username, credentials.getNewPassword()); userStoreManager.updateCredentialByAdmin(username, credentials.getNewPassword());
return Response.status(Response.Status.OK).entity("UserImpl password by username: " + return Response.status(Response.Status.OK).entity("UserImpl password by username: " +
username + " was successfully changed.").build(); username + " was successfully changed.").build();
} catch (UserStoreException e) { } 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); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
String msg = "Could not change the password of the user: " + username + String msg = "Could not change the password of the user: " + username +
". The Character Encoding is not supported."; ". The Character Encoding is not supported.";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }

Loading…
Cancel
Save