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;
}
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);
}
}
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;
}
Aggregations