Search in sources :

Example 1 with QueryException

use of org.forgerock.openam.rest.query.QueryException in project OpenAM by OpenRock.

the class ResourceTypesResource method queryCollection.

/**
     * Reads the details of all {@link org.forgerock.openam.entitlement.ResourceType}s in the system.
     *
     * The user's {@link org.forgerock.json.resource.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) {
    String principalName = "unknown";
    String realm = getRealm(context);
    QueryFilter<JsonPointer> queryFilter = request.getQueryFilter();
    try {
        Subject subject = getSubject(context);
        principalName = PrincipalRestUtils.getPrincipalNameFromSubject(subject);
        Map<String, Map<String, Set<String>>> configData = resourceTypeService.getResourceTypesData(subject, realm);
        Set<String> filterResults;
        if (queryFilter == null) {
            filterResults = configData.keySet();
        } else {
            filterResults = queryFilter.accept(new DataQueryFilterVisitor(), configData);
        }
        List<ResourceResponse> results = new ArrayList<>();
        for (String uuid : filterResults) {
            ResourceType resourceType = resourceTypeService.getResourceType(subject, realm, uuid);
            results.add(newResourceResponse(resourceType.getUUID(), null, new JsonResourceType(resourceType).toJsonValue()));
        }
        QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request);
        return QueryResponsePresentation.perform(handler, request, results);
    } catch (EntitlementException ee) {
        if (logger.errorEnabled()) {
            logger.error("ResourceTypesResource :: QUERY by " + principalName + ": Caused EntitlementException: ", ee);
        }
        return exceptionMappingHandler.handleError(context, request, ee).asPromise();
    } catch (QueryException e) {
        return new BadRequestException(e.getL10NMessage(ServerContextUtils.getLocaleFromContext(context))).asPromise();
    }
}
Also used : JsonResourceType(org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType) JsonResourceType(org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType) ResourceType(org.forgerock.openam.entitlement.ResourceType) JsonPointer(org.forgerock.json.JsonPointer) Subject(javax.security.auth.Subject) EntitlementException(com.sun.identity.entitlement.EntitlementException) QueryException(org.forgerock.openam.rest.query.QueryException) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) BadRequestException(org.forgerock.json.resource.BadRequestException) DataQueryFilterVisitor(org.forgerock.openam.rest.query.DataQueryFilterVisitor)

Example 2 with QueryException

use of org.forgerock.openam.rest.query.QueryException in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method queryShouldHandleQueryException.

@Test
public void queryShouldHandleQueryException() throws Exception {
    //given
    setupExistingResourceTypeFromJson(jsonResourceType);
    QueryRequest queryRequest = mock(QueryRequest.class);
    QueryResponseHandler queryHandler = makeQueryResponseHandler();
    Throwable t = new QueryException(QueryException.QueryErrorCode.FILTER_BOOLEAN_LITERAL_FALSE);
    when(resourceTypeService.getResourceTypesData(any(Subject.class), anyString())).thenThrow(t);
    //when
    Promise<QueryResponse, ResourceException> promise = resourceTypesResource.queryCollection(mockServerContext, queryRequest, queryHandler);
    //then
    assertThat(promise).failedWithResourceException().withCode(ResourceException.BAD_REQUEST);
}
Also used : QueryException(org.forgerock.openam.rest.query.QueryException) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponseHandler(org.forgerock.openam.rest.query.QueryResponseHandler) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Aggregations

Subject (javax.security.auth.Subject)2 QueryException (org.forgerock.openam.rest.query.QueryException)2 EntitlementException (com.sun.identity.entitlement.EntitlementException)1 JsonPointer (org.forgerock.json.JsonPointer)1 BadRequestException (org.forgerock.json.resource.BadRequestException)1 QueryRequest (org.forgerock.json.resource.QueryRequest)1 QueryResponse (org.forgerock.json.resource.QueryResponse)1 ResourceException (org.forgerock.json.resource.ResourceException)1 ResourceResponse (org.forgerock.json.resource.ResourceResponse)1 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)1 ResourceType (org.forgerock.openam.entitlement.ResourceType)1 JsonResourceType (org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType)1 DataQueryFilterVisitor (org.forgerock.openam.rest.query.DataQueryFilterVisitor)1 QueryResponseHandler (org.forgerock.openam.rest.query.QueryResponseHandler)1 Test (org.testng.annotations.Test)1