Search in sources :

Example 1 with BulkRequestData

use of org.wso2.charon3.core.objects.bulk.BulkRequestData 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 2 with BulkRequestData

use of org.wso2.charon3.core.objects.bulk.BulkRequestData 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 3 with BulkRequestData

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

the class BulkRequestProcessor method processBulkRequests.

public BulkResponseData processBulkRequests(BulkRequestData bulkRequestData) throws BadRequestException {
    BulkResponseData bulkResponseData = new BulkResponseData();
    SCIMResponse response = null;
    for (BulkRequestContent bulkRequestContent : bulkRequestData.getUserOperationRequests()) {
        if (failOnError == 0) {
            bulkResponseData.addUserOperation(getBulkResponseContent(bulkRequestContent, userResourceManager));
        } else {
            if (errors < failOnError) {
                bulkResponseData.addUserOperation(getBulkResponseContent(bulkRequestContent, userResourceManager));
            }
        }
    }
    for (BulkRequestContent bulkRequestContent : bulkRequestData.getGroupOperationRequests()) {
        if (failOnError == 0) {
            bulkResponseData.addGroupOperation(getBulkResponseContent(bulkRequestContent, groupResourceManager));
        } else {
            if (errors < failOnError) {
                bulkResponseData.addGroupOperation(getBulkResponseContent(bulkRequestContent, groupResourceManager));
            }
        }
    }
    bulkResponseData.setSchema(SCIMConstants.BULK_RESPONSE_URI);
    return bulkResponseData;
}
Also used : BulkRequestContent(org.wso2.charon3.core.objects.bulk.BulkRequestContent) BulkResponseData(org.wso2.charon3.core.objects.bulk.BulkResponseData)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)2 BulkRequestContent (org.wso2.charon3.core.objects.bulk.BulkRequestContent)2 BulkRequestData (org.wso2.charon3.core.objects.bulk.BulkRequestData)2 BulkResponseData (org.wso2.charon3.core.objects.bulk.BulkResponseData)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1 CharonException (org.wso2.charon3.core.exceptions.CharonException)1 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)1 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)1