use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class ApplicationsResourceTest method reservedInternalAppIsMappedDuringQuery.
@Test
public void reservedInternalAppIsMappedDuringQuery() throws EntitlementException, IllegalAccessException, InstantiationException {
// Override the creation of the application wrapper so to return a mocked version.
applicationsResource = new ApplicationsResource(debug, applicationManagerWrapper, applicationTypeManagerWrapper, queryAttributes, resourceErrorHandler) {
@Override
protected ApplicationWrapper createApplicationWrapper(Application application, ApplicationTypeManagerWrapper type) {
ApplicationWrapper wrapper = mock(ApplicationWrapper.class);
String appName = application.getName();
given(wrapper.getName()).willReturn(appName);
try {
JsonValue jsonValue = JsonValueBuilder.jsonValue().put("name", "agentProtectedApplication").build();
given(wrapper.toJsonValue()).willReturn(jsonValue);
} catch (EntitlementException e) {
fail();
}
return wrapper;
}
};
// Given...
SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
RealmContext realmContext = new RealmContext(mockSubjectContext);
realmContext.setSubRealm("abc", "abc");
Context serverContext = ClientContext.newInternalClientContext(realmContext);
QueryRequest request = mock(QueryRequest.class);
given(request.getSortKeys()).willReturn(Arrays.asList(SortKey.ascendingOrder("name")));
Subject subject = new Subject();
given(mockSubjectContext.getCallerSubject()).willReturn(subject);
Set<String> appNames = asSet("iPlanetAMWebAgentService");
given(applicationManagerWrapper.search(eq(subject), eq("/abc"), any(Set.class))).willReturn(appNames);
Application app = mock(Application.class);
given(applicationManagerWrapper.getApplication(eq(subject), eq("/abc"), eq("iPlanetAMWebAgentService"))).willReturn(app);
given(app.getName()).willReturn("agentProtectedApplication");
QueryResourceHandler handler = mock(QueryResourceHandler.class);
given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
// When...
applicationsResource.queryCollection(serverContext, request, handler);
// Then...
verify(applicationManagerWrapper).search(eq(subject), eq("/abc"), any(Set.class));
verify(applicationManagerWrapper).getApplication(eq(subject), eq("/abc"), anyString());
ArgumentCaptor<ResourceResponse> resourceCapture = ArgumentCaptor.forClass(ResourceResponse.class);
verify(handler).handleResource(resourceCapture.capture());
ResourceResponse resource = resourceCapture.getValue();
assertThat(resource.getId()).isEqualTo("agentProtectedApplication");
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class ApplicationTypesResource method readInstance.
/**
* Reads the details of a single instance of an {@link ApplicationType} - the instance
* referred to by the passed-in resourceId.
*
* The user's {@link SecurityContext} must indicate they are a user with administrator-level access.
*
* @param context {@inheritDoc}
* @param resourceId {@inheritDoc}
* @param request {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId, ReadRequest request) {
//auth
final Subject mySubject = getContextSubject(context);
if (mySubject == null) {
debug.error("ApplicationsTypesResource :: READ : Unknown Subject");
return new InternalServerErrorException().asPromise();
}
final String principalName = PrincipalRestUtils.getPrincipalNameFromSubject(mySubject);
final ApplicationType applType = typeManager.getApplicationType(mySubject, resourceId);
final ApplicationTypeWrapper wrap = new ApplicationTypeWrapper(applType);
if (applType == null) {
if (debug.errorEnabled()) {
debug.error("ApplicationTypesResource :: READ by " + principalName + ": Requested application type short name not found: " + resourceId);
}
return new NotFoundException().asPromise();
}
try {
final ResourceResponse resource = newResourceResponse(resourceId, String.valueOf(System.currentTimeMillis()), JsonValue.json(wrap.toJsonValue()));
return newResultPromise(resource);
} catch (IOException e) {
if (debug.errorEnabled()) {
debug.error("ApplicationTypesResource :: READ by " + principalName + ": Could not jsonify class associated with defined Type: " + resourceId, e);
}
return new InternalServerErrorException().asPromise();
}
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class DecisionCombinersResource method queryCollection.
/**
* {@inheritDoc}
*/
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler handler) {
final Set<String> combinerTypeNames = new TreeSet<>();
List<ResourceResponse> combinerTypes = new ArrayList<>();
final String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
combinerTypeNames.addAll(entitlementRegistry.getCombinersShortNames());
for (String combinerTypeName : combinerTypeNames) {
final Class<? extends EntitlementCombiner> conditionClass = entitlementRegistry.getCombinerType(combinerTypeName);
if (conditionClass == null) {
if (debug.warningEnabled()) {
debug.warning("DecisionCombinersResource :: QUERY by " + principalName + ": Listed combiner short name not found: " + combinerTypeName);
}
continue;
}
final JsonValue json = jsonify(combinerTypeName);
if (json != null) {
if (json != null) {
String id = json.get(JSON_OBJ_TITLE).asString();
combinerTypes.add(newResourceResponse(id, null, json));
}
}
}
QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request);
return QueryResponsePresentation.perform(handler, request, combinerTypes);
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class ResourceTypesResource method readInstance.
/**
* Reads the details of a single instance of an {@link org.forgerock.openam.entitlement.ResourceType} - the instance
* referred to by the passed-in resourceId.
*
* The user's {@link org.forgerock.json.resource.SecurityContext} must indicate they are a user with
* administrator-level access.
*
* @param context {@inheritDoc}
* @param resourceId {@inheritDoc}
* @param request {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId, ReadRequest request) {
String principalName = "unknown";
try {
Subject theSubject = getSubject(context);
principalName = PrincipalRestUtils.getPrincipalNameFromSubject(theSubject);
final String realm = getRealm(context);
ResourceType resourceType = resourceTypeService.getResourceType(theSubject, realm, resourceId);
if (resourceType == null) {
throw new EntitlementException(NO_SUCH_RESOURCE_TYPE, resourceId, realm);
}
JsonResourceType wrapper = new JsonResourceType(resourceType);
final ResourceResponse resource = newResourceResponse(resourceId, String.valueOf(System.currentTimeMillis()), JsonValue.json(wrapper.toJsonValue()));
return newResultPromise(resource);
} catch (EntitlementException ee) {
if (logger.errorEnabled()) {
logger.error("ResourceTypesResource :: READ by " + principalName + ": Could not jsonify class associated with defined Type: " + resourceId, ee);
}
return exceptionMappingHandler.handleError(context, request, ee).asPromise();
}
}
use of org.forgerock.json.resource.ResourceResponse 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();
}
}
Aggregations