use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class BulkWebService method processUserOperation.
private BulkOperation processUserOperation(BulkOperation operation, Map<String, String> processedBulkIds) throws Exception {
log.info(" Operation is for User ");
// Intercept bulkId
User user = null;
if (operation.getData() != null) {
// Required in a request when
// "method" is "POST", "PUT", or
// "PATCH".
String serializedData = serialize(operation.getData());
for (Map.Entry<String, String> entry : processedBulkIds.entrySet()) {
String key = "bulkId:" + entry.getKey();
serializedData = serializedData.replaceAll(key, entry.getValue());
}
user = deserializeToUser(serializedData);
}
String userRootEndpoint = appConfiguration.getBaseEndpoint() + "/scim/v2/Users/";
if (operation.getMethod().equalsIgnoreCase(HttpMethod.POST)) {
log.info(" Method is POST ");
try {
user = scim2UserService.createUser(user);
GluuCustomPerson gluuPerson = personService.getPersonByUid(user.getUserName());
String inum = gluuPerson.getInum();
// String location = (new
// StringBuilder()).append(domain).append("/Users/").append(inum).toString();
String location = userRootEndpoint + inum;
operation.setLocation(location);
operation.setStatus(String.valueOf(Response.Status.CREATED.getStatusCode()));
operation.setResponse(user);
// Set aside successfully-processed bulkId
// bulkId is only required in POST
processedBulkIds.put(operation.getBulkId(), user.getId());
} catch (DuplicateEntryException ex) {
log.error("DuplicateEntryException", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.CONFLICT.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, ex.getMessage()));
} catch (PersonRequiredFieldsException ex) {
log.error("PersonRequiredFieldsException: ", ex);
operation.setStatus(String.valueOf(Response.Status.BAD_REQUEST.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_VALUE, ex.getMessage()));
} catch (Exception ex) {
log.error("Failed to create user", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, null, INTERNAL_SERVER_ERROR_MESSAGE));
}
} else if (operation.getMethod().equalsIgnoreCase(HttpMethod.PUT)) {
log.info(" Method is PUT ");
String path = operation.getPath();
String id = getId(path);
for (Map.Entry<String, String> entry : processedBulkIds.entrySet()) {
String key = "bulkId:" + entry.getKey();
if (id.equalsIgnoreCase(key)) {
id = id.replaceAll(key, entry.getValue());
break;
}
}
try {
user = scim2UserService.updateUser(id, user);
// String location = (new
// StringBuilder()).append(domain).append("/Users/").append(personiD).toString();
String location = userRootEndpoint + id;
operation.setLocation(location);
operation.setStatus(String.valueOf(Response.Status.OK.getStatusCode()));
operation.setResponse(user);
// bulkId is only required in POST
if (operation.getBulkId() != null) {
processedBulkIds.put(operation.getBulkId(), user.getId());
}
} catch (EntryPersistenceException ex) {
log.error("Failed to update user", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.NOT_FOUND.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.NOT_FOUND, ErrorScimType.INVALID_VALUE, "Resource " + id + " not found"));
} catch (DuplicateEntryException ex) {
log.error("DuplicateEntryException", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.CONFLICT.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, ex.getMessage()));
} catch (Exception ex) {
log.error("Failed to update user", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, null, INTERNAL_SERVER_ERROR_MESSAGE));
}
} else if (operation.getMethod().equalsIgnoreCase(HttpMethod.DELETE)) {
log.info(" Method is DELETE ");
String path = operation.getPath();
String id = getId(path);
for (Map.Entry<String, String> entry : processedBulkIds.entrySet()) {
String key = "bulkId:" + entry.getKey();
if (id.equalsIgnoreCase(key)) {
id = id.replaceAll(key, entry.getValue());
break;
}
}
try {
scim2UserService.deleteUser(id);
// Location may be omitted on DELETE
operation.setStatus(String.valueOf(Response.Status.OK.getStatusCode()));
operation.setResponse("User " + id + " deleted");
// bulkId is only required in POST
if (operation.getBulkId() != null) {
processedBulkIds.put(operation.getBulkId(), id);
}
} catch (EntryPersistenceException ex) {
log.error("Failed to delete user", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.NOT_FOUND.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.NOT_FOUND, null, "Resource " + id + " not found"));
} catch (Exception ex) {
log.error("Failed to delete user", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, null, INTERNAL_SERVER_ERROR_MESSAGE));
}
}
return operation;
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class BulkWebService method processGroupOperation.
private BulkOperation processGroupOperation(BulkOperation operation, Map<String, String> processedBulkIds) throws Exception {
log.info(" Operation is for Group ");
// Intercept bulkId
Group group = null;
if (operation.getData() != null) {
// Required in a request when
// "method" is "POST", "PUT", or
// "PATCH".
String serializedData = serialize(operation.getData());
for (Map.Entry<String, String> entry : processedBulkIds.entrySet()) {
String key = "bulkId:" + entry.getKey();
serializedData = serializedData.replaceAll(key, entry.getValue());
}
group = deserializeToGroup(serializedData);
}
String groupRootEndpoint = appConfiguration.getBaseEndpoint() + "/scim/v2/Groups/";
if (operation.getMethod().equalsIgnoreCase(HttpMethod.POST)) {
log.info(" Method is POST ");
try {
group = scim2GroupService.createGroup(group);
GluuGroup gluuGroup = groupService.getGroupByDisplayName(group.getDisplayName());
String id = gluuGroup.getInum();
// String location = (new
// StringBuilder()).append(domain).append("/Groups/").append(id).toString();
String location = groupRootEndpoint + id;
operation.setLocation(location);
operation.setStatus(String.valueOf(Response.Status.CREATED.getStatusCode()));
operation.setResponse(group);
// Set aside successfully-processed bulkId
// bulkId is only required in POST
processedBulkIds.put(operation.getBulkId(), group.getId());
} catch (DuplicateEntryException ex) {
log.error("DuplicateEntryException", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.CONFLICT.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, ex.getMessage()));
} catch (Exception ex) {
log.error("Failed to create group", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, null, INTERNAL_SERVER_ERROR_MESSAGE));
}
} else if (operation.getMethod().equalsIgnoreCase(HttpMethod.PUT)) {
log.info(" Method is PUT ");
String path = operation.getPath();
String id = getId(path);
for (Map.Entry<String, String> entry : processedBulkIds.entrySet()) {
String key = "bulkId:" + entry.getKey();
if (id.equalsIgnoreCase(key)) {
id = id.replaceAll(key, entry.getValue());
break;
}
}
try {
group = scim2GroupService.updateGroup(id, group);
// String location = (new
// StringBuilder()).append(domain).append("/Groups/").append(groupiD).toString();
String location = groupRootEndpoint + id;
operation.setLocation(location);
operation.setStatus(String.valueOf(Response.Status.OK.getStatusCode()));
operation.setResponse(group);
// bulkId is only required in POST
if (operation.getBulkId() != null) {
processedBulkIds.put(operation.getBulkId(), group.getId());
}
} catch (EntryPersistenceException ex) {
log.error("Failed to update group", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.NOT_FOUND.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.NOT_FOUND, ErrorScimType.INVALID_VALUE, "Resource " + id + " not found"));
} catch (DuplicateEntryException ex) {
log.error("DuplicateEntryException", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.CONFLICT.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, ex.getMessage()));
} catch (Exception ex) {
log.error("Failed to update group", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, null, INTERNAL_SERVER_ERROR_MESSAGE));
}
} else if (operation.getMethod().equalsIgnoreCase(HttpMethod.DELETE)) {
log.info(" Method is DELETE ");
String path = operation.getPath();
String id = getId(path);
for (Map.Entry<String, String> entry : processedBulkIds.entrySet()) {
String key = "bulkId:" + entry.getKey();
if (id.equalsIgnoreCase(key)) {
id = id.replaceAll(key, entry.getValue());
break;
}
}
try {
scim2GroupService.deleteGroup(id);
// Location may be omitted on DELETE
operation.setStatus(String.valueOf(Response.Status.OK.getStatusCode()));
operation.setResponse("Group " + id + " deleted");
// bulkId is only required in POST
if (operation.getBulkId() != null) {
processedBulkIds.put(operation.getBulkId(), id);
}
} catch (EntryPersistenceException ex) {
log.error("Failed to delete group", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.NOT_FOUND.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.NOT_FOUND, null, "Resource " + id + " not found"));
} catch (Exception ex) {
log.error("Failed to delete group", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, null, INTERNAL_SERVER_ERROR_MESSAGE));
}
}
return operation;
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class GroupWebService method updateGroup.
@Path("{id}")
@PUT
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response updateGroup(@HeaderParam("Authorization") String authorization, @PathParam("id") String id, ScimGroup group) throws Exception {
Response authorizationResponse = processAuthorization(authorization);
if (authorizationResponse != null) {
return authorizationResponse;
}
try {
GluuGroup gluuGroup = groupService.getGroupByInum(id);
if (gluuGroup == null) {
return getErrorResponse("Resource " + id + " not found", Response.Status.NOT_FOUND.getStatusCode());
} else {
// Validate if attempting to update displayName of a different id
if (gluuGroup.getDisplayName() != null) {
GluuGroup groupToFind = new GluuGroup();
groupToFind.setDisplayName(group.getDisplayName());
List<GluuGroup> foundGroups = groupService.findGroups(groupToFind, 2);
if (foundGroups != null && foundGroups.size() > 0) {
for (GluuGroup foundGroup : foundGroups) {
if (foundGroup != null && !foundGroup.getInum().equalsIgnoreCase(gluuGroup.getInum())) {
throw new DuplicateEntryException("Cannot update displayName of a different id: " + group.getDisplayName());
}
}
}
}
}
GluuGroup newGluuGroup = copyUtils.copy(group, gluuGroup, true);
if (group.getMembers().size() > 0) {
serviceUtil.personMembersAdder(newGluuGroup, groupService.getDnForGroup(id));
}
// For custom script: update group
if (externalScimService.isEnabled()) {
externalScimService.executeScimUpdateGroupMethods(newGluuGroup);
}
groupService.updateGroup(newGluuGroup);
log.debug(" group updated ");
ScimGroup newGroup = copyUtils.copy(newGluuGroup, null);
URI location = new URI("/Groups/" + id);
return Response.ok(newGroup).location(location).build();
} catch (EntryPersistenceException ex) {
ex.printStackTrace();
return getErrorResponse("Resource " + id + " not found", Response.Status.NOT_FOUND.getStatusCode());
} catch (DuplicateEntryException ex) {
log.error("Failed to update group", ex);
ex.printStackTrace();
return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
} catch (Exception ex) {
log.error("Failed to update group", ex);
ex.printStackTrace();
return getErrorResponse(INTERNAL_SERVER_ERROR_MESSAGE, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class GroupWebService method createGroup.
@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createGroup(@HeaderParam("Authorization") String authorization, ScimGroup group) throws Exception {
Response authorizationResponse = processAuthorization(authorization);
if (authorizationResponse != null) {
return authorizationResponse;
}
// Return HTTP response with status code 201 Created
log.debug(" copying gluuGroup ");
GluuGroup gluuGroup = copyUtils.copy(group, null, false);
if (gluuGroup == null) {
return getErrorResponse("Failed to create group", Response.Status.BAD_REQUEST.getStatusCode());
}
try {
log.debug(" generating inum ");
String inum = groupService.generateInumForNewGroup();
log.debug(" getting DN ");
String dn = groupService.getDnForGroup(inum);
log.debug(" getting iname ");
String iname = groupService.generateInameForNewGroup(group.getDisplayName().replaceAll(" ", ""));
log.debug(" setting dn ");
gluuGroup.setDn(dn);
log.debug(" setting inum ");
gluuGroup.setInum(inum);
log.debug(" setting iname ");
gluuGroup.setIname(iname);
log.info("group.getMembers().size() : " + group.getMembers().size());
if (group.getMembers().size() > 0) {
serviceUtil.personMembersAdder(gluuGroup, dn);
}
// For custom script: create group
if (externalScimService.isEnabled()) {
externalScimService.executeScimCreateGroupMethods(gluuGroup);
}
log.debug("adding new GluuGroup");
groupService.addGroup(gluuGroup);
ScimGroup newGroup = copyUtils.copy(gluuGroup, null);
String uri = "/Groups/" + newGroup.getId();
return Response.created(URI.create(uri)).entity(newGroup).build();
} catch (DuplicateEntryException ex) {
log.error("Failed to create group", ex);
ex.printStackTrace();
return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
} catch (Exception ex) {
log.error("Failed to create group", ex);
ex.printStackTrace();
return getErrorResponse(INTERNAL_SERVER_ERROR_MESSAGE, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class UserWebService method patchUser.
// PATCH WEBSERVICES
@Path("/patch/{id}")
@PUT
@Consumes({ Constants.MEDIA_TYPE_SCIM_JSON, MediaType.APPLICATION_JSON })
@Produces({ Constants.MEDIA_TYPE_SCIM_JSON + "; charset=utf-8", MediaType.APPLICATION_JSON + "; charset=utf-8" })
@HeaderParam("Accept")
@DefaultValue(Constants.MEDIA_TYPE_SCIM_JSON)
@ApiOperation(value = "patch user", notes = "Update user (https://tools.ietf.org/html/rfc7644#section-3.5.1)", response = User.class)
public Response patchUser(@HeaderParam("Authorization") String authorization, @QueryParam(OxTrustConstants.QUERY_PARAMETER_TEST_MODE_OAUTH2_TOKEN) final String token, @PathParam("id") String id, @ApiParam(value = "User", required = true) ScimPatchUser user, @QueryParam(OxTrustConstants.QUERY_PARAMETER_ATTRIBUTES) final String attributesArray) throws Exception {
Response authorizationResponse;
if (jsonConfigurationService.getOxTrustappConfiguration().isScimTestMode()) {
log.info(" ##### SCIM Test Mode is ACTIVE");
authorizationResponse = processTestModeAuthorization(token);
} else {
authorizationResponse = processAuthorization(authorization);
}
if (authorizationResponse != null) {
return authorizationResponse;
}
try {
User updatedUser = scim2UserService.patchUser(id, user);
// Serialize to JSON
String json = serializeToJson(updatedUser, attributesArray);
URI location = new URI(updatedUser.getMeta().getLocation());
return Response.ok(json).location(location).build();
} catch (EntryPersistenceException ex) {
log.error("Failed to update user", ex);
ex.printStackTrace();
return getErrorResponse(Response.Status.NOT_FOUND, ErrorScimType.INVALID_VALUE, "Resource " + id + " not found");
} catch (DuplicateEntryException ex) {
log.error("DuplicateEntryException", ex);
ex.printStackTrace();
return getErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, ex.getMessage());
} catch (Exception ex) {
log.error("Failed to update user", ex);
ex.printStackTrace();
return getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MESSAGE);
}
}
Aggregations