Search in sources :

Example 1 with PersonRequiredFieldsException

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;
}
Also used : GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) User(org.gluu.oxtrust.model.scim2.User) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) PersonRequiredFieldsException(org.gluu.oxtrust.exception.PersonRequiredFieldsException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) PersonRequiredFieldsException(org.gluu.oxtrust.exception.PersonRequiredFieldsException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException)

Example 2 with PersonRequiredFieldsException

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());
    }
}
Also used : VirtualListViewResponse(org.xdi.ldap.model.VirtualListViewResponse) Response(javax.ws.rs.core.Response) GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) ScimPerson(org.gluu.oxtrust.model.scim.ScimPerson) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) PersonRequiredFieldsException(org.gluu.oxtrust.exception.PersonRequiredFieldsException) PersonRequiredFieldsException(org.gluu.oxtrust.exception.PersonRequiredFieldsException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 3 with PersonRequiredFieldsException

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);
    }
}
Also used : ListResponse(org.gluu.oxtrust.model.scim2.ListResponse) Response(javax.ws.rs.core.Response) VirtualListViewResponse(org.xdi.ldap.model.VirtualListViewResponse) User(org.gluu.oxtrust.model.scim2.User) ScimPatchUser(org.gluu.oxtrust.model.scim2.ScimPatchUser) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) PersonRequiredFieldsException(org.gluu.oxtrust.exception.PersonRequiredFieldsException) URI(java.net.URI) PersonRequiredFieldsException(org.gluu.oxtrust.exception.PersonRequiredFieldsException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(com.wordnik.swagger.annotations.ApiOperation)

Aggregations

PersonRequiredFieldsException (org.gluu.oxtrust.exception.PersonRequiredFieldsException)3 DuplicateEntryException (org.gluu.site.ldap.exception.DuplicateEntryException)3 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)3 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2 Response (javax.ws.rs.core.Response)2 GluuCustomPerson (org.gluu.oxtrust.model.GluuCustomPerson)2 User (org.gluu.oxtrust.model.scim2.User)2 VirtualListViewResponse (org.xdi.ldap.model.VirtualListViewResponse)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 URI (java.net.URI)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 DefaultValue (javax.ws.rs.DefaultValue)1 HeaderParam (javax.ws.rs.HeaderParam)1 ScimPerson (org.gluu.oxtrust.model.scim.ScimPerson)1 ListResponse (org.gluu.oxtrust.model.scim2.ListResponse)1 ScimPatchUser (org.gluu.oxtrust.model.scim2.ScimPatchUser)1