Search in sources :

Example 51 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class SubjectTypesResource method readInstance.

/**
     * {@inheritDoc}
     *
     * Uses the {@link EntitlementRegistry} to locate the {@link EntitlementSubject} to return.
     */
@Override
public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId, ReadRequest request) {
    final Class<? extends EntitlementSubject> subjectClass = entitlementRegistry.getSubjectType(resourceId);
    final String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
    if (subjectClass == null) {
        if (debug.errorEnabled()) {
            debug.error("SubjectTypesResource :: READ by " + principalName + "Requested subject short name not found: " + resourceId);
        }
        return new NotFoundException().asPromise();
    }
    final JsonValue json = jsonify(subjectClass, resourceId, LogicalSubject.class.isAssignableFrom(subjectClass));
    final ResourceResponse resource = newResourceResponse(resourceId, String.valueOf(System.currentTimeMillis()), json);
    return newResultPromise(resource);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) LogicalSubject(com.sun.identity.entitlement.LogicalSubject) JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException)

Example 52 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class ApplicationTypesResource method queryCollection.

/**
     * Reads the details of all {@link ApplicationType}s in the system.
     *
     * The user's {@link SecurityContext} must indicate they are a user with administrator-level access.
     *
     * @param context {@inheritDoc}
     * @param request {@inheritDoc}
     * @param handler {@inheritDoc}
     */
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler handler) {
    //auth
    final Subject mySubject = getContextSubject(context);
    if (mySubject == null) {
        debug.error("ApplicationsTypesResource :: QUERY : Unknown Subject");
        return new InternalServerErrorException().asPromise();
    }
    final String principalName = PrincipalRestUtils.getPrincipalNameFromSubject(mySubject);
    //select
    final Set<String> appTypeNames = typeManager.getApplicationTypeNames(mySubject);
    List<ApplicationTypeWrapper> appTypes = new LinkedList<>();
    for (String appTypeName : appTypeNames) {
        final ApplicationType type = typeManager.getApplicationType(mySubject, appTypeName);
        final ApplicationTypeWrapper wrap = new ApplicationTypeWrapper(type);
        if (type != null) {
            appTypes.add(wrap);
        } else {
            if (debug.warningEnabled()) {
                debug.warning("ApplicationTypesResource :: QUERY by " + principalName + ": ApplicationType was not found: " + appTypeName);
            }
        }
    }
    final List<ResourceResponse> applicationsList = getResourceResponses(appTypes);
    QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request);
    return QueryResponsePresentation.perform(handler, request, applicationsList);
}
Also used : ApplicationType(com.sun.identity.entitlement.ApplicationType) ApplicationTypeWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationTypeWrapper) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) Subject(javax.security.auth.Subject) LinkedList(java.util.LinkedList)

Example 53 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class ApplicationsResource method queryCollection.

/**
     * Queries for a collection of resources.
     *
     * @param context {@inheritDoc}
     * @param request {@inheritDoc}
     * @param handler {@inheritDoc}
     */
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler handler) {
    //auth
    final Subject mySubject = getContextSubject(context);
    if (mySubject == null) {
        debug.error("ApplicationsResource :: UPDATE : Unknown Subject");
        return new BadRequestException().asPromise();
    }
    //select
    final String realm = getRealm(context);
    final String principalName = PrincipalRestUtils.getPrincipalNameFromSubject(mySubject);
    try {
        List<ResourceResponse> results = new ArrayList<>();
        final Set<String> appNames = query(request, mySubject, realm);
        for (String appName : appNames) {
            final Application application = appManager.getApplication(mySubject, realm, appName);
            if (application == null) {
                debug.warning("Unable to find application " + appName);
                continue;
            }
            ApplicationWrapper wrapper = createApplicationWrapper(application, appTypeManagerWrapper);
            results.add(newResourceResponse(wrapper.getName(), null, wrapper.toJsonValue()));
        }
        QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request);
        return QueryResponsePresentation.perform(handler, request, results);
    } catch (EntitlementException e) {
        if (debug.errorEnabled()) {
            debug.error("ApplicationsResource :: QUERY by " + principalName + ": Failed to query resource.", e);
        }
        return exceptionMappingHandler.handleError(context, request, e).asPromise();
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) ApplicationWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper) ResourceResponse(org.forgerock.json.resource.ResourceResponse) BadRequestException(org.forgerock.json.resource.BadRequestException) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject)

Example 54 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class PagingQueryResponseHandlerTest method shouldRespectOffset.

@Test
public void shouldRespectOffset() {
    // Given
    testHandler = new PagingQueryResponseHandler(mockHandler, 1, 1);
    ResourceResponse expectedResource = newResourceResponse("b", null, null);
    // When
    testHandler.handleResource(newResourceResponse("ignored", null, null));
    testHandler.handleResource(expectedResource);
    // Then
    verify(mockHandler).handleResource(expectedResource);
    verifyNoMoreInteractions(mockHandler);
}
Also used : Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) Test(org.testng.annotations.Test)

Example 55 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class ScriptResource method queryCollection.

@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler resultHandler) {
    final QueryFilter<JsonPointer> filter = request.getQueryFilter();
    try {
        final Set<ScriptConfiguration> configs;
        if (filter == null) {
            configs = serviceFactory.create(getContextSubject(context), getRealm(context)).getAll();
        } else {
            QueryFilter<String> stringQueryFilter = filter.accept(new QueryByStringFilterConverter(), null);
            configs = serviceFactory.create(getContextSubject(context), getRealm(context)).get(stringQueryFilter);
        }
        List<ResourceResponse> results = new ArrayList<>();
        for (ScriptConfiguration configuration : configs) {
            String id = configuration.getId();
            results.add(newResourceResponse(id, null, asJson(configuration)));
        }
        QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request);
        return QueryResponsePresentation.perform(resultHandler, request, results);
    } catch (ScriptException se) {
        return exceptionMappingHandler.handleError(context, request, se).asPromise();
    }
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryByStringFilterConverter(org.forgerock.openam.rest.query.QueryByStringFilterConverter) ArrayList(java.util.ArrayList) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) JsonPointer(org.forgerock.json.JsonPointer)

Aggregations

ResourceResponse (org.forgerock.json.resource.ResourceResponse)227 ResourceException (org.forgerock.json.resource.ResourceException)171 Test (org.testng.annotations.Test)154 JsonValue (org.forgerock.json.JsonValue)123 Context (org.forgerock.services.context.Context)83 RealmContext (org.forgerock.openam.rest.RealmContext)72 Subject (javax.security.auth.Subject)57 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)48 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)44 CreateRequest (org.forgerock.json.resource.CreateRequest)41 ClientContext (org.forgerock.services.context.ClientContext)41 ArrayList (java.util.ArrayList)38 EntitlementException (com.sun.identity.entitlement.EntitlementException)30 ReadRequest (org.forgerock.json.resource.ReadRequest)30 NotFoundException (org.forgerock.json.resource.NotFoundException)29 UpdateRequest (org.forgerock.json.resource.UpdateRequest)29 Matchers.anyString (org.mockito.Matchers.anyString)28 HashSet (java.util.HashSet)27 BadRequestException (org.forgerock.json.resource.BadRequestException)26 DeleteRequest (org.forgerock.json.resource.DeleteRequest)26