Search in sources :

Example 16 with InternalErrorException

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

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

Example 18 with InternalErrorException

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

the class JSONEncoder method encodeBulkResponseData.

/*
     * Encode given bulkResponseData object and return the encoded string
     *
     * @param bulkResponseData
     * @return
     */
public String encodeBulkResponseData(BulkResponseData bulkResponseData) throws InternalErrorException {
    String encodedString = "";
    List<BulkResponseContent> userResponseDataList = bulkResponseData.getUserOperationResponse();
    List<BulkResponseContent> groupResponseDataList = bulkResponseData.getGroupOperationResponse();
    JSONObject rootObject = new JSONObject();
    // encode schemas
    try {
        // set the [schemas]
        this.encodeArrayOfValues(SCIMConstants.CommonSchemaConstants.SCHEMAS, bulkResponseData.getSchemas().toArray(), rootObject);
        // [Operations] - multi value attribute
        ArrayList<JSONObject> operationResponseList = new ArrayList<>();
        for (BulkResponseContent userOperationResponse : userResponseDataList) {
            encodeResponseContent(userOperationResponse, operationResponseList);
        }
        for (BulkResponseContent groupOperationResponse : groupResponseDataList) {
            encodeResponseContent(groupOperationResponse, operationResponseList);
        }
        // set operations
        this.encodeArrayOfValues(SCIMConstants.OperationalConstants.OPERATIONS, operationResponseList.toArray(), rootObject);
        encodedString = rootObject.toString();
    } catch (JSONException e) {
        throw new InternalErrorException("Error in encoding the response");
    }
    return encodedString;
}
Also used : BulkResponseContent(org.wso2.charon3.core.objects.bulk.BulkResponseContent) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException)

Example 19 with InternalErrorException

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

the class BulkResourceManager method processBulkData.

public SCIMResponse processBulkData(String data, UserManager userManager) {
    BulkResponseData bulkResponseData;
    try {
        // Get encoder and decoder from AbstractResourceEndpoint
        encoder = getEncoder();
        decoder = getDecoder();
        BulkRequestData bulkRequestDataObject;
        // decode the request
        bulkRequestDataObject = decoder.decodeBulkData(data);
        bulkRequestProcessor.setFailOnError(bulkRequestDataObject.getFailOnErrors());
        bulkRequestProcessor.setUserManager(userManager);
        // Get bulk response data
        bulkResponseData = bulkRequestProcessor.processBulkRequests(bulkRequestDataObject);
        // encode the BulkResponseData object
        String finalEncodedResponse = encoder.encodeBulkResponseData(bulkResponseData);
        // careate SCIM response message
        Map<String, String> responseHeaders = new HashMap<String, String>();
        // add location header
        responseHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        // create the final response
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, finalEncodedResponse, responseHeaders);
    } catch (CharonException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (BadRequestException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    }
}
Also used : BulkRequestData(org.wso2.charon3.core.objects.bulk.BulkRequestData) HashMap(java.util.HashMap) BulkResponseData(org.wso2.charon3.core.objects.bulk.BulkResponseData) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) CharonException(org.wso2.charon3.core.exceptions.CharonException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Example 20 with InternalErrorException

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

the class GroupResourceManager method listWithPOST.

/*
     * this facilitates the querying using HTTP POST
     * @param resourceString
     * @param usermanager
     * @return
     */
@Override
public SCIMResponse listWithPOST(String resourceString, UserManager userManager) {
    JSONEncoder encoder = null;
    JSONDecoder decoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder
        decoder = getDecoder();
        // return core group schema
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getGroupResourceSchema();
        // create the search request object
        SearchRequest searchRequest = decoder.decodeSearchRequestBody(resourceString, schema);
        searchRequest.setCount(ResourceManagerUtil.processCount(searchRequest.getCountStr()));
        searchRequest.setStartIndex(ResourceManagerUtil.processCount(searchRequest.getStartIndexStr()));
        if (searchRequest.getSchema() != null && !searchRequest.getSchema().equals(SCIMConstants.SEARCH_SCHEMA_URI)) {
            throw new BadRequestException("Provided schema is invalid", ResponseCodeConstants.INVALID_VALUE);
        }
        // check whether provided sortOrder is valid or not
        if (searchRequest.getSortOder() != null) {
            if (!(searchRequest.getSortOder().equalsIgnoreCase(SCIMConstants.OperationalConstants.ASCENDING) || searchRequest.getSortOder().equalsIgnoreCase(SCIMConstants.OperationalConstants.DESCENDING))) {
                String error = " Invalid sortOrder value is specified";
                throw new BadRequestException(error, ResponseCodeConstants.INVALID_VALUE);
            }
        }
        // ascending.
        if (searchRequest.getSortOder() == null && searchRequest.getSortBy() != null) {
            searchRequest.setSortOder(SCIMConstants.OperationalConstants.ASCENDING);
        }
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), searchRequest.getAttributesAsString(), searchRequest.getExcludedAttributesAsString());
        List<Object> returnedGroups;
        int totalResults = 0;
        // API user should pass a usermanager usermanager to UserResourceEndpoint.
        if (userManager != null) {
            List<Object> tempList = userManager.listGroupsWithPost(searchRequest, requiredAttributes);
            totalResults = (int) tempList.get(0);
            tempList.remove(0);
            returnedGroups = tempList;
            for (Object group : returnedGroups) {
                // perform service provider side validation.
                ServerSideValidator.validateRetrievedSCIMObjectInList((Group) group, schema, searchRequest.getAttributesAsString(), searchRequest.getExcludedAttributesAsString());
            }
            // create a listed resource object out of the returned users list.
            ListedResource listedResource = createListedResource(returnedGroups, searchRequest.getStartIndex(), totalResults);
            // convert the listed resource into specific format.
            String encodedListedResource = encoder.encodeSCIMObject(listedResource);
            // 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);
            return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedListedResource, responseHeaders);
        } else {
            String error = "Provided user manager handler is null.";
            // throw internal server error.
            throw new InternalErrorException(error);
        }
    } catch (CharonException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (NotFoundException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (BadRequestException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    }
}
Also used : SearchRequest(org.wso2.charon3.core.utils.codeutils.SearchRequest) 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) ListedResource(org.wso2.charon3.core.objects.ListedResource) 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

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)25 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)20 CharonException (org.wso2.charon3.core.exceptions.CharonException)19 HashMap (java.util.HashMap)17 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)16 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)16 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)15 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)14 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)12 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)12 JSONException (org.json.JSONException)9 JSONObject (org.json.JSONObject)8 User (org.wso2.charon3.core.objects.User)8 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)7 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)6 JSONArray (org.json.JSONArray)5 Attribute (org.wso2.charon3.core.attributes.Attribute)5 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)5 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)5 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)5