Search in sources :

Example 1 with CacheHandler

use of org.swisspush.gateleen.cache.CacheHandler in project gateleen by swisspush.

the class RunConfig method buildRoutingContextHandler.

/**
 * Builds a handler for {@link RoutingContext}s with a "default" behaviour.
 */
public Handler<RoutingContext> buildRoutingContextHandler() {
    // add refreshables
    if (propertyHandler != null) {
        if (router != null) {
            propertyHandler.addRefreshable(router);
        }
        if (schedulerResourceManager != null) {
            propertyHandler.addRefreshable(schedulerResourceManager);
        }
    }
    return new Handler<RoutingContext>() {

        @Override
        public void handle(final RoutingContext ctx) {
            HttpServerRequest request = ctx.request();
            if (!request.headers().contains("x-rp-unique_id")) {
                request.headers().set("x-rp-unique_id", new UUID(random.nextLong(), random.nextLong()).toString().replace("-", ""));
            }
            request.exceptionHandler(exception -> LoggerFactory.getLogger(verticleClass).trace("Exception in client", exception));
            logRequest(request);
            if (qosHandler != null && qosHandler.handle(request)) {
                return;
            }
            if (corsHandler != null) {
                corsHandler.handle(request);
                if (corsHandler.isOptionsRequest(request)) {
                    return;
                }
            }
            if (contentTypeConstraintHandler != null && contentTypeConstraintHandler.handle(request)) {
                return;
            }
            if (authorizer != null) {
                authorizer.authorize(request).onComplete(event -> {
                    if (event.succeeded() && event.result()) {
                        handleRequest(request);
                    } else if (event.failed()) {
                        ResponseStatusCodeLogUtil.info(request, StatusCode.INTERNAL_SERVER_ERROR, RunConfig.class);
                        request.response().setStatusCode(StatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
                        request.response().setStatusMessage(StatusCode.INTERNAL_SERVER_ERROR.getStatusMessage());
                        request.response().end(event.cause().getMessage());
                    }
                });
            } else {
                handleRequest(request);
            }
        }

        private void handleRequest(final HttpServerRequest request) {
            if (request.path().equals(SERVER_ROOT + "/cleanup")) {
                QueuingHandler.cleanup(vertx);
                request.response().end();
                return;
            }
            if (PackingHandler.isPacked(request)) {
                request.bodyHandler(new PackingHandler(request, new QueuingHandler(vertx, redisAPI, request, monitoringHandler)));
            } else {
                if (QueuingHandler.isQueued(request)) {
                    setISO8601Timestamps(request);
                    request.bodyHandler(new QueuingHandler(vertx, redisAPI, request, monitoringHandler));
                } else {
                    if (cacheHandler != null && cacheHandler.handle(request)) {
                        return;
                    }
                    if (copyResourceHandler != null && copyResourceHandler.handle(request)) {
                        return;
                    }
                    if (request.path().startsWith(SERVER_ROOT + "/queuing/")) {
                        queueBrowser.handle(request);
                        return;
                    }
                    if (hookHandler != null && hookHandler.handle(request)) {
                        return;
                    }
                    if (eventBusHandler != null && eventBusHandler.handle(request)) {
                        return;
                    }
                    if (kafkaHandler != null && kafkaHandler.handle(request)) {
                        return;
                    }
                    if (validationHandler != null && validationHandler.isToValidate(request)) {
                        validationHandler.handle(request);
                        return;
                    }
                    if (loggingResourceManager != null && loggingResourceManager.handleLoggingResource(request)) {
                        return;
                    }
                    if (configurationResourceManager != null && configurationResourceManager.handleConfigurationResource(request)) {
                        return;
                    }
                    if (validationResourceManager != null && validationResourceManager.handleValidationResource(request)) {
                        return;
                    }
                    if (schedulerResourceManager != null && schedulerResourceManager.handleSchedulerResource(request)) {
                        return;
                    }
                    if (queueCircuitBreakerConfigurationResourceManager != null && queueCircuitBreakerConfigurationResourceManager.handleConfigurationResource(request)) {
                        return;
                    }
                    if (propertyHandler != null && propertyHandler.handle(request)) {
                        return;
                    }
                    if (zipExtractHandler != null && zipExtractHandler.handle(request)) {
                        return;
                    }
                    if (delegateHandler != null && delegateHandler.handle(request)) {
                        return;
                    }
                    if (customHttpResponseHandler != null && customHttpResponseHandler.handle(request)) {
                        return;
                    }
                    if (userProfileHandler != null && userProfileHandler.isUserProfileRequest(request)) {
                        userProfileHandler.handle(request);
                    } else if (roleProfileHandler != null && roleProfileHandler.isRoleProfileRequest(request)) {
                        roleProfileHandler.handle(request);
                    } else if (expansionHandler != null && expansionHandler.isZipRequest(request)) {
                        expansionHandler.handleZipRecursion(request);
                    } else if (expansionHandler != null && expansionHandler.isExpansionRequest(request)) {
                        expansionHandler.handleExpansionRecursion(request);
                    } else if (deltaHandler != null && deltaHandler.isDeltaRequest(request)) {
                        setISO8601Timestamps(request);
                        deltaHandler.handle(request, router);
                    } else if (mergeHandler != null && mergeHandler.handle(request)) {
                        return;
                    } else {
                        setISO8601Timestamps(request);
                        router.route(request);
                    }
                }
            }
        }
    };
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) QueuingHandler(org.swisspush.gateleen.queue.queuing.QueuingHandler) PackingHandler(org.swisspush.gateleen.packing.PackingHandler) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ValidationHandler(org.swisspush.gateleen.validation.ValidationHandler) QueuingHandler(org.swisspush.gateleen.queue.queuing.QueuingHandler) EventBusHandler(org.swisspush.gateleen.core.event.EventBusHandler) KafkaHandler(org.swisspush.gateleen.kafka.KafkaHandler) CopyResourceHandler(org.swisspush.gateleen.core.resource.CopyResourceHandler) UserProfileHandler(org.swisspush.gateleen.user.UserProfileHandler) ZipExtractHandler(org.swisspush.gateleen.expansion.ZipExtractHandler) CacheHandler(org.swisspush.gateleen.cache.CacheHandler) DelegateHandler(org.swisspush.gateleen.delegate.DelegateHandler) CustomHttpResponseHandler(org.swisspush.gateleen.routing.CustomHttpResponseHandler) CORSHandler(org.swisspush.gateleen.core.cors.CORSHandler) ContentTypeConstraintHandler(org.swisspush.gateleen.security.content.ContentTypeConstraintHandler) HookHandler(org.swisspush.gateleen.hook.HookHandler) MonitoringHandler(org.swisspush.gateleen.monitoring.MonitoringHandler) QoSHandler(org.swisspush.gateleen.qos.QoSHandler) RoleProfileHandler(org.swisspush.gateleen.user.RoleProfileHandler) DeltaHandler(org.swisspush.gateleen.delta.DeltaHandler) PropertyHandler(org.swisspush.gateleen.core.property.PropertyHandler) ExpansionHandler(org.swisspush.gateleen.expansion.ExpansionHandler) PackingHandler(org.swisspush.gateleen.packing.PackingHandler) MergeHandler(org.swisspush.gateleen.merge.MergeHandler) Handler(io.vertx.core.Handler) UUID(java.util.UUID)

Example 2 with CacheHandler

use of org.swisspush.gateleen.cache.CacheHandler in project gateleen by swisspush.

the class AbstractTest method setupBeforeClass.

/**
 * Starts redis before the test classes are instantiated.
 */
@BeforeClass
public static void setupBeforeClass(TestContext context) {
    Async async = context.async();
    vertx = Vertx.vertx();
    jedis = new Jedis("localhost", REDIS_PORT, 10000);
    jedis.flushAll();
    final JsonObject info = new JsonObject();
    final LocalHttpClient selfClient = new LocalHttpClient(vertx);
    props.putAll(RunConfig.buildRedisProps("localhost", REDIS_PORT));
    String redisHost = (String) props.get("redis.host");
    Integer redisPort = (Integer) props.get("redis.port");
    props.put(ExpansionHandler.MAX_EXPANSION_LEVEL_HARD_PROPERTY, "100");
    props.put(ExpansionHandler.MAX_EXPANSION_LEVEL_SOFT_PROPERTY, "4");
    RunConfig.deployModules(vertx, AbstractTest.class, props, success -> {
        if (success) {
            RedisClient redisClient = new RedisClient(vertx, new RedisOptions().setConnectionString("redis://" + redisHost + ":" + redisPort));
            RedisAPI redisAPI = RedisAPI.api(redisClient);
            ResourceStorage storage = new EventBusResourceStorage(vertx.eventBus(), Address.storageAddress() + "-main");
            MonitoringHandler monitoringHandler = new MonitoringHandler(vertx, storage, PREFIX);
            ConfigurationResourceManager configurationResourceManager = new ConfigurationResourceManager(vertx, storage);
            String eventBusConfigurationResource = SERVER_ROOT + "/admin/v1/hookconfig";
            EventBusHandler eventBusHandler = new EventBusHandler(vertx, SERVER_ROOT + "/event/v1/", SERVER_ROOT + "/event/v1/sock/", "event-", "channels/([^/]+).*", configurationResourceManager, eventBusConfigurationResource);
            eventBusHandler.setEventbusBridgePingInterval(RunConfig.EVENTBUS_BRIDGE_PING_INTERVAL);
            LoggingResourceManager loggingResourceManager = new LoggingResourceManager(vertx, storage, SERVER_ROOT + "/admin/v1/logging");
            UserProfileHandler userProfileHandler = new UserProfileHandler(vertx, storage, RunConfig.buildUserProfileConfiguration());
            RoleProfileHandler roleProfileHandler = new RoleProfileHandler(vertx, storage, SERVER_ROOT + "/roles/v1/([^/]+)/profile");
            qosHandler = new QoSHandler(vertx, storage, SERVER_ROOT + "/admin/v1/qos", props, PREFIX);
            Lock lock = new RedisBasedLock(redisClient);
            QueueClient queueClient = new QueueClient(vertx, monitoringHandler);
            ReducedPropagationManager reducedPropagationManager = new ReducedPropagationManager(vertx, new RedisReducedPropagationStorage(redisAPI), queueClient, lock);
            reducedPropagationManager.startExpiredQueueProcessing(1000);
            hookHandler = new HookHandler(vertx, selfClient, storage, loggingResourceManager, monitoringHandler, SERVER_ROOT + "/users/v1/%s/profile", ROOT + "/server/hooks/v1/", queueClient, false, reducedPropagationManager);
            propertyHandler = new PropertyHandler(ROOT, props);
            schedulerResourceManager = new SchedulerResourceManager(vertx, redisAPI, storage, monitoringHandler, SERVER_ROOT + "/admin/v1/schedulers");
            ResetMetricsController resetMetricsController = new ResetMetricsController(vertx);
            resetMetricsController.registerResetMetricsControlMBean(JMX_DOMAIN, PREFIX);
            LogController logController = new LogController();
            logController.registerLogConfiguratorMBean(JMX_DOMAIN);
            ZipExtractHandler zipExtractHandler = new ZipExtractHandler(selfClient);
            DelegateHandler delegateHandler = new DelegateHandler(vertx, selfClient, storage, monitoringHandler, DELEGATE_ROOT, props, null);
            MergeHandler mergeHandler = new MergeHandler(selfClient);
            cacheHandler = new CacheHandler(new DefaultCacheDataFetcher(new ClientRequestCreator(selfClient)), new RedisCacheStorage(vertx, lock, redisAPI, 60000), SERVER_ROOT + "/cache");
            customHttpResponseHandler = new CustomHttpResponseHandler(RETURN_HTTP_STATUS_ROOT);
            // ------
            RuleProvider ruleProvider = new RuleProvider(vertx, RULES_ROOT, storage, props);
            QueueCircuitBreakerRulePatternToCircuitMapping rulePatternToCircuitMapping = new QueueCircuitBreakerRulePatternToCircuitMapping();
            QueueCircuitBreakerConfigurationResourceManager queueCircuitBreakerConfigurationResourceManager = new QueueCircuitBreakerConfigurationResourceManager(vertx, storage, SERVER_ROOT + "/admin/v1/circuitbreaker");
            QueueCircuitBreakerStorage queueCircuitBreakerStorage = new RedisQueueCircuitBreakerStorage(redisAPI);
            QueueCircuitBreakerHttpRequestHandler requestHandler = new QueueCircuitBreakerHttpRequestHandler(vertx, queueCircuitBreakerStorage, SERVER_ROOT + "/queuecircuitbreaker/circuit");
            QueueCircuitBreaker queueCircuitBreaker = new QueueCircuitBreakerImpl(vertx, lock, Address.redisquesAddress(), queueCircuitBreakerStorage, ruleProvider, rulePatternToCircuitMapping, queueCircuitBreakerConfigurationResourceManager, requestHandler, CIRCUIT_BREAKER_REST_API_PORT);
            new QueueProcessor(vertx, selfClient, monitoringHandler, queueCircuitBreaker);
            final QueueBrowser queueBrowser = new QueueBrowser(vertx, SERVER_ROOT + "/queuing", Address.redisquesAddress(), monitoringHandler);
            new CustomRedisMonitor(vertx, redisAPI, "main", "rest-storage", 10).start();
            Router router = new Router(vertx, storage, props, loggingResourceManager, monitoringHandler, selfClient, SERVER_ROOT, SERVER_ROOT + "/admin/v1/routing/rules", SERVER_ROOT + "/users/v1/%s/profile", info, STORAGE_PORT, (Handler<Void>) aVoid -> {
                System.out.println("Router initialized!");
                hookHandler.init();
                delegateHandler.init();
            });
            router.enableRoutingConfiguration(configurationResourceManager, SERVER_ROOT + "/admin/v1/routing/config");
            System.setProperty("org.swisspush.gateleen.addcorsheaders", "true");
            RunConfig runConfig = RunConfig.with().cacheHandler(cacheHandler).corsHandler(new CORSHandler()).deltaHandler(new DeltaHandler(redisAPI, selfClient, ruleProvider)).expansionHandler(new ExpansionHandler(vertx, storage, selfClient, props, ROOT, RULES_ROOT)).hookHandler(hookHandler).qosHandler(qosHandler).copyResourceHandler(new CopyResourceHandler(selfClient, SERVER_ROOT + "/v1/copy")).eventBusHandler(eventBusHandler).roleProfileHandler(roleProfileHandler).userProfileHandler(userProfileHandler).loggingResourceManager(loggingResourceManager).configurationResourceManager(configurationResourceManager).queueCircuitBreakerConfigurationResourceManager(queueCircuitBreakerConfigurationResourceManager).schedulerResourceManager(schedulerResourceManager).propertyHandler(propertyHandler).zipExtractHandler(zipExtractHandler).delegateHandler(delegateHandler).mergeHandler(mergeHandler).customHttpResponseHandler(customHttpResponseHandler).build(vertx, redisAPI, AbstractTest.class, router, monitoringHandler, queueBrowser);
            Handler<RoutingContext> routingContextHandlerrNew = runConfig.buildRoutingContextHandler();
            selfClient.setRoutingContexttHandler(routingContextHandlerrNew);
            mainServer = vertx.createHttpServer();
            io.vertx.ext.web.Router vertxRouter = io.vertx.ext.web.Router.router(vertx);
            eventBusHandler.install(vertxRouter);
            vertxRouter.route().handler(routingContextHandlerrNew);
            mainServer.requestHandler(vertxRouter);
            mainServer.listen(MAIN_PORT, event -> {
                if (event.succeeded()) {
                    async.complete();
                } else {
                    context.fail("Server not listening on port " + MAIN_PORT);
                }
            });
        }
    });
    async.awaitSuccess();
}
Also used : ZipExtractHandler(org.swisspush.gateleen.expansion.ZipExtractHandler) TestContext(io.vertx.ext.unit.TestContext) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) QueueClient(org.swisspush.gateleen.queue.queuing.QueueClient) EventBusHandler(org.swisspush.gateleen.core.event.EventBusHandler) HttpServer(io.vertx.core.http.HttpServer) RoutingContext(io.vertx.ext.web.RoutingContext) Parser(io.restassured.parsing.Parser) RunConfig(org.swisspush.gateleen.runconfig.RunConfig) RedisBasedLock(org.swisspush.gateleen.core.lock.impl.RedisBasedLock) RequestSpecification(io.restassured.specification.RequestSpecification) LoggingResourceManager(org.swisspush.gateleen.logging.LoggingResourceManager) After(org.junit.After) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) AfterClass(org.junit.AfterClass) Lock(org.swisspush.gateleen.core.lock.Lock) CopyResourceHandler(org.swisspush.gateleen.core.resource.CopyResourceHandler) QueueCircuitBreakerImpl(org.swisspush.gateleen.queue.queuing.circuitbreaker.impl.QueueCircuitBreakerImpl) UserProfileHandler(org.swisspush.gateleen.user.UserProfileHandler) RedisReducedPropagationStorage(org.swisspush.gateleen.hook.reducedpropagation.impl.RedisReducedPropagationStorage) ZipExtractHandler(org.swisspush.gateleen.expansion.ZipExtractHandler) RedisQueueCircuitBreakerStorage(org.swisspush.gateleen.queue.queuing.circuitbreaker.impl.RedisQueueCircuitBreakerStorage) Address(org.swisspush.gateleen.core.util.Address) Router(org.swisspush.gateleen.routing.Router) LocalHttpClient(org.swisspush.gateleen.core.http.LocalHttpClient) Set(java.util.Set) RedisAPI(io.vertx.redis.client.RedisAPI) QueueProcessor(org.swisspush.gateleen.queue.queuing.QueueProcessor) CacheHandler(org.swisspush.gateleen.cache.CacheHandler) DelegateHandler(org.swisspush.gateleen.delegate.DelegateHandler) ResourceStorage(org.swisspush.gateleen.core.storage.ResourceStorage) CustomHttpResponseHandler(org.swisspush.gateleen.routing.CustomHttpResponseHandler) SchedulerResourceManager(org.swisspush.gateleen.scheduler.SchedulerResourceManager) RestAssured(io.restassured.RestAssured) CORSHandler(org.swisspush.gateleen.core.cors.CORSHandler) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) HookHandler(org.swisspush.gateleen.hook.HookHandler) ClientRequestCreator(org.swisspush.gateleen.core.http.ClientRequestCreator) QueueCircuitBreakerHttpRequestHandler(org.swisspush.gateleen.queue.queuing.circuitbreaker.api.QueueCircuitBreakerHttpRequestHandler) HashMap(java.util.HashMap) MonitoringHandler(org.swisspush.gateleen.monitoring.MonitoringHandler) QueueCircuitBreakerConfigurationResourceManager(org.swisspush.gateleen.queue.queuing.circuitbreaker.configuration.QueueCircuitBreakerConfigurationResourceManager) EventBusResourceStorage(org.swisspush.gateleen.core.storage.EventBusResourceStorage) QueueCircuitBreakerRulePatternToCircuitMapping(org.swisspush.gateleen.queue.queuing.circuitbreaker.util.QueueCircuitBreakerRulePatternToCircuitMapping) ConfigurationResourceManager(org.swisspush.gateleen.core.configuration.ConfigurationResourceManager) CustomRedisMonitor(org.swisspush.gateleen.monitoring.CustomRedisMonitor) QoSHandler(org.swisspush.gateleen.qos.QoSHandler) RoleProfileHandler(org.swisspush.gateleen.user.RoleProfileHandler) LogController(org.swisspush.gateleen.logging.LogController) DeltaHandler(org.swisspush.gateleen.delta.DeltaHandler) RuleProvider(org.swisspush.gateleen.routing.RuleProvider) ManagementFactory(java.lang.management.ManagementFactory) QueueCircuitBreakerStorage(org.swisspush.gateleen.queue.queuing.circuitbreaker.QueueCircuitBreakerStorage) Before(org.junit.Before) DefaultCacheDataFetcher(org.swisspush.gateleen.cache.fetch.DefaultCacheDataFetcher) ResetMetricsController(org.swisspush.gateleen.monitoring.ResetMetricsController) ReducedPropagationManager(org.swisspush.gateleen.hook.reducedpropagation.ReducedPropagationManager) javax.management(javax.management) Vertx(io.vertx.core.Vertx) RedisClient(io.vertx.redis.client.impl.RedisClient) PropertyHandler(org.swisspush.gateleen.core.property.PropertyHandler) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) RedisOptions(io.vertx.redis.client.RedisOptions) QueueBrowser(org.swisspush.gateleen.queue.queuing.QueueBrowser) RedisCacheStorage(org.swisspush.gateleen.cache.storage.RedisCacheStorage) ExpansionHandler(org.swisspush.gateleen.expansion.ExpansionHandler) Jedis(redis.clients.jedis.Jedis) MergeHandler(org.swisspush.gateleen.merge.MergeHandler) Handler(io.vertx.core.Handler) QueueCircuitBreaker(org.swisspush.gateleen.queue.queuing.circuitbreaker.QueueCircuitBreaker) SchedulerResourceManager(org.swisspush.gateleen.scheduler.SchedulerResourceManager) JsonObject(io.vertx.core.json.JsonObject) RedisOptions(io.vertx.redis.client.RedisOptions) UserProfileHandler(org.swisspush.gateleen.user.UserProfileHandler) RedisCacheStorage(org.swisspush.gateleen.cache.storage.RedisCacheStorage) Jedis(redis.clients.jedis.Jedis) RoutingContext(io.vertx.ext.web.RoutingContext) Async(io.vertx.ext.unit.Async) LogController(org.swisspush.gateleen.logging.LogController) CustomHttpResponseHandler(org.swisspush.gateleen.routing.CustomHttpResponseHandler) EventBusHandler(org.swisspush.gateleen.core.event.EventBusHandler) ReducedPropagationManager(org.swisspush.gateleen.hook.reducedpropagation.ReducedPropagationManager) RedisAPI(io.vertx.redis.client.RedisAPI) CORSHandler(org.swisspush.gateleen.core.cors.CORSHandler) Router(org.swisspush.gateleen.routing.Router) DeltaHandler(org.swisspush.gateleen.delta.DeltaHandler) RedisBasedLock(org.swisspush.gateleen.core.lock.impl.RedisBasedLock) RedisClient(io.vertx.redis.client.impl.RedisClient) MonitoringHandler(org.swisspush.gateleen.monitoring.MonitoringHandler) RoleProfileHandler(org.swisspush.gateleen.user.RoleProfileHandler) LocalHttpClient(org.swisspush.gateleen.core.http.LocalHttpClient) CustomRedisMonitor(org.swisspush.gateleen.monitoring.CustomRedisMonitor) CacheHandler(org.swisspush.gateleen.cache.CacheHandler) QueueCircuitBreakerConfigurationResourceManager(org.swisspush.gateleen.queue.queuing.circuitbreaker.configuration.QueueCircuitBreakerConfigurationResourceManager) ConfigurationResourceManager(org.swisspush.gateleen.core.configuration.ConfigurationResourceManager) QueueClient(org.swisspush.gateleen.queue.queuing.QueueClient) DelegateHandler(org.swisspush.gateleen.delegate.DelegateHandler) QueueCircuitBreakerImpl(org.swisspush.gateleen.queue.queuing.circuitbreaker.impl.QueueCircuitBreakerImpl) ClientRequestCreator(org.swisspush.gateleen.core.http.ClientRequestCreator) RedisQueueCircuitBreakerStorage(org.swisspush.gateleen.queue.queuing.circuitbreaker.impl.RedisQueueCircuitBreakerStorage) RuleProvider(org.swisspush.gateleen.routing.RuleProvider) RedisReducedPropagationStorage(org.swisspush.gateleen.hook.reducedpropagation.impl.RedisReducedPropagationStorage) QueueCircuitBreakerHttpRequestHandler(org.swisspush.gateleen.queue.queuing.circuitbreaker.api.QueueCircuitBreakerHttpRequestHandler) QueueCircuitBreakerConfigurationResourceManager(org.swisspush.gateleen.queue.queuing.circuitbreaker.configuration.QueueCircuitBreakerConfigurationResourceManager) ExpansionHandler(org.swisspush.gateleen.expansion.ExpansionHandler) MergeHandler(org.swisspush.gateleen.merge.MergeHandler) PropertyHandler(org.swisspush.gateleen.core.property.PropertyHandler) CopyResourceHandler(org.swisspush.gateleen.core.resource.CopyResourceHandler) ResetMetricsController(org.swisspush.gateleen.monitoring.ResetMetricsController) RedisQueueCircuitBreakerStorage(org.swisspush.gateleen.queue.queuing.circuitbreaker.impl.RedisQueueCircuitBreakerStorage) QueueCircuitBreakerStorage(org.swisspush.gateleen.queue.queuing.circuitbreaker.QueueCircuitBreakerStorage) QoSHandler(org.swisspush.gateleen.qos.QoSHandler) QueueCircuitBreaker(org.swisspush.gateleen.queue.queuing.circuitbreaker.QueueCircuitBreaker) QueueCircuitBreakerRulePatternToCircuitMapping(org.swisspush.gateleen.queue.queuing.circuitbreaker.util.QueueCircuitBreakerRulePatternToCircuitMapping) LoggingResourceManager(org.swisspush.gateleen.logging.LoggingResourceManager) RunConfig(org.swisspush.gateleen.runconfig.RunConfig) RedisBasedLock(org.swisspush.gateleen.core.lock.impl.RedisBasedLock) Lock(org.swisspush.gateleen.core.lock.Lock) QueueProcessor(org.swisspush.gateleen.queue.queuing.QueueProcessor) ResourceStorage(org.swisspush.gateleen.core.storage.ResourceStorage) EventBusResourceStorage(org.swisspush.gateleen.core.storage.EventBusResourceStorage) EventBusResourceStorage(org.swisspush.gateleen.core.storage.EventBusResourceStorage) DefaultCacheDataFetcher(org.swisspush.gateleen.cache.fetch.DefaultCacheDataFetcher) QueueBrowser(org.swisspush.gateleen.queue.queuing.QueueBrowser) HookHandler(org.swisspush.gateleen.hook.HookHandler) BeforeClass(org.junit.BeforeClass)

