Search in sources :

Example 1 with ConflictException

use of org.wso2.charon3.core.exceptions.ConflictException in project charon by wso2.

the class GroupResourceManager method create.

/*
     * Create group in the service provider given the submitted payload that contains the SCIM group
     * resource, format and the handler to usermanager.
     *
     * @param scimObjectString - Payload of HTTP request, which contains the SCIM object.
     * @param usermanager
     * @param  attributes
     * @param excludeAttributes
     * @return
     */
@Override
public SCIMResponse create(String scimObjectString, UserManager userManager, String attributes, String excludeAttributes) {
    JSONEncoder encoder = null;
    JSONDecoder decoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder
        decoder = getDecoder();
        // returns core-group schema
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getGroupResourceSchema();
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        // decode the SCIM group object, encoded in the submitted payload.
        Group group = (Group) decoder.decodeResource(scimObjectString, schema, new Group());
        // validate decoded group
        ServerSideValidator.validateCreatedSCIMObject(group, SCIMSchemaDefinitions.SCIM_GROUP_SCHEMA);
        // handover the SCIM User object to the group usermanager provided by the SP.
        Group createdGroup;
        // need to send back the newly created group in the response payload
        createdGroup = ((UserManager) userManager).createGroup(group, requiredAttributes);
        // encode the newly created SCIM group object and add id attribute to Location header.
        String encodedGroup;
        Map<String, String> httpHeaders = new HashMap<String, String>();
        if (createdGroup != null) {
            encodedGroup = encoder.encodeSCIMObject(createdGroup);
            // add location header
            httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.GROUP_ENDPOINT) + "/" + createdGroup.getId());
            httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String message = "Newly created Group resource is null..";
            throw new InternalErrorException(message);
        }
        // put the uri of the Group object in the response header parameter.
        return new SCIMResponse(ResponseCodeConstants.CODE_CREATED, encodedGroup, httpHeaders);
    } catch (InternalErrorException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (ConflictException e) {
        return encodeSCIMException(e);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return encodeSCIMException(e);
    }
}
Also used : Group(org.wso2.charon3.core.objects.Group) HashMap(java.util.HashMap) ConflictException(org.wso2.charon3.core.exceptions.ConflictException) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Example 2 with ConflictException

use of org.wso2.charon3.core.exceptions.ConflictException in project carbon-apimgt by wso2.

the class RestApiUtil method handleConflict.

/**
 * Logs the error, builds a ConflictException with specified details and throws it
 *
 * @param description description of the error
 * @param log Log instance
 * @throws ConflictException
 */
public static void handleConflict(String description, Log log) throws ConflictException {
    ConflictException conflictException = buildConflictException(RestApiConstants.STATUS_CONFLICT_MESSAGE_DEFAULT, description);
    log.error(description);
    throw conflictException;
}
Also used : ConflictException(org.wso2.carbon.apimgt.rest.api.util.exception.ConflictException)

Example 3 with ConflictException

use of org.wso2.charon3.core.exceptions.ConflictException in project carbon-apimgt by wso2.

the class RestApiUtil method handleResourceAlreadyExistsError.

/**
 * Logs the error, builds a ConflictException with specified details and throws it
 *
 * @param description description of the error
 * @param log Log instance
 * @throws ConflictException
 */
public static void handleResourceAlreadyExistsError(String description, Log log) throws ConflictException {
    ConflictException conflictException = buildConflictException(RestApiConstants.STATUS_CONFLICT_MESSAGE_RESOURCE_ALREADY_EXISTS, description);
    log.error(description);
    throw conflictException;
}
Also used : ConflictException(org.wso2.carbon.apimgt.rest.api.util.exception.ConflictException)

Example 4 with ConflictException

use of org.wso2.charon3.core.exceptions.ConflictException in project carbon-apimgt by wso2.

the class RestApiUtil method handleResourceAlreadyExistsError.

/**
 * Logs the error, builds a ConflictException with specified details and throws it
 *
 * @param description description of the error
 * @param t Throwable instance
 * @param log Log instance
 * @throws ConflictException
 */
public static void handleResourceAlreadyExistsError(String description, Throwable t, Log log) throws ConflictException {
    ConflictException conflictException = buildConflictException(RestApiConstants.STATUS_CONFLICT_MESSAGE_RESOURCE_ALREADY_EXISTS, description);
    log.error(description, t);
    throw conflictException;
}
Also used : ConflictException(org.wso2.carbon.apimgt.rest.api.util.exception.ConflictException)

Example 5 with ConflictException

use of org.wso2.charon3.core.exceptions.ConflictException in project charon by wso2.

the class MeResourceManager method create.

@Override
public SCIMResponse create(String scimObjectString, UserManager userManager, String attributes, String excludeAttributes) {
    JSONEncoder encoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder
        JSONDecoder decoder = getDecoder();
        // obtain the schema corresponding to user
        // unless configured returns core-user schema or else returns extended user schema)
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getUserResourceSchema();
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        // decode the SCIM User object, encoded in the submitted payload.
        User user = (User) decoder.decodeResource(scimObjectString, schema, new User());
        // validate the created user.
        ServerSideValidator.validateCreatedSCIMObject(user, schema);
        User createdUser;
        if (userManager != null) {
            /*handover the SCIM User object to the user usermanager provided by the SP.
            need to send back the newly created user in the response payload*/
            createdUser = userManager.createMe(user, requiredAttributes);
        } else {
            String error = "Provided user manager handler is null.";
            // throw internal server error.
            throw new InternalErrorException(error);
        }
        // encode the newly created SCIM user object and add id attribute to Location header.
        String encodedUser;
        Map<String, String> responseHeaders = new HashMap<String, String>();
        if (createdUser != null) {
            // create a deep copy of the user object since we are going to change it.
            User copiedUser = (User) CopyUtil.deepCopy(createdUser);
            // need to remove password before returning
            ServerSideValidator.validateReturnedAttributes(copiedUser, attributes, excludeAttributes);
            encodedUser = encoder.encodeSCIMObject(copiedUser);
            // add location header
            responseHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.USER_ENDPOINT) + "/" + createdUser.getId());
            responseHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String error = "Newly created User resource is null.";
            throw new InternalErrorException(error);
        }
        // put the uri of the User object in the response header parameter.
        return new SCIMResponse(ResponseCodeConstants.CODE_CREATED, encodedUser, responseHeaders);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (ConflictException e) {
        return encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return encodeSCIMException(e);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    }
}
Also used : User(org.wso2.charon3.core.objects.User) HashMap(java.util.HashMap) ConflictException(org.wso2.charon3.core.exceptions.ConflictException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Aggregations

ConflictException (org.wso2.carbon.apimgt.rest.api.util.exception.ConflictException)5 HashMap (java.util.HashMap)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)3 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)3 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)3 CharonException (org.wso2.charon3.core.exceptions.CharonException)3 ConflictException (org.wso2.charon3.core.exceptions.ConflictException)3 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)3 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)3 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)3 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)3 Log (org.apache.commons.logging.Log)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 APIMgtAuthorizationFailedException (org.wso2.carbon.apimgt.api.APIMgtAuthorizationFailedException)2 APIMgtResourceAlreadyExistsException (org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)2 ApplicationNameWhiteSpaceValidationException (org.wso2.carbon.apimgt.api.ApplicationNameWhiteSpaceValidationException)2 ApplicationNameWithInvalidCharactersException (org.wso2.carbon.apimgt.api.ApplicationNameWithInvalidCharactersException)2