/** * Converts this response to a streaming form (HttpResponse). * * Converts this response to a HttpResponse object which represents the HTTP response as a * stream of bytes. * * @return A streaming HttpResponse object. */ public HttpResponse toStreamingResponse() { if (this.body.length == 0) { return new HttpResponse.Builder(this, Observable.empty()).build(); } else { return new HttpResponse.Builder(this, just(Unpooled.copiedBuffer(this.body))).build(); } }
@Override protected LiveHttpResponse doHandle(LiveHttpRequest request) { try { String path = request.path(); if (!path.startsWith(root) || path.contains("..")) { return error(FORBIDDEN); } return new HttpResponse.Builder(OK) .body(resourceBody(path), true) .header(CONTENT_TYPE, mediaTypeOf(path)) .build() .stream(); } catch (FileNotFoundException e) { return error(NOT_FOUND); } catch (IOException e) { return error(INTERNAL_SERVER_ERROR); } }
/** * Aggregates content stream and converts this response to a {@link HttpResponse}. * <p> * Returns a {@link Eventual <HttpResponse>} that eventually produces a * {@link HttpResponse}. The resulting full response object has the same * response line, headers, and content as this response. * <p> * The content stream is aggregated asynchronously. The stream may be connected * to a network socket or some other content producer. Once aggregated, a * HttpResponse object is emitted on the returned {@link Eventual}. * <p> * A sole {@code maxContentBytes} argument is a backstop defence against excessively * long content streams. The {@code maxContentBytes} should be set to a sensible * value according to your application requirements and heap size. When the content * size stream exceeds the {@code maxContentBytes}, a @{link ContentOverflowException} * is emitted on the returned observable. * * @param maxContentBytes maximum expected content size * @return a {@link Eventual} */ public Eventual<HttpResponse> aggregate(int maxContentBytes) { return Eventual.from(body.aggregate(maxContentBytes)) .map(it -> new HttpResponse.Builder(this, decodeAndRelease(it)) .disableValidation() .build() ); }
/** * Return a new {@link HttpResponse.Builder} that will inherit properties from this response. * <p> * This allows a new response to be made that is identical to this one * except for the properties overridden by the builder methods. * * @return new builder based on this response */ public Builder newBuilder() { return new Builder(this); }
/** * Creates an HTTP response builder with a given status and empty body. * * @param status response status * @return a new builder */ public static Builder response(HttpResponseStatus status) { return new Builder(status); }
/** * Return a new {@link HttpResponse.Builder} that will inherit properties from this response. * This allows a new response to be made that will be identical to this one except for the properties * overridden by the builder methods. * * @return new builder based on this response */ public Builder newBuilder() { return new Builder(this); }
/** * Creates a builder with a given HTTP status code. * * @param status status code * @return {@code this} */ public static Builder response(HttpResponseStatus status) { return new Builder(status); }
/** * Return a new {@link HttpResponse.Builder} that will inherit properties from this response. * This allows a new response to be made that will be identical to this one except for the properties * overridden by the builder methods. * * @param response a response * @return new builder based on the given response */ public static Builder newBuilder(HttpResponse response) { return new Builder(response); }
/** * Creates an HTTP response builder with a status of 200 OK and empty body. * * @return a new builder */ public static Builder response() { return new Builder(); }