Search in sources :

Example 36 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.

the class JSONDecoder method decodeSearchRequestBody.

/*
     * decode the raw string and create a search object
     * @param scimResourceString
     * @return
     * @throws BadRequestException
     */
public SearchRequest decodeSearchRequestBody(String scimResourceString, SCIMResourceTypeSchema schema) throws BadRequestException {
    FilterTreeManager filterTreeManager = null;
    Node rootNode = null;
    // decode the string and create search object
    try {
        JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
        SearchRequest searchRequest = new SearchRequest();
        ArrayList<String> attributes = new ArrayList<>();
        ArrayList<String> excludedAttributes = new ArrayList<>();
        JSONArray attributesValues = (JSONArray) decodedJsonObj.opt(SCIMConstants.OperationalConstants.ATTRIBUTES);
        JSONArray excludedAttributesValues = (JSONArray) decodedJsonObj.opt(SCIMConstants.OperationalConstants.EXCLUDED_ATTRIBUTES);
        JSONArray schemas = (JSONArray) decodedJsonObj.opt(SCIMConstants.CommonSchemaConstants.SCHEMAS);
        if (schemas.length() != 1) {
            throw new BadRequestException("Schema is invalid", ResponseCodeConstants.INVALID_VALUE);
        }
        if (attributesValues != null) {
            for (int i = 0; i < attributesValues.length(); i++) {
                attributes.add((String) attributesValues.get(i));
            }
        }
        if (excludedAttributesValues != null) {
            for (int i = 0; i < excludedAttributesValues.length(); i++) {
                excludedAttributes.add((String) excludedAttributesValues.get(i));
            }
        }
        if (decodedJsonObj.opt(SCIMConstants.OperationalConstants.FILTER) != null) {
            filterTreeManager = new FilterTreeManager((String) decodedJsonObj.opt(SCIMConstants.OperationalConstants.FILTER), schema);
            rootNode = filterTreeManager.buildTree();
        }
        searchRequest.setAttributes(attributes);
        searchRequest.setExcludedAttributes(excludedAttributes);
        searchRequest.setSchema((String) schemas.get(0));
        searchRequest.setCountStr(decodedJsonObj.optString(SCIMConstants.OperationalConstants.COUNT));
        searchRequest.setStartIndexStr(decodedJsonObj.optString(SCIMConstants.OperationalConstants.START_INDEX));
        searchRequest.setFilter(rootNode);
        if (!decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_BY).equals("")) {
            searchRequest.setSortBy(decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_BY));
        }
        if (!decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_ORDER).equals("")) {
            searchRequest.setSortOder(decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_ORDER));
        }
        return searchRequest;
    } catch (JSONException | IOException e) {
        logger.error("Error while decoding the resource string");
        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
    }
}
Also used : SearchRequest(org.wso2.charon3.core.utils.codeutils.SearchRequest) Node(org.wso2.charon3.core.utils.codeutils.Node) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) FilterTreeManager(org.wso2.charon3.core.utils.codeutils.FilterTreeManager) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException)

Example 37 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.

the class JSONDecoder method buildPrimitiveMultiValuedAttribute.

/*
     * Return a primitive type multi valued attribute with the user defined value included and necessary
     * attribute characteristics set
     *
     * @param attributeSchema - Attribute schema
     * @param attributeValues - values for the attribute
     * @return MultiValuedAttribute
     */
