org.springframework.http.client.JdkHttp2ClientHttpResponse Maven / Gradle / Ivy
The newest version!
package org.springframework.http.client;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
class JdkHttp2ClientHttpResponse extends AbstractClientHttpResponse {
private final HttpResponse response;
@Nullable
private volatile HttpHeaders headers;
public JdkHttp2ClientHttpResponse(HttpResponse response) {
this.response = response;
}
@Override
public int getRawStatusCode() {
return this.response.statusCode();
}
@Override
public String getStatusText() {
HttpStatus httpStatus = HttpStatus.resolve(getRawStatusCode());
return httpStatus.getReasonPhrase();
}
@Override
public InputStream getBody() {
InputStream body = this.response.body();
return body == null ? StreamUtils.emptyInput() : body;
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = this.headers;
if (headers == null) {
headers = new HttpHeaders();
Map> headerMap = this.response.headers().map();
for (Map.Entry> entry : headerMap.entrySet()) {
headers.addAll(entry.getKey(), entry.getValue());
}
this.headers = headers;
}
return headers;
}
@Override
public void close() {
InputStream body = this.response.body();
if (body != null) {
try {
body.close();
} catch (IOException e) {
/*ignore*/
}
}
}
}