Search in sources :

Example 1 with MonitoringHandler

use of org.swisspush.gateleen.monitoring.MonitoringHandler 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 MonitoringHandler

use of org.swisspush.gateleen.monitoring.MonitoringHandler in project gateleen by swisspush.

the class ForwarderTest method setUp.

@Before
public void setUp() {
    vertx = Mockito.mock(Vertx.class);
    loggingResourceManager = Mockito.mock(LoggingResourceManager.class);
    monitoringHandler = Mockito.mock(MonitoringHandler.class);
    httpClient = Mockito.mock(HttpClient.class);
    storage = new MockResourceStorage(ImmutableMap.of(RULES_PATH, RULES));
    logger = LoggerFactory.getLogger(ForwarderTest.class);
}
Also used : MonitoringHandler(org.swisspush.gateleen.monitoring.MonitoringHandler) HttpClient(io.vertx.core.http.HttpClient) MockResourceStorage(org.swisspush.gateleen.core.storage.MockResourceStorage) LoggingResourceManager(org.swisspush.gateleen.logging.LoggingResourceManager) Vertx(io.vertx.core.Vertx) Before(org.junit.Before)

Example 3 with MonitoringHandler

use of org.swisspush.gateleen.monitoring.MonitoringHandler in project gateleen by swisspush.

the class RouterTest method testRequestHopValidationWithLimitZero.

@Test
public void testRequestHopValidationWithLimitZero(TestContext context) {
    storage = new MockResourceStorage(ImmutableMap.of(rulesPath, RULES_WITH_HOPS, serverUrl + "/loop/4/resource", RANDOM_RESOURCE));
    Router router = new Router(vertx, storage, properties, loggingResourceManager, monitoringHandler, httpClient, serverUrl, rulesPath, userProfilePath, info, storagePort);
    ConfigurationResourceManager configurationResourceManager = Mockito.mock(ConfigurationResourceManager.class);
    router.enableRoutingConfiguration(configurationResourceManager, serverUrl + "/admin/v1/routing/config");
    context.assertFalse(router.isRoutingBroken(), "Routing should not be broken");
    context.assertNull(router.getRoutingBrokenMessage(), "RoutingBrokenMessage should be null");
    // change the hops limit to 0, so no re-routing is allowed
    router.resourceChanged(serverUrl + "/admin/v1/routing/config", Buffer.buffer("{\"request.hops.limit\":0}"));
    final DummyHttpServerResponse response = new DummyHttpServerResponse();
    response.setStatusCode(StatusCode.OK.getStatusCode());
    response.setStatusMessage(StatusCode.OK.getStatusMessage());
    class GETRandomResourceRequest extends DummyHttpServerRequest {

        MultiMap headers = MultiMap.caseInsensitiveMultiMap();

        @Override
        public HttpMethod method() {
            return HttpMethod.GET;
        }

        @Override
        public String uri() {
            return "/gateleen/server/loop/4/resource";
        }

        @Override
        public String path() {
            return "/gateleen/server/loop/4/resource";
        }

        @Override
        public MultiMap headers() {
            return headers;
        }

        @Override
        public MultiMap params() {
            return MultiMap.caseInsensitiveMultiMap();
        }

        @Override
        public HttpServerRequest bodyHandler(Handler<Buffer> bodyHandler) {
            bodyHandler.handle(Buffer.buffer(RANDOM_RESOURCE));
            return this;
        }

        @Override
        public HttpServerRequest handler(Handler<Buffer> handler) {
            handler.handle(Buffer.buffer(RANDOM_RESOURCE));
            return this;
        }

        @Override
        public HttpServerRequest endHandler(Handler<Void> endHandler) {
            endHandler.handle(null);
            return this;
        }

        @Override
        public DummyHttpServerResponse response() {
            return response;
        }
    }
    GETRandomResourceRequest request = new GETRandomResourceRequest();
    router.route(request);
    context.assertEquals("1", request.headers().get("x-hops"), "x-hops header should have value 1");
    context.assertEquals(StatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), request.response().getStatusCode(), "StatusCode should be 500");
    context.assertEquals("Request hops limit exceeded", request.response().getStatusMessage(), "StatusMessage should be 'Request hops limit exceeded'");
}
Also used : MultiMap(io.vertx.core.MultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) DummyHttpServerResponse(org.swisspush.gateleen.core.http.DummyHttpServerResponse) DummyHttpServerRequest(org.swisspush.gateleen.core.http.DummyHttpServerRequest) MockResourceStorage(org.swisspush.gateleen.core.storage.MockResourceStorage) MonitoringHandler(org.swisspush.gateleen.monitoring.MonitoringHandler) Handler(io.vertx.core.Handler) ConfigurationResourceManager(org.swisspush.gateleen.core.configuration.ConfigurationResourceManager) Test(org.junit.Test)

Example 4 with MonitoringHandler

use of org.swisspush.gateleen.monitoring.MonitoringHandler in project gateleen by swisspush.

