use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.
the class DefaultJerseyStreamingHttpRouter method handle0.
private void handle0(final HttpServiceContext serviceCtx, final StreamingHttpRequest req, final StreamingHttpResponseFactory factory, final Subscriber<? super StreamingHttpResponse> subscriber, final DelayedCancellable delayedCancellable) {
final CharSequence baseUri = baseUriFunction.apply(serviceCtx, req);
final CharSequence requestTarget = req.requestTarget();
final StringBuilder requestUriBuilder = new StringBuilder(baseUri.length() + requestTarget.length()).append(baseUri);
// Jersey expects the baseUri ends in a '/' and if not will return 404s
assert baseUri.length() == 0 || baseUri.charAt(baseUri.length() - 1) == '/';
if (requestTarget.length() > 0 && requestTarget.charAt(0) == '/') {
requestUriBuilder.append(requestTarget, 1, requestTarget.length());
} else {
requestUriBuilder.append(requestTarget);
}
final URI baseURI;
final URI requestURI;
try {
baseURI = URI.create(baseUri.toString());
requestURI = URI.create(requestUriBuilder.toString());
} catch (IllegalArgumentException cause) {
Buffer message = serviceCtx.executionContext().bufferAllocator().fromAscii(cause.getMessage());
StreamingHttpResponse response = factory.badRequest().payloadBody(from(message));
response.headers().set(CONTENT_LENGTH, Integer.toString(message.readableBytes()));
response.headers().set(CONTENT_TYPE, TEXT_PLAIN);
subscriber.onSuccess(response);
return;
}
final ContainerRequest containerRequest = new CloseSignalHandoffAbleContainerRequest(baseURI, requestURI, req.method().name(), UNAUTHENTICATED_SECURITY_CONTEXT, new MapPropertiesDelegate(), new ResourceConfig());
req.headers().forEach(h -> containerRequest.getHeaders().add(h.getKey().toString(), h.getValue().toString()));
final BufferPublisherInputStream entityStream = new BufferPublisherInputStream(req.payloadBody(), publisherInputStreamQueueCapacity);
containerRequest.setEntityStream(entityStream);
initRequestProperties(entityStream, containerRequest);
final DefaultContainerResponseWriter responseWriter = new DefaultContainerResponseWriter(containerRequest, req.version(), serviceCtx, factory, subscriber);
containerRequest.setWriter(responseWriter);
containerRequest.setRequestScopedInitializer(injectionManager -> {
injectionManager.<Ref<ConnectionContext>>getInstance(CONNECTION_CONTEXT_REF_TYPE).set(serviceCtx);
injectionManager.<Ref<StreamingHttpRequest>>getInstance(HTTP_REQUEST_REF_TYPE).set(req);
});
delayedCancellable.delayedCancellable(responseWriter::dispose);
applicationHandler.handle(containerRequest);
}
use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.
the class TestServiceStreaming method handle.
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext context, final StreamingHttpRequest req, final StreamingHttpResponseFactory factory) {
LOGGER.debug("({}) Handling {}", counter, req.toString((a, b) -> b));
final StreamingHttpResponse response;
switch(req.path()) {
case SVC_ECHO:
response = newEchoResponse(req, factory);
break;
case SVC_COUNTER_NO_LAST_CHUNK:
response = newTestCounterResponse(context, req, factory);
break;
case SVC_COUNTER:
response = newTestCounterResponseWithLastPayloadChunk(context, req, factory);
break;
case SVC_LARGE_LAST:
response = newLargeLastChunkResponse(context, req, factory);
break;
case SVC_TEST_PUBLISHER:
response = newTestPublisherResponse(req, factory);
break;
case SVC_NO_CONTENT:
response = newNoContentResponse(req, factory);
break;
case SVC_NO_CONTENT_AFTER_READ:
return req.payloadBody().ignoreElements().concat(succeeded(newNoContentResponse(req, factory)));
case SVC_ROT13:
response = newRot13Response(req, factory);
break;
case SVC_NEVER:
return req.payloadBody().ignoreElements().concat(never());
case SVC_THROW_ERROR:
response = throwErrorSynchronously();
break;
case SVC_SINGLE_ERROR:
return Single.failed(DELIBERATE_EXCEPTION);
case SVC_ERROR_BEFORE_READ:
response = throwErrorBeforeRead(req, factory);
break;
case SVC_ERROR_DURING_READ:
response = throwErrorDuringRead(req, factory);
break;
default:
response = newNotFoundResponse(req, factory);
}
return succeeded(response);
}
use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.
the class TestServiceStreaming method newEchoResponse.
private static StreamingHttpResponse newEchoResponse(final StreamingHttpRequest req, final StreamingHttpResponseFactory factory) {
final StreamingHttpResponse response = factory.ok().version(req.version()).transformMessageBody(pub -> pub.ignoreElements().merge(req.messageBody())).transform(new StatelessTrailersTransformer<>());
final CharSequence contentLength = req.headers().get(CONTENT_LENGTH);
if (contentLength != null) {
response.addHeader(CONTENT_LENGTH, contentLength);
}
final CharSequence contentType = req.headers().get(CONTENT_TYPE);
if (contentType != null) {
response.addHeader(CONTENT_TYPE, contentType);
}
final CharSequence transferEncoding = req.headers().get(TRANSFER_ENCODING);
if (transferEncoding != null) {
response.addHeader(TRANSFER_ENCODING, transferEncoding);
}
return response;
}
use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.
the class ContentLengthAndTrailersTest method setUp.
private void setUp(HttpProtocol protocol, String content) {
this.protocol = protocol;
this.content = content;
protocol(protocol.config);
serviceFilterFactory(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
// Use transform to simulate access to request trailers
return delegate().handle(ctx, request.transform(new StatelessTrailersTransformer<>()), responseFactory).map(response -> {
final HttpHeaders headers = request.headers();
if (headers.contains(CONTENT_LENGTH)) {
response.setHeader(CLIENT_CONTENT_LENGTH, mergeValues(headers.values(CONTENT_LENGTH)));
}
if (headers.contains(TRANSFER_ENCODING)) {
response.setHeader(CLIENT_TRANSFER_ENCODING, mergeValues(headers.values(TRANSFER_ENCODING)));
}
return response;
});
}
});
setUp(CACHED, CACHED_SERVER);
}
use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.
the class ContentLengthTest method shouldCalculateRequestContentLengthFromSingleItemPublisher.
@Test
void shouldCalculateRequestContentLengthFromSingleItemPublisher() throws Exception {
StreamingHttpRequest request = newAggregatedRequest().toStreamingRequest().payloadBody(from("Hello"), appSerializerUtf8FixLen());
setRequestContentLengthAndVerify(request, contentEqualTo("9"));
}
Aggregations