private void setHostHeader() { url.authority() .ifPresent(authority -> header(HOST, authority.hostAndPort())); }
private void 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)); } }
private void ensureMethodIsValid() { checkArgument(isMethodValid(), "Unrecognised HTTP method=%s", this.method); }
/** * Builds a new full request based on the settings configured in this builder. * If {@code validate} is set to true: * <ul> * <li>the host header will be set if absent</li> * <li>an exception will be thrown if the content length is not an integer, or more than one content length exists</li> * <li>an exception will be thrown if the request method is not a valid HTTP method</li> * </ul> * * @return a new full request */ public StreamingHttpRequest build() { if (validate) { ensureContentLengthIsValid(); ensureMethodIsValid(); setHostHeader(); } return new StreamingHttpRequest(this); }
/** * Adds a new header with the specified {@code name} and {@code value}. * <p/> * Will not replace any existing values for the header. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ public Builder addHeader(CharSequence name, Object value) { checkNotCookie(name); this.headers.add(name, value); return this; }
/** * Sets the (only) value for the header with the specified name. * <p/> * All existing values for the same header will be removed. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ public Builder header(CharSequence name, Object value) { checkNotCookie(name); this.headers.set(name, value); return this; }
/** * Creates a request with the DELETE method. * * @param uri URI * @return {@code this} */ public static Builder delete(String uri) { return new Builder(DELETE, uri); }
/** * Creates a request with the HEAD method. * * @param uri URI * @return {@code this} */ public static Builder head(String uri) { return new Builder(HEAD, uri); }
/** * Return a new {@link Builder} that will inherit properties from this request. * This allows a new request 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 request */ public Builder newBuilder() { return new Builder(this); }
/** * Creates a request with the PATCH method. * * @param uri URI * @return {@code this} */ public static Builder patch(String uri) { return new Builder(PATCH, uri); }
/** * Creates a request with the POST method. * * @param uri URI * @return {@code this} */ public static Builder post(String uri) { return new Builder(POST, uri); }
/** * 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 a request with the GET method. * * @param uri URI * @return {@code this} */ public static Builder get(String uri) { return new Builder(GET, uri); }
/** * Creates a request with the PUT method. * * @param uri URI * @return {@code this} */ public static Builder put(String uri) { return new Builder(PUT, uri); }
/** * Enables Keep-Alive. * * @return {@code this} */ public Builder enableKeepAlive() { return header(CONNECTION, KEEP_ALIVE); }