Search in sources :

Example 51 with BadRequestException

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

the class BadRequestExceptionTestCase method testGetMessage.

@Test
public void testGetMessage() throws Exception {
    ErrorDTO errorDTO = new ErrorDTO();
    errorDTO.setDescription(testMessage);
    BadRequestException badRequestException = new BadRequestException(errorDTO);
    String message = badRequestException.getMessage();
    Assert.assertEquals(message, testMessage);
}
Also used : ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) BadRequestException(org.wso2.carbon.apimgt.rest.api.common.exception.BadRequestException) Test(org.testng.annotations.Test)

Example 52 with BadRequestException

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

the class JSONDecoder method buildComplexValue.

/*
     * To build a complex type value of a Multi Valued Attribute. (eg. Email with value,type,primary as sub attributes
     *
     * @param attributeSchema
     * @param jsonObject
     * @return ComplexAttribute
     */
private ComplexAttribute buildComplexValue(AttributeSchema attributeSchema, JSONObject jsonObject) throws CharonException, BadRequestException {
    ComplexAttribute complexAttribute = new ComplexAttribute(attributeSchema.getName());
    Map<String, Attribute> subAttributesMap = new HashMap<String, Attribute>();
    List<SCIMAttributeSchema> subAttributeSchemas = ((SCIMAttributeSchema) attributeSchema).getSubAttributeSchemas();
    for (SCIMAttributeSchema subAttributeSchema : subAttributeSchemas) {
        Object subAttributeValue = jsonObject.opt(subAttributeSchema.getName());
        // setting up a name for the complex attribute for the reference purpose
        if (subAttributeSchema.getName().equals(SCIMConstants.CommonSchemaConstants.VALUE)) {
            // (value,type) pair is considered as a primary key for each entry
            if (subAttributeValue != null) {
                Object subAttributeValueForType = jsonObject.opt(SCIMConstants.CommonSchemaConstants.TYPE);
                if (subAttributeValueForType != null) {
                    complexAttribute.setName(attributeSchema.getName() + "_" + subAttributeValue + "_" + subAttributeValueForType);
                } else {
                    complexAttribute.setName(attributeSchema.getName() + "_" + subAttributeValue + "_" + SCIMConstants.DEFAULT);
                }
            } else {
                Object subAttributeValueFortype = jsonObject.opt(SCIMConstants.CommonSchemaConstants.TYPE);
                if (subAttributeValueFortype != null) {
                    complexAttribute.setName(attributeSchema.getName() + "_" + SCIMConstants.DEFAULT + "_" + subAttributeValueFortype);
                } else {
                    complexAttribute.setName(attributeSchema.getName() + "_" + SCIMConstants.DEFAULT + "_" + SCIMConstants.DEFAULT);
                }
            }
        }
        if (subAttributeValue != null) {
            if (subAttributeSchema.getMultiValued()) {
                if (subAttributeValue instanceof JSONArray) {
                    MultiValuedAttribute multiValuedAttribute = buildPrimitiveMultiValuedAttribute(subAttributeSchema, (JSONArray) subAttributeValue);
                    // let the attribute factory to set the sub attribute of a complex
                    // attribute to detect schema violations.
                    multiValuedAttribute = (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedAttribute);
                    subAttributesMap.put(subAttributeSchema.getName(), multiValuedAttribute);
                } else {
                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                }
            } else {
                if (subAttributeValue instanceof String || subAttributeValue instanceof Boolean || subAttributeValue instanceof Integer) {
                    SimpleAttribute simpleAttribute = buildSimpleAttribute(subAttributeSchema, subAttributeValue);
                    // let the attribute factory to set the sub attribute of a complex
                    // attribute to detect schema violations.
                    simpleAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(subAttributeSchema, simpleAttribute);
                    subAttributesMap.put(subAttributeSchema.getName(), simpleAttribute);
                } else {
                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                }
            }
        }
    }
    complexAttribute.setSubAttributesList(subAttributesMap);
    return (ComplexAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, 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) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) JSONArray(org.json.JSONArray) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) 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 53 with BadRequestException

use of org.wso2.charon3.core.exceptions.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 54 with BadRequestException

use of org.wso2.charon3.core.exceptions.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 55 with BadRequestException

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

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 HashMap (java.util.HashMap)23 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)23 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 JSONException (org.json.JSONException)17 JSONObject (org.json.JSONObject)17 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)16 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 ArrayList (java.util.ArrayList)9