Search in sources :

Example 11 with ByteStream

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

the class NettyToStyxRequestDecoder method makeAStyxRequestFrom.

@VisibleForTesting
LiveHttpRequest.Builder makeAStyxRequestFrom(HttpRequest request, Observable<ByteBuf> content) {
    Url url = UrlDecoder.decodeUrl(unwiseCharEncoder, request);
    LiveHttpRequest.Builder requestBuilder = new LiveHttpRequest.Builder().method(toStyxMethod(request.method())).url(url).version(toStyxVersion(request.protocolVersion())).id(uniqueIdSupplier.get()).body(new ByteStream(toPublisher(content.map(Buffers::fromByteBuf))));
    stream(request.headers().spliterator(), false).forEach(entry -> requestBuilder.addHeader(entry.getKey(), entry.getValue()));
    return requestBuilder;
}
Also used : LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) ByteStream(com.hotels.styx.api.ByteStream) Url(com.hotels.styx.api.Url) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 12 with ByteStream

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

the class HttpPipelineHandler method exceptionToResponse.

private LiveHttpResponse exceptionToResponse(Throwable cause, LiveHttpRequest request, CharSequence originsHeaderName) {
    HttpResponseStatus status = status(cause instanceof PluginException ? cause.getCause() : cause);
    String message = status.code() >= 500 ? "Site temporarily unavailable." : status.description();
    LiveHttpResponse.Transformer builder = responseEnhancer.enhance(response(status).body(new ByteStream(Flux.just(new Buffer(message, UTF_8)))).build().newBuilder(), request).header(CONTENT_LENGTH, message.getBytes(UTF_8).length).header(CONNECTION, "close");
    if (originsHeaderName != null && originFromException(cause) != null) {
        return builder.header(originsHeaderName, originFromException(cause)).build();
    } else {
        return builder.build();
    }
}
Also used : Buffer(com.hotels.styx.api.Buffer) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) PluginException(com.hotels.styx.api.plugins.spi.PluginException) ByteStream(com.hotels.styx.api.ByteStream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse)

Aggregations

ByteStream (com.hotels.styx.api.ByteStream)12 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)11 Test (org.junit.jupiter.api.Test)10 TransportLostException (com.hotels.styx.api.exceptions.TransportLostException)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)9 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 ExecutionException (java.util.concurrent.ExecutionException)9 Buffer (com.hotels.styx.api.Buffer)8 LoggingHandler (io.netty.handler.logging.LoggingHandler)3 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)2 InetSocketAddress (java.net.InetSocketAddress)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Level (ch.qos.logback.classic.Level)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Buffers.toByteBuf (com.hotels.styx.api.Buffers.toByteBuf)1 Context (com.hotels.styx.api.HttpInterceptor.Context)1 HttpResponseStatus (com.hotels.styx.api.HttpResponseStatus)1 OK (com.hotels.styx.api.HttpResponseStatus.OK)1 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)1