use of io.servicetalk.http.api.StreamingHttpService in project servicetalk by apple.
the class RequestResponseContextTest method service.
@Override
void service(final StreamingHttpService service) {
final StreamingHttpService newService;
assert api != null;
switch(api) {
case AsyncAggregated:
newService = toStreamingHttpService((HttpService) (ctx, request, responseFactory) -> {
HttpResponse response = responseFactory.ok().payloadBody(request.payloadBody());
transferContext(request, response);
return succeeded(response);
}, offloadNone()).adaptor();
break;
case AsyncStreaming:
newService = (ctx, request, responseFactory) -> {
StreamingHttpResponse response = responseFactory.ok().payloadBody(request.payloadBody());
transferContext(request, response);
return succeeded(response);
};
break;
case BlockingAggregated:
newService = toStreamingHttpService((BlockingHttpService) (ctx, request, responseFactory) -> {
HttpResponse response = responseFactory.ok().payloadBody(request.payloadBody());
transferContext(request, response);
return response;
}, offloadNone()).adaptor();
break;
case BlockingStreaming:
newService = toStreamingHttpService((ctx, request, response) -> {
transferContext(request, response);
try (HttpPayloadWriter<Buffer> writer = response.sendMetaData()) {
for (Buffer chunk : request.payloadBody()) {
writer.write(chunk);
}
}
}, offloadNone()).adaptor();
break;
default:
throw new IllegalStateException("Unknown api: " + api);
}
super.service(newService);
}
use of io.servicetalk.http.api.StreamingHttpService in project servicetalk by apple.
the class FlushStrategyOnServerTest method setUp.
private void setUp(final Param param) {
this.interceptor = new OutboundWriteEventsInterceptor();
this.headersFactory = DefaultHttpHeadersFactory.INSTANCE;
final StreamingHttpService service = (ctx, request, responseFactory) -> {
StreamingHttpResponse resp = responseFactory.ok();
if (request.headers().get(USE_EMPTY_RESP_BODY) == null) {
resp.payloadBody(from("Hello", "World"), appSerializerUtf8FixLen());
}
if (request.headers().get(USE_AGGREGATED_RESP) != null) {
return resp.toResponse().map(HttpResponse::toStreamingResponse);
}
return succeeded(resp);
};
final DefaultHttpExecutionContext httpExecutionContext = new DefaultHttpExecutionContext(DEFAULT_ALLOCATOR, globalExecutionContext().ioExecutor(), EXECUTOR_RULE.executor(), param.executionStrategy);
final ReadOnlyHttpServerConfig config = new HttpServerConfig().asReadOnly();
final ReadOnlyTcpServerConfig tcpReadOnly = new TcpServerConfig().asReadOnly();
try {
serverContext = TcpServerBinder.bind(localAddress(0), tcpReadOnly, true, httpExecutionContext, null, (channel, observer) -> {
final ConnectionObserver connectionObserver = config.tcpConfig().transportObserver().onNewConnection(channel.localAddress(), channel.remoteAddress());
return initChannel(channel, httpExecutionContext, config, new TcpServerChannelInitializer(tcpReadOnly, connectionObserver).andThen(channel1 -> channel1.pipeline().addLast(interceptor)), service, true, connectionObserver);
}, connection -> connection.process(true)).map(delegate -> new NettyHttpServerContext(delegate, service, httpExecutionContext)).toFuture().get();
} catch (Exception e) {
fail(e);
}
client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).protocols(h1Default()).buildBlocking();
}
use of io.servicetalk.http.api.StreamingHttpService in project servicetalk by apple.
the class GrpcRouter method populateRoutes.
private static void populateRoutes(final GrpcExecutionContext executionContext, final Map<String, StreamingHttpService> allRoutes, final Map<String, RouteProvider> routes, final CompositeCloseable closeable, final Map<String, GrpcExecutionStrategy> executionStrategies) {
for (Map.Entry<String, RouteProvider> entry : routes.entrySet()) {
final String path = entry.getKey();
final ServiceAdapterHolder adapterHolder = entry.getValue().buildRoute(executionContext);
final StreamingHttpService route = closeable.append(adapterHolder.adaptor());
final GrpcExecutionStrategy routeStrategy = executionStrategies.getOrDefault(path, null);
final HttpExecutionStrategy missing = null == routeStrategy ? HttpExecutionStrategies.offloadNever() : executionContext.executionStrategy().missing(routeStrategy);
verifyNoOverrides(allRoutes.put(path, null != routeStrategy && missing.isRequestResponseOffloaded() ? StreamingHttpServiceToOffloadedStreamingHttpService.offloadService(adapterHolder.serviceInvocationStrategy(), executionContext.executor(), IoThreadFactory.IoThread::currentThreadIsIoThread, route) : route), path, emptyMap());
LOGGER.debug("route strategy for path={} : ctx={} route={} → using={}", path, executionContext.executionStrategy(), routeStrategy, missing);
}
}
use of io.servicetalk.http.api.StreamingHttpService in project servicetalk by apple.
the class DefaultFallbackServiceTest method testDefaultFallbackService.
@Test
void testDefaultFallbackService() throws Exception {
final StreamingHttpService fixture = DefaultFallbackServiceStreaming.instance();
final Single<StreamingHttpResponse> responseSingle = fixture.handle(ctx, request, reqRespFactory);
final StreamingHttpResponse response = responseSingle.toFuture().get();
assert response != null;
assertEquals(HTTP_1_1, response.version());
assertEquals(NOT_FOUND, response.status());
assertEquals(ZERO, response.headers().get(CONTENT_LENGTH));
assertEquals(TEXT_PLAIN_UTF_8, response.headers().get(CONTENT_TYPE));
}
use of io.servicetalk.http.api.StreamingHttpService in project servicetalk by apple.
the class HttpPredicateRouterBuilderHeaderTest method testWhenHeaderIsPresent.
@Test
void testWhenHeaderIsPresent() {
final StreamingHttpService service = new HttpPredicateRouterBuilder().whenHeader("host").isPresent().thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
when(headers.valuesIterator("host")).then(answerIteratorOf("localhost"));
assertSame(responseA, service.handle(ctx, request, reqRespFactory));
when(headers.valuesIterator("host")).thenReturn(emptyIterator());
assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
Aggregations