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();
}
}
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);
}
Aggregations