Example 3 with CacheHandler

use of org.swisspush.gateleen.cache.CacheHandler in project gateleen by swisspush.

the class Server method start.

@Override
public void start() {
    final LocalHttpClient selfClient = new LocalHttpClient(vertx);
    final JsonObject info = new JsonObject();
    final Map<String, Object> props = RunConfig.buildRedisProps("localhost", defaultRedisPort);
    /*
         * Just for demonstration purposes. In real-life use a request header name to group the requests
         * and set it as vm option like -Dorg.swisspush.request.rule.property=MY_HEADER_NAME
         */
    System.setProperty(MonitoringHandler.REQUEST_PER_RULE_PROPERTY, "x-appid");
    try {
        String externalConfig = System.getProperty("org.swisspush.config.dir") + "/config.properties";
        Resource externalConfigResource = new FileSystemResource(externalConfig);
        if (externalConfigResource.exists()) {
            log.info("Merging external config {}", externalConfig);
            props.putAll(RunConfig.subMap(new ResourcePropertySource(externalConfigResource).getSource(), "redis."));
        } else {
            log.info("No external config found under {}", externalConfig);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    String redisHost = (String) props.get("redis.host");
    Integer redisPort = (Integer) props.get("redis.port");
    props.put(ExpansionHandler.MAX_EXPANSION_LEVEL_HARD_PROPERTY, "100");
    props.put(ExpansionHandler.MAX_EXPANSION_LEVEL_SOFT_PROPERTY, "50");
    RunConfig.deployModules(vertx, Server.class, props, success -> {
        if (success) {
            redisClient = new RedisClient(vertx, new RedisOptions().setConnectionString("redis://" + redisHost + ":" + redisPort));
            redisApi = RedisAPI.api(redisClient);
            new CustomRedisMonitor(vertx, redisApi, "main", "rest-storage", 10).start();
            storage = new EventBusResourceStorage(vertx.eventBus(), Address.storageAddress() + "-main");
            corsHandler = new CORSHandler();
            RuleProvider ruleProvider = new RuleProvider(vertx, RULES_ROOT, storage, props);
            deltaHandler = new DeltaHandler(redisApi, selfClient, ruleProvider, true);
            expansionHandler = new ExpansionHandler(ruleProvider, selfClient, props, ROOT);
            copyResourceHandler = new CopyResourceHandler(selfClient, SERVER_ROOT + "/v1/copy");
            monitoringHandler = new MonitoringHandler(vertx, storage, PREFIX, SERVER_ROOT + "/monitoring/rpr");
            Lock lock = new RedisBasedLock(redisClient);
            cacheStorage = new RedisCacheStorage(vertx, lock, redisApi, 20 * 1000);
            cacheDataFetcher = new DefaultCacheDataFetcher(new ClientRequestCreator(selfClient));
            cacheHandler = new CacheHandler(cacheDataFetcher, cacheStorage, SERVER_ROOT + "/cache");
            qosHandler = new QoSHandler(vertx, storage, SERVER_ROOT + "/admin/v1/qos", props, PREFIX);
            qosHandler.enableResourceLogging(true);
            configurationResourceManager = new ConfigurationResourceManager(vertx, storage);
            configurationResourceManager.enableResourceLogging(true);
            eventBusHandler = new EventBusHandler(vertx, SERVER_ROOT + "/event/v1/", SERVER_ROOT + "/event/v1/sock/", "event/channels/", "channels/([^/]+).*", configurationResourceManager, SERVER_ROOT + "/admin/v1/hookconfig");
            eventBusHandler.setEventbusBridgePingInterval(RunConfig.EVENTBUS_BRIDGE_PING_INTERVAL);
            loggingResourceManager = new LoggingResourceManager(vertx, storage, SERVER_ROOT + "/admin/v1/logging");
            loggingResourceManager.enableResourceLogging(true);
            ContentTypeConstraintRepository repository = new ContentTypeConstraintRepository();
            contentTypeConstraintHandler = new ContentTypeConstraintHandler(configurationResourceManager, repository, SERVER_ROOT + "/admin/v1/contentTypeConstraints", Arrays.asList(new PatternHolder("application/json"), new PatternHolder("application/x-www-form-urlencoded")));
            contentTypeConstraintHandler.initialize();
            userProfileHandler = new UserProfileHandler(vertx, storage, RunConfig.buildUserProfileConfiguration());
            userProfileHandler.enableResourceLogging(true);
            roleProfileHandler = new RoleProfileHandler(vertx, storage, SERVER_ROOT + "/roles/v1/([^/]+)/profile");
            roleProfileHandler.enableResourceLogging(true);
            QueueClient queueClient = new QueueClient(vertx, monitoringHandler);
            reducedPropagationManager = new ReducedPropagationManager(vertx, new RedisReducedPropagationStorage(redisApi), queueClient, lock);
            reducedPropagationManager.startExpiredQueueProcessing(5000);
            hookHandler = new HookHandler(vertx, selfClient, storage, loggingResourceManager, monitoringHandler, SERVER_ROOT + "/users/v1/%s/profile", SERVER_ROOT + "/hooks/v1/", queueClient, false, reducedPropagationManager);
            hookHandler.enableResourceLogging(true);
            authorizer = new Authorizer(vertx, storage, SERVER_ROOT + "/security/v1/", ROLE_PATTERN, ROLE_PREFIX, props);
            authorizer.enableResourceLogging(true);
            validationResourceManager = new ValidationResourceManager(vertx, storage, SERVER_ROOT + "/admin/v1/validation");
            validationResourceManager.enableResourceLogging(true);
            validationSchemaProvider = new DefaultValidationSchemaProvider(vertx, new ClientRequestCreator(selfClient), Duration.ofSeconds(30));
            validator = new Validator(storage, ROOT + "/schemas/apis/", validationSchemaProvider);
            validationHandler = new ValidationHandler(validationResourceManager, selfClient, validator);
            KafkaProducerRepository kafkaProducerRepository = new KafkaProducerRepository(vertx);
            KafkaMessageSender kafkaMessageSender = new KafkaMessageSender();
            KafkaMessageValidator messageValidator = new KafkaMessageValidator(validationResourceManager, validator);
            kafkaHandler = new KafkaHandler(configurationResourceManager, messageValidator, kafkaProducerRepository, kafkaMessageSender, SERVER_ROOT + "/admin/v1/kafka/topicsConfig", SERVER_ROOT + "/streaming/");
            kafkaHandler.initialize();
            schedulerResourceManager = new SchedulerResourceManager(vertx, redisApi, storage, monitoringHandler, SERVER_ROOT + "/admin/v1/schedulers");
            schedulerResourceManager.enableResourceLogging(true);
            zipExtractHandler = new ZipExtractHandler(selfClient);
            delegateHandler = new DelegateHandler(vertx, selfClient, storage, monitoringHandler, SERVER_ROOT + "/admin/v1/delegates/", props, null);
            delegateHandler.enableResourceLogging(true);
            customHttpResponseHandler = new CustomHttpResponseHandler(RETURN_HTTP_STATUS_ROOT);
            router = Router.builder().withServerPath(SERVER_ROOT).withRulesPath(SERVER_ROOT + "/admin/v1/routing/rules").withUserProfilePath(SERVER_ROOT + "/users/v1/%s/profile").withVertx(vertx).withSelfClient(selfClient).withStorage(storage).withStoragePort(storagePort).withInfo(info).withMonitoringHandler(monitoringHandler).withLoggingResourceManager(loggingResourceManager).withResourceLogging(true).withRoutingConfiguration(configurationResourceManager, SERVER_ROOT + "/admin/v1/routing/config").withHttpClientFactory(this::createHttpClientForRouter).addDoneHandler(aVoid -> {
                hookHandler.init();
                delegateHandler.init();
            }).build();
            QueueCircuitBreakerRulePatternToCircuitMapping rulePatternToCircuitMapping = new QueueCircuitBreakerRulePatternToCircuitMapping();
            queueCircuitBreakerConfigurationResourceManager = new QueueCircuitBreakerConfigurationResourceManager(vertx, storage, SERVER_ROOT + "/admin/v1/circuitbreaker");
            queueCircuitBreakerConfigurationResourceManager.enableResourceLogging(true);
            QueueCircuitBreakerStorage queueCircuitBreakerStorage = new RedisQueueCircuitBreakerStorage(redisApi);
            QueueCircuitBreakerHttpRequestHandler requestHandler = new QueueCircuitBreakerHttpRequestHandler(vertx, queueCircuitBreakerStorage, SERVER_ROOT + "/queuecircuitbreaker/circuit");
            QueueCircuitBreaker queueCircuitBreaker = new QueueCircuitBreakerImpl(vertx, lock, Address.redisquesAddress(), queueCircuitBreakerStorage, ruleProvider, rulePatternToCircuitMapping, queueCircuitBreakerConfigurationResourceManager, requestHandler, circuitBreakerPort);
            new QueueProcessor(vertx, selfClient, monitoringHandler, queueCircuitBreaker);
            final QueueBrowser queueBrowser = new QueueBrowser(vertx, SERVER_ROOT + "/queuing", Address.redisquesAddress(), monitoringHandler);
            LogController logController = new LogController();
            logController.registerLogConfiguratorMBean(JMX_DOMAIN);
            ResetMetricsController resetMetricsController = new ResetMetricsController(vertx);
            resetMetricsController.registerResetMetricsControlMBean(JMX_DOMAIN, PREFIX);
            RunConfig runConfig = RunConfig.with().authorizer(authorizer).validationResourceManager(validationResourceManager).validationHandler(validationHandler).cacheHandler(cacheHandler).corsHandler(corsHandler).deltaHandler(deltaHandler).expansionHandler(expansionHandler).hookHandler(hookHandler).qosHandler(qosHandler).copyResourceHandler(copyResourceHandler).eventBusHandler(eventBusHandler).kafkaHandler(kafkaHandler).roleProfileHandler(roleProfileHandler).userProfileHandler(userProfileHandler).loggingResourceManager(loggingResourceManager).configurationResourceManager(configurationResourceManager).queueCircuitBreakerConfigurationResourceManager(queueCircuitBreakerConfigurationResourceManager).schedulerResourceManager(schedulerResourceManager).zipExtractHandler(zipExtractHandler).delegateHandler(delegateHandler).customHttpResponseHandler(customHttpResponseHandler).contentTypeConstraintHandler(contentTypeConstraintHandler).build(vertx, redisApi, Server.class, router, monitoringHandler, queueBrowser);
            Handler<RoutingContext> routingContextHandlerrNew = runConfig.buildRoutingContextHandler();
            selfClient.setRoutingContexttHandler(routingContextHandlerrNew);
            HttpServerOptions options = new HttpServerOptions();
            // till vertx2 100-continues was performed automatically (per default),
            // since vertx3 it is off per default.
            options.setHandle100ContinueAutomatically(true);
            mainServer = vertx.createHttpServer(options);
            io.vertx.ext.web.Router vertxRouter = io.vertx.ext.web.Router.router(vertx);
            eventBusHandler.install(vertxRouter);
            vertxRouter.route().handler(routingContextHandlerrNew);
            mainServer.requestHandler(vertxRouter);
            mainServer.listen(mainPort);
        }
    });
}
Also used : ZipExtractHandler(org.swisspush.gateleen.expansion.ZipExtractHandler) KafkaHandler(org.swisspush.gateleen.kafka.KafkaHandler) SchedulerResourceManager(org.swisspush.gateleen.scheduler.SchedulerResourceManager) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) RedisOptions(io.vertx.redis.client.RedisOptions) FileSystemResource(org.springframework.core.io.FileSystemResource) UserProfileHandler(org.swisspush.gateleen.user.UserProfileHandler) RedisCacheStorage(org.swisspush.gateleen.cache.storage.RedisCacheStorage) ResourcePropertySource(org.springframework.core.io.support.ResourcePropertySource) RoutingContext(io.vertx.ext.web.RoutingContext) Authorizer(org.swisspush.gateleen.security.authorization.Authorizer) ContentTypeConstraintRepository(org.swisspush.gateleen.security.content.ContentTypeConstraintRepository) LogController(org.swisspush.gateleen.logging.LogController) CustomHttpResponseHandler(org.swisspush.gateleen.routing.CustomHttpResponseHandler) EventBusHandler(org.swisspush.gateleen.core.event.EventBusHandler) ReducedPropagationManager(org.swisspush.gateleen.hook.reducedpropagation.ReducedPropagationManager) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) CORSHandler(org.swisspush.gateleen.core.cors.CORSHandler) DeltaHandler(org.swisspush.gateleen.delta.DeltaHandler) RedisBasedLock(org.swisspush.gateleen.core.lock.impl.RedisBasedLock) RedisClient(io.vertx.redis.client.impl.RedisClient) PatternHolder(org.swisspush.gateleen.security.PatternHolder) MonitoringHandler(org.swisspush.gateleen.monitoring.MonitoringHandler) RoleProfileHandler(org.swisspush.gateleen.user.RoleProfileHandler) LocalHttpClient(org.swisspush.gateleen.core.http.LocalHttpClient) JsonObject(io.vertx.core.json.JsonObject) KafkaProducerRepository(org.swisspush.gateleen.kafka.KafkaProducerRepository) CustomRedisMonitor(org.swisspush.gateleen.monitoring.CustomRedisMonitor) CacheHandler(org.swisspush.gateleen.cache.CacheHandler) QueueCircuitBreakerConfigurationResourceManager(org.swisspush.gateleen.queue.queuing.circuitbreaker.configuration.QueueCircuitBreakerConfigurationResourceManager) ConfigurationResourceManager(org.swisspush.gateleen.core.configuration.ConfigurationResourceManager) QueueClient(org.swisspush.gateleen.queue.queuing.QueueClient) DelegateHandler(org.swisspush.gateleen.delegate.DelegateHandler) QueueCircuitBreakerImpl(org.swisspush.gateleen.queue.queuing.circuitbreaker.impl.QueueCircuitBreakerImpl) KafkaMessageSender(org.swisspush.gateleen.kafka.KafkaMessageSender) ClientRequestCreator(org.swisspush.gateleen.core.http.ClientRequestCreator) RedisQueueCircuitBreakerStorage(org.swisspush.gateleen.queue.queuing.circuitbreaker.impl.RedisQueueCircuitBreakerStorage) RuleProvider(org.swisspush.gateleen.routing.RuleProvider) RedisReducedPropagationStorage(org.swisspush.gateleen.hook.reducedpropagation.impl.RedisReducedPropagationStorage) QueueCircuitBreakerHttpRequestHandler(org.swisspush.gateleen.queue.queuing.circuitbreaker.api.QueueCircuitBreakerHttpRequestHandler) QueueCircuitBreakerConfigurationResourceManager(org.swisspush.gateleen.queue.queuing.circuitbreaker.configuration.QueueCircuitBreakerConfigurationResourceManager) ExpansionHandler(org.swisspush.gateleen.expansion.ExpansionHandler) KafkaMessageValidator(org.swisspush.gateleen.kafka.KafkaMessageValidator) CopyResourceHandler(org.swisspush.gateleen.core.resource.CopyResourceHandler) ResetMetricsController(org.swisspush.gateleen.monitoring.ResetMetricsController) RedisQueueCircuitBreakerStorage(org.swisspush.gateleen.queue.queuing.circuitbreaker.impl.RedisQueueCircuitBreakerStorage) QueueCircuitBreakerStorage(org.swisspush.gateleen.queue.queuing.circuitbreaker.QueueCircuitBreakerStorage) QoSHandler(org.swisspush.gateleen.qos.QoSHandler) QueueCircuitBreaker(org.swisspush.gateleen.queue.queuing.circuitbreaker.QueueCircuitBreaker) QueueCircuitBreakerRulePatternToCircuitMapping(org.swisspush.gateleen.queue.queuing.circuitbreaker.util.QueueCircuitBreakerRulePatternToCircuitMapping) LoggingResourceManager(org.swisspush.gateleen.logging.LoggingResourceManager) IOException(java.io.IOException) RunConfig(org.swisspush.gateleen.runconfig.RunConfig) RedisBasedLock(org.swisspush.gateleen.core.lock.impl.RedisBasedLock) Lock(org.swisspush.gateleen.core.lock.Lock) QueueProcessor(org.swisspush.gateleen.queue.queuing.QueueProcessor) ContentTypeConstraintHandler(org.swisspush.gateleen.security.content.ContentTypeConstraintHandler) EventBusResourceStorage(org.swisspush.gateleen.core.storage.EventBusResourceStorage) DefaultCacheDataFetcher(org.swisspush.gateleen.cache.fetch.DefaultCacheDataFetcher) KafkaMessageValidator(org.swisspush.gateleen.kafka.KafkaMessageValidator) QueueBrowser(org.swisspush.gateleen.queue.queuing.QueueBrowser) HookHandler(org.swisspush.gateleen.hook.HookHandler)

