use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class SmsRealmProvider method handleDelete.
@Override
public Promise<ResourceResponse, ResourceException> handleDelete(Context serverContext, DeleteRequest request) {
RealmContext realmContext = serverContext.asContext(RealmContext.class);
String realmPath = realmContext.getResolvedRealm();
try {
OrganizationConfigManager realmManager = new OrganizationConfigManager(getSSOToken(), realmPath);
final ResourceResponse resource = getResource(getJsonValue(realmPath));
realmManager.deleteSubOrganization(null, false);
String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext);
debug.message("RealmResource.deleteInstance :: DELETE of realm " + realmPath + " performed by " + principalName);
return newResultPromise(resource);
} catch (SMSException smse) {
ResourceException exception = configureErrorMessage(smse);
if (exception instanceof NotFoundException) {
debug.warning("RealmResource.deleteInstance() : Cannot find {}", realmPath, smse);
return exception.asPromise();
} else if (exception instanceof ForbiddenException || exception instanceof PermanentException || exception instanceof ConflictException || exception instanceof BadRequestException) {
debug.warning("RealmResource.deleteInstance() : Cannot DELETE {}", realmPath, smse);
return exception.asPromise();
} else {
return new BadRequestException(exception.getMessage(), exception).asPromise();
}
} catch (Exception e) {
return new BadRequestException(e.getMessage(), e).asPromise();
}
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class CoreTokenResource method readInstance.
/**
* Read the token based on its Token ID.
*
* If successful, the Token will be returned to the caller in serialised JSON format.
*
* @param serverContext Required context.
* @param tokenId The TokenID of the Token to read.
* @param readRequest Not used.
*/
public Promise<ResourceResponse, ResourceException> readInstance(Context serverContext, String tokenId, ReadRequest readRequest) {
String principal = PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext);
try {
Token token = store.read(tokenId);
if (token == null) {
error("READ by {0}: No token resource to read with ID: {1}", principal, tokenId);
return generateNotFoundException(tokenId).asPromise();
}
String json = serialisation.serialise(token);
ResourceResponse response = newResourceResponse(tokenId, String.valueOf(System.currentTimeMillis()), JsonValueBuilder.toJsonValue(json));
debug("READ by {0}: Read token resource with ID: {1}", principal, tokenId);
return newResultPromise(response);
} catch (CoreTokenException e) {
error(e, "READ by {0}: Error reading token resource with ID: {1}", principal, tokenId);
return generateException(e).asPromise();
}
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class CoreTokenResource method deleteInstance.
/**
* Delete the instance referred to by its Resource path.
*
* @param serverContext Required context.
* @param tokenId The TokenID of the token to delete.
* @param deleteRequest Not used.
*/
public Promise<ResourceResponse, ResourceException> deleteInstance(Context serverContext, String tokenId, DeleteRequest deleteRequest) {
String principal = PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext);
try {
store.deleteAsync(tokenId);
Map<String, String> result = new HashMap<String, String>();
result.put(TOKEN_ID, tokenId);
ResourceResponse resource = newResourceResponse(tokenId, String.valueOf(System.currentTimeMillis()), new JsonValue(result));
debug("DELETE by {0}: Deleted token resource with ID: {1}", principal, tokenId);
return newResultPromise(resource);
} catch (CoreTokenException e) {
error(e, "DELETE by {0}: Error deleting token resource with ID: {1}", principal, tokenId);
return generateException(e).asPromise();
}
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class CoreTokenResource method updateInstance.
/**
* Update a Token based on the Resource path.
*
* @param serverContext Required context.
* @param tokenId The tokenId to update. This must be the same TokenId as the serialised Token.
* @param updateRequest Contains the JSON serialised Token to update.
*/
public Promise<ResourceResponse, ResourceException> updateInstance(Context serverContext, String tokenId, UpdateRequest updateRequest) {
String principal = PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext);
String value = updateRequest.getContent().toString();
Token newToken = serialisation.deserialise(value, Token.class);
try {
store.updateAsync(newToken);
ResourceResponse resource = newResourceResponse(newToken.getTokenId(), String.valueOf(System.currentTimeMillis()), new JsonValue("Token Updated"));
debug("UPDATE by {0}: Updated token resource with ID: {1}", principal, tokenId);
return newResultPromise(resource);
} catch (CoreTokenException e) {
error(e, "UPDATE by {0}: Error updating token resource with ID: {1}", principal, tokenId);
return generateException(e).asPromise();
}
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class IdentityResourceV2 method createInstance.
/**
* {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> createInstance(final Context context, final CreateRequest request) {
RealmContext realmContext = context.asContext(RealmContext.class);
final String realm = realmContext.getResolvedRealm();
try {
// anyone can create an account add
SSOToken admin = getSSOToken(getCookieFromServerContext(context));
final JsonValue jVal = request.getContent();
String resourceId = request.getNewResourceId();
IdentityDetails identity = jsonValueToIdentityDetails(objectType, jVal, realm);
// check to see if request has included resource ID
if (resourceId != null) {
if (identity.getName() != null) {
if (!resourceId.equalsIgnoreCase(identity.getName())) {
ResourceException be = new BadRequestException("id in path does not match id in request body");
debug.error("IdentityResource.createInstance() :: Cannot CREATE ", be);
return be.asPromise();
}
}
identity.setName(resourceId);
} else {
resourceId = identity.getName();
}
UserAttributeInfo userAttributeInfo = configHandler.getConfig(realm, UserAttributeInfoBuilder.class);
enforceWhiteList(context, request.getContent(), objectType, userAttributeInfo.getValidCreationAttributes());
final String id = resourceId;
return attemptResourceCreation(realm, admin, identity, resourceId).thenAsync(new AsyncFunction<IdentityDetails, ResourceResponse, ResourceException>() {
@Override
public Promise<ResourceResponse, ResourceException> apply(IdentityDetails dtls) {
if (dtls != null) {
String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
debug.message("IdentityResource.createInstance :: CREATE of resourceId={} in realm={} " + "performed by principalName={}", id, realm, principalName);
ResourceResponse resource = newResourceResponse(id, "0", identityDetailsToJsonValue(dtls));
return newResultPromise(resource);
} else {
debug.error("IdentityResource.createInstance() :: Identity not found");
return new NotFoundException("Identity not found").asPromise();
}
}
});
} catch (SSOException e) {
return new ForbiddenException(e).asPromise();
} catch (BadRequestException bre) {
return bre.asPromise();
}
}
Aggregations