Search in sources :

Example 1 with JSONEncoder

use of org.wso2.charon3.core.encoder.JSONEncoder 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 JSONEncoder

use of org.wso2.charon3.core.encoder.JSONEncoder in project charon by wso2.

the class GroupResourceManager method get.

/*
     * Retrieves a group resource given an unique group id. Mapped to HTTP GET request.
     *
     * @param id          - unique resource id
     * @param usermanager
     * @param attributes
     * @param excludeAttributes
     * @return SCIM response to be returned.
     */
@Override
public SCIMResponse get(String id, UserManager userManager, String attributes, String excludeAttributes) {
    JSONEncoder encoder = null;
    try {
        // obtain the correct encoder according to the format requested.
        encoder = getEncoder();
        // 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);
        // API user should pass a usermanager usermanager to GroupResourceEndpoint.
        // retrieve the group from the provided usermanager.
        Group group = ((UserManager) userManager).getGroup(id, requiredAttributes);
        // if group not found, return an error in relevant format.
        if (group == null) {
            String message = "Group not found in the user store.";
            throw new NotFoundException(message);
        }
        ServerSideValidator.validateRetrievedSCIMObjectInList(group, schema, attributes, excludeAttributes);
        // convert the group into specific format.
        String encodedGroup = encoder.encodeSCIMObject(group);
        // if there are any http headers to be added in the response header.
        Map<String, String> httpHeaders = new HashMap<String, String>();
        httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedGroup, httpHeaders);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return encodeSCIMException(e);
    }
}
Also used : Group(org.wso2.charon3.core.objects.Group) HashMap(java.util.HashMap) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) UserManager(org.wso2.charon3.core.extensions.UserManager) 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 3 with JSONEncoder

use of org.wso2.charon3.core.encoder.JSONEncoder in project charon by wso2.

the class GroupResourceManager method updateWithPUT.

/*
     * method which corresponds to HTTP PUT - delete the group
     * @param existingId
     * @param scimObjectString
     * @param usermanager
     * @param attributes
     * @param excludeAttributes
     * @return
     */
@Override
public SCIMResponse updateWithPUT(String existingId, String scimObjectString, UserManager userManager, String attributes, String excludeAttributes) {
    // needs to validate the incoming object. eg: id can not be set by the consumer.
    JSONEncoder encoder = null;
    JSONDecoder decoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder.
        decoder = getDecoder();
        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 User object, encoded in the submitted payload.
        Group group = (Group) decoder.decodeResource(scimObjectString, schema, new Group());
        Group updatedGroup = null;
        if (userManager != null) {
            // retrieve the old object
            Group oldGroup = userManager.getGroup(existingId, ResourceManagerUtil.getAllAttributeURIs(schema));
            if (oldGroup != null) {
                Group newGroup = (Group) ServerSideValidator.validateUpdatedSCIMObject(oldGroup, group, schema);
                updatedGroup = userManager.updateGroup(oldGroup, newGroup, requiredAttributes);
            } else {
                String error = "No user exists with the given id: " + existingId;
                throw new NotFoundException(error);
            }
        } else {
            String error = "Provided user manager handler is null.";
            throw new InternalErrorException(error);
        }
        // encode the newly created SCIM user object and add id attribute to Location header.
        String encodedGroup;
        Map<String, String> httpHeaders = new HashMap<String, String>();
        if (updatedGroup != null) {
            // create a deep copy of the user object since we are going to change it.
            Group copiedGroup = (Group) CopyUtil.deepCopy(updatedGroup);
            // need to remove password before returning
            ServerSideValidator.validateReturnedAttributes(copiedGroup, attributes, excludeAttributes);
            encodedGroup = encoder.encodeSCIMObject(copiedGroup);
            // add location header
            httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.GROUP_ENDPOINT) + "/" + updatedGroup.getId());
            httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String error = "Updated Group resource is null.";
            throw new InternalErrorException(error);
        }
        // put the uri of the User object in the response header parameter.
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedGroup, httpHeaders);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return encodeSCIMException(e);
    }
}
Also used : Group(org.wso2.charon3.core.objects.Group) HashMap(java.util.HashMap) 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 4 with JSONEncoder

use of org.wso2.charon3.core.encoder.JSONEncoder in project charon by wso2.

