headersConsumer);
/**
* Set the list of acceptable {@linkplain MediaType media types}, as
* specified by the {@code Accept} header.
* @param acceptableMediaTypes the acceptable media types
*/
B accept(MediaType... acceptableMediaTypes);
/**
* Set the list of acceptable {@linkplain Charset charsets}, as specified
* by the {@code Accept-Charset} header.
* @param acceptableCharsets the acceptable charsets
*/
B acceptCharset(Charset... acceptableCharsets);
/**
* Set the value of the {@code If-Modified-Since} header.
* @param ifModifiedSince the new value of the header
* @since 5.1.4
*/
B ifModifiedSince(ZonedDateTime ifModifiedSince);
/**
* Set the value of the {@code If-Modified-Since} header.
* @param ifModifiedSince the new value of the header
* @since 5.1.4
*/
B ifModifiedSince(Instant ifModifiedSince);
/**
* Set the value of the {@code If-Modified-Since} header.
* The date should be specified as the number of milliseconds since
* January 1, 1970 GMT.
* @param ifModifiedSince the new value of the header
*/
B ifModifiedSince(long ifModifiedSince);
/**
* Set the values of the {@code If-None-Match} header.
* @param ifNoneMatches the new value of the header
*/
B ifNoneMatch(String... ifNoneMatches);
/**
* Builds the request entity with no body.
* @return the request entity
* @see BodyBuilder#body(Object)
*/
RequestEntity build();
}
/**
* Defines a builder that adds a body to the response entity.
*/
public interface BodyBuilder extends HeadersBuilder {
/**
* Set the length of the body in bytes, as specified by the
* {@code Content-Length} header.
* @param contentLength the content length
* @return this builder
* @see HttpHeaders#setContentLength(long)
*/
BodyBuilder contentLength(long contentLength);
/**
* Set the {@linkplain MediaType media type} of the body, as specified
* by the {@code Content-Type} header.
* @param contentType the content type
* @return this builder
* @see HttpHeaders#setContentType(MediaType)
*/
BodyBuilder contentType(MediaType contentType);
/**
* Set the body of the request entity and build the RequestEntity.
* @param the type of the body
* @param body the body of the request entity
* @return the built request entity
*/
RequestEntity body(T body);
/**
* Set the body and type of the request entity and build the RequestEntity.
* @param the type of the body
* @param body the body of the request entity
* @param type the type of the body, useful for generic type resolution
* @return the built request entity
* @since 4.3
*/
RequestEntity body(T body, Type type);
}
private static class DefaultBodyBuilder implements BodyBuilder {
private final HttpMethod method;
private final HttpHeaders headers = new HttpHeaders();
@Nullable
private final URI uri;
@Nullable
private final String uriTemplate;
@Nullable
private final Object[] uriVarsArray;
@Nullable
private final Map uriVarsMap;
DefaultBodyBuilder(HttpMethod method, URI url) {
this.method = method;
this.uri = url;
this.uriTemplate = null;
this.uriVarsArray = null;
this.uriVarsMap = null;
}
DefaultBodyBuilder(HttpMethod method, String uriTemplate, Object... uriVars) {
this.method = method;
this.uri = null;
this.uriTemplate = uriTemplate;
this.uriVarsArray = uriVars;
this.uriVarsMap = null;
}
DefaultBodyBuilder(HttpMethod method, String uriTemplate, Map uriVars) {
this.method = method;
this.uri = null;
this.uriTemplate = uriTemplate;
this.uriVarsArray = null;
this.uriVarsMap = uriVars;
}
@Override
public BodyBuilder header(String headerName, String... headerValues) {
for (String headerValue : headerValues) {
this.headers.add(headerName, headerValue);
}
return this;
}
@Override
public BodyBuilder headers(@Nullable HttpHeaders headers) {
if (headers != null) {
this.headers.putAll(headers);
}
return this;
}
@Override
public BodyBuilder headers(Consumer headersConsumer) {
headersConsumer.accept(this.headers);
return this;
}
@Override
public BodyBuilder accept(MediaType... acceptableMediaTypes) {
this.headers.setAccept(Arrays.asList(acceptableMediaTypes));
return this;
}
@Override
public BodyBuilder acceptCharset(Charset... acceptableCharsets) {
this.headers.setAcceptCharset(Arrays.asList(acceptableCharsets));
return this;
}
@Override
public BodyBuilder contentLength(long contentLength) {
this.headers.setContentLength(contentLength);
return this;
}
@Override
public BodyBuilder contentType(MediaType contentType) {
this.headers.setContentType(contentType);
return this;
}
@Override
public BodyBuilder ifModifiedSince(ZonedDateTime ifModifiedSince) {
this.headers.setIfModifiedSince(ifModifiedSince);
return this;
}
@Override
public BodyBuilder ifModifiedSince(Instant ifModifiedSince) {
this.headers.setIfModifiedSince(ifModifiedSince);
return this;
}
@Override
public BodyBuilder ifModifiedSince(long ifModifiedSince) {
this.headers.setIfModifiedSince(ifModifiedSince);
return this;
}
@Override
public BodyBuilder ifNoneMatch(String... ifNoneMatches) {
this.headers.setIfNoneMatch(Arrays.asList(ifNoneMatches));
return this;
}
@Override
public RequestEntity build() {
return buildInternal(null, null);
}
@Override
public RequestEntity body(T body) {
return buildInternal(body, null);
}
@Override
public RequestEntity body(T body, Type type) {
return buildInternal(body, type);
}
private RequestEntity buildInternal(@Nullable T body, @Nullable Type type) {
if (this.uri != null) {
return new RequestEntity<>(body, this.headers, this.method, this.uri, type);
}
else if (this.uriTemplate != null){
return new UriTemplateRequestEntity<>(body, this.headers, this.method, type,
this.uriTemplate, this.uriVarsArray, this.uriVarsMap);
}
else {
throw new IllegalStateException("Neither URI nor URI template");
}
}
}
/**
* RequestEntity initialized with a URI template and variables instead of a {@link URI}.
* @since 5.3
* @param the body type
*/
public static class UriTemplateRequestEntity extends RequestEntity {
private final String uriTemplate;
@Nullable
private final Object[] uriVarsArray;
@Nullable
private final Map uriVarsMap;
UriTemplateRequestEntity(
@Nullable T body, @Nullable MultiValueMap headers,
@Nullable HttpMethod method, @Nullable Type type, String uriTemplate,
@Nullable Object[] uriVarsArray, @Nullable Map uriVarsMap) {
super(body, headers, method, null, type);
this.uriTemplate = uriTemplate;
this.uriVarsArray = uriVarsArray;
this.uriVarsMap = uriVarsMap;
}
public String getUriTemplate() {
return this.uriTemplate;
}
@Nullable
public Object[] getVars() {
return this.uriVarsArray;
}
@Nullable
public Map getVarsMap() {
return this.uriVarsMap;
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!super.equals(other)) {
return false;
}
UriTemplateRequestEntity> otherEntity = (UriTemplateRequestEntity>) other;
return (ObjectUtils.nullSafeEquals(this.uriTemplate, otherEntity.uriTemplate) &&
ObjectUtils.nullSafeEquals(this.uriVarsArray, otherEntity.uriVarsArray) &&
ObjectUtils.nullSafeEquals(this.uriVarsMap, otherEntity.uriVarsMap));
}
@Override
public int hashCode() {
return (29 * super.hashCode() + ObjectUtils.nullSafeHashCode(this.uriTemplate));
}
@Override
public String toString() {
return format(getMethod(), getUriTemplate(), getBody(), getHeaders());
}
}
}