use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class ResourceSetRegistrationEndpoint method generateETag.
private Tag generateETag(ResourceSetDescription resourceSetDescription) {
int hashCode = resourceSetDescription.hashCode();
JsonValue description = resourceSetDescription.getDescription();
if (!description.isDefined(OAuth2Constants.ResourceSets.LABELS)) {
description.put(OAuth2Constants.ResourceSets.LABELS, null);
hashCode = resourceSetDescription.hashCode();
description.remove(OAuth2Constants.ResourceSets.LABELS);
}
return new Tag(Integer.toString(hashCode), true);
}
use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class JsonValueToJsonBytesConverterTest method testMapRoundTrip.
@Test
public void testMapRoundTrip() throws Exception {
//Given
JsonValue jsonValue = json(object(field("name", "fred"), field("list", array(1, 2, 3))));
//When
byte[] bytes = converter.convertFrom(jsonValue);
JsonValue value = converter.convertBack(bytes);
//Then
assertThat(value.isMap()).isTrue();
assertThat(value.isList()).isFalse();
assertThat(value.asMap().keySet()).contains("name", "list");
assertThat(value.get("name").isString()).isTrue();
assertThat(value.get("name").asString()).isEqualTo("fred");
assertThat(value.get("list").isList()).isTrue();
assertThat(value.get("list").asList()).contains(1, 2, 3);
}
use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class AuditHistory method queryCollection.
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler handler) {
AMIdentity identity = getIdentity(context);
Set<UmaAuditEntry> history;
try {
if (request.getQueryFilter().toString().equals("true")) {
history = auditLogger.getEntireHistory(identity);
} else {
history = auditLogger.getHistory(identity, request);
}
} catch (ServerException e) {
return new InternalServerErrorException(e).asPromise();
}
List<ResourceResponse> results = new ArrayList<>();
for (UmaAuditEntry entry : history) {
JsonValue result = entry.asJson();
results.add(newResourceResponse(entry.getId(), String.valueOf(result.hashCode()), result));
}
QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request);
return QueryResponsePresentation.perform(handler, request, results);
}
use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class PendingRequestResource method actionCollection.
@Override
public Promise<ActionResponse, ResourceException> actionCollection(Context context, ActionRequest request) {
try {
if (APPROVE_ACTION_ID.equalsIgnoreCase(request.getAction())) {
List<Promise<Void, ResourceException>> promises = new ArrayList<>();
JsonValue content = request.getContent();
for (UmaPendingRequest pendingRequest : queryResourceOwnerPendingRequests(context)) {
promises.add(service.approvePendingRequest(context, pendingRequest.getId(), content.get(pendingRequest.getId()), ServerContextUtils.getRealm(context)));
}
return handlePendingRequestApproval(promises);
} else if (DENY_ACTION_ID.equalsIgnoreCase(request.getAction())) {
for (UmaPendingRequest pendingRequest : queryResourceOwnerPendingRequests(context)) {
service.denyPendingRequest(pendingRequest.getId(), ServerContextUtils.getRealm(context));
}
return newResultPromise(newActionResponse((json(object()))));
} else {
return new NotSupportedException("Action, " + request.getAction() + ", is not supported.").asPromise();
}
} catch (ResourceException e) {
return e.asPromise();
}
}
use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class PolicyGraph method moveScope.
/**
* Moves the scopes that are incorrectly active/inactive to a policy that has the opposite state.
* @param moveFrom A map of policy owners to policies the scope is incorrectly currently in.
* @param moveTo A map of policy owner to existing policies the scope might be moved to.
* @param context The context for passing to the policy resource delegate.
* @param policyResourceDelegate To be used for deleting any policies that are emptied of scopes (actions).
* @param allMovingRights All the scopes that need switching state.
* @param createdPolicies Policies that are being created by this update.
* @param updatedPolicies Policies that are being updated by this update.
* @param scope The current scope being operated on.
* @param newPolicyActive Whether the scope is being moved to active state.
* @param promises Promises for all policy updates.
* @param user The user for whom we are switching scope state.
* @throws BadRequestException If the UmaPolicy cannot be created for new policy.
*/
private void moveScope(Map<String, JsonValue> moveFrom, Map<String, JsonValue> moveTo, Context context, PolicyResourceDelegate policyResourceDelegate, Set<String> allMovingRights, Set<JsonValue> createdPolicies, Set<JsonValue> updatedPolicies, String scope, boolean newPolicyActive, List<Promise<List<ResourceResponse>, ResourceException>> promises, String user) throws BadRequestException {
JsonPointer scopePointer = new JsonPointer(BACKEND_POLICY_ACTION_VALUES_KEY).child(scope);
for (Map.Entry<String, JsonValue> ownedPolicy : moveFrom.entrySet()) {
String owner = ownedPolicy.getKey();
JsonValue policy = ownedPolicy.getValue();
JsonValue ownedMoveTo = moveTo.get(owner);
boolean policyToMoveToAlreadyExists = ownedMoveTo != null;
if (policyToMoveToAlreadyExists) {
ownedMoveTo.put(scopePointer, true);
// If this policy is being created already, no need to update.
if (!createdPolicies.contains(ownedMoveTo)) {
updatedPolicies.add(ownedMoveTo);
}
policy.remove(scopePointer);
} else if (allScopesAreSwitchingState(allMovingRights, policy)) {
policy.put(ACTIVE_KEY, true);
} else {
// Create a new policy to move to
JsonValue newPolicy = UmaPolicy.valueOf(resourceSet, json(object(field(POLICY_ID_KEY, resourceSet.getId()), field(PERMISSIONS_KEY, array(object(field(SUBJECT_KEY, user), field(SCOPES_KEY, array(scope)))))))).asUnderlyingPolicies(owner).iterator().next();
newPolicy.put(ACTIVE_KEY, newPolicyActive);
createdPolicies.add(newPolicy);
moveTo.put(owner, newPolicy);
policy.remove(scopePointer);
}
if (policy.get(BACKEND_POLICY_ACTION_VALUES_KEY).size() == 0) {
// No scopes left in the policy, so it can be removed.
updatedPolicies.remove(policy);
promises.add(policyResourceDelegate.deletePolicies(context, singleton(policy.get("_id").asString())));
} else {
updatedPolicies.add(policy);
}
}
}
Aggregations