Example 4 with CacheHandler

use of org.swisspush.gateleen.cache.CacheHandler in project gateleen by swisspush.

the class CacheHandlerTest method testOnlyGETRequestsWithCacheControlHeadersAreHandled.

@Test
public void testOnlyGETRequestsWithCacheControlHeadersAreHandled(TestContext context) {
    when(cacheStorage.cachedRequest(anyString())).thenReturn(Future.succeededFuture(Optional.of(bufferFromJson(new JsonObject()))));
    HttpServerResponse response = spy(new Response());
    Request putRequest = new Request(HttpMethod.PUT, "/some/path", MultiMap.caseInsensitiveMultiMap(), response);
    context.assertFalse(cacheHandler.handle(putRequest));
    Request getRequestNoHeaders = new Request(HttpMethod.GET, "/some/path", MultiMap.caseInsensitiveMultiMap(), response);
    context.assertFalse(cacheHandler.handle(getRequestNoHeaders));
    MultiMap headers = MultiMap.caseInsensitiveMultiMap();
    headers.add("foo", "bar");
    Request getRequestOtherHeaders = new Request(HttpMethod.GET, "/some/path", headers, response);
    context.assertFalse(cacheHandler.handle(getRequestOtherHeaders));
    headers = MultiMap.caseInsensitiveMultiMap();
    headers.add(DEFAULT_CACHE_CONTROL_HEADER, "max-age=120");
    Request getRequestWithCacheControlHeaders = new Request(HttpMethod.GET, "/some/path", headers, response);
    context.assertTrue(cacheHandler.handle(getRequestWithCacheControlHeaders));
    // custom cache control header
    cacheHandler = new CacheHandler(dataFetcher, cacheStorage, "/playground/server/cache", "x-cache-control");
    headers = new HeadersMultiMap();
    headers.add("x-cache-control", "max-age=120");
    Request getRequestWithCustomCacheControlHeaders = new Request(HttpMethod.GET, "/some/path", headers, response);
    context.assertTrue(cacheHandler.handle(getRequestWithCustomCacheControlHeaders));
}
Also used : DummyHttpServerResponse(org.swisspush.gateleen.core.http.DummyHttpServerResponse) HttpServerResponse(io.vertx.core.http.HttpServerResponse) MultiMap(io.vertx.core.MultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) DummyHttpServerResponse(org.swisspush.gateleen.core.http.DummyHttpServerResponse) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServerRequest(io.vertx.core.http.HttpServerRequest) DummyHttpServerRequest(org.swisspush.gateleen.core.http.DummyHttpServerRequest) JsonObject(io.vertx.core.json.JsonObject) CacheHandler(org.swisspush.gateleen.cache.CacheHandler) Test(org.junit.Test)

