use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class SoapSTSPublishServiceRequestHandler method handleQuery.
public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest request, QueryResourceHandler handler) {
QueryFilter<JsonPointer> queryFilter = request.getQueryFilter();
if (queryFilter == null) {
return new BadRequestException(getQueryUsageString()).asPromise();
}
String realm;
try {
realm = getRealmFromQueryFilter(queryFilter);
} catch (STSPublishException e) {
return e.asPromise();
}
try {
if (!realmValidator.isRealm(realm)) {
return new BadRequestException("The specified realm does not exist.").asPromise();
}
final List<SoapSTSInstanceConfig> publishedInstances = publisher.getPublishedInstances(realm);
for (SoapSTSInstanceConfig instanceConfig : publishedInstances) {
/*
Although instanceConfig.toJson() will yield the JsonValue which the handleResource invocation requires,
the SoapSTSInstanceConfig is a complicated nesting of JsonValue objects, which should be 'homogenized'
into a json format prior to inclusion in the response.
*/
handler.handleResource(newResourceResponse(instanceConfig.getDeploymentSubPath(), getInstanceConfigEtag(instanceConfig), new JsonValue(mapStringToJson(instanceConfig.toJson().toString()))));
}
return newResultPromise(newQueryResponse());
} catch (STSPublishException e) {
logger.error("Exception caught obtaining soap sts instances for realm " + (realm != null ? realm : "null realm") + "; Exception: " + e);
return e.asPromise();
}
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class TokenGenerationService method queryCollection.
@Override
public Promise<QueryResponse, ResourceException> queryCollection(final Context serverContext, final QueryRequest queryRequest, final QueryResourceHandler queryResultHandler) {
QueryFilter<JsonPointer> queryFilter = queryRequest.getQueryFilter();
if (queryFilter == null) {
return new BadRequestException(getUsageString()).asPromise();
}
try {
final QueryFilter<CoreTokenField> coreTokenFieldQueryFilter = convertToCoreTokenFieldQueryFilter(queryFilter);
final List<STSIssuedTokenState> issuedTokens = ctsTokenPersistence.listTokens(coreTokenFieldQueryFilter);
for (STSIssuedTokenState tokenState : issuedTokens) {
queryResultHandler.handleResource(newResourceResponse(tokenState.getTokenId(), EMPTY_STRING, tokenState.toJson()));
}
return newResultPromise(newQueryResponse());
} catch (CTSTokenPersistenceException e) {
logger.error("Exception caught obtaining list of sts-issued tokens: " + e, e);
return e.asPromise();
}
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class UmaPolicyApplicationListener method deletePolicies.
private void deletePolicies(String realm, String resourceServerId) {
RealmContext realmContext = new RealmContext(new RootContext());
realmContext.setDnsAlias("/", realm);
final Context context = new AdminSubjectContext(logger, sessionCache, realmContext);
QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.equalTo(new JsonPointer("applicationName"), resourceServerId));
final List<ResourceResponse> resources = new ArrayList<>();
policyResource.handleQuery(context, request, new QueryResourceHandler() {
@Override
public boolean handleResource(ResourceResponse resource) {
resources.add(resource);
return true;
}
}).thenAsync(new AsyncFunction<QueryResponse, List<ResourceResponse>, ResourceException>() {
@Override
public Promise<List<ResourceResponse>, ResourceException> apply(QueryResponse response) {
List<Promise<ResourceResponse, ResourceException>> promises = new ArrayList<>();
for (ResourceResponse policy : resources) {
DeleteRequest deleteRequest = Requests.newDeleteRequest("", policy.getId());
promises.add(policyResource.handleDelete(context, deleteRequest));
}
Promise<List<ResourceResponse>, ResourceException> when = Promises.when(promises);
return when;
}
}).thenOnException(new ExceptionHandler<ResourceException>() {
@Override
public void handleException(ResourceException error) {
logger.error(error.getReason());
}
});
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class UmaPolicyServiceImpl method internalReadPolicy.
/**
* {@inheritDoc}
*/
private Promise<UmaPolicy, ResourceException> internalReadPolicy(final Context context, final String resourceSetId) {
String resourceOwnerUid = getResourceOwnerUid(context);
QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.and(QueryFilter.equalTo(new JsonPointer("resourceTypeUuid"), resourceSetId), QueryFilter.equalTo(new JsonPointer("createdBy"), resourceOwnerUid)));
return policyResourceDelegate.queryPolicies(context, request).thenAsync(new AsyncFunction<Pair<QueryResponse, List<ResourceResponse>>, UmaPolicy, ResourceException>() {
@Override
public Promise<UmaPolicy, ResourceException> apply(Pair<QueryResponse, List<ResourceResponse>> value) {
try {
if (value.getSecond().isEmpty()) {
return new NotFoundException("UMA Policy not found, " + resourceSetId).asPromise();
} else {
ResourceSetDescription resourceSet = getResourceSet(getRealm(context), resourceSetId);
UmaPolicy umaPolicy = UmaPolicy.fromUnderlyingPolicies(resourceSet, value.getSecond());
return newResultPromise(umaPolicy);
}
} catch (ResourceException e) {
return e.asPromise();
}
}
});
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class UmaPolicyServiceImpl method updatePolicy.
/**
* {@inheritDoc}
*/
@Override
public //TODO need to check if need to delete backend policies
Promise<UmaPolicy, ResourceException> updatePolicy(//TODO need to check if need to delete backend policies
final Context context, //TODO need to check if need to delete backend policies
final String resourceSetId, JsonValue policy) {
final UmaPolicy updatedUmaPolicy;
final ResourceSetDescription resourceSet;
try {
resourceSet = getResourceSet(getRealm(context), resourceSetId);
updatedUmaPolicy = UmaPolicy.valueOf(resourceSet, resolveUsernameToUID(context, policy));
boolean canShare = canUserShareResourceSet(resourceSet.getResourceOwnerId(), contextHelper.getUserId(context), resourceSet.getClientId(), getRealm(context), resourceSet.getId(), updatedUmaPolicy.getScopes());
if (!canShare) {
return new ForbiddenException().asPromise();
}
validateScopes(resourceSet, updatedUmaPolicy.getScopes());
} catch (ResourceException e) {
return e.asPromise();
}
return internalReadPolicy(context, resourceSetId).thenAsync(beforeResourceSharedModified(updatedUmaPolicy)).thenOnResult(new ResultHandler<UmaPolicy>() {
@Override
public void handleResult(UmaPolicy currentUmaPolicy) {
Set<String> modifiedScopes = new HashSet<>(updatedUmaPolicy.getScopes());
modifiedScopes.retainAll(currentUmaPolicy.getScopes());
Set<String> removedScopes = new HashSet<>(currentUmaPolicy.getScopes());
removedScopes.removeAll(modifiedScopes);
for (JsonValue policy : currentUmaPolicy.asUnderlyingPolicies(contextHelper.getUserId(context))) {
for (String scope : removedScopes) {
if (policy.get("actionValues").isDefined(scope)) {
policyResourceDelegate.queryPolicies(context, Requests.newQueryRequest("").setQueryFilter(QueryFilter.and(QueryFilter.equalTo(new JsonPointer("createdBy"), contextHelper.getUserUid(context)), QueryFilter.equalTo(new JsonPointer("name"), policy.get("name").asString())))).thenAsync(new DeleteOldPolicyFunction(context));
}
}
}
}
}).thenOnResult(new ResultHandler<UmaPolicy>() {
@Override
public void handleResult(UmaPolicy currentUmaPolicy) {
Set<String> modifiedScopes = new HashSet<>(currentUmaPolicy.getScopes());
modifiedScopes.retainAll(updatedUmaPolicy.getScopes());
Set<String> deletedScopes = new HashSet<>(updatedUmaPolicy.getScopes());
deletedScopes.removeAll(modifiedScopes);
for (JsonValue policy : updatedUmaPolicy.asUnderlyingPolicies(contextHelper.getUserId(context))) {
for (String scope : deletedScopes) {
if (policy.get("actionValues").isDefined(scope)) {
policyResourceDelegate.createPolicies(context, singleton(policy));
}
}
}
}
}).thenOnResult(new ResultHandler<UmaPolicy>() {
@Override
public void handleResult(UmaPolicy currentUmaPolicy) {
String uid = contextHelper.getUserId(context);
Set<String> underlyingPolicyIds = new HashSet<>(currentUmaPolicy.getUnderlyingPolicyIds());
Set<JsonValue> newUnderlyingPolicies = updatedUmaPolicy.asUnderlyingPolicies(uid);
for (JsonValue value : newUnderlyingPolicies) {
underlyingPolicyIds.remove(value.get("name").asString());
}
policyResourceDelegate.deletePolicies(context, underlyingPolicyIds);
}
}).thenAsync(new UpdatePolicyGraphStatesFunction<UmaPolicy>(resourceSet, context)).thenAsync(new UpdateUmaPolicyFunction(context, updatedUmaPolicy, resourceSetId, resourceSet));
}
Aggregations