Search in sources :

Example 1 with Eventual

use of com.hotels.styx.api.Eventual in project styx by ExpediaGroup.

the class TestPlugin method intercept.

@Override
public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) {
    String header = xHcomPluginsHeader(request);
    LiveHttpRequest newRequest = request.newBuilder().header(X_HCOM_PLUGINS_HEADER, header).header("X-Hcom-Styx-Started", styxStarted).header("X-Hcom-Styx-Stopped", styxStopped).build();
    Function<ByteBuf, String> byteBufStringFunction = byteBuf -> byteBuf.toString(UTF_8);
    return chain.proceed(newRequest).flatMap(response -> response.aggregate(1 * 1024 * 1024)).map(response -> response.newBuilder().header(X_HCOM_PLUGINS_HEADER, header).header("X-Hcom-Styx-Started", styxStarted).header("X-Hcom-Styx-Stopped", styxStopped).addHeader("X-Plugin-Identifier", config.getId()).build()).map(HttpResponse::stream);
}
Also used : Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) Plugin(com.hotels.styx.api.plugins.spi.Plugin) PluginFactory(com.hotels.styx.api.plugins.spi.PluginFactory) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Map(java.util.Map) Collections(java.util.Collections) Function(java.util.function.Function) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with Eventual

use of com.hotels.styx.api.Eventual in project styx by ExpediaGroup.

the class UrlPatternRouter method handle.

@Override
public Eventual<HttpResponse> handle(HttpRequest request, HttpInterceptor.Context context) {
    for (RouteDescriptor route : alternatives) {
        if (request.method().equals(route.method())) {
            Matcher match = route.uriPattern().matcher(request.path());
            LOGGER.debug("Request path '{}' matching against route pattern '{}' matches: {}", new Object[] { request.path(), route.uriPattern(), match.matches() });
            if (match.matches()) {
                Map<String, String> placeholders = route.placeholderNames().stream().collect(toMap(name -> name, match::group));
                context.add(PLACEHOLDERS_KEY, placeholders);
                try {
                    return route.handler().handle(request, context);
                } catch (Exception cause) {
                    LOGGER.error("ERROR: {} {}", new Object[] { request.method(), request.path(), cause });
                    return Eventual.of(response(INTERNAL_SERVER_ERROR).build());
                }
            }
        }
    }
    return Eventual.of(response(NOT_FOUND).build());
}
Also used : Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) Logger(org.slf4j.Logger) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) PUT(com.hotels.styx.api.HttpMethod.PUT) HttpResponse.response(com.hotels.styx.api.HttpResponse.response) NOT_FOUND(com.hotels.styx.api.HttpResponseStatus.NOT_FOUND) HttpRequest(com.hotels.styx.api.HttpRequest) ArrayList(java.util.ArrayList) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) List(java.util.List) Matcher(java.util.regex.Matcher) Collectors.toMap(java.util.stream.Collectors.toMap) WebServiceHandler(com.hotels.styx.api.WebServiceHandler) Map(java.util.Map) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Pattern(java.util.regex.Pattern) DELETE(com.hotels.styx.api.HttpMethod.DELETE) LinkedList(java.util.LinkedList) GET(com.hotels.styx.api.HttpMethod.GET) POST(com.hotels.styx.api.HttpMethod.POST) HttpMethod(com.hotels.styx.api.HttpMethod) Matcher(java.util.regex.Matcher)

Example 3 with Eventual

use of com.hotels.styx.api.Eventual in project styx by ExpediaGroup.

the class StandardHttpPipelineTest method sendsExceptionUponExtraSubscriptionInsideInterceptor.

@ParameterizedTest
@MethodSource("multipleSubscriptionInterceptors")
public void sendsExceptionUponExtraSubscriptionInsideInterceptor(HttpInterceptor interceptor) throws Exception {
    HttpHandler handler = (request, context) -> Eventual.of(response(OK).build());
    List<HttpInterceptor> interceptors = singletonList(interceptor);
    StandardHttpPipeline pipeline = new StandardHttpPipeline(interceptors, handler, RequestTracker.NO_OP);
    Eventual<LiveHttpResponse> responseObservable = pipeline.handle(get("/").build(), requestContext());
    assertThrows(IllegalStateException.class, () -> Mono.from(responseObservable).block());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpHandler(com.hotels.styx.api.HttpHandler) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with Eventual

use of com.hotels.styx.api.Eventual in project styx by ExpediaGroup.

the class StandardHttpPipelineTest method sendsExceptionUponMultipleSubscription.

@Test
public void sendsExceptionUponMultipleSubscription() {
    HttpHandler handler = (request, context) -> Eventual.of(response(OK).build());
    StandardHttpPipeline pipeline = new StandardHttpPipeline(handler);
    Eventual<LiveHttpResponse> responseObservable = pipeline.handle(get("/").build(), requestContext());
    LiveHttpResponse response = Mono.from(responseObservable).block();
    assertThat(response.status(), is(OK));
    assertThrows(IllegalStateException.class, () -> Mono.from(responseObservable).block());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with Eventual

use of com.hotels.styx.api.Eventual in project styx by ExpediaGroup.

the class EarlyReturnExamplePluginTest method returnsEarlyWhenHeaderIsPresent.

@Test
public void returnsEarlyWhenHeaderIsPresent() {
    EarlyReturnExamplePlugin plugin = new EarlyReturnExamplePlugin();
    LiveHttpRequest request = LiveHttpRequest.get("/").header("X-Respond", "foo").build();
    HttpInterceptor.Chain chain = request1 -> Eventual.of(LiveHttpResponse.response().build());
    Eventual<LiveHttpResponse> eventualLive = plugin.intercept(request, chain);
    Eventual<HttpResponse> eventual = eventualLive.flatMap(response -> response.aggregate(100));
    HttpResponse response = Mono.from(eventual).block();
    assertThat(response.bodyAs(UTF_8), is("Responding from plugin"));
}
Also used : HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Mono(reactor.core.publisher.Mono) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Eventual (com.hotels.styx.api.Eventual)6 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)5 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)5 HttpHandler (com.hotels.styx.api.HttpHandler)4 HttpResponse (com.hotels.styx.api.HttpResponse)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Test (org.junit.jupiter.api.Test)3 Mono (reactor.core.publisher.Mono)3 OK (com.hotels.styx.api.HttpResponseStatus.OK)2 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)2 LiveHttpRequest.get (com.hotels.styx.api.LiveHttpRequest.get)2 LiveHttpResponse.response (com.hotels.styx.api.LiveHttpResponse.response)2 HttpInterceptorContext (com.hotels.styx.server.HttpInterceptorContext)2 RequestTracker (com.hotels.styx.server.track.RequestTracker)2 Support.requestContext (com.hotels.styx.support.Support.requestContext)2 InetSocketAddress (java.net.InetSocketAddress)2 Arrays.asList (java.util.Arrays.asList)2 Collections.singletonList (java.util.Collections.singletonList)2