package org.springframework.http.server;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
public class ServletServerHttpResponse implements ServerHttpResponse {
private final HttpServletResponse servletResponse;
private final HttpHeaders ;
private boolean = false;
private boolean bodyUsed = false;
@Nullable
private HttpHeaders ;
public ServletServerHttpResponse(HttpServletResponse servletResponse) {
Assert.notNull(servletResponse, "HttpServletResponse must not be null");
this.servletResponse = servletResponse;
this.headers = new ServletResponseHttpHeaders();
}
public HttpServletResponse getServletResponse() {
return this.servletResponse;
}
@Override
public void setStatusCode(HttpStatus status) {
Assert.notNull(status, "HttpStatus must not be null");
this.servletResponse.setStatus(status.value());
}
@Override
public HttpHeaders () {
if (this.readOnlyHeaders != null) {
return this.readOnlyHeaders;
}
else if (this.headersWritten) {
this.readOnlyHeaders = HttpHeaders.readOnlyHttpHeaders(this.headers);
return this.readOnlyHeaders;
}
else {
return this.headers;
}
}
@Override
public OutputStream getBody() throws IOException {
this.bodyUsed = true;
writeHeaders();
return this.servletResponse.getOutputStream();
}
@Override
public void flush() throws IOException {
writeHeaders();
if (this.bodyUsed) {
this.servletResponse.flushBuffer();
}
}
@Override
public void close() {
writeHeaders();
}
private void () {
if (!this.headersWritten) {
getHeaders().forEach((headerName, headerValues) -> {
for (String headerValue : headerValues) {
this.servletResponse.addHeader(headerName, headerValue);
}
});
if (this.servletResponse.getContentType() == null && this.headers.getContentType() != null) {
this.servletResponse.setContentType(this.headers.getContentType().toString());
}
if (this.servletResponse.getCharacterEncoding() == null && this.headers.getContentType() != null &&
this.headers.getContentType().getCharset() != null) {
this.servletResponse.setCharacterEncoding(this.headers.getContentType().getCharset().name());
}
long contentLength = getHeaders().getContentLength();
if (contentLength != -1) {
this.servletResponse.setContentLengthLong(contentLength);
}
this.headersWritten = true;
}
}
private class extends HttpHeaders {
private static final long = 3410708522401046302L;
@Override
public boolean (Object key) {
return (super.containsKey(key) || (get(key) != null));
}
@Override
@Nullable
public String (String headerName) {
if (headerName.equalsIgnoreCase(CONTENT_TYPE)) {
String value = super.getFirst(headerName);
return (value != null ? value : servletResponse.getHeader(headerName));
}
else {
String value = servletResponse.getHeader(headerName);
return (value != null ? value : super.getFirst(headerName));
}
}
@Override
public List<String> (Object key) {
Assert.isInstanceOf(String.class, key, "Key must be a String-based header name");
String headerName = (String) key;
if (headerName.equalsIgnoreCase(CONTENT_TYPE)) {
return Collections.singletonList(getFirst(headerName));
}
Collection<String> values1 = servletResponse.getHeaders(headerName);
if (headersWritten) {
return new ArrayList<>(values1);
}
boolean isEmpty1 = CollectionUtils.isEmpty(values1);
List<String> values2 = super.get(key);
boolean isEmpty2 = CollectionUtils.isEmpty(values2);
if (isEmpty1 && isEmpty2) {
return null;
}
List<String> values = new ArrayList<>();
if (!isEmpty1) {
values.addAll(values1);
}
if (!isEmpty2) {
values.addAll(values2);
}
return values;
}
}
}