Search in sources :

Example 46 with JsonValue

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);
}
Also used : JsonValue(org.forgerock.json.JsonValue) Tag(org.restlet.data.Tag)

Example 47 with JsonValue

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);
}
Also used : JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 48 with JsonValue

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);
}
Also used : ServerException(org.forgerock.openam.sm.datalayer.store.ServerException) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) AMIdentity(com.sun.identity.idm.AMIdentity) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) UmaAuditEntry(org.forgerock.openam.sm.datalayer.impl.uma.UmaAuditEntry)

Example 49 with JsonValue

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();
    }
}
Also used : Promise(org.forgerock.util.promise.Promise) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) UmaPendingRequest(org.forgerock.openam.sm.datalayer.impl.uma.UmaPendingRequest) ResourceException(org.forgerock.json.resource.ResourceException) NotSupportedException(org.forgerock.json.resource.NotSupportedException)

Example 50 with JsonValue

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);
        }
    }
}
Also used : JsonValue(org.forgerock.json.JsonValue) JsonPointer(org.forgerock.json.JsonPointer) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JsonValue (org.forgerock.json.JsonValue)575 Test (org.testng.annotations.Test)333 ResourceException (org.forgerock.json.resource.ResourceException)144 ResourceResponse (org.forgerock.json.resource.ResourceResponse)123 RealmContext (org.forgerock.openam.rest.RealmContext)70 Context (org.forgerock.services.context.Context)63 HashSet (java.util.HashSet)56 SSOException (com.iplanet.sso.SSOException)54 ArrayList (java.util.ArrayList)51 BadRequestException (org.forgerock.json.resource.BadRequestException)47 Privilege (com.sun.identity.entitlement.Privilege)46 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)46 SSOToken (com.iplanet.sso.SSOToken)43 SMSException (com.sun.identity.sm.SMSException)42 HashMap (java.util.HashMap)42 NotFoundException (org.forgerock.json.resource.NotFoundException)41 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)41 CreateRequest (org.forgerock.json.resource.CreateRequest)40 OpenSSOPrivilege (com.sun.identity.entitlement.opensso.OpenSSOPrivilege)39 Subject (javax.security.auth.Subject)32