the class MeResourceManager method get.

@Override
public SCIMResponse get(String userName, UserManager userManager, String attributes, String excludeAttributes) {
    JSONEncoder encoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // 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);
        /*API user should pass a usermanager impl to UserResourceEndpoint.
            retrieve the user from the provided UM handler.*/
        User user = ((UserManager) userManager).getMe(userName, requiredAttributes);
        // if user not found, return an error in relevant format.
        if (user == null) {
            String error = "User not found in the user store.";
            throw new NotFoundException(error);
        }
        // perform service provider side validation.
        ServerSideValidator.validateRetrievedSCIMObject(user, schema, attributes, excludeAttributes);
        // convert the user into requested format.
        String encodedUser = encoder.encodeSCIMObject(user);
        // if there are any http headers to be added in the response header.
        Map<String, String> responseHeaders = new HashMap<String, String>();
        responseHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        responseHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.USER_ENDPOINT) + "/" + user.getId());
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedUser, responseHeaders);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    }
}
Also used : User(org.wso2.charon3.core.objects.User) HashMap(java.util.HashMap) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) UserManager(org.wso2.charon3.core.extensions.UserManager) 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 5 with JSONEncoder

use of org.wso2.charon3.core.encoder.JSONEncoder in project charon by wso2.

the class ResourceTypeResourceManager method getResourceType.

/*
     * return RESOURCE_TYPE schema
     *
     * @return
     */
private SCIMResponse getResourceType() {
    JSONEncoder encoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder
        JSONDecoder decoder = getDecoder();
        // get the service provider config schema
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getResourceTypeResourceSchema();
        // create a string in json format for user resource type with relevant values
        String scimUserObjectString = encoder.buildUserResourceTypeJsonBody();
        // create a string in json format for group resource type with relevant values
        String scimGroupObjectString = encoder.buildGroupResourceTypeJsonBody();
        // build the user abstract scim object
        AbstractSCIMObject userResourceTypeObject = (AbstractSCIMObject) decoder.decodeResource(scimUserObjectString, schema, new AbstractSCIMObject());
        // add meta data
        userResourceTypeObject = ServerSideValidator.validateResourceTypeSCIMObject(userResourceTypeObject);
        // build the group abstract scim object
        AbstractSCIMObject groupResourceTypeObject = (AbstractSCIMObject) decoder.decodeResource(scimGroupObjectString, schema, new AbstractSCIMObject());
        // add meta data
        groupResourceTypeObject = ServerSideValidator.validateResourceTypeSCIMObject(groupResourceTypeObject);
        // build the root abstract scim object
        AbstractSCIMObject resourceTypeObject = buildCombinedResourceType(userResourceTypeObject, groupResourceTypeObject);
        // encode the newly created SCIM Resource Type object.
        String encodedObject;
        Map<String, String> responseHeaders = new HashMap<String, String>();
        if (resourceTypeObject != null) {
            // create a deep copy of the resource type object since we are going to change it.
            AbstractSCIMObject copiedObject = (AbstractSCIMObject) CopyUtil.deepCopy(resourceTypeObject);
            encodedObject = encoder.encodeSCIMObject(copiedObject);
            // add location header
            responseHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.RESOURCE_TYPE_ENDPOINT));
            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 resource type object in the response header parameter.
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedObject, responseHeaders);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return encodeSCIMException(e);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (JSONException e) {
        return null;
    }
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) HashMap(java.util.HashMap) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) JSONException(org.json.JSONException) 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

HashMap (java.util.HashMap)15 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)15 CharonException (org.wso2.charon3.core.exceptions.CharonException)15 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)15 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)15 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)15 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)12 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)10 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)9 User (org.wso2.charon3.core.objects.User)6 ListedResource (org.wso2.charon3.core.objects.ListedResource)4 ConflictException (org.wso2.charon3.core.exceptions.ConflictException)3 UserManager (org.wso2.charon3.core.extensions.UserManager)3 Group (org.wso2.charon3.core.objects.Group)3 IOException (java.io.IOException)2 JSONException (org.json.JSONException)2 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)2 FilterTreeManager (org.wso2.charon3.core.utils.codeutils.FilterTreeManager)2 Node (org.wso2.charon3.core.utils.codeutils.Node)2