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);
}
}
}
}
};
}
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);
}
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'");
}
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");
}
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");
}
Aggregations