Search in sources :

Example 1 with RoutingContext

use of io.vertx.reactivex.ext.web.RoutingContext in project knotx by Cognifide.

the class KnotxEngineHandler method handleRoute.

private void handleRoute(final RoutingContext context, final String address, final Map<String, RoutingEntry> routing) {
    KnotContext knotContext = context.get(KnotContext.KEY);
    proxies.computeIfAbsent(address, adr -> KnotProxy.createProxyWithOptions(vertx, adr, configuration.getDeliveryOptions())).rxProcess(knotContext).doOnSuccess(ctx -> context.put(KnotContext.KEY, ctx)).subscribe(ctx -> OptionalAction.of(Optional.ofNullable(ctx.getTransition())).ifPresent(on -> {
        RoutingEntry entry = routing.get(on);
        if (entry != null) {
            handleRoute(context, entry.address(), entry.onTransition());
        } else {
            LOGGER.debug("Received transition '{}' from '{}'. No further routing available for the transition. Go to the response generation.", on, address);
            // last knot can return default transition
            context.put(KnotContext.KEY, ctx);
            context.next();
        }
    }).ifNotPresent(() -> {
        LOGGER.debug("Request processing finished by {} Knot. Go to the response generation", address);
        context.put(KnotContext.KEY, ctx);
        context.next();
    }), error -> {
        LOGGER.error("Error happened while communicating with {} engine", error, address);
        context.fail(error);
    });
}
Also used : KnotContext(io.knotx.dataobjects.KnotContext) OptionalAction(io.knotx.util.OptionalAction) KnotProxy(io.knotx.reactivex.proxy.KnotProxy) HashMap(java.util.HashMap) RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) LoggerFactory(io.vertx.core.logging.LoggerFactory) RoutingEntry(io.knotx.server.configuration.RoutingEntry) Vertx(io.vertx.reactivex.core.Vertx) Map(java.util.Map) Optional(java.util.Optional) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) KnotxServerConfiguration(io.knotx.server.configuration.KnotxServerConfiguration) RoutingEntry(io.knotx.server.configuration.RoutingEntry) KnotContext(io.knotx.dataobjects.KnotContext)

Example 2 with RoutingContext

use of io.vertx.reactivex.ext.web.RoutingContext in project knotx by Cognifide.

the class SupportedMethodsAndPathsHandlerTest method mockContext.

private RoutingContext mockContext(String requestPath, HttpMethod method) {
    RoutingContext context = Mockito.mock(RoutingContext.class);
    HttpServerRequest httpRequest = Mockito.mock(HttpServerRequest.class);
    Mockito.when(context.request()).thenReturn(httpRequest);
    Mockito.when(httpRequest.path()).thenReturn(requestPath);
    Mockito.when(httpRequest.method()).thenReturn(method);
    return context;
}
Also used : RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) HttpServerRequest(io.vertx.reactivex.core.http.HttpServerRequest)

Example 3 with RoutingContext

use of io.vertx.reactivex.ext.web.RoutingContext in project knotx by Cognifide.

the class SupportedMethodsAndPathsHandlerTest method handle_whenRequestPathAllowedButMethodNotAllowedInCustomFlow_expectRequestFailedMethodNotAllowed.

@Test
public void handle_whenRequestPathAllowedButMethodNotAllowedInCustomFlow_expectRequestFailedMethodNotAllowed() throws Exception {
    RoutingContext context = mockContext("/content/page.html", HttpMethod.POST);
    tested.handle(context);
    Mockito.verify(context, Mockito.times(1)).fail(405);
}
Also used : RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) Test(org.junit.Test)

Example 4 with RoutingContext

use of io.vertx.reactivex.ext.web.RoutingContext in project knotx by Cognifide.

the class SupportedMethodsAndPathsHandlerTest method handle_whenRequestPathAllowedButMethodNotAllowedInDefaultFlow_expectRequestFailedMethodNotAllowed.

@Test
public void handle_whenRequestPathAllowedButMethodNotAllowedInDefaultFlow_expectRequestFailedMethodNotAllowed() throws Exception {
    RoutingContext context = mockContext("/content/page.html", HttpMethod.POST);
    tested.handle(context);
    Mockito.verify(context, Mockito.times(1)).fail(405);
}
Also used : RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) Test(org.junit.Test)

Example 5 with RoutingContext

use of io.vertx.reactivex.ext.web.RoutingContext in project knotx by Cognifide.

the class SupportedMethodsAndPathsHandlerTest method handle_whenRequestPathNotAllowed_expectRequestFailedNotFound.

@Test
public void handle_whenRequestPathNotAllowed_expectRequestFailedNotFound() throws Exception {
    RoutingContext context = mockContext("/notallowed/page.html", HttpMethod.GET);
    tested.handle(context);
    Mockito.verify(context, Mockito.times(1)).fail(404);
}
Also used : RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) Test(org.junit.Test)

Aggregations

RoutingContext (io.vertx.reactivex.ext.web.RoutingContext)9 Test (org.junit.Test)5 KnotContext (io.knotx.dataobjects.KnotContext)3 KnotxServerConfiguration (io.knotx.server.configuration.KnotxServerConfiguration)3 Handler (io.vertx.core.Handler)3 Logger (io.vertx.core.logging.Logger)3 LoggerFactory (io.vertx.core.logging.LoggerFactory)3 Vertx (io.vertx.reactivex.core.Vertx)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 KnotProxy (io.knotx.reactivex.proxy.KnotProxy)2 Optional (java.util.Optional)2 ClientResponse (io.knotx.dataobjects.ClientResponse)1 Fragment (io.knotx.dataobjects.Fragment)1 RepositoryConnectorProxy (io.knotx.reactivex.proxy.RepositoryConnectorProxy)1 RepositoryEntry (io.knotx.server.configuration.RepositoryEntry)1 RoutingEntry (io.knotx.server.configuration.RoutingEntry)1 OptionalAction (io.knotx.util.OptionalAction)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 MultiMap (io.vertx.reactivex.core.MultiMap)1