org.springframework.web.reactive.function.server.DefaultEntityResponseBuilder Maven / Gradle / Ivy
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.function.server;
import java.net.URI;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import reactor.core.publisher.Mono;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.server.ServerWebExchange;
/**
* Default {@link EntityResponse.Builder} implementation.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 5.0
*/
class DefaultEntityResponseBuilder implements EntityResponse.Builder {
private final T entity;
private final BodyInserter inserter;
private int status = HttpStatus.OK.value();
private final HttpHeaders headers = new HttpHeaders();
private final MultiValueMap cookies = new LinkedMultiValueMap<>();
private final Map hints = new HashMap<>();
public DefaultEntityResponseBuilder(T entity, BodyInserter inserter) {
this.entity = entity;
this.inserter = inserter;
}
@Override
public EntityResponse.Builder status(HttpStatus status) {
Assert.notNull(status, "HttpStatus must not be null");
this.status = status.value();
return this;
}
@Override
public EntityResponse.Builder status(int status) {
this.status = status;
return this;
}
@Override
public EntityResponse.Builder cookie(ResponseCookie cookie) {
Assert.notNull(cookie, "ResponseCookie must not be null");
this.cookies.add(cookie.getName(), cookie);
return this;
}
@Override
public EntityResponse.Builder cookies(Consumer> cookiesConsumer) {
cookiesConsumer.accept(this.cookies);
return this;
}
@Override
public EntityResponse.Builder header(String headerName, String... headerValues) {
for (String headerValue : headerValues) {
this.headers.add(headerName, headerValue);
}
return this;
}
@Override
public EntityResponse.Builder headers(HttpHeaders headers) {
this.headers.putAll(headers);
return this;
}
@Override
public EntityResponse.Builder allow(HttpMethod... allowedMethods) {
this.headers.setAllow(new LinkedHashSet<>(Arrays.asList(allowedMethods)));
return this;
}
@Override
public EntityResponse.Builder allow(Set allowedMethods) {
this.headers.setAllow(allowedMethods);
return this;
}
@Override
public EntityResponse.Builder contentLength(long contentLength) {
this.headers.setContentLength(contentLength);
return this;
}
@Override
public EntityResponse.Builder contentType(MediaType contentType) {
this.headers.setContentType(contentType);
return this;
}
@Override
public EntityResponse.Builder eTag(String etag) {
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
}
if (!etag.endsWith("\"")) {
etag = etag + "\"";
}
this.headers.setETag(etag);
return this;
}
@Override
public EntityResponse.Builder hint(String key, Object value) {
this.hints.put(key, value);
return this;
}
@Override
public EntityResponse.Builder lastModified(ZonedDateTime lastModified) {
ZonedDateTime gmt = lastModified.withZoneSameInstant(ZoneId.of("GMT"));
String headerValue = DateTimeFormatter.RFC_1123_DATE_TIME.format(gmt);
this.headers.set(HttpHeaders.LAST_MODIFIED, headerValue);
return this;
}
@Override
public EntityResponse.Builder location(URI location) {
this.headers.setLocation(location);
return this;
}
@Override
public EntityResponse.Builder cacheControl(CacheControl cacheControl) {
String ccValue = cacheControl.getHeaderValue();
if (ccValue != null) {
this.headers.setCacheControl(cacheControl.getHeaderValue());
}
return this;
}
@Override
public EntityResponse.Builder varyBy(String... requestHeaders) {
this.headers.setVary(Arrays.asList(requestHeaders));
return this;
}
@Override
public Mono> build() {
return Mono.just(new DefaultEntityResponse(
this.status, this.headers, this.cookies, this.entity, this.inserter, this.hints));
}
private static final class DefaultEntityResponse
extends DefaultServerResponseBuilder.AbstractServerResponse
implements EntityResponse {
private final T entity;
private final BodyInserter inserter;
private final Map hints;
public DefaultEntityResponse(int statusCode, HttpHeaders headers,
MultiValueMap cookies, T entity,
BodyInserter inserter, Map hints) {
super(statusCode, headers, cookies);
this.entity = entity;
this.inserter = inserter;
this.hints = hints;
}
@Override
public T entity() {
return this.entity;
}
@Override
public BodyInserter inserter() {
return this.inserter;
}
@Override
protected Mono writeToInternal(ServerWebExchange exchange, Context context) {
return inserter().insert(exchange.getResponse(), new BodyInserter.Context() {
@Override
public List> messageWriters() {
return context.messageWriters();
}
@Override
public Optional serverRequest() {
return Optional.of(exchange.getRequest());
}
@Override
public Map hints() {
return hints;
}
});
}
}
}