Aggregations

CacheHandler (org.swisspush.gateleen.cache.CacheHandler)4 JsonObject (io.vertx.core.json.JsonObject)3 RoutingContext (io.vertx.ext.web.RoutingContext)3 CORSHandler (org.swisspush.gateleen.core.cors.CORSHandler)3 EventBusHandler (org.swisspush.gateleen.core.event.EventBusHandler)3 CopyResourceHandler (org.swisspush.gateleen.core.resource.CopyResourceHandler)3 DelegateHandler (org.swisspush.gateleen.delegate.DelegateHandler)3 DeltaHandler (org.swisspush.gateleen.delta.DeltaHandler)3 ExpansionHandler (org.swisspush.gateleen.expansion.ExpansionHandler)3 ZipExtractHandler (org.swisspush.gateleen.expansion.ZipExtractHandler)3 HookHandler (org.swisspush.gateleen.hook.HookHandler)3 Handler (io.vertx.core.Handler)2 HttpServerRequest (io.vertx.core.http.HttpServerRequest)2 RedisOptions (io.vertx.redis.client.RedisOptions)2 RedisClient (io.vertx.redis.client.impl.RedisClient)2 PropertyHandler (org.swisspush.gateleen.core.property.PropertyHandler)2 MergeHandler (org.swisspush.gateleen.merge.MergeHandler)2 MonitoringHandler (org.swisspush.gateleen.monitoring.MonitoringHandler)2 QoSHandler (org.swisspush.gateleen.qos.QoSHandler)2 CustomHttpResponseHandler (org.swisspush.gateleen.routing.CustomHttpResponseHandler)2