From 0c54753b3447a4392d558140bb8362e67984956d Mon Sep 17 00:00:00 2001 From: prabathabey Date: Wed, 22 Jun 2016 15:31:55 +0530 Subject: [PATCH] Cleaning up UserManagementService API implementation --- .../device/mgt/jaxrs/beans/BasicUserInfo.java | 68 +++++++++++++++++++ .../mgt/jaxrs/beans/BasicUserInfoList.java | 57 ++++++++++++++++ .../beans/{UserWrapper.java => UserInfo.java} | 58 ++-------------- .../{UserList.java => UserInfoList.java} | 9 ++- .../service/api/UserManagementService.java | 25 ++----- .../impl/UserManagementServiceImpl.java | 36 ++++------ 6 files changed, 157 insertions(+), 96 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoList.java rename components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/{UserWrapper.java => UserInfo.java} (52%) rename components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/{UserList.java => UserInfoList.java} (89%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java new file mode 100644 index 0000000000..467fd8afb5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.jaxrs.beans; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel(value = "BasicUserInfo", description = "Basic user information and the roles of the user.") +public class BasicUserInfo { + + private String username; + + @ApiModelProperty(name = "firstname", value = "The first name of the user.", required = true ) + private String firstname; + @ApiModelProperty(name = "lastname", value = "The last name of the user.", required = true ) + private String lastname; + @ApiModelProperty(name = "emailAddress", value = "The email address of the user.", required = true ) + private String emailAddress; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getFirstname() { + return firstname; + } + + public void setFirstname(String firstname) { + this.firstname = firstname; + } + + public String getLastname() { + return lastname; + } + + public void setLastname(String lastname) { + this.lastname = lastname; + } + + public String getEmailAddress() { + return emailAddress; + } + + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoList.java new file mode 100644 index 0000000000..c8e58383c0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoList.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.jaxrs.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.ArrayList; +import java.util.List; + +@ApiModel(value = "BasicUserInfoList", description = "This contains basic details of a set of users that matches " + + "a given criteria as a collection") +public class BasicUserInfoList extends BasePaginatedResult { + + private List users = new ArrayList<>(); + + @ApiModelProperty(value = "List of devices returned") + @JsonProperty("users") + public List getList() { + return users; + } + + public void setList(List users) { + this.users = users; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + + sb.append(" count: ").append(getCount()).append(",\n"); + sb.append(" next: ").append(getNext()).append(",\n"); + sb.append(" previous: ").append(getPrevious()).append(",\n"); + sb.append(" users: [").append(users).append("\n"); + sb.append("]}\n"); + return sb.toString(); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfo.java similarity index 52% rename from components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserWrapper.java rename to components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfo.java index 8009ae233e..4513c6f647 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfo.java @@ -21,60 +21,23 @@ package org.wso2.carbon.device.mgt.jaxrs.beans; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; -@ApiModel(value = "UserWrapper", description = "User details and the roles of the user.") -public class UserWrapper { - - private String username; - /* - Base64 encoded password - */ +@ApiModel(value = "UserInfo", description = "User details and the roles of the user.") +public class UserInfo extends BasicUserInfo { @ApiModelProperty(name = "password", value = "Base64 encoded password.", required = true ) private String password; - @ApiModelProperty(name = "firstname", value = "The first name of the user.", required = true ) - private String firstname; - @ApiModelProperty(name = "lastname", value = "The last name of the user.", required = true ) - private String lastname; - @ApiModelProperty(name = "emailAddress", value = "The email address of the user.", required = true ) - private String emailAddress; + @ApiModelProperty(name = "roles", value = "List of roles.", required = true ) private String[] roles; - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getFirstname() { - return firstname; - } - - public void setFirstname(String firstname) { - this.firstname = firstname; - } - - public String getLastname() { - return lastname; - } - - public void setLastname(String lastname) { - this.lastname = lastname; - } - - public String getEmailAddress() { - return emailAddress; + public String getPassword() { + return password; } - public void setEmailAddress(String emailAddress) { - this.emailAddress = emailAddress; + public void setPassword(String password) { + this.password = password; } - /* - Giving a clone of the array since arrays are mutable - */ public String[] getRoles() { String[] copiedRoles = roles; if (roles != null){ @@ -87,11 +50,4 @@ public class UserWrapper { this.roles = roles; } - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfoList.java similarity index 89% rename from components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserList.java rename to components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfoList.java index a205f0a3ac..bc98be244e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserList.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfoList.java @@ -27,17 +27,17 @@ import java.util.List; @ApiModel(value = "List of users", description = "This contains a set of users that matches a given " + "criteria as a collection") -public class UserList extends BasePaginatedResult { +public class UserInfoList extends BasePaginatedResult { - private List users = new ArrayList<>(); + private List users = new ArrayList<>(); @ApiModelProperty(value = "List of devices returned") @JsonProperty("users") - public List getList() { + public List getList() { return users; } - public void setList(List users) { + public void setList(List users) { this.users = users; } @@ -54,5 +54,4 @@ public class UserList extends BasePaginatedResult { return sb.toString(); } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java index 89ac39b3eb..fbbeb51736 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java @@ -21,10 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.api; import io.swagger.annotations.*; import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.Permission; -import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; -import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper; -import org.wso2.carbon.device.mgt.jaxrs.beans.UserList; -import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper; +import org.wso2.carbon.device.mgt.jaxrs.beans.*; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @@ -95,7 +92,7 @@ public interface UserManagementService { @ApiParam( name = "user", value = "User related details.", - required = true) UserWrapper user); + required = true) UserInfo user); @GET @Path("/{username}") @@ -105,13 +102,13 @@ public interface UserManagementService { value = "Getting details of a user.", notes = "If you wish to get the details of a specific user that is registered with EMM," + " you can do so using the REST API.", - response = UserWrapper.class, + response = BasicUserInfo.class, tags = "User Management") @ApiResponses(value = { @ApiResponse( code = 200, message = "OK. \n Successfully fetched the requested role.", - response = UserWrapper.class, + response = BasicUserInfo.class, responseHeaders = { @ResponseHeader( name = "Content-Type", @@ -209,7 +206,7 @@ public interface UserManagementService { @ApiParam( name = "userData", value = "User related details.", - required = true) UserWrapper userData); + required = true) UserInfo userData); @DELETE @Path("/{username}") @@ -247,15 +244,12 @@ public interface UserManagementService { value = "Get the role list of a user.", notes = "A user can be assigned to one or more role in EMM. Using this REST API you are " + "able to get the role/roles a user is assigned to.", - response = String.class, - responseContainer = "List", tags = "User Management") @ApiResponses(value = { @ApiResponse( code = 200, message = "OK. \n Successfully fetched the role list assigned to the user.", - response = String.class, - responseContainer = "List", + response = RoleList.class, responseHeaders = { @ResponseHeader( name = "Content-Type", @@ -297,15 +291,12 @@ public interface UserManagementService { value = "Get user list", notes = "If you wish to get the details of all the users registered with EMM, you can do so " + "using the REST API", - response = UserList.class, - responseContainer = "List", tags = "User Management") @ApiResponses(value = { @ApiResponse( code = 200, message = "OK. \n Successfully fetched the requested role.", - response = UserList.class, - responseContainer = "List", + response = UserInfoList.class, responseHeaders = { @ResponseHeader( name = "Content-Type", @@ -364,8 +355,6 @@ public interface UserManagementService { + "search for that user by giving a character or a few characters in the username. " + "You will be given a list of users having the user name with the exact order of the " + "characters you provided.", - response = String.class, - responseContainer = "List", tags = "User Management") @ApiResponses(value = { @ApiResponse( 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 ee67791fdd..177290e9f4 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 @@ -18,7 +18,6 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; -import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -26,12 +25,9 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo; -import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; -import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper; +import org.wso2.carbon.device.mgt.jaxrs.beans.*; import org.wso2.carbon.device.mgt.jaxrs.service.api.UserManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*; -import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper; -import org.wso2.carbon.device.mgt.jaxrs.beans.UserList; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder; @@ -55,7 +51,7 @@ public class UserManagementServiceImpl implements UserManagementService { @POST @Override - public Response addUser(UserWrapper userWrapper) { + public Response addUser(UserInfo userWrapper) { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (userStoreManager.isExistingUser(userWrapper.getUsername())) { @@ -172,7 +168,7 @@ public class UserManagementServiceImpl implements UserManagementService { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (userStoreManager.isExistingUser(username)) { - UserWrapper user = new UserWrapper(); + BasicUserInfo user = new BasicUserInfo(); user.setUsername(username); user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); @@ -203,7 +199,7 @@ public class UserManagementServiceImpl implements UserManagementService { @PUT @Path("/{username}") @Override - public Response updateUser(@PathParam("username") String username, UserWrapper userWrapper) { + public Response updateUser(@PathParam("username") String username, UserInfo userWrapper) { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (userStoreManager.isExistingUser(userWrapper.getUsername())) { @@ -316,8 +312,9 @@ public class UserManagementServiceImpl implements UserManagementService { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (userStoreManager.isExistingUser(username)) { - return Response.status(Response.Status.OK).entity(Collections.singletonList( - getFilteredRoles(userStoreManager, username))).build(); + RoleList result = new RoleList(); + result.setList(getFilteredRoles(userStoreManager, username)); + return Response.status(Response.Status.OK).entity(result).build(); } else { // Outputting debug message upon trying to remove non-existing user if (log.isDebugEnabled()) { @@ -343,7 +340,7 @@ public class UserManagementServiceImpl implements UserManagementService { if (log.isDebugEnabled()) { log.debug("Getting the list of users with all user-related information"); } - List userList, offsetList; + List userList, offsetList; String appliedFilter = ((filter == null) || filter.isEmpty() ? "*" : filter); int appliedLimit = (limit <= 0) ? -1 : (limit + offset); @@ -353,9 +350,9 @@ public class UserManagementServiceImpl implements UserManagementService { //As the listUsers function accepts limit only to accommodate offset we are passing offset + limit String[] users = userStoreManager.listUsers(appliedFilter, appliedLimit); userList = new ArrayList<>(users.length); - UserWrapper user; + BasicUserInfo user; for (String username : users) { - user = new UserWrapper(); + user = new BasicUserInfo(); user.setUsername(username); user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); @@ -368,12 +365,7 @@ public class UserManagementServiceImpl implements UserManagementService { } else { offsetList = new ArrayList<>(); } - -// if (offsetList.size() <= 0) { -// return Response.status(Response.Status.NOT_FOUND).entity("No users available for retrieval").build(); -// } - - UserList result = new UserList(); + BasicUserInfoList result = new BasicUserInfoList(); result.setList(offsetList); result.setCount(offsetList.size()); @@ -394,14 +386,14 @@ public class UserManagementServiceImpl implements UserManagementService { if (log.isDebugEnabled()) { log.debug("Getting the list of users with all user-related information using the filter : " + filter); } - List userList; + List userList; try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); String[] users = userStoreManager.listUsers(filter + "*", -1); userList = new ArrayList<>(users.length); - UserWrapper user; + UserInfo user; for (String username : users) { - user = new UserWrapper(); + user = new UserInfo(); user.setUsername(username); user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME));