use of org.forgerock.openam.entitlement.ResourceType in project OpenAM by OpenRock.
the class ResourceTypesResource method createInstance.
/**
* Create {@link org.forgerock.openam.entitlement.ResourceType} 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}
*/
@Override
public Promise<ResourceResponse, ResourceException> createInstance(Context context, CreateRequest request) {
if (METHOD_PUT.equalsIgnoreCase(context.asContext(HttpContext.class).getMethod())) {
return getException(METHOD_NOT_ALLOWED).asPromise();
}
String principalName = "unknown";
try {
final Subject subject = getSubject(context);
principalName = PrincipalRestUtils.getPrincipalNameFromSubject(subject);
final JsonResourceType jsonWrapper = createJsonResourceType(request.getContent());
if (StringUtils.isEmpty(jsonWrapper.getName())) {
throw new EntitlementException(MISSING_RESOURCE_TYPE_NAME);
}
// Here we save the resource type and use that returned, since the resource type service
// adds all manner of good stuff - creation dates, updated dates, etc. etc. It is the resource type filled
// out with this extra stuff that we put into the resource and the user gets to see.
//
final ResourceType savedResourceType = resourceTypeService.saveResourceType(subject, getRealm(context), jsonWrapper.getResourceType(true));
if (logger.messageEnabled()) {
logger.message("ResourceTypeResource :: CREATE by " + principalName + ": for Resource Type: " + savedResourceType.getName());
}
return newResultPromise(newResourceResponse(savedResourceType.getUUID(), null, new JsonResourceType(savedResourceType).toJsonValue()));
} catch (EntitlementException e) {
if (logger.errorEnabled()) {
logger.error("ResourceTypeResource :: CREATE by " + principalName + ": Resource Type creation failed. ", e);
}
return exceptionMappingHandler.handleError(context, request, e).asPromise();
}
}
use of org.forgerock.openam.entitlement.ResourceType 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.openam.entitlement.ResourceType 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.entitlement.ResourceType in project OpenAM by OpenRock.
the class ResourceTypeConfigurationImpl method getResourceTypes.
@Override
public Set<ResourceType> getResourceTypes(final QueryFilter<SmsAttribute> queryFilter, final Subject subject, final String realm) throws EntitlementException {
final SSOToken token = SubjectUtils.getSSOToken(subject);
final String dn = getResourceTypeBaseDN(realm);
final Filter filter = queryFilter.accept(new SmsQueryFilterVisitor(), null);
final Set<ResourceType> resourceTypes = new HashSet<ResourceType>();
try {
if (SMSEntry.checkIfEntryExists(dn, token)) {
// Interaction with legacy service.
@SuppressWarnings("unchecked") final Iterator<SMSDataEntry> iterator = (Iterator<SMSDataEntry>) SMSEntry.search(token, dn, filter.toString(), 0, 0, false, false, Collections.emptySet());
while (iterator.hasNext()) {
final SMSDataEntry entry = iterator.next();
final String name = entry.getAttributeValue(CONFIG_NAME);
// Extract the resource types UUID from the LDAP DN representation.
final String uuid = LDAPUtils.getName(DN.valueOf(entry.getDN()));
// Interaction with legacy service.
@SuppressWarnings("unchecked") final Set<String> actionSet = entry.getAttributeValues(CONFIG_ACTIONS);
final Map<String, Boolean> actions = getActions(actionSet);
// Interaction with legacy service.
@SuppressWarnings("unchecked") final Set<String> resources = entry.getAttributeValues(CONFIG_PATTERNS);
final String description = entry.getAttributeValue(CONFIG_DESCRIPTION);
final String createdBy = entry.getAttributeValue(CONFIG_CREATED_BY);
final String creationDate = entry.getAttributeValue(CONFIG_CREATION_DATE);
final String modifiedBy = entry.getAttributeValue(CONFIG_LAST_MODIFIED_BY);
final String modifiedDate = entry.getAttributeValue(CONFIG_LAST_MODIFIED_DATE);
final ResourceType resourceType = ResourceType.builder().setUUID(uuid).setName(name).setActions(actions).setPatterns(resources).setDescription(description).setCreatedBy(createdBy).setCreationDate(Long.parseLong(creationDate)).setLastModifiedBy(modifiedBy).setLastModifiedDate(Long.parseLong(modifiedDate)).build();
resourceTypes.add(resourceType);
}
}
} catch (SMSException smsE) {
throw new EntitlementException(RESOURCE_TYPE_RETRIEVAL_ERROR, realm, smsE);
}
return resourceTypes;
}
use of org.forgerock.openam.entitlement.ResourceType in project OpenAM by OpenRock.
the class ApplicationV1Filter method filterUpdate.
/**
* Update expects the application json to contain both actions and resources; these attributes are part of the old
* json definition for an application. It also expects that the mentioned application exists with exactly one
* resource type - no resource types or many resource types is not acceptable, else it is impossible to determine
* which resource type applies to the set of actions and resources being passed as part of the application json.
* <p/>
* Changes to the actions and/or resources will be reflected in the applications associated resource type.
*
* @param context
* the filter chain context
* @param request
* the update request
* @param next
* a request handler representing the remainder of the filter chain
*/
@Override
public Promise<ResourceResponse, ResourceException> filterUpdate(final Context context, final UpdateRequest request, final RequestHandler next) {
final JsonValue jsonValue = request.getContent();
final Map<String, Boolean> actions = jsonValue.get(ACTIONS).asMap(Boolean.class);
final Set<String> resources = jsonValue.get(RESOURCES).asSet(String.class);
final String bodyRealm = jsonValue.get(REALM).asString();
final String pathRealm = contextHelper.getRealm(context);
if (actions == null) {
return new BadRequestException("Invalid actions defined in request").asPromise();
}
if (resources == null) {
return new BadRequestException("Invalid resources defined in request").asPromise();
}
if (!pathRealm.equals(bodyRealm)) {
return resourceErrorHandler.handleError(context, request, new EntitlementException(EntitlementException.INVALID_APP_REALM, new String[] { bodyRealm, pathRealm })).asPromise();
}
final Subject callingSubject = contextHelper.getSubject(context);
final String applicationName = request.getResourcePath();
try {
final ApplicationService applicationService = applicationServiceFactory.create(callingSubject, pathRealm);
final Application application = applicationService.getApplication(applicationName);
if (application == null) {
return new NotFoundException("Unable to find application " + applicationName).asPromise();
}
if (application.getResourceTypeUuids().size() != 1) {
return new BadRequestException("Cannot modify application with more than one " + "resource type using version 1.0 of this endpoint").asPromise();
}
// Retrieve the resource type from the applications single resource type.
final String resourceTypeUuid = application.getResourceTypeUuids().iterator().next();
ResourceType resourceType = resourceTypeService.getResourceType(callingSubject, pathRealm, resourceTypeUuid);
boolean resourceTypeModified = false;
if (!actions.equals(resourceType.getActions())) {
resourceTypeModified = true;
resourceType = resourceType.populatedBuilder().setActions(actions).build();
}
if (!resources.equals(resourceType.getPatterns())) {
resourceTypeModified = true;
resourceType = resourceType.populatedBuilder().setPatterns(resources).build();
}
if (resourceTypeModified) {
resourceTypeService.updateResourceType(callingSubject, pathRealm, resourceType);
}
// Ensure the resource type UUID isn't lost.
jsonValue.put(RESOURCE_TYPE_UUIDS, new HashSet<String>(Arrays.asList(resourceTypeUuid)));
} catch (EntitlementException eE) {
debug.error("Error filtering application update CREST request", eE);
return resourceErrorHandler.handleError(context, request, eE).asPromise();
}
// Forward onto next handler.
return applicationTransformer.transform(next.handleUpdate(context, request), context);
}
Aggregations