use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.
the class JSONDecoder method decodeSearchRequestBody.
/*
* decode the raw string and create a search object
* @param scimResourceString
* @return
* @throws BadRequestException
*/
public SearchRequest decodeSearchRequestBody(String scimResourceString, SCIMResourceTypeSchema schema) throws BadRequestException {
FilterTreeManager filterTreeManager = null;
Node rootNode = null;
// decode the string and create search object
try {
JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
SearchRequest searchRequest = new SearchRequest();
ArrayList<String> attributes = new ArrayList<>();
ArrayList<String> excludedAttributes = new ArrayList<>();
JSONArray attributesValues = (JSONArray) decodedJsonObj.opt(SCIMConstants.OperationalConstants.ATTRIBUTES);
JSONArray excludedAttributesValues = (JSONArray) decodedJsonObj.opt(SCIMConstants.OperationalConstants.EXCLUDED_ATTRIBUTES);
JSONArray schemas = (JSONArray) decodedJsonObj.opt(SCIMConstants.CommonSchemaConstants.SCHEMAS);
if (schemas.length() != 1) {
throw new BadRequestException("Schema is invalid", ResponseCodeConstants.INVALID_VALUE);
}
if (attributesValues != null) {
for (int i = 0; i < attributesValues.length(); i++) {
attributes.add((String) attributesValues.get(i));
}
}
if (excludedAttributesValues != null) {
for (int i = 0; i < excludedAttributesValues.length(); i++) {
excludedAttributes.add((String) excludedAttributesValues.get(i));
}
}
if (decodedJsonObj.opt(SCIMConstants.OperationalConstants.FILTER) != null) {
filterTreeManager = new FilterTreeManager((String) decodedJsonObj.opt(SCIMConstants.OperationalConstants.FILTER), schema);
rootNode = filterTreeManager.buildTree();
}
searchRequest.setAttributes(attributes);
searchRequest.setExcludedAttributes(excludedAttributes);
searchRequest.setSchema((String) schemas.get(0));
searchRequest.setCountStr(decodedJsonObj.optString(SCIMConstants.OperationalConstants.COUNT));
searchRequest.setStartIndexStr(decodedJsonObj.optString(SCIMConstants.OperationalConstants.START_INDEX));
searchRequest.setFilter(rootNode);
if (!decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_BY).equals("")) {
searchRequest.setSortBy(decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_BY));
}
if (!decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_ORDER).equals("")) {
searchRequest.setSortOder(decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_ORDER));
}
return searchRequest;
} catch (JSONException | IOException e) {
logger.error("Error while decoding the resource string");
throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
}
}
use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.
the class JSONDecoder method buildPrimitiveMultiValuedAttribute.
/*
* Return a primitive type multi valued attribute with the user defined value included and necessary
* attribute characteristics set
*
* @param attributeSchema - Attribute schema
* @param attributeValues - values for the attribute
* @return MultiValuedAttribute
*/
public MultiValuedAttribute buildPrimitiveMultiValuedAttribute(AttributeSchema attributeSchema, JSONArray attributeValues) throws CharonException, BadRequestException {
try {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
List<Object> primitiveValues = new ArrayList<Object>();
// 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 String || attributeValue instanceof Boolean || attributeValue instanceof Integer || attributeValue == null) {
// If an attribute is passed without a value, no need to save it.
if (attributeValue == null) {
continue;
}
primitiveValues.add(attributeValue);
} else {
String error = "Unknown JSON representation for the MultiValued attribute " + attributeSchema.getName() + " which has data type as " + attributeSchema.getType();
throw new BadRequestException(error, ResponseCodeConstants.INVALID_SYNTAX);
}
}
multiValuedAttribute.setAttributePrimitiveValues(primitiveValues);
return (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
} catch (JSONException e) {
String error = "Error in accessing JSON value of multivalued attribute";
throw new CharonException(error, e);
}
}
use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.
the class AbstractSCIMObject method setResourceType.
/*
* set the resourceType of the meta attribute
*
* @param resourceType
*/
public void setResourceType(String resourceType) throws BadRequestException, CharonException {
// create the resourceType attribute as defined in schema.
SimpleAttribute resourceTypeAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.RESOURCE_TYPE, new SimpleAttribute(SCIMConstants.CommonSchemaConstants.RESOURCE_TYPE, resourceType));
// check meta complex attribute already exist.
if (getMetaAttribute() != null) {
ComplexAttribute metaAttribute = getMetaAttribute();
// check version attribute already exist
if (metaAttribute.isSubAttributeExist(resourceTypeAttribute.getName())) {
String error = "Read only attribute is tried to modify";
throw new CharonException(error);
} else {
metaAttribute.setSubAttribute(resourceTypeAttribute);
}
} else {
// create meta attribute and set the sub attribute.
createMetaAttribute();
getMetaAttribute().setSubAttribute(resourceTypeAttribute);
}
}
use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.
the class Group method setMemberCommon.
/*
* set member to the group
* @param userId
* @param userName
* @return
* @throws BadRequestException
* @throws CharonException
*/
private ComplexAttribute setMemberCommon(String userId, String userName) throws BadRequestException, CharonException {
ComplexAttribute complexAttribute = new ComplexAttribute();
complexAttribute.setName(SCIMConstants.GroupSchemaConstants.MEMBERS + "_" + userId + SCIMConstants.DEFAULT);
SimpleAttribute valueSimpleAttribute = new SimpleAttribute(SCIMConstants.CommonSchemaConstants.VALUE, userId);
DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.VALUE, valueSimpleAttribute);
SimpleAttribute displaySimpleAttribute = new SimpleAttribute(SCIMConstants.GroupSchemaConstants.DISPLAY, userName);
DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.DISPLAY, displaySimpleAttribute);
complexAttribute.setSubAttribute(valueSimpleAttribute);
complexAttribute.setSubAttribute(displaySimpleAttribute);
DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.MEMBERS, complexAttribute);
return complexAttribute;
}
use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.
the class Group method setMember.
/*
* set a member to the group
* @param userId
* @param userName
* @throws BadRequestException
* @throws CharonException
*/
public void setMember(String userId, String userName) throws BadRequestException, CharonException {
if (this.isAttributeExist(SCIMConstants.GroupSchemaConstants.MEMBERS)) {
MultiValuedAttribute members = (MultiValuedAttribute) this.attributeList.get(SCIMConstants.GroupSchemaConstants.MEMBERS);
ComplexAttribute complexAttribute = setMemberCommon(userId, userName);
members.setAttributeValue(complexAttribute);
} else {
MultiValuedAttribute members = new MultiValuedAttribute(SCIMConstants.GroupSchemaConstants.MEMBERS);
DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.MEMBERS, members);
ComplexAttribute complexAttribute = setMemberCommon(userId, userName);
members.setAttributeValue(complexAttribute);
this.setAttribute(members);
}
}
Aggregations