use of org.gluu.oxtrust.exception.PersonRequiredFieldsException 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.oxtrust.exception.PersonRequiredFieldsException in project oxTrust by GluuFederation.
the class UserWebService method createUser.
@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createUser(@HeaderParam("Authorization") String authorization, ScimPerson person) throws Exception {
Response authorizationResponse = processAuthorization(authorization);
if (authorizationResponse != null) {
return authorizationResponse;
}
try {
log.debug(" copying gluuperson ");
GluuCustomPerson gluuPerson = copyUtils.copy(person, null, false);
if (gluuPerson == null) {
return getErrorResponse("Failed to create user", Response.Status.BAD_REQUEST.getStatusCode());
}
log.debug(" generating inum ");
// inumService.generateInums(Configuration.INUM_TYPE_PEOPLE_SLUG);
String inum = personService.generateInumForNewPerson();
// //personService.generateInumForNewPerson();
log.debug(" getting DN ");
String dn = personService.getDnForPerson(inum);
log.debug(" getting iname ");
String iname = personService.generateInameForNewPerson(person.getUserName());
log.debug(" setting dn ");
gluuPerson.setDn(dn);
log.debug(" setting inum ");
gluuPerson.setInum(inum);
log.debug(" setting iname ");
gluuPerson.setIname(iname);
log.debug(" setting commonName ");
gluuPerson.setCommonName(gluuPerson.getGivenName() + " " + gluuPerson.getSurname());
log.info("gluuPerson.getMemberOf().size() : " + gluuPerson.getMemberOf().size());
if (person.getGroups().size() > 0) {
log.info(" jumping to groupMembersAdder ");
log.info("gluuPerson.getDn() : " + gluuPerson.getDn());
serviceUtil.groupMembersAdder(gluuPerson, gluuPerson.getDn());
}
// Sync email, forward ("oxTrustEmail" -> "mail")
gluuPerson = ServiceUtil.syncEmailForward(gluuPerson, false);
// For custom script: create user
if (externalScimService.isEnabled()) {
externalScimService.executeScimCreateUserMethods(gluuPerson);
}
log.debug("adding new GluuPerson");
personService.addPerson(gluuPerson);
ScimPerson newPerson = copyUtils.copy(gluuPerson, null);
String uri = "/Users/" + newPerson.getId();
return Response.created(URI.create(uri)).entity(newPerson).build();
} catch (DuplicateEntryException ex) {
log.error("Failed to create user", ex);
ex.printStackTrace();
return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
} catch (PersonRequiredFieldsException ex) {
log.error("PersonRequiredFieldsException: ", ex);
return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
} catch (Exception ex) {
log.error("Failed to create user", ex);
ex.printStackTrace();
return getErrorResponse(ex.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
}
use of org.gluu.oxtrust.exception.PersonRequiredFieldsException in project oxTrust by GluuFederation.
the class UserWebService method createUser.
@POST
@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 = "Create user", notes = "Create user (https://tools.ietf.org/html/rfc7644#section-3.3)", response = User.class)
public Response createUser(@HeaderParam("Authorization") String authorization, @QueryParam(OxTrustConstants.QUERY_PARAMETER_TEST_MODE_OAUTH2_TOKEN) final String token, @ApiParam(value = "User", required = true) User 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 createdUser = scim2UserService.createUser(user);
// Serialize to JSON
String json = serializeToJson(createdUser, attributesArray);
URI location = new URI(createdUser.getMeta().getLocation());
// Return HTTP response with status code 201 Created
return Response.created(location).entity(json).build();
} catch (DuplicateEntryException ex) {
log.error("DuplicateEntryException", ex);
ex.printStackTrace();
return getErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, ex.getMessage());
} catch (PersonRequiredFieldsException ex) {
log.error("PersonRequiredFieldsException: ", ex);
return getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_VALUE, ex.getMessage());
} catch (Exception ex) {
log.error("Failed to create user", ex.getMessage());
return getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MESSAGE);
}
}
Aggregations