use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.
the class MeResourceManager method get.
@Override
public SCIMResponse get(String userName, UserManager userManager, String attributes, String excludeAttributes) {
JSONEncoder encoder = null;
try {
// obtain the json encoder
encoder = getEncoder();
// obtain the schema corresponding to user
// unless configured returns core-user schema or else returns extended user schema)
SCIMResourceTypeSchema schema = getSchema(userManager);
// get the URIs of required attributes which must be given a value
Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
/*API user should pass a usermanager impl to UserResourceEndpoint.
retrieve the user from the provided UM handler.*/
User user = ((UserManager) userManager).getMe(userName, requiredAttributes);
// if user not found, return an error in relevant format.
if (user == null) {
String error = "User not found in the user store.";
throw new NotFoundException(error);
}
// perform service provider side validation.
ServerSideValidator.validateRetrievedSCIMObject(user, schema, attributes, excludeAttributes);
// convert the user into requested format.
String encodedUser = encoder.encodeSCIMObject(user);
// 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);
responseHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.USER_ENDPOINT) + "/" + user.getId());
return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedUser, responseHeaders);
} catch (NotFoundException e) {
return encodeSCIMException(e);
} catch (CharonException e) {
return encodeSCIMException(e);
} catch (BadRequestException | NotImplementedException e) {
return encodeSCIMException(e);
}
}
use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.
the class MeResourceManager method updateWithPATCH.
/**
* Update the user resource by sequence of operations.
*
* @param existingId
* @param scimObjectString
* @param userManager
* @param attributes
* @param excludeAttributes
* @return
*/
public SCIMResponse updateWithPATCH(String existingId, String scimObjectString, UserManager userManager, String attributes, String excludeAttributes) {
try {
if (userManager == null) {
String error = "Provided user manager handler is null.";
throw new InternalErrorException(error);
}
// obtain the json decoder.
JSONDecoder decoder = getDecoder();
// decode the SCIM User object, encoded in the submitted payload.
List<PatchOperation> opList = decoder.decodeRequest(scimObjectString);
SCIMResourceTypeSchema schema = getSchema(userManager);
// get the user from the user core
User oldUser = userManager.getMe(existingId, ResourceManagerUtil.getAllAttributeURIs(schema));
if (oldUser == null) {
throw new NotFoundException("No associated user exits in the user store.");
}
// make a copy of the original user
User copyOfOldUser = (User) CopyUtil.deepCopy(oldUser);
// make another copy of original user.
// this will be used to restore to the original condition if failure occurs.
User originalUser = (User) CopyUtil.deepCopy(copyOfOldUser);
User newUser = null;
for (PatchOperation operation : opList) {
if (operation.getOperation().equals(SCIMConstants.OperationalConstants.ADD)) {
if (newUser == null) {
newUser = (User) PatchOperationUtil.doPatchAdd(operation, getDecoder(), oldUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
} else {
newUser = (User) PatchOperationUtil.doPatchAdd(operation, getDecoder(), newUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
}
} else if (operation.getOperation().equals(SCIMConstants.OperationalConstants.REMOVE)) {
if (newUser == null) {
newUser = (User) PatchOperationUtil.doPatchRemove(operation, oldUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
} else {
newUser = (User) PatchOperationUtil.doPatchRemove(operation, newUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
}
} else if (operation.getOperation().equals(SCIMConstants.OperationalConstants.REPLACE)) {
if (newUser == null) {
newUser = (User) PatchOperationUtil.doPatchReplace(operation, getDecoder(), oldUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
} else {
newUser = (User) PatchOperationUtil.doPatchReplace(operation, getDecoder(), newUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
}
} else {
throw new BadRequestException("Unknown operation.", ResponseCodeConstants.INVALID_SYNTAX);
}
}
// get the URIs of required attributes which must be given a value
Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
User validatedUser = (User) ServerSideValidator.validateUpdatedSCIMObject(originalUser, newUser, schema);
newUser = userManager.updateMe(validatedUser, requiredAttributes);
// encode the newly created SCIM user object and add id attribute to Location header.
String encodedUser;
Map<String, String> httpHeaders = new HashMap<String, String>();
if (newUser != null) {
// create a deep copy of the user object since we are going to change it.
User copiedUser = (User) CopyUtil.deepCopy(newUser);
// need to remove password before returning
ServerSideValidator.validateReturnedAttributes(copiedUser, attributes, excludeAttributes);
encodedUser = getEncoder().encodeSCIMObject(copiedUser);
// add location header
httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.USER_ENDPOINT) + "/" + newUser.getId());
httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
} else {
String error = "Updated User resource is null.";
throw new CharonException(error);
}
// put the URI of the User object in the response header parameter.
return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedUser, httpHeaders);
} catch (NotFoundException e) {
return encodeSCIMException(e);
} catch (BadRequestException e) {
return encodeSCIMException(e);
} catch (NotImplementedException e) {
return encodeSCIMException(e);
} catch (CharonException e) {
return encodeSCIMException(e);
} catch (InternalErrorException e) {
return encodeSCIMException(e);
} catch (RuntimeException e) {
CharonException e1 = new CharonException("Error in performing the patch operation on user resource.", e);
return encodeSCIMException(e1);
}
}
use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.
the class ResourceTypeResourceManager method buildCombinedResourceType.
/*
* This combines the user and group resource type AbstractSCIMObjects and build a
* one root AbstractSCIMObjects
*
* @param userObject
* @param groupObject
* @return
* @throws CharonException
*/
private AbstractSCIMObject buildCombinedResourceType(AbstractSCIMObject userObject, AbstractSCIMObject groupObject) throws CharonException {
AbstractSCIMObject rootObject = new AbstractSCIMObject();
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(SCIMConstants.ListedResourceSchemaConstants.RESOURCES);
userObject.getSchemaList().clear();
userObject.setSchema(SCIMConstants.RESOURCE_TYPE_SCHEMA_URI);
multiValuedAttribute.setAttributePrimitiveValue(userObject);
groupObject.getSchemaList().clear();
groupObject.setSchema(SCIMConstants.RESOURCE_TYPE_SCHEMA_URI);
multiValuedAttribute.setAttributePrimitiveValue(groupObject);
rootObject.setAttribute(multiValuedAttribute);
rootObject.setSchema(SCIMConstants.LISTED_RESOURCE_CORE_SCHEMA_URI);
// Using a hard coded value of 2 since currently we only support two items in the list.
SimpleAttribute totalResults = new SimpleAttribute(SCIMConstants.CommonSchemaConstants.TOTAL_RESULTS, 2);
rootObject.setAttribute(totalResults);
return rootObject;
}
use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.
the class ResourceTypeResourceManager method getResourceType.
/*
* return RESOURCE_TYPE schema
*
* @return
*/
private SCIMResponse getResourceType() {
JSONEncoder encoder = null;
try {
// obtain the json encoder
encoder = getEncoder();
// obtain the json decoder
JSONDecoder decoder = getDecoder();
// get the service provider config schema
SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getResourceTypeResourceSchema();
// create a string in json format for user resource type with relevant values
String scimUserObjectString = encoder.buildUserResourceTypeJsonBody();
// create a string in json format for group resource type with relevant values
String scimGroupObjectString = encoder.buildGroupResourceTypeJsonBody();
// build the user abstract scim object
AbstractSCIMObject userResourceTypeObject = (AbstractSCIMObject) decoder.decodeResource(scimUserObjectString, schema, new AbstractSCIMObject());
// add meta data
userResourceTypeObject = ServerSideValidator.validateResourceTypeSCIMObject(userResourceTypeObject);
// build the group abstract scim object
AbstractSCIMObject groupResourceTypeObject = (AbstractSCIMObject) decoder.decodeResource(scimGroupObjectString, schema, new AbstractSCIMObject());
// add meta data
groupResourceTypeObject = ServerSideValidator.validateResourceTypeSCIMObject(groupResourceTypeObject);
// build the root abstract scim object
AbstractSCIMObject resourceTypeObject = buildCombinedResourceType(userResourceTypeObject, groupResourceTypeObject);
// encode the newly created SCIM Resource Type object.
String encodedObject;
Map<String, String> responseHeaders = new HashMap<String, String>();
if (resourceTypeObject != null) {
// create a deep copy of the resource type object since we are going to change it.
AbstractSCIMObject copiedObject = (AbstractSCIMObject) CopyUtil.deepCopy(resourceTypeObject);
encodedObject = encoder.encodeSCIMObject(copiedObject);
// add location header
responseHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.RESOURCE_TYPE_ENDPOINT));
responseHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
} else {
String error = "Newly created User resource is null.";
throw new InternalErrorException(error);
}
// put the uri of the resource type object in the response header parameter.
return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedObject, responseHeaders);
} catch (CharonException e) {
return encodeSCIMException(e);
} catch (BadRequestException e) {
return encodeSCIMException(e);
} catch (InternalErrorException e) {
return encodeSCIMException(e);
} catch (NotFoundException e) {
return encodeSCIMException(e);
} catch (JSONException e) {
return null;
}
}
use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.
the class RoleResourceManager method createRole.
@Override
public SCIMResponse createRole(String postRequest, RoleManager roleManager) {
try {
if (roleManager == null) {
String error = "Provided role manager is null.";
throw new InternalErrorException(error);
}
JSONEncoder encoder = getEncoder();
JSONDecoder decoder = getDecoder();
SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getRoleResourceSchema();
Role role = decoder.decodeResource(postRequest, schema, new Role());
ServerSideValidator.validateCreatedSCIMObject(role, SCIMSchemaDefinitions.SCIM_ROLE_SCHEMA);
Role createdRole = roleManager.createRole(role);
String encodedRole;
Map<String, String> httpHeaders = new HashMap<>();
if (createdRole != null) {
encodedRole = encoder.encodeSCIMObject(createdRole);
httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.ROLE_ENDPOINT) + "/" + createdRole.getId());
httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
} else {
String message = "Newly created Role resource is null.";
throw new InternalErrorException(message);
}
return new SCIMResponse(ResponseCodeConstants.CODE_CREATED, encodedRole, httpHeaders);
} catch (InternalErrorException | BadRequestException | ConflictException | CharonException | NotFoundException | NotImplementedException e) {
return encodeSCIMException(e);
}
}
Aggregations