Merge branch 'rest-api-improvements' of https://github.com/wso2/carbon-device-mgt into rest-api-improvements

revert-70aa11f8
madhawap 8 years ago
commit ee11bde8b0

@ -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;
}
}

@ -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<BasicUserInfo> users = new ArrayList<>();
@ApiModelProperty(value = "List of devices returned")
@JsonProperty("users")
public List<BasicUserInfo> getList() {
return users;
}
public void setList(List<BasicUserInfo> 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();
}
}

@ -21,60 +21,23 @@ package org.wso2.carbon.device.mgt.jaxrs.beans;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "UserWrapper", description = "User details and the roles of the user.") @ApiModel(value = "UserInfo", description = "User details and the roles of the user.")
public class UserWrapper { public class UserInfo extends BasicUserInfo {
private String username;
/*
Base64 encoded password
*/
@ApiModelProperty(name = "password", value = "Base64 encoded password.", required = true ) @ApiModelProperty(name = "password", value = "Base64 encoded password.", required = true )
private String password; 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 ) @ApiModelProperty(name = "roles", value = "List of roles.", required = true )
private String[] roles; private String[] roles;
public String getUsername() { public String getPassword() {
return username; return password;
}
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) { public void setPassword(String password) {
this.emailAddress = emailAddress; this.password = password;
} }
/*
Giving a clone of the array since arrays are mutable
*/
public String[] getRoles() { public String[] getRoles() {
String[] copiedRoles = roles; String[] copiedRoles = roles;
if (roles != null){ if (roles != null){
@ -87,11 +50,4 @@ public class UserWrapper {
this.roles = roles; this.roles = roles;
} }
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
} }

@ -27,17 +27,17 @@ import java.util.List;
@ApiModel(value = "List of users", description = "This contains a set of users that matches a given " + @ApiModel(value = "List of users", description = "This contains a set of users that matches a given " +
"criteria as a collection") "criteria as a collection")
public class UserList extends BasePaginatedResult { public class UserInfoList extends BasePaginatedResult {
private List<UserWrapper> users = new ArrayList<>(); private List<UserInfo> users = new ArrayList<>();
@ApiModelProperty(value = "List of devices returned") @ApiModelProperty(value = "List of devices returned")
@JsonProperty("users") @JsonProperty("users")
public List<UserWrapper> getList() { public List<UserInfo> getList() {
return users; return users;
} }
public void setList(List<UserWrapper> users) { public void setList(List<UserInfo> users) {
this.users = users; this.users = users;
} }
@ -53,5 +53,4 @@ public class UserList extends BasePaginatedResult {
return sb.toString(); return sb.toString();
} }
} }

@ -21,10 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.api;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.API;
import org.wso2.carbon.apimgt.annotations.api.Permission; 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.*;
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 javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
@ -95,7 +92,7 @@ public interface UserManagementService {
@ApiParam( @ApiParam(
name = "user", name = "user",
value = "User related details.", value = "User related details.",
required = true) UserWrapper user); required = true) UserInfo user);
@GET @GET
@Path("/{username}") @Path("/{username}")
@ -105,13 +102,13 @@ public interface UserManagementService {
value = "Getting details of a user.", value = "Getting details of a user.",
notes = "If you wish to get the details of a specific user that is registered with EMM," 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.", + " you can do so using the REST API.",
response = UserWrapper.class, response = BasicUserInfo.class,
tags = "User Management") tags = "User Management")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse( @ApiResponse(
code = 200, code = 200,
message = "OK. \n Successfully fetched the requested role.", message = "OK. \n Successfully fetched the requested role.",
response = UserWrapper.class, response = BasicUserInfo.class,
responseHeaders = { responseHeaders = {
@ResponseHeader( @ResponseHeader(
name = "Content-Type", name = "Content-Type",
@ -209,7 +206,7 @@ public interface UserManagementService {
@ApiParam( @ApiParam(
name = "userData", name = "userData",
value = "User related details.", value = "User related details.",
required = true) UserWrapper userData); required = true) UserInfo userData);
@DELETE @DELETE
@Path("/{username}") @Path("/{username}")
@ -247,15 +244,12 @@ public interface UserManagementService {
value = "Get the role list of a user.", 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 " 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.", + "able to get the role/roles a user is assigned to.",
response = String.class,
responseContainer = "List",
tags = "User Management") tags = "User Management")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse( @ApiResponse(
code = 200, code = 200,
message = "OK. \n Successfully fetched the role list assigned to the user.", message = "OK. \n Successfully fetched the role list assigned to the user.",
response = String.class, response = RoleList.class,
responseContainer = "List",
responseHeaders = { responseHeaders = {
@ResponseHeader( @ResponseHeader(
name = "Content-Type", name = "Content-Type",
@ -297,15 +291,12 @@ public interface UserManagementService {
value = "Get user list", value = "Get user list",
notes = "If you wish to get the details of all the users registered with EMM, you can do so " notes = "If you wish to get the details of all the users registered with EMM, you can do so "
+ "using the REST API", + "using the REST API",
response = UserList.class,
responseContainer = "List",
tags = "User Management") tags = "User Management")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse( @ApiResponse(
code = 200, code = 200,
message = "OK. \n Successfully fetched the requested role.", message = "OK. \n Successfully fetched the requested role.",
response = UserList.class, response = UserInfoList.class,
responseContainer = "List",
responseHeaders = { responseHeaders = {
@ResponseHeader( @ResponseHeader(
name = "Content-Type", 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. " + "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 " + "You will be given a list of users having the user name with the exact order of the "
+ "characters you provided.", + "characters you provided.",
response = String.class,
responseContainer = "List",
tags = "User Management") tags = "User Management")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse( @ApiResponse(

@ -18,7 +18,6 @@
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.impl; 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.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo; 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.*;
import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper;
import org.wso2.carbon.device.mgt.jaxrs.service.api.UserManagementService; 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.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.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder; import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder;
@ -55,7 +51,7 @@ public class UserManagementServiceImpl implements UserManagementService {
@POST @POST
@Override @Override
public Response addUser(UserWrapper userWrapper) { public Response addUser(UserInfo userWrapper) {
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (userStoreManager.isExistingUser(userWrapper.getUsername())) { if (userStoreManager.isExistingUser(userWrapper.getUsername())) {
@ -172,7 +168,7 @@ public class UserManagementServiceImpl implements UserManagementService {
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (userStoreManager.isExistingUser(username)) { if (userStoreManager.isExistingUser(username)) {
UserWrapper user = new UserWrapper(); BasicUserInfo user = new BasicUserInfo();
user.setUsername(username); user.setUsername(username);
user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS));
user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME));
@ -203,7 +199,7 @@ public class UserManagementServiceImpl implements UserManagementService {
@PUT @PUT
@Path("/{username}") @Path("/{username}")
@Override @Override
public Response updateUser(@PathParam("username") String username, UserWrapper userWrapper) { public Response updateUser(@PathParam("username") String username, UserInfo userWrapper) {
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (userStoreManager.isExistingUser(userWrapper.getUsername())) { if (userStoreManager.isExistingUser(userWrapper.getUsername())) {
@ -316,8 +312,9 @@ public class UserManagementServiceImpl implements UserManagementService {
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (userStoreManager.isExistingUser(username)) { if (userStoreManager.isExistingUser(username)) {
return Response.status(Response.Status.OK).entity(Collections.singletonList( RoleList result = new RoleList();
getFilteredRoles(userStoreManager, username))).build(); result.setList(getFilteredRoles(userStoreManager, username));
return Response.status(Response.Status.OK).entity(result).build();
} else { } else {
// Outputting debug message upon trying to remove non-existing user // Outputting debug message upon trying to remove non-existing user
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -343,7 +340,7 @@ public class UserManagementServiceImpl implements UserManagementService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Getting the list of users with all user-related information"); log.debug("Getting the list of users with all user-related information");
} }
List<UserWrapper> userList, offsetList; List<BasicUserInfo> userList, offsetList;
String appliedFilter = ((filter == null) || filter.isEmpty() ? "*" : filter); String appliedFilter = ((filter == null) || filter.isEmpty() ? "*" : filter);
int appliedLimit = (limit <= 0) ? -1 : (limit + offset); 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 //As the listUsers function accepts limit only to accommodate offset we are passing offset + limit
String[] users = userStoreManager.listUsers(appliedFilter, appliedLimit); String[] users = userStoreManager.listUsers(appliedFilter, appliedLimit);
userList = new ArrayList<>(users.length); userList = new ArrayList<>(users.length);
UserWrapper user; BasicUserInfo user;
for (String username : users) { for (String username : users) {
user = new UserWrapper(); user = new BasicUserInfo();
user.setUsername(username); user.setUsername(username);
user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS));
user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME));
@ -368,12 +365,7 @@ public class UserManagementServiceImpl implements UserManagementService {
} else { } else {
offsetList = new ArrayList<>(); offsetList = new ArrayList<>();
} }
BasicUserInfoList result = new BasicUserInfoList();
// if (offsetList.size() <= 0) {
// return Response.status(Response.Status.NOT_FOUND).entity("No users available for retrieval").build();
// }
UserList result = new UserList();
result.setList(offsetList); result.setList(offsetList);
result.setCount(offsetList.size()); result.setCount(offsetList.size());
@ -394,14 +386,14 @@ public class UserManagementServiceImpl implements UserManagementService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Getting the list of users with all user-related information using the filter : " + filter); log.debug("Getting the list of users with all user-related information using the filter : " + filter);
} }
List<UserWrapper> userList; List<UserInfo> userList;
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
String[] users = userStoreManager.listUsers(filter + "*", -1); String[] users = userStoreManager.listUsers(filter + "*", -1);
userList = new ArrayList<>(users.length); userList = new ArrayList<>(users.length);
UserWrapper user; UserInfo user;
for (String username : users) { for (String username : users) {
user = new UserWrapper(); user = new UserInfo();
user.setUsername(username); user.setUsername(username);
user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS));
user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME));

Loading…
Cancel
Save