Search in sources :

Example 1 with JsonResourceType

use of org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType 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();
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) JsonResourceType(org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType) JsonResourceType(org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType) ResourceType(org.forgerock.openam.entitlement.ResourceType) Subject(javax.security.auth.Subject)

Example 2 with JsonResourceType

use of org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType 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();
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) JsonResourceType(org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonResourceType(org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType) ResourceType(org.forgerock.openam.entitlement.ResourceType) Subject(javax.security.auth.Subject)

Example 3 with JsonResourceType

use of org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType 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();
    }
}
Also used : JsonResourceType(org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType) JsonResourceType(org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType) ResourceType(org.forgerock.openam.entitlement.ResourceType) JsonPointer(org.forgerock.json.JsonPointer) Subject(javax.security.auth.Subject) EntitlementException(com.sun.identity.entitlement.EntitlementException) QueryException(org.forgerock.openam.rest.query.QueryException) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) BadRequestException(org.forgerock.json.resource.BadRequestException) DataQueryFilterVisitor(org.forgerock.openam.rest.query.DataQueryFilterVisitor)

Example 4 with JsonResourceType

use of org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType in project OpenAM by OpenRock.

the class ResourceTypesResource method updateInstance.

/**
     * Update a {@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> updateInstance(Context context, String resourceId, UpdateRequest request) {
    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);
        }
        ResourceType resourceTypeToUpdate = jsonWrapper.getResourceType(false);
        if (!StringUtils.isEqualTo(resourceId, resourceTypeToUpdate.getUUID())) {
            throw new EntitlementException(RESOURCE_TYPE_ID_MISMATCH);
        }
        final ResourceType updatedResourceType = resourceTypeService.updateResourceType(subject, getRealm(context), resourceTypeToUpdate);
        if (logger.messageEnabled()) {
            logger.message("ResourceTypeResource :: UPDATE by " + principalName + ": for Resource Type: " + jsonWrapper.getName());
        }
        return newResultPromise(newResourceResponse(updatedResourceType.getUUID(), null, new JsonResourceType(updatedResourceType).toJsonValue()));
    } catch (EntitlementException e) {
        if (logger.errorEnabled()) {
            logger.error("ResourceTypeResource :: UPDATE by " + principalName + ": Resource Type update failed. ", e);
        }
        return exceptionMappingHandler.handleError(context, request, e).asPromise();
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) JsonResourceType(org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType) JsonResourceType(org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType) ResourceType(org.forgerock.openam.entitlement.ResourceType) Subject(javax.security.auth.Subject)

Aggregations

EntitlementException (com.sun.identity.entitlement.EntitlementException)4 Subject (javax.security.auth.Subject)4 ResourceType (org.forgerock.openam.entitlement.ResourceType)4 JsonResourceType (org.forgerock.openam.entitlement.rest.wrappers.JsonResourceType)4 ResourceResponse (org.forgerock.json.resource.ResourceResponse)2 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)2 JsonPointer (org.forgerock.json.JsonPointer)1 BadRequestException (org.forgerock.json.resource.BadRequestException)1 DataQueryFilterVisitor (org.forgerock.openam.rest.query.DataQueryFilterVisitor)1 QueryException (org.forgerock.openam.rest.query.QueryException)1