public MultiValuedAttribute buildPrimitiveMultiValuedAttribute(AttributeSchema attributeSchema, JSONArray attributeValues) throws CharonException, BadRequestException {
    try {
        MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
        List<Object> primitiveValues = new ArrayList<Object>();
        // iterate through JSONArray and create the list of string values.
        for (int i = 0; i < attributeValues.length(); i++) {
            Object attributeValue = attributeValues.get(i);
            if (attributeValue instanceof String || attributeValue instanceof Boolean || attributeValue instanceof Integer || attributeValue == null) {
                // If an attribute is passed without a value, no need to save it.
                if (attributeValue == null) {
                    continue;
                }
                primitiveValues.add(attributeValue);
            } else {
                String error = "Unknown JSON representation for the MultiValued attribute " + attributeSchema.getName() + " which has data type as " + attributeSchema.getType();
                throw new BadRequestException(error, ResponseCodeConstants.INVALID_SYNTAX);
            }
        }
        multiValuedAttribute.setAttributePrimitiveValues(primitiveValues);
        return (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
    } catch (JSONException e) {
        String error = "Error in accessing JSON value of multivalued attribute";
        throw new CharonException(error, e);
    }
}
Also used : ArrayList(java.util.ArrayList) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONException(org.json.JSONException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject) SCIMObject(org.wso2.charon3.core.objects.SCIMObject) CharonException(org.wso2.charon3.core.exceptions.CharonException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 38 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.

the class AbstractSCIMObject method setResourceType.

/*
     * set the resourceType of the meta attribute
     *
     * @param resourceType
     */
public void setResourceType(String resourceType) throws BadRequestException, CharonException {
    // create the resourceType attribute as defined in schema.
    SimpleAttribute resourceTypeAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.RESOURCE_TYPE, new SimpleAttribute(SCIMConstants.CommonSchemaConstants.RESOURCE_TYPE, resourceType));
    // check meta complex attribute already exist.
    if (getMetaAttribute() != null) {
        ComplexAttribute metaAttribute = getMetaAttribute();
        // check version attribute already exist
        if (metaAttribute.isSubAttributeExist(resourceTypeAttribute.getName())) {
            String error = "Read only attribute is tried to modify";
            throw new CharonException(error);
        } else {
            metaAttribute.setSubAttribute(resourceTypeAttribute);
        }
    } else {
        // create meta attribute and set the sub attribute.
        createMetaAttribute();
        getMetaAttribute().setSubAttribute(resourceTypeAttribute);
    }
}
Also used : SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) CharonException(org.wso2.charon3.core.exceptions.CharonException)

Example 39 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.

the class Group method setMemberCommon.

/*
     * set member to the group
     * @param userId
     * @param userName
     * @return
     * @throws BadRequestException
     * @throws CharonException
     */
private ComplexAttribute setMemberCommon(String userId, String userName) throws BadRequestException, CharonException {
    ComplexAttribute complexAttribute = new ComplexAttribute();
    complexAttribute.setName(SCIMConstants.GroupSchemaConstants.MEMBERS + "_" + userId + SCIMConstants.DEFAULT);
    SimpleAttribute valueSimpleAttribute = new SimpleAttribute(SCIMConstants.CommonSchemaConstants.VALUE, userId);
    DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.VALUE, valueSimpleAttribute);
    SimpleAttribute displaySimpleAttribute = new SimpleAttribute(SCIMConstants.GroupSchemaConstants.DISPLAY, userName);
    DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.DISPLAY, displaySimpleAttribute);
    complexAttribute.setSubAttribute(valueSimpleAttribute);
    complexAttribute.setSubAttribute(displaySimpleAttribute);
    DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.MEMBERS, complexAttribute);
    return complexAttribute;
}
Also used : SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute)

Example 40 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.

the class Group method setMember.

/*
     * set a member to the group
     * @param userId
     * @param userName
     * @throws BadRequestException
     * @throws CharonException
     */
public void setMember(String userId, String userName) throws BadRequestException, CharonException {
    if (this.isAttributeExist(SCIMConstants.GroupSchemaConstants.MEMBERS)) {
        MultiValuedAttribute members = (MultiValuedAttribute) this.attributeList.get(SCIMConstants.GroupSchemaConstants.MEMBERS);
        ComplexAttribute complexAttribute = setMemberCommon(userId, userName);
        members.setAttributeValue(complexAttribute);
    } else {
        MultiValuedAttribute members = new MultiValuedAttribute(SCIMConstants.GroupSchemaConstants.MEMBERS);
        DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.MEMBERS, members);
        ComplexAttribute complexAttribute = setMemberCommon(userId, userName);
        members.setAttributeValue(complexAttribute);
        this.setAttribute(members);
    }
}
Also used : ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)63 CharonException (org.wso2.charon3.core.exceptions.CharonException)31 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)30 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)27 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)23 HashMap (java.util.HashMap)22 Attribute (org.wso2.charon3.core.attributes.Attribute)20 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)19 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)19 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 JSONObject (org.json.JSONObject)16 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)16 JSONException (org.json.JSONException)15 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)14 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)14 User (org.wso2.charon3.core.objects.User)12 JSONArray (org.json.JSONArray)11 BadRequestException (org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException)10