/** * Aggregates and converts this streaming request FullHttpResponse. * <p> * Aggregates up to maxContentLength bytes of HTTP response content stream. Once content is * aggregated, this streaming HttpResponse instance is converted to a FullHttpResponse object * with the aggregated content set as a message body. * <p> * This method aggregates the content stream asynchronously. Once the FullHttpResponse is * available, it will be emitted as an Observable onNext event. If the number of content bytes * exceeds maxContentLength an exception is emitted as Observable onError event. * <p> * Performance considerations: An instantiation of FullHttpResponse takes a copy of the aggregated * HTTP message content. * * @param maxContentLength Maximum content bytes accepted from the HTTP content stream. * @return An {Observable} that emits the FullHttpResponse once it is available. */ public Observable<FullHttpResponse> toFullResponse(int maxContentLength) { return body.aggregate(maxContentLength) .map(decoded -> new FullHttpResponse.Builder(this, decoded.copy().array())) .map(FullHttpResponse.Builder::build); }
Builder ensureContentLengthIsValid() { List<String> contentLengths = headers.build().getAll(CONTENT_LENGTH); checkArgument(contentLengths.size() <= 1, "Duplicate Content-Length found. %s", contentLengths); if (contentLengths.size() == 1) { checkArgument(isInteger(contentLengths.get(0)), "Invalid Content-Length found. %s", contentLengths.get(0)); } return this; }
/** * Sets the response body. * * This method encodes the content to a byte array provided, and * sets the Content-Length header *if* the setContentLength * argument is true. * * @param content response body * @param setContentLength If true, Content-Length header is set, otherwise it is not set. * @return {@code this} */ public Builder body(byte[] content, boolean setContentLength) { this.body = content == null ? new byte[0] : content.clone(); if (setContentLength) { header(CONTENT_LENGTH, this.body.length); } return this; }
public Observable<FullHttpResponse> toFullHttpResponse(int maxContentBytes) { CompositeByteBuf byteBufs = compositeBuffer(); return body .lift(disableFlowControl()) .doOnError(e -> byteBufs.release()) .collect(() -> byteBufs, (composite, part) -> { long newSize = composite.readableBytes() + part.readableBytes(); if (newSize > maxContentBytes) { release(composite); release(part); throw new ContentOverflowException(format("Maximum content size exceeded. Maximum size allowed is %d bytes.", maxContentBytes)); } composite.addComponent(part); composite.writerIndex(composite.writerIndex() + part.readableBytes()); }) .map(StreamingHttpResponse::decodeAndRelease) .map(decoded -> new FullHttpResponse.Builder(this, decoded).build()); }
/** * Sets the response body. * * This method encodes the content to a byte array using the specified * charset, and sets the Content-Length header *if* the setContentLength * argument is true. * * @param content response body * @param charset Charset used for encoding response body. * @param setContentLength If true, Content-Length header is set, otherwise it is not set. * @return {@code this} */ public Builder body(String content, Charset charset, boolean setContentLength) { requireNonNull(charset, "Charset is not provided."); String sanitised = content == null ? "" : content; return body(sanitised.getBytes(charset), setContentLength); }
/** * Builds a new full response based on the settings configured in this builder. * If {@code validate} is set to true: * <ul> * <li>an exception will be thrown if the content length is not an integer, or more than one content length exists</li> * </ul> * * @return a new full response */ public FullHttpResponse build() { if (validate) { ensureContentLengthIsValid(); } return new FullHttpResponse(this); }
public Builder newBuilder() { return new Builder(this); }
/** * Sets the request body. * * This method encodes a String content to a byte array using the specified * charset, and sets the Content-Length header accordingly. * * @param content request body * @param charset Charset for string encoding. * @return {@code this} */ public Builder body(String content, Charset charset) { return body(content, charset, true); }
/** * Adds a response cookie (adds a new Set-Cookie header). * * @param name cookie name * @param value cookie value * @return {@code this} */ public Builder addCookie(String name, String value) { return addCookie(HttpCookie.cookie(name, value)); }
/** * 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); }
/** * 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(); }