Java Code Examples for com.linecorp.armeria.common.HttpResponse#streaming()
The following examples show how to use
com.linecorp.armeria.common.HttpResponse#streaming() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamingHttpFile.java From armeria with Apache License 2.0 | 6 votes |
@Override protected final HttpResponse doRead(ResponseHeaders headers, long length, Executor fileReadExecutor, ByteBufAllocator alloc) throws IOException { final T in = newStream(); if (in == null) { return null; } boolean submitted = false; try { final HttpResponseWriter res = HttpResponse.streaming(); res.write(headers); fileReadExecutor.execute(() -> doRead(res, in, 0, length, fileReadExecutor, alloc)); submitted = true; return res; } finally { if (!submitted) { close(in); } } }
Example 2
Source File: ResponseConversionUtil.java From armeria with Apache License 2.0 | 6 votes |
private static HttpResponseWriter aggregateFrom(CompletableFuture<?> future, ResponseHeaders headers, HttpHeaders trailers, Function<Object, HttpData> contentConverter) { final HttpResponseWriter writer = HttpResponse.streaming(); future.handle((result, cause) -> { if (cause != null) { writer.close(cause); return null; } try { final HttpData content = contentConverter.apply(result); writer.write(headers); writer.write(content); if (!trailers.isEmpty()) { writer.write(trailers); } writer.close(); } catch (Exception e) { writer.close(e); } return null; }); return writer; }
Example 3
Source File: DefaultHttpResponseTest.java From armeria with Apache License 2.0 | 6 votes |
/** * The aggregation future must be completed even if the response being aggregated has been aborted. */ @ParameterizedTest @ValueSource(booleans = { true, false }) void abortedAggregation(boolean executorSpecified) { final Thread mainThread = Thread.currentThread(); final HttpResponseWriter res = HttpResponse.streaming(); final CompletableFuture<AggregatedHttpResponse> future; // Practically same execution, but we need to test the both case due to code duplication. if (executorSpecified) { future = res.aggregate(CommonPools.workerGroup().next()); } else { future = res.aggregate(); } final AtomicReference<Thread> callbackThread = new AtomicReference<>(); assertThatThrownBy(() -> { final CompletableFuture<AggregatedHttpResponse> f = future.whenComplete((unused, cause) -> callbackThread.set(Thread.currentThread())); res.abort(); f.join(); }).hasCauseInstanceOf(AbortedStreamException.class); assertThat(callbackThread.get()).isNotSameAs(mainThread); }
Example 4
Source File: TestConverters.java From armeria with Apache License 2.0 | 6 votes |
private static HttpResponse httpResponse(HttpData data) { final HttpResponseWriter res = HttpResponse.streaming(); final long current = System.currentTimeMillis(); final ResponseHeadersBuilder headers = ResponseHeaders.builder(HttpStatus.OK); headers.setInt(HttpHeaderNames.CONTENT_LENGTH, data.length()); headers.setTimeMillis(HttpHeaderNames.DATE, current); final MediaType contentType = ServiceRequestContext.current().negotiatedResponseMediaType(); if (contentType != null) { headers.contentType(contentType); } res.write(headers.build()); res.write(data); res.close(); return res; }
Example 5
Source File: DefaultHttpResponseTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void ignoresAfterTrailersIsWritten() { final HttpResponseWriter res = HttpResponse.streaming(); res.write(ResponseHeaders.of(100)); res.write(HttpHeaders.of(HttpHeaderNames.of("a"), "b")); res.write(ResponseHeaders.of(200)); res.write(HttpHeaders.of(HttpHeaderNames.of("c"), "d")); // Split headers is trailers. // Ignored after trailers is written. res.write(HttpData.ofUtf8("foo")); res.write(HttpHeaders.of(HttpHeaderNames.of("e"), "f")); res.write(HttpHeaders.of(HttpHeaderNames.of("g"), "h")); res.close(); final AggregatedHttpResponse aggregated = res.aggregate().join(); // Informational headers assertThat(aggregated.informationals()).containsExactly( ResponseHeaders.of(HttpStatus.CONTINUE, HttpHeaderNames.of("a"), "b")); // Non-informational header assertThat(aggregated.headers()).isEqualTo(ResponseHeaders.of(200)); assertThat(aggregated.contentUtf8()).isEmpty(); assertThat(aggregated.trailers()).isEqualTo(HttpHeaders.of(HttpHeaderNames.of("c"), "d")); }
Example 6
Source File: ConcurrencyLimitingClientTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void testUnlimitedRequest() throws Exception { final ClientRequestContext ctx = newContext(); final HttpRequest req = newReq(); final HttpResponseWriter actualRes = HttpResponse.streaming(); when(delegate.execute(ctx, req)).thenReturn(actualRes); final ConcurrencyLimitingClient client = newDecorator(0).apply(delegate); // A request should be delegated immediately, creating no deferred response. final HttpResponse res = client.execute(ctx, req); verify(delegate).execute(ctx, req); assertThat(res.isOpen()).isTrue(); assertThat(client.numActiveRequests()).isEqualTo(1); // Complete the response, leaving no active requests. closeAndDrain(actualRes, res); await().untilAsserted(() -> assertThat(client.numActiveRequests()).isZero()); }
Example 7
Source File: ConcurrencyLimitingClientTest.java From armeria with Apache License 2.0 | 5 votes |
/** * Tests if the request is not delegated but closed when the timeout is reached before delegation. */ @Test void testTimeout() throws Exception { final ClientRequestContext ctx1 = newContext(); final ClientRequestContext ctx2 = newContext(); final HttpRequest req1 = newReq(); final HttpRequest req2 = newReq(); final HttpResponseWriter actualRes1 = HttpResponse.streaming(); final HttpResponseWriter actualRes2 = HttpResponse.streaming(); when(delegate.execute(ctx1, req1)).thenReturn(actualRes1); final ConcurrencyLimitingClient client = newDecorator(1, 500, TimeUnit.MILLISECONDS).apply(delegate); // Send two requests, where only the first one is delegated. final HttpResponse res1 = client.execute(ctx1, req1); final HttpResponse res2 = client.execute(ctx2, req2); // Let req2 time out. Thread.sleep(1000); res2.subscribe(NoopSubscriber.get()); assertThatThrownBy(() -> res2.whenComplete().join()) .hasCauseInstanceOf(UnprocessedRequestException.class) .hasRootCauseInstanceOf(RequestTimeoutException.class); assertThat(res2.isOpen()).isFalse(); // req1 should not time out because it's been delegated already. res1.subscribe(NoopSubscriber.get()); assertThat(res1.isOpen()).isTrue(); assertThat(res1.whenComplete()).isNotDone(); // Close req1 and make sure req2 does not affect numActiveRequests. actualRes1.close(); await().untilAsserted(() -> assertThat(client.numActiveRequests()).isZero()); }
Example 8
Source File: AnnotatedServiceExceptionHandlerTest.java From armeria with Apache License 2.0 | 5 votes |
@Override public HttpResponse handleException(ServiceRequestContext ctx, HttpRequest req, Throwable cause) { final HttpResponseWriter response = HttpResponse.streaming(); response.write(ResponseHeaders.of(HttpStatus.OK)); // Timeout may occur before responding. ctx.eventLoop().schedule((Runnable) response::close, 10, TimeUnit.SECONDS); return response; }
Example 9
Source File: AnnotatedServiceTest.java From armeria with Apache License 2.0 | 5 votes |
@Post @Path("/a/string-async2") public HttpResponse postStringAsync2(AggregatedHttpRequest request, RequestContext ctx) { validateContext(ctx); final HttpResponseWriter response = HttpResponse.streaming(); response.write(ResponseHeaders.of(HttpStatus.OK)); response.write(request.content()); response.close(); return response; }
Example 10
Source File: TruncatingHttpResponseTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void smallContent() throws InterruptedException { final HttpResponseWriter writer = HttpResponse.streaming(); writer.write(() -> ResponseHeaders.of(HttpStatus.OK)); writer.write(() -> HttpData.ofUtf8("1234567890")); writer.close(); final TruncatingHttpResponse response = new TruncatingHttpResponse(writer, 20); final AggregatedHttpResponse agg = response.aggregate().join(); assertThat(agg.contentUtf8()).isEqualTo("1234567890"); }
Example 11
Source File: DefaultHttpResponseTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void closeMustReleaseAggregatedContent() { final HttpResponseWriter res = HttpResponse.streaming(); final PooledHttpData data = PooledHttpData.wrap(Unpooled.copiedBuffer("foo", StandardCharsets.UTF_8)).withEndOfStream(); res.close(); res.close(AggregatedHttpResponse.of(ResponseHeaders.of(200), data)); assertThat(data.refCnt()).isZero(); }
Example 12
Source File: HttpResponseExceptionTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void testHttpResponse() throws Exception { final HttpResponseWriter response = HttpResponse.streaming(); final HttpResponseException exception = HttpResponseException.of(response); response.write(ResponseHeaders.of(HttpStatus.INTERNAL_SERVER_ERROR, HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8)); response.close(); final AggregatedHttpResponse message = exception.httpResponse().aggregate().join(); assertThat(message.status()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(message.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8); }
Example 13
Source File: HttpClientIntegrationTest.java From armeria with Apache License 2.0 | 5 votes |
@Override protected HttpResponse serve(PooledHttpService delegate, ServiceRequestContext ctx, PooledHttpRequest req) throws Exception { final PooledHttpResponse res = delegate.serve(ctx, req); final HttpResponseWriter decorated = HttpResponse.streaming(); res.subscribeWithPooledObjects(new Subscriber<HttpObject>() { @Override public void onSubscribe(Subscription s) { s.request(Long.MAX_VALUE); } @Override public void onNext(HttpObject httpObject) { if (httpObject instanceof ByteBufHolder) { try { decorated.write(HttpData.copyOf(((ByteBufHolder) httpObject).content())); } finally { ReferenceCountUtil.safeRelease(httpObject); } } else { decorated.write(httpObject); } } @Override public void onError(Throwable t) { decorated.close(t); } @Override public void onComplete() { decorated.close(); } }); return decorated; }
Example 14
Source File: HttpClientIntegrationTest.java From armeria with Apache License 2.0 | 5 votes |
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final HttpResponse res = unwrap().serve(ctx, req); final HttpResponseWriter decorated = HttpResponse.streaming(); res.subscribe(new Subscriber<HttpObject>() { @Override public void onSubscribe(Subscription s) { s.request(Long.MAX_VALUE); } @Override public void onNext(HttpObject httpObject) { decorated.write(httpObject); } @Override public void onError(Throwable t) { decorated.close(t); } @Override public void onComplete() { decorated.close(); } }); return decorated; }
Example 15
Source File: AnimationService.java From armeria with Apache License 2.0 | 5 votes |
@Override protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) throws Exception { // Create a response for streaming. If you don't need to stream, use HttpResponse.of(...) instead. final HttpResponseWriter res = HttpResponse.streaming(); res.write(ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8)); res.whenConsumed().thenRun(() -> streamData(ctx.eventLoop(), res, 0)); return res; }
Example 16
Source File: DefaultHttpResponseTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void closeWithAggregatedResponse() { // Always headers only. final HttpResponseWriter res1 = HttpResponse.streaming(); res1.close(AggregatedHttpResponse.of(204)); StepVerifier.create(res1) .expectNext(ResponseHeaders.of(204)) .expectComplete() .verify(); // Headers only. final HttpResponseWriter res2 = HttpResponse.streaming(); res2.close(AggregatedHttpResponse.of(ResponseHeaders.of(200))); StepVerifier.create(res2) .expectNext(ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_LENGTH, 0)) .expectComplete() .verify(); // Headers and body. final HttpResponseWriter res3 = HttpResponse.streaming(); res3.close(AggregatedHttpResponse.of(ResponseHeaders.of(200), HttpData.ofUtf8("foo"))); StepVerifier.create(res3) .expectNext(ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_LENGTH, 3)) .expectNext(HttpData.ofUtf8("foo")) .expectComplete() .verify(); // Headers, body and trailers. final HttpResponseWriter res4 = HttpResponse.streaming(); res4.close(AggregatedHttpResponse.of(ResponseHeaders.of(200), HttpData.ofUtf8("bar"), HttpHeaders.of("x-trailer", true))); StepVerifier.create(res4) .expectNext(ResponseHeaders.of(200)) .expectNext(HttpData.ofUtf8("bar")) .expectNext(HttpHeaders.of("x-trailer", true)) .expectComplete() .verify(); }
Example 17
Source File: PooledResponseBufferBenchmark.java From armeria with Apache License 2.0 | 5 votes |
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final HttpResponse res = unwrap().serve(ctx, req); final HttpResponseWriter decorated = HttpResponse.streaming(); res.subscribe(new Subscriber<HttpObject>() { @Override public void onSubscribe(Subscription s) { s.request(Long.MAX_VALUE); } @Override public void onNext(HttpObject httpObject) { decorated.write(httpObject); } @Override public void onError(Throwable t) { decorated.close(t); } @Override public void onComplete() { decorated.close(); } }); return decorated; }
Example 18
Source File: PooledResponseBufferBenchmark.java From armeria with Apache License 2.0 | 5 votes |
@Override protected HttpResponse serve(PooledHttpService delegate, ServiceRequestContext ctx, PooledHttpRequest req) throws Exception { final PooledHttpResponse res = delegate.serve(ctx, req); final HttpResponseWriter decorated = HttpResponse.streaming(); res.subscribeWithPooledObjects(new Subscriber<HttpObject>() { @Override public void onSubscribe(Subscription s) { s.request(Long.MAX_VALUE); } @Override public void onNext(HttpObject httpObject) { decorated.write(httpObject); } @Override public void onError(Throwable t) { decorated.close(t); } @Override public void onComplete() { decorated.close(); } }); return decorated; }
Example 19
Source File: FramedGrpcService.java From armeria with Apache License 2.0 | 4 votes |
@Override protected HttpResponse doPost(ServiceRequestContext ctx, PooledHttpRequest req) throws Exception { final MediaType contentType = req.contentType(); final SerializationFormat serializationFormat = findSerializationFormat(contentType); if (serializationFormat == null) { return HttpResponse.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE, MediaType.PLAIN_TEXT_UTF_8, "Missing or invalid Content-Type header."); } ctx.logBuilder().serializationFormat(serializationFormat); final String methodName = GrpcRequestUtil.determineMethod(ctx); if (methodName == null) { return HttpResponse.of(HttpStatus.BAD_REQUEST, MediaType.PLAIN_TEXT_UTF_8, "Invalid path."); } final ServerMethodDefinition<?, ?> method = registry.lookupMethod(methodName); if (method == null) { return HttpResponse.of( (ResponseHeaders) ArmeriaServerCall.statusToTrailers( ctx, Status.UNIMPLEMENTED.withDescription("Method not found: " + methodName), new Metadata(), false)); } if (useClientTimeoutHeader) { final String timeoutHeader = req.headers().get(GrpcHeaderNames.GRPC_TIMEOUT); if (timeoutHeader != null) { try { final long timeout = TimeoutHeaderUtil.fromHeaderValue(timeoutHeader); if (timeout == 0) { ctx.clearRequestTimeout(); } else { ctx.setRequestTimeout(TimeoutMode.SET_FROM_NOW, Duration.ofNanos(timeout)); } } catch (IllegalArgumentException e) { return HttpResponse.of( (ResponseHeaders) ArmeriaServerCall.statusToTrailers( ctx, GrpcStatus.fromThrowable(e), new Metadata(), false)); } } } final int methodIndex = methodName.lastIndexOf('/') + 1; ctx.logBuilder().name(method.getMethodDescriptor().getServiceName(), methodName.substring(methodIndex)); ctx.logBuilder().defer(RequestLogProperty.REQUEST_CONTENT, RequestLogProperty.RESPONSE_CONTENT); final HttpResponseWriter res = HttpResponse.streaming(); final ArmeriaServerCall<?, ?> call = startCall( methodName, method, ctx, req.headers(), res, serializationFormat); if (call != null) { ctx.setRequestTimeoutHandler(() -> call.close(Status.CANCELLED, new Metadata())); req.subscribeWithPooledObjects(call.messageReader(), ctx.eventLoop()); req.whenComplete().handleAsync(call.messageReader(), ctx.eventLoop()); } return res; }
Example 20
Source File: ResponseConversionUtil.java From armeria with Apache License 2.0 | 3 votes |
/** * Returns a new {@link HttpResponseWriter} which sends a streaming response from the specified * {@link Publisher}. * * @param publisher publishes objects * @param headers to be written to the returned {@link HttpResponseWriter} * @param trailers to be written to the returned {@link HttpResponseWriter} * @param contentConverter converts the published objects into streaming contents of the response */ public static <T> HttpResponseWriter streamingFrom(Publisher<T> publisher, ResponseHeaders headers, HttpHeaders trailers, Function<T, HttpData> contentConverter) { final HttpResponseWriter writer = HttpResponse.streaming(); publisher.subscribe(new StreamingSubscriber<>(writer, headers, trailers, contentConverter)); return writer; }