Search in sources :

Example 76 with Group

use of org.wso2.charon3.core.objects.Group 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 77 with Group

use of org.wso2.charon3.core.objects.Group in project charon by wso2.

the class GroupResourceManager method updateWithPATCH.

/*
     * method which corresponds to HTTP PATCH - patch the group
     * @param existingId
     * @param scimObjectString
     * @param usermanager
     * @param attributes
     * @param excludeAttributes
     * @return
     */
public SCIMResponse updateWithPATCH(String existingId, String scimObjectString, UserManager userManager, String attributes, String excludeAttributes) {
    try {
        if (userManager == null) {
            String error = "Provided user manager handler is null.";
            throw new InternalErrorException(error);
        }
        // obtain the json decoder.
        JSONDecoder decoder = getDecoder();
        // decode the SCIM User object, encoded in the submitted payload.
        List<PatchOperation> opList = decoder.decodeRequest(scimObjectString);
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getGroupResourceSchema();
        // get the group from the user core
        Group oldGroup = userManager.getGroup(existingId, ResourceManagerUtil.getAllAttributeURIs(schema));
        if (oldGroup == null) {
            throw new NotFoundException("No group with the id : " + existingId + " in the user store.");
        }
        // make a copy of the original group
        Group copyOfOldGroup = (Group) CopyUtil.deepCopy(oldGroup);
        // make another copy of original group.
        // this will be used to restore to the original condition if failure occurs.
        Group originalGroup = (Group) CopyUtil.deepCopy(copyOfOldGroup);
        Group newGroup = null;
        for (PatchOperation operation : opList) {
            if (operation.getOperation().equals(SCIMConstants.OperationalConstants.ADD)) {
                if (newGroup == null) {
                    newGroup = (Group) PatchOperationUtil.doPatchAdd(operation, getDecoder(), oldGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                } else {
                    newGroup = (Group) PatchOperationUtil.doPatchAdd(operation, getDecoder(), newGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                }
            } else if (operation.getOperation().equals(SCIMConstants.OperationalConstants.REMOVE)) {
                if (newGroup == null) {
                    newGroup = (Group) PatchOperationUtil.doPatchRemove(operation, oldGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                } else {
                    newGroup = (Group) PatchOperationUtil.doPatchRemove(operation, newGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                }
            } else if (operation.getOperation().equals(SCIMConstants.OperationalConstants.REPLACE)) {
                if (newGroup == null) {
                    newGroup = (Group) PatchOperationUtil.doPatchReplace(operation, getDecoder(), oldGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                } else {
                    newGroup = (Group) PatchOperationUtil.doPatchReplace(operation, getDecoder(), newGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                }
            } else {
                throw new BadRequestException("Unknown operation.", ResponseCodeConstants.INVALID_SYNTAX);
            }
        }
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        Group validatedGroup = (Group) ServerSideValidator.validateUpdatedSCIMObject(originalGroup, newGroup, schema);
        newGroup = userManager.updateGroup(originalGroup, validatedGroup, 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 (newGroup != null) {
            // create a deep copy of the group object since we are going to change it.
            Group copiedGroup = (Group) CopyUtil.deepCopy(newGroup);
            // validate before return.
            ServerSideValidator.validateReturnedAttributes(copiedGroup, attributes, excludeAttributes);
            encodedGroup = getEncoder().encodeSCIMObject(copiedGroup);
            // add location header
            httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.USER_ENDPOINT) + "/" + newGroup.getId());
            httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String error = "Updated group resource is null.";
            throw new CharonException(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 AbstractResourceManager.encodeSCIMException(e);
    } catch (BadRequestException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (CharonException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (RuntimeException e) {
        CharonException e1 = new CharonException("Error in performing the patch operation on group resource.", e);
        return AbstractResourceManager.encodeSCIMException(e1);
    }
}
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) PatchOperation(org.wso2.charon3.core.utils.codeutils.PatchOperation) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Example 78 with Group

use of org.wso2.charon3.core.objects.Group 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)

Example 79 with Group

use of org.wso2.charon3.core.objects.Group in project charon by wso2.

the class ResourceTypeResourceManager method buildCombinedResourceType.

/*
     * This combines the user and group resource type AbstractSCIMObjects and build a
     * one root AbstractSCIMObjects
     *
     * @param userObject
     * @param groupObject
     * @return
     * @throws CharonException
     */
private AbstractSCIMObject buildCombinedResourceType(AbstractSCIMObject userObject, AbstractSCIMObject groupObject) throws CharonException {
    Map<String, Attribute> userObjectAttributeList = userObject.getAttributeList();
    Map<String, Attribute> groupObjectAttributeList = groupObject.getAttributeList();
    AbstractSCIMObject rootObject = new AbstractSCIMObject();
    MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(SCIMConstants.ResourceTypeSchemaConstants.RESOURCE_TYPE);
    ComplexAttribute userComplexAttribute = new ComplexAttribute();
    for (Attribute attribute : userObjectAttributeList.values()) {
        userComplexAttribute.setSubAttribute(attribute);
    }
    multiValuedAttribute.setAttributeValue(userComplexAttribute);
    ComplexAttribute groupComplexAttribute = new ComplexAttribute();
    for (Attribute attribute : groupObjectAttributeList.values()) {
        groupComplexAttribute.setSubAttribute(attribute);
    }
    multiValuedAttribute.setAttributeValue(groupComplexAttribute);
    rootObject.setAttribute(multiValuedAttribute);
    rootObject.setSchema(SCIMConstants.RESOURCE_TYPE_SCHEMA_URI);
    return rootObject;
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 80 with Group

use of org.wso2.charon3.core.objects.Group in project charon by wso2.

the class JSONDecoder method setRequestData.

private void setRequestData(String requestType, String requestMethod, String requestVersion, JSONObject member, List<BulkRequestContent> usersEndpointOperationList, List<BulkRequestContent> groupsEndpointOperationList) {
    // create user request list
    if (requestType.contains(SCIMConstants.USER_ENDPOINT)) {
        BulkRequestContent newRequestData = getBulkRequestContent(member, requestMethod, requestType, requestVersion);
        usersEndpointOperationList.add(newRequestData);
    }
    // create group request list
    if (requestType.contains(SCIMConstants.GROUP_ENDPOINT)) {
        BulkRequestContent newRequestData = getBulkRequestContent(member, requestMethod, requestType, requestVersion);
        groupsEndpointOperationList.add(newRequestData);
    }
}
Also used : BulkRequestContent(org.wso2.charon3.core.objects.bulk.BulkRequestContent)

Aggregations

Test (org.testng.annotations.Test)128 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)97 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)94 Event (org.wso2.siddhi.core.event.Event)87 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)78 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)51 Query (org.wso2.siddhi.query.api.execution.query.Query)17 ArrayList (java.util.ArrayList)16 CharonException (org.wso2.charon3.core.exceptions.CharonException)15 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)13 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)12 HashMap (java.util.HashMap)8 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)8 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)8 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)7 UserManager (org.wso2.charon3.core.extensions.UserManager)7 Group (org.wso2.charon3.core.objects.Group)7 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 Produces (javax.ws.rs.Produces)6