the class RouterTest method testGETAnyResourceAfterFixingBrokenRouterShouldWorkAgain.

@Test
public void testGETAnyResourceAfterFixingBrokenRouterShouldWorkAgain(TestContext context) {
    Router router = new Router(vertx, storage, properties, loggingResourceManager, monitoringHandler, httpClient, serverUrl, rulesPath, userProfilePath, info, storagePort);
    context.assertTrue(router.isRoutingBroken(), "Routing should be broken because of missing properties entry");
    context.assertNotNull(router.getRoutingBrokenMessage(), "RoutingBrokenMessage should contain 'gateleen.test.prop.1' property");
    context.assertTrue(router.getRoutingBrokenMessage().contains("gateleen.test.prop.1"), "RoutingBrokenMessage should contain 'gateleen.test.prop.1' property");
    // get random resource with broken routing. Should not work
    final DummyHttpServerResponse response = new DummyHttpServerResponse();
    class GETRandomResourceRequest extends DummyHttpServerRequest {

        @Override
        public HttpMethod method() {
            return HttpMethod.GET;
        }

        @Override
        public String uri() {
            return "/gateleen/server/random/resource";
        }

        @Override
        public String path() {
            return "/gateleen/server/random/resource";
        }

        @Override
        public MultiMap headers() {
            return MultiMap.caseInsensitiveMultiMap();
        }

        @Override
        public DummyHttpServerResponse response() {
            return response;
        }
    }
    GETRandomResourceRequest request = new GETRandomResourceRequest();
    router.route(request);
    context.assertTrue(router.isRoutingBroken(), "Routing should still be broken");
    context.assertNotNull(router.getRoutingBrokenMessage(), "RoutingBrokenMessage should contain 'gateleen.test.prop.1' property");
    context.assertTrue(router.getRoutingBrokenMessage().contains("gateleen.test.prop.1"), "RoutingBrokenMessage should contain 'gateleen.test.prop.1' property");
    context.assertEquals(StatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), request.response().getStatusCode(), "StatusCode should be 500");
    context.assertEquals(StatusCode.INTERNAL_SERVER_ERROR.getStatusMessage(), request.response().getStatusMessage(), "StatusMessage should be Internal Server Error");
    context.assertTrue(request.response().getResultBuffer().contains("Routing is broken"), "Routing is broken message should be returned");
    context.assertTrue(request.response().getResultBuffer().contains("gateleen.test.prop.1"), "The message should contain 'gateleen.test.prop.1' in the message");
    // fix routing
    final DummyHttpServerResponse responseFix = new DummyHttpServerResponse();
    class UpdateRulesWithValidResourceRequest extends DummyHttpServerRequest {

        @Override
        public HttpMethod method() {
            return HttpMethod.PUT;
        }

        @Override
        public String uri() {
            return "/gateleen/server/admin/v1/routing/rules";
        }

        @Override
        public HttpServerRequest bodyHandler(Handler<Buffer> bodyHandler) {
            bodyHandler.handle(Buffer.buffer(RULES_WITH_VALID_PROPS));
            return this;
        }

        @Override
        public MultiMap headers() {
            return MultiMap.caseInsensitiveMultiMap();
        }

        @Override
        public HttpServerResponse response() {
            return responseFix;
        }
    }
    router.route(new UpdateRulesWithValidResourceRequest());
    context.assertFalse(router.isRoutingBroken(), "Routing should not be broken anymore");
    context.assertNull(router.getRoutingBrokenMessage(), "RoutingBrokenMessage should be null");
    // retry get random resource. Should work now
    final DummyHttpServerResponse responseRandomResource = new DummyHttpServerResponse();
    class GETRandomResourceAgainRequest extends DummyHttpServerRequest {

        @Override
        public HttpMethod method() {
            return HttpMethod.GET;
        }

        @Override
        public String uri() {
            return "/gateleen/server/random/resource";
        }

        @Override
        public String path() {
            return "/gateleen/server/random/resource";
        }

        @Override
        public MultiMap params() {
            return MultiMap.caseInsensitiveMultiMap();
        }

        @Override
        public MultiMap headers() {
            return MultiMap.caseInsensitiveMultiMap();
        }

        @Override
        public DummyHttpServerResponse response() {
            return responseRandomResource;
        }

        @Override
        public HttpServerRequest pause() {
            return this;
        }
    }
    GETRandomResourceAgainRequest requestRandomResource = new GETRandomResourceAgainRequest();
    router.route(requestRandomResource);
    context.assertFalse(router.isRoutingBroken(), "Routing should not be broken anymore");
    context.assertNull(router.getRoutingBrokenMessage(), "RoutingBrokenMessage should be null");
}
Also used : DummyHttpServerResponse(org.swisspush.gateleen.core.http.DummyHttpServerResponse) DummyHttpServerRequest(org.swisspush.gateleen.core.http.DummyHttpServerRequest) MonitoringHandler(org.swisspush.gateleen.monitoring.MonitoringHandler) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 5 with MonitoringHandler

