Fix duplicate group and role error handling responses

device-lifecycle
navodzoysa 2 years ago
parent a21c88f3a7
commit a367cd6b35

@ -54,6 +54,7 @@ import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.registry.resource.services.utils.ChangeRolePermissionsUtil; import org.wso2.carbon.registry.resource.services.utils.ChangeRolePermissionsUtil;
import org.wso2.carbon.user.api.*; import org.wso2.carbon.user.api.*;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager; import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.constants.UserCoreErrorConstants.ErrorMessages;
import org.wso2.carbon.user.mgt.UserRealmProxy; import org.wso2.carbon.user.mgt.UserRealmProxy;
import org.wso2.carbon.user.mgt.common.UIPermissionNode; import org.wso2.carbon.user.mgt.common.UIPermissionNode;
import org.wso2.carbon.user.mgt.common.UserAdminException; import org.wso2.carbon.user.mgt.common.UserAdminException;
@ -316,21 +317,31 @@ public class RoleManagementServiceImpl implements RoleManagementService {
entity("Role '" + roleInfo.getRoleName() + "' has " + "successfully been" entity("Role '" + roleInfo.getRoleName() + "' has " + "successfully been"
+ " added").build(); + " added").build();
} catch (UserStoreException e) { } catch (UserStoreException e) {
String errorCode = "";
String errorMessage = e.getMessage();
if (errorMessage != null && !errorMessage.isEmpty() &&
errorMessage.contains(ErrorMessages.ERROR_CODE_ROLE_ALREADY_EXISTS.getCode())) {
errorCode = e.getMessage().split("-")[0].trim();
}
if (ErrorMessages.ERROR_CODE_ROLE_ALREADY_EXISTS.getCode().equals(errorCode)) {
String roleName = roleInfo.getRoleName().split("/")[1];
String msg = "Role already exists with name " + roleName + ".";
log.warn(msg);
return Response.status(Response.Status.CONFLICT).entity(msg).build();
} else {
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);
return Response.serverError().entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); }
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
String msg = "Error occurred while composing the URI at which the information of the newly created role " + String msg = "Error occurred while composing the URI at which the information of the newly created role " +
"can be retrieved"; "can be retrieved";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
String msg = "Error occurred while encoding role name"; String msg = "Error occurred while encoding role name";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }

@ -47,6 +47,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException;
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
@ -140,13 +141,16 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (HttpReportingUtil.isTrackerEnabled()){ if (HttpReportingUtil.isTrackerEnabled()){
existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId); existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId);
int groupId = existingGroup.getGroupId(); int groupId = existingGroup.getGroupId();
try {
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() DeviceManagementDataHolder.getInstance().getDeviceAPIClientService()
.addGroup(deviceGroup, groupId, tenantId); .addGroup(deviceGroup, groupId, tenantId);
} catch (TrackerAlreadyExistException e) {
throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName());
} }
// add a group if not exist in traccar starts } else {
throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName());
} }
}
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while adding deviceGroup '" + deviceGroup.getName() + "' to database."; String msg = "Error occurred while adding deviceGroup '" + deviceGroup.getName() + "' to database.";

@ -648,7 +648,7 @@ public class TraccarClientFactory {
TrackerManagementDAOFactory.openConnection(); TrackerManagementDAOFactory.openConnection();
trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId); trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId);
if (trackerGroupInfo != null) { if (trackerGroupInfo != null) {
String msg = "The group already exit"; String msg = "The group already exists in Traccar.";
log.error(msg); log.error(msg);
throw new TrackerAlreadyExistException(msg); throw new TrackerAlreadyExistException(msg);
} }

@ -555,7 +555,7 @@ public class TraccarClientImpl implements TraccarClient {
TrackerManagementDAOFactory.openConnection(); TrackerManagementDAOFactory.openConnection();
trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId); trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId);
if (trackerGroupInfo != null) { if (trackerGroupInfo != null) {
String msg = "The group already exit"; String msg = "The group already exists in Traccar.";
log.error(msg); log.error(msg);
throw new TrackerAlreadyExistException(msg); throw new TrackerAlreadyExistException(msg);
} }

Loading…
Cancel
Save