Search in sources :

Example 51 with BadRequestException

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

the class JSONDecoder method decodeResource.

/**
 * Decode the resource string sent in the SCIM request payload.
 *
 * @param scimResourceString - json encoded string of user info
 * @param resourceSchema     - SCIM defined user schema
 * @param scimObject         - a container holding the attributes and schema list
 * @return SCIMObject
 */
public SCIMObject decodeResource(String scimResourceString, ResourceTypeSchema resourceSchema, AbstractSCIMObject scimObject) throws BadRequestException, CharonException, InternalErrorException {
    try {
        // decode the string into json representation
        JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
        // get the attribute schemas list from the schema that defines the given resource
        List<AttributeSchema> attributeSchemas = resourceSchema.getAttributesList();
        // set the schemas in scimobject
        for (int i = 0; i < resourceSchema.getSchemasList().size(); i++) {
            scimObject.setSchema(resourceSchema.getSchemasList().get(i));
        }
        // iterate through the schema and extract the attributes.
        for (AttributeSchema attributeSchema : attributeSchemas) {
            // obtain the user defined value for given key- attribute schema name
            Object attributeValObj = decodedJsonObj.opt(attributeSchema.getName());
            if (attributeValObj == null) {
                // user may define the attribute by its fully qualified uri
                attributeValObj = decodedJsonObj.opt(attributeSchema.getURI());
            }
            SCIMDefinitions.DataType attributeSchemaDataType = attributeSchema.getType();
            if (attributeSchemaDataType.equals(STRING) || attributeSchemaDataType.equals(BINARY) || attributeSchemaDataType.equals(BOOLEAN) || attributeSchemaDataType.equals(DATE_TIME) || attributeSchemaDataType.equals(DECIMAL) || attributeSchemaDataType.equals(INTEGER) || attributeSchemaDataType.equals(REFERENCE)) {
                if (!attributeSchema.getMultiValued()) {
                    if (attributeValObj instanceof String || attributeValObj instanceof Boolean || attributeValObj instanceof Integer || attributeValObj == null) {
                        // If an attribute is passed without a value, no need to save it.
                        if (attributeValObj == null) {
                            continue;
                        }
                        // if the corresponding schema data type is String/Boolean/Binary/Decimal/Integer/DataTime
                        // or Reference, it is a SimpleAttribute.
                        scimObject.setAttribute(buildSimpleAttribute(attributeSchema, attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the simple attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                } else {
                    if (attributeValObj instanceof JSONArray || attributeValObj == null) {
                        // If an attribute is passed without a value, no need to save it.
                        if (attributeValObj == null) {
                            continue;
                        }
                        scimObject.setAttribute(buildPrimitiveMultiValuedAttribute(attributeSchema, (JSONArray) attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the primitive multivalued attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                }
            } else if (attributeSchemaDataType.equals(COMPLEX)) {
                if (attributeSchema.getMultiValued() == true) {
                    if (attributeValObj instanceof JSONArray || attributeValObj == null) {
                        if (attributeValObj == null) {
                            continue;
                        }
                        // if the corresponding json value object is JSONArray, it is a MultiValuedAttribute.
                        scimObject.setAttribute(buildComplexMultiValuedAttribute(attributeSchema, (JSONArray) attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the complex multivalued attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                } else if (attributeSchema.getMultiValued() == false) {
                    if (attributeValObj instanceof JSONObject || attributeValObj == null) {
                        if (attributeValObj == null) {
                            continue;
                        }
                        // if the corresponding json value object is JSONObject, it is a ComplexAttribute.
                        scimObject.setAttribute(buildComplexAttribute(attributeSchema, (JSONObject) attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the complex attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                }
            }
        }
        return scimObject;
    } catch (JSONException e) {
        logger.error("json error in decoding the resource");
        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) SCIMDefinitions(org.wso2.charon3.core.schema.SCIMDefinitions) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject) SCIMObject(org.wso2.charon3.core.objects.SCIMObject)

Example 52 with BadRequestException

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

the class JSONDecoder method decodeRequest.

/*
     * This method is to extract operations from the PATCH request body and create separate PatchOperation objects
     * for each operation
     * @param scimResourceString
     * @return
     */
public ArrayList<PatchOperation> decodeRequest(String scimResourceString) throws BadRequestException {
    ArrayList<PatchOperation> operationList = new ArrayList<PatchOperation>();
    try {
        // decode the string into json representation
        JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
        // obtain the Operations values
        JSONArray operationJsonList = (JSONArray) decodedJsonObj.opt(SCIMConstants.OperationalConstants.OPERATIONS);
        // for each operation, create a PatchOperation object and add the relevant values to it
        for (int count = 0; count < operationJsonList.length(); count++) {
            JSONObject operation = (JSONObject) operationJsonList.get(count);
            PatchOperation patchOperation = new PatchOperation();
            String op = (String) operation.opt(SCIMConstants.OperationalConstants.OP);
            if (op.equalsIgnoreCase(SCIMConstants.OperationalConstants.ADD)) {
                patchOperation.setOperation(SCIMConstants.OperationalConstants.ADD);
            } else if (op.equalsIgnoreCase(SCIMConstants.OperationalConstants.REMOVE)) {
                patchOperation.setOperation(SCIMConstants.OperationalConstants.REMOVE);
            } else if (op.equalsIgnoreCase(SCIMConstants.OperationalConstants.REPLACE)) {
                patchOperation.setOperation(SCIMConstants.OperationalConstants.REPLACE);
            }
            patchOperation.setPath((String) operation.opt(SCIMConstants.OperationalConstants.PATH));
            patchOperation.setValues(operation.opt(SCIMConstants.OperationalConstants.VALUE));
            operationList.add(patchOperation);
        }
    } catch (JSONException e) {
        logger.error("json error in decoding the request");
        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
    }
    return operationList;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) PatchOperation(org.wso2.charon3.core.utils.codeutils.PatchOperation) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException)

Example 53 with BadRequestException

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

the class JSONDecoder method buildComplexAttribute.

/*
     * Return a complex attribute with the user defined sub values included and necessary attribute characteristics set
     *
     * @param complexAttributeSchema - complex attribute schema
     * @param jsonObject             - sub attributes values for the complex attribute
     * @return ComplexAttribute
     */
public ComplexAttribute buildComplexAttribute(AttributeSchema complexAttributeSchema, JSONObject jsonObject) throws BadRequestException, CharonException, InternalErrorException, JSONException {
    ComplexAttribute complexAttribute = new ComplexAttribute(complexAttributeSchema.getName());
    Map<String, Attribute> subAttributesMap = new HashMap<String, Attribute>();
    // list of sub attributes of the complex attribute
    List<SCIMAttributeSchema> subAttributeSchemas = ((SCIMAttributeSchema) complexAttributeSchema).getSubAttributeSchemas();
    // iterate through the complex attribute schema and extract the sub attributes.
    for (AttributeSchema subAttributeSchema : subAttributeSchemas) {
        // obtain the user defined value for given key- attribute schema name
        Object attributeValObj = jsonObject.opt(subAttributeSchema.getName());
        SCIMDefinitions.DataType subAttributeSchemaType = subAttributeSchema.getType();
        if (subAttributeSchemaType.equals(STRING) || subAttributeSchemaType.equals(BINARY) || subAttributeSchemaType.equals(BOOLEAN) || subAttributeSchemaType.equals(DATE_TIME) || subAttributeSchemaType.equals(DECIMAL) || subAttributeSchemaType.equals(INTEGER) || subAttributeSchemaType.equals(REFERENCE)) {
            if (!subAttributeSchema.getMultiValued()) {
                if (attributeValObj instanceof String || attributeValObj instanceof Boolean || attributeValObj instanceof Integer || attributeValObj == null) {
                    // If an attribute is passed without a value, no need to save it.
                    if (attributeValObj == null) {
                        continue;
                    }
                    // if the corresponding schema data type is String/Boolean/Binary/Decimal/Integer/DataTime
                    // or Reference, it is a SimpleAttribute.
                    subAttributesMap.put(subAttributeSchema.getName(), buildSimpleAttribute(subAttributeSchema, attributeValObj));
                } else {
                    logger.error("Error decoding the sub attribute");
                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                }
            } else {
                if (attributeValObj instanceof JSONArray || attributeValObj == null) {
                    // If an attribute is passed without a value, no need to save it.
                    if (attributeValObj == null) {
                        continue;
                    }
                    subAttributesMap.put(subAttributeSchema.getName(), buildPrimitiveMultiValuedAttribute(subAttributeSchema, (JSONArray) attributeValObj));
                } else {
                    logger.error("Error decoding the sub attribute");
                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                }
            }
        // this case is only valid for the extension schema
        // As according to the spec we have complex attribute inside complex attribute only for extension,
        // we need to treat it separately
        } else if (complexAttributeSchema.getName().equals(SCIMResourceSchemaManager.getInstance().getExtensionName())) {
            if (subAttributeSchemaType.equals(COMPLEX)) {
                // check for user defined extension's schema violation
                List<SCIMAttributeSchema> subList = subAttributeSchema.getSubAttributeSchemas();
                for (AttributeSchema attributeSchema : subList) {
                    if (attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                        String error = "Complex attribute can not have complex sub attributes";
                        throw new InternalErrorException(error);
                    }
                }
                if (subAttributeSchema.getMultiValued() == true) {
                    if (attributeValObj instanceof JSONArray || attributeValObj == null) {
                        if (attributeValObj == null) {
                            continue;
                        }
                        MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(subAttributeSchema.getName());
                        JSONArray attributeValues = null;
                        List<Attribute> complexAttributeValues = new ArrayList<Attribute>();
                        try {
                            attributeValues = (JSONArray) attributeValObj;
                        } catch (Exception e) {
                            logger.error("Error decoding the extension");
                            throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                        }
                        // 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 JSONObject) {
                                JSONObject complexAttributeValue = (JSONObject) attributeValue;
                                complexAttributeValues.add(buildComplexValue(subAttributeSchema, complexAttributeValue));
                            } else {
                                String error = "Unknown JSON representation for the MultiValued attribute " + subAttributeSchema.getName() + " which has data type as " + subAttributeSchema.getType();
                                throw new BadRequestException(error, ResponseCodeConstants.INVALID_SYNTAX);
                            }
                            multiValuedAttribute.setAttributeValues(complexAttributeValues);
                            MultiValuedAttribute complexMultiValuedSubAttribute = (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedAttribute);
                            subAttributesMap.put(complexMultiValuedSubAttribute.getName(), complexMultiValuedSubAttribute);
                        }
                    } else {
                        logger.error("Error decoding the extension sub attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                } else {
                    if (attributeValObj instanceof JSONObject || attributeValObj == null) {
                        if (attributeValObj == null) {
                            continue;
                        }
                        ComplexAttribute complexSubAttribute = buildComplexAttribute(subAttributeSchema, (JSONObject) attributeValObj);
                        subAttributesMap.put(complexSubAttribute.getName(), complexSubAttribute);
                    } else {
                        logger.error("Error decoding the extension sub attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                }
            }
        } else {
            String error = "Complex attribute can not have complex sub attributes";
            throw new InternalErrorException(error);
        }
    }
    complexAttribute.setSubAttributesList(subAttributesMap);
    return (ComplexAttribute) DefaultAttributeFactory.createAttribute(complexAttributeSchema, complexAttribute);
}
Also used : MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) HashMap(java.util.HashMap) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) JSONArray(org.json.JSONArray) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) SCIMDefinitions(org.wso2.charon3.core.schema.SCIMDefinitions) JSONException(org.json.JSONException) CharonException(org.wso2.charon3.core.exceptions.CharonException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) IOException(java.io.IOException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) JSONObject(org.json.JSONObject) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject) SCIMObject(org.wso2.charon3.core.objects.SCIMObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 54 with BadRequestException

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

the class JSONDecoder method decodeBulkData.

/**
 * Decode BulkRequestData Json Sting.
 *
 * @param bulkResourceString
 * @return BulkRequestData Object
 */
public BulkRequestData decodeBulkData(String bulkResourceString) throws BadRequestException {
    BulkRequestData bulkRequestDataObject = new BulkRequestData();
    List<BulkRequestContent> usersEndpointOperationList = new ArrayList<BulkRequestContent>();
    List<BulkRequestContent> groupsEndpointOperationList = new ArrayList<BulkRequestContent>();
    int failOnErrorsAttribute = 0;
    List<String> schemas = new ArrayList<String>();
    JSONObject decodedObject = null;
    try {
        decodedObject = new JSONObject(new JSONTokener(bulkResourceString));
        // prepare the schema list
        JSONArray membersAttributeSchemas = (JSONArray) decodedObject.opt(SCIMConstants.CommonSchemaConstants.SCHEMAS);
        for (int i = 0; i < membersAttributeSchemas.length(); i++) {
            schemas.add(membersAttributeSchemas.get(i).toString());
        }
        bulkRequestDataObject.setSchemas(schemas);
        // get [operations] from the Json String and prepare the request List
        JSONArray membersAttributeOperations = (JSONArray) decodedObject.opt(SCIMConstants.OperationalConstants.OPERATIONS);
        for (int i = 0; i < membersAttributeOperations.length(); i++) {
            JSONObject member = (JSONObject) membersAttributeOperations.get(i);
            // Request path - /Users or /Groups
            String requestType = member.optString(SCIMConstants.OperationalConstants.PATH);
            if (requestType == null) {
                throw new BadRequestException("Missing required attribute : path", ResponseCodeConstants.INVALID_SYNTAX);
            }
            // Request method  - POST,PUT..etc
            String requestMethod = member.optString(SCIMConstants.OperationalConstants.METHOD);
            if (requestMethod == null) {
                throw new BadRequestException("Missing required attribute : method", ResponseCodeConstants.INVALID_SYNTAX);
            }
            // Request version
            String requestVersion = member.optString(SCIMConstants.OperationalConstants.VERSION);
            if (requestMethod.equals(SCIMConstants.OperationalConstants.POST)) {
                if (!member.optString(SCIMConstants.OperationalConstants.BULK_ID).equals("") && member.optString(SCIMConstants.OperationalConstants.BULK_ID) != null) {
                    setRequestData(requestType, requestMethod, requestVersion, member, usersEndpointOperationList, groupsEndpointOperationList);
                } else {
                    String error = "JSON string could not be decoded properly.Required " + "attribute BULK_ID is missing in the request";
                    logger.error(error);
                    throw new BadRequestException(error, ResponseCodeConstants.INVALID_VALUE);
                }
            } else {
                setRequestData(requestType, requestMethod, requestVersion, member, usersEndpointOperationList, groupsEndpointOperationList);
            }
        }
        // extract [failOnErrors] attribute from Json string
        failOnErrorsAttribute = decodedObject.optInt(SCIMConstants.OperationalConstants.FAIL_ON_ERRORS);
        bulkRequestDataObject.setFailOnErrors(failOnErrorsAttribute);
        bulkRequestDataObject.setUserOperationRequests(usersEndpointOperationList);
        bulkRequestDataObject.setGroupOperationRequests(groupsEndpointOperationList);
    } catch (JSONException e1) {
        String error = "JSON string could not be decoded properly.";
        logger.error(error);
        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
    }
    return bulkRequestDataObject;
}
Also used : JSONTokener(org.json.JSONTokener) BulkRequestData(org.wso2.charon3.core.objects.bulk.BulkRequestData) JSONObject(org.json.JSONObject) BulkRequestContent(org.wso2.charon3.core.objects.bulk.BulkRequestContent) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONException(org.json.JSONException)

Example 55 with BadRequestException

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

the class JSONDecoder method decode.

public AbstractSCIMObject decode(String scimResourceString, SCIMResourceTypeSchema schema) throws CharonException, BadRequestException {
    try {
        JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
        AbstractSCIMObject scimObject = null;
        if (schema.getSchemasList().contains(SCIMConstants.GROUP_CORE_SCHEMA_URI)) {
            scimObject = (AbstractSCIMObject) decodeResource(decodedJsonObj.toString(), schema, new Group());
        } else {
            scimObject = (AbstractSCIMObject) decodeResource(decodedJsonObj.toString(), schema, new User());
        }
        return scimObject;
    } catch (JSONException | InternalErrorException | CharonException e) {
        throw new CharonException("Error in decoding the request", e);
    } catch (BadRequestException e) {
        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
    }
}
Also used : JSONTokener(org.json.JSONTokener) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) Group(org.wso2.charon3.core.objects.Group) User(org.wso2.charon3.core.objects.User) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) CharonException(org.wso2.charon3.core.exceptions.CharonException)

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