use of org.swisspush.gateleen.monitoring.MonitoringHandler in project gateleen by swisspush.

the class RouterTest method testRequestHopValidationLimitNotYetReached.

@Test
public void testRequestHopValidationLimitNotYetReached(TestContext context) {
    storage = new MockResourceStorage(ImmutableMap.of(rulesPath, RULES_WITH_HOPS, serverUrl + "/loop/4/resource", RANDOM_RESOURCE));
    Router router = new Router(vertx, storage, properties, loggingResourceManager, monitoringHandler, httpClient, serverUrl, rulesPath, userProfilePath, info, storagePort);
    ConfigurationResourceManager configurationResourceManager = Mockito.mock(ConfigurationResourceManager.class);
    router.enableRoutingConfiguration(configurationResourceManager, serverUrl + "/admin/v1/routing/config");
    context.assertFalse(router.isRoutingBroken(), "Routing should not be broken");
    context.assertNull(router.getRoutingBrokenMessage(), "RoutingBrokenMessage should be null");
    // change the hops limit to 5
    router.resourceChanged(serverUrl + "/admin/v1/routing/config", Buffer.buffer("{\"request.hops.limit\":5}"));
    final DummyHttpServerResponse response = new DummyHttpServerResponse();
    response.setStatusCode(StatusCode.OK.getStatusCode());
    response.setStatusMessage(StatusCode.OK.getStatusMessage());
    class GETRandomResourceRequest extends DummyHttpServerRequest {

        MultiMap headers = MultiMap.caseInsensitiveMultiMap();

        @Override
        public HttpMethod method() {
            return HttpMethod.GET;
        }

        @Override
        public String uri() {
            return "/gateleen/server/loop/4/resource";
        }

        @Override
        public String path() {
            return "/gateleen/server/loop/4/resource";
        }

        @Override
        public MultiMap headers() {
            return headers;
        }

        @Override
        public MultiMap params() {
            return MultiMap.caseInsensitiveMultiMap();
        }

        @Override
        public HttpServerRequest bodyHandler(Handler<Buffer> bodyHandler) {
            bodyHandler.handle(Buffer.buffer(RANDOM_RESOURCE));
            return this;
        }

        @Override
        public HttpServerRequest handler(Handler<Buffer> handler) {
            handler.handle(Buffer.buffer(RANDOM_RESOURCE));
            return this;
        }

        @Override
        public HttpServerRequest endHandler(Handler<Void> endHandler) {
            endHandler.handle(null);
            return this;
        }

        @Override
        public DummyHttpServerResponse response() {
            return response;
        }
    }
    GETRandomResourceRequest request = new GETRandomResourceRequest();
    router.route(request);
    context.assertEquals("1", request.headers().get("x-hops"), "x-hops header should have value 1");
    context.assertEquals(StatusCode.OK.getStatusCode(), request.response().getStatusCode(), "StatusCode should be 200");
    context.assertEquals(StatusCode.OK.getStatusMessage(), request.response().getStatusMessage(), "StatusMessage should be OK");
}
Also used : MultiMap(io.vertx.core.MultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) DummyHttpServerResponse(org.swisspush.gateleen.core.http.DummyHttpServerResponse) DummyHttpServerRequest(org.swisspush.gateleen.core.http.DummyHttpServerRequest) MockResourceStorage(org.swisspush.gateleen.core.storage.MockResourceStorage) MonitoringHandler(org.swisspush.gateleen.monitoring.MonitoringHandler) Handler(io.vertx.core.Handler) ConfigurationResourceManager(org.swisspush.gateleen.core.configuration.ConfigurationResourceManager) Test(org.junit.Test)

Aggregations

MonitoringHandler (org.swisspush.gateleen.monitoring.MonitoringHandler)16 Handler (io.vertx.core.Handler)12 Test (org.junit.Test)10 MultiMap (io.vertx.core.MultiMap)8 Before (org.junit.Before)8 Vertx (io.vertx.core.Vertx)7 JsonObject (io.vertx.core.json.JsonObject)7 MockResourceStorage (org.swisspush.gateleen.core.storage.MockResourceStorage)7 Async (io.vertx.ext.unit.Async)5 TestContext (io.vertx.ext.unit.TestContext)5 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)5 RunWith (org.junit.runner.RunWith)5 ConfigurationResourceManager (org.swisspush.gateleen.core.configuration.ConfigurationResourceManager)5 DummyHttpServerRequest (org.swisspush.gateleen.core.http.DummyHttpServerRequest)5 DummyHttpServerResponse (org.swisspush.gateleen.core.http.DummyHttpServerResponse)5 Address (org.swisspush.gateleen.core.util.Address)5 LoggingResourceManager (org.swisspush.gateleen.logging.LoggingResourceManager)5 Buffer (io.vertx.core.buffer.Buffer)4 Message (io.vertx.core.eventbus.Message)4 HeadersMultiMap (io.vertx.core.http.impl.headers.HeadersMultiMap)4