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