class ApplicationV1Filter implements Filter {

    @Override
    public Promise<ResourceResponse, ResourceException> filterCreate(final Context context,
            final CreateRequest 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();
        }

        try {
            final ResourceType resourceType = findOrCreateResourceType(actions, resources, context, request);
            jsonValue.put(RESOURCE_TYPE_UUIDS, new HashSet<>(Arrays.asList(resourceType.getUUID())));

            // Forward onto next handler.
            return applicationTransformer.transform(next.handleCreate(context, request), context);

        } catch (EntitlementException eE) {
            debug.error("Error filtering application create CREST request", eE);
            return resourceErrorHandler.handleError(context, request, eE).asPromise();
        }
    }

    @Inject public  ApplicationV1Filter(final ResourceTypeService resourceTypeService,
                               final ApplicationServiceFactory applicationServiceFactory,
                               final ExceptionMappingHandler<EntitlementException, ResourceException> resourceErrorHandler,
                               final ContextHelper contextHelper,
                               final ApplicationV1FilterTransformer applicationTransformer,
                               @Named("frRest") final Debug debug);

}