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