use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class TrustedDevicesResourceTest method shouldQueryTrustedDevices.
@Test
public void shouldQueryTrustedDevices() throws ResourceException {
//Given
QueryRequest request = Requests.newQueryRequest("");
Connection connection = newInternalConnection(newCollection(resource));
QueryResourceHandler handler = mock(QueryResourceHandler.class);
List<JsonValue> devices = new ArrayList<>();
devices.add(json(object(field("name", "NAME_1"), field("lastSelectedDate", new Date().getTime()))));
devices.add(json(object(field("name", "NAME_2"), field("lastSelectedDate", new Date().getTime() + 1000))));
given(dao.getDeviceProfiles(anyString(), anyString())).willReturn(devices);
//When
connection.query(ctx(), request, handler);
//Then
verify(handler, times(2)).handleResource(Matchers.<ResourceResponse>anyObject());
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class AuditRestRouteProvider method addResourceRoutes.
@Override
public void addResourceRoutes(ResourceRouter rootRouter, ResourceRouter realmRouter) {
rootRouter.route("global-audit").auditAs(AUDIT, AuditEndpointAuditFilter.class).authorizeWith(SpecialOrAdminOrAgentAuthzModule.class).forVersion(1).toRequestHandler(STARTS_WITH, auditServiceProvider.getDefaultAuditService());
rootRouter.route("realm-audit").auditAs(AUDIT, AuditEndpointAuditFilter.class).authorizeWith(SpecialOrAdminOrAgentAuthzModule.class).forVersion(1).through(RealmContextFilter.class).toRequestHandler(STARTS_WITH, new RequestHandler() {
@Override
public Promise<ActionResponse, ResourceException> handleAction(Context context, ActionRequest actionRequest) {
return getAuditService(context).handleAction(context, actionRequest);
}
@Override
public Promise<ResourceResponse, ResourceException> handleCreate(Context context, CreateRequest createRequest) {
return getAuditService(context).handleCreate(context, createRequest);
}
@Override
public Promise<ResourceResponse, ResourceException> handleDelete(Context context, DeleteRequest deleteRequest) {
return getAuditService(context).handleDelete(context, deleteRequest);
}
@Override
public Promise<ResourceResponse, ResourceException> handlePatch(Context context, PatchRequest patchRequest) {
return getAuditService(context).handlePatch(context, patchRequest);
}
@Override
public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest queryRequest, QueryResourceHandler queryResourceHandler) {
return getAuditService(context).handleQuery(context, queryRequest, queryResourceHandler);
}
@Override
public Promise<ResourceResponse, ResourceException> handleRead(Context context, ReadRequest readRequest) {
return getAuditService(context).handleRead(context, readRequest);
}
@Override
public Promise<ResourceResponse, ResourceException> handleUpdate(Context context, UpdateRequest updateRequest) {
return getAuditService(context).handleUpdate(context, updateRequest);
}
private AMAuditService getAuditService(Context context) {
String realm = context.asContext(RealmContext.class).getResolvedRealm();
if (StringUtils.isEmpty(realm)) {
logger.warn("Context contained RealmContext but had an empty resolved realm");
return auditServiceProvider.getDefaultAuditService();
}
return auditServiceProvider.getAuditService(realm);
}
});
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class UmaPolicyApplicationListener method deletePolicies.
private void deletePolicies(String realm, String resourceServerId) {
RealmContext realmContext = new RealmContext(new RootContext());
realmContext.setDnsAlias("/", realm);
final Context context = new AdminSubjectContext(logger, sessionCache, realmContext);
QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.equalTo(new JsonPointer("applicationName"), resourceServerId));
final List<ResourceResponse> resources = new ArrayList<>();
policyResource.handleQuery(context, request, new QueryResourceHandler() {
@Override
public boolean handleResource(ResourceResponse resource) {
resources.add(resource);
return true;
}
}).thenAsync(new AsyncFunction<QueryResponse, List<ResourceResponse>, ResourceException>() {
@Override
public Promise<List<ResourceResponse>, ResourceException> apply(QueryResponse response) {
List<Promise<ResourceResponse, ResourceException>> promises = new ArrayList<>();
for (ResourceResponse policy : resources) {
DeleteRequest deleteRequest = Requests.newDeleteRequest("", policy.getId());
promises.add(policyResource.handleDelete(context, deleteRequest));
}
Promise<List<ResourceResponse>, ResourceException> when = Promises.when(promises);
return when;
}
}).thenOnException(new ExceptionHandler<ResourceException>() {
@Override
public void handleException(ResourceException error) {
logger.error(error.getReason());
}
});
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class UmaPolicyServiceImpl method internalReadPolicy.
/**
* {@inheritDoc}
*/
private Promise<UmaPolicy, ResourceException> internalReadPolicy(final Context context, final String resourceSetId) {
String resourceOwnerUid = getResourceOwnerUid(context);
QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.and(QueryFilter.equalTo(new JsonPointer("resourceTypeUuid"), resourceSetId), QueryFilter.equalTo(new JsonPointer("createdBy"), resourceOwnerUid)));
return policyResourceDelegate.queryPolicies(context, request).thenAsync(new AsyncFunction<Pair<QueryResponse, List<ResourceResponse>>, UmaPolicy, ResourceException>() {
@Override
public Promise<UmaPolicy, ResourceException> apply(Pair<QueryResponse, List<ResourceResponse>> value) {
try {
if (value.getSecond().isEmpty()) {
return new NotFoundException("UMA Policy not found, " + resourceSetId).asPromise();
} else {
ResourceSetDescription resourceSet = getResourceSet(getRealm(context), resourceSetId);
UmaPolicy umaPolicy = UmaPolicy.fromUnderlyingPolicies(resourceSet, value.getSecond());
return newResultPromise(umaPolicy);
}
} catch (ResourceException e) {
return e.asPromise();
}
}
});
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class PolicyResourceDelegateTest method shouldQueryPolicies.
@Test
public void shouldQueryPolicies() throws ResourceException {
//Given
Context context = mock(Context.class);
QueryRequest request = mock(QueryRequest.class);
QueryResourceHandler handler = mock(QueryResourceHandler.class);
//When
delegate.queryPolicies(context, request, handler);
//Then
verify(policyResource).handleQuery(context, request, handler);
}
Aggregations