Skip to content

Commit 6479566

Browse files
committed
Add ClientHttpResponseDecorator
See gh-28190
1 parent 6b1a845 commit 6479566

File tree

4 files changed

+99
-43
lines changed

4 files changed

+99
-43
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.client;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
22+
import org.springframework.http.HttpHeaders;
23+
import org.springframework.http.HttpStatusCode;
24+
import org.springframework.http.client.ClientHttpResponse;
25+
import org.springframework.util.Assert;
26+
27+
28+
/**
29+
* Wrap and delegate to an existing {@link ClientHttpResponse}.
30+
*
31+
* @author Rossen Stoyanchev
32+
* @since 6.0
33+
*/
34+
class ClientHttpResponseDecorator implements ClientHttpResponse {
35+
36+
private final ClientHttpResponse delegate;
37+
38+
39+
public ClientHttpResponseDecorator(ClientHttpResponse delegate) {
40+
Assert.notNull(delegate, "ClientHttpResponse delegate is required");
41+
this.delegate = delegate;
42+
}
43+
44+
45+
/**
46+
* Return the wrapped response.
47+
*/
48+
public ClientHttpResponse getDelegate() {
49+
return this.delegate;
50+
}
51+
52+
53+
@Override
54+
public HttpStatusCode getStatusCode() throws IOException {
55+
return this.delegate.getStatusCode();
56+
}
57+
58+
@SuppressWarnings("deprecation")
59+
@Override
60+
public int getRawStatusCode() throws IOException {
61+
return this.delegate.getRawStatusCode();
62+
}
63+
64+
@Override
65+
public String getStatusText() throws IOException {
66+
return this.delegate.getStatusText();
67+
}
68+
69+
@Override
70+
public HttpHeaders getHeaders() {
71+
return this.delegate.getHeaders();
72+
}
73+
74+
@Override
75+
public InputStream getBody() throws IOException {
76+
return this.delegate.getBody();
77+
}
78+
79+
@Override
80+
public void close() {
81+
this.delegate.close();
82+
}
83+
84+
}

spring-web/src/main/java/org/springframework/web/client/HttpMessageConverterExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -86,7 +86,7 @@ public HttpMessageConverterExtractor(Type responseType, List<HttpMessageConverte
8686
@Override
8787
@SuppressWarnings({"unchecked", "rawtypes", "resource"})
8888
public T extractData(ClientHttpResponse response) throws IOException {
89-
MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response);
89+
IntrospectingClientHttpResponse responseWrapper = new IntrospectingClientHttpResponse(response);
9090
if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) {
9191
return null;
9292
}
Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import java.io.InputStream;
2121
import java.io.PushbackInputStream;
2222

23-
import org.springframework.http.HttpHeaders;
2423
import org.springframework.http.HttpStatus;
2524
import org.springframework.http.HttpStatusCode;
2625
import org.springframework.http.client.ClientHttpResponse;
@@ -32,19 +31,18 @@
3231
* by actually reading the input stream.
3332
*
3433
* @author Brian Clozel
34+
* @author Rossen Stoyanchev
3535
* @since 4.1.5
3636
* @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3.3">RFC 7230 Section 3.3.3</a>
3737
*/
38-
class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse {
39-
40-
private final ClientHttpResponse response;
38+
class IntrospectingClientHttpResponse extends ClientHttpResponseDecorator {
4139

4240
@Nullable
4341
private PushbackInputStream pushbackInputStream;
4442

4543

46-
public MessageBodyClientHttpResponseWrapper(ClientHttpResponse response) {
47-
this.response = response;
44+
public IntrospectingClientHttpResponse(ClientHttpResponse response) {
45+
super(response);
4846
}
4947

5048

@@ -82,7 +80,7 @@ public boolean hasMessageBody() throws IOException {
8280
*/
8381
@SuppressWarnings("ConstantConditions")
8482
public boolean hasEmptyMessageBody() throws IOException {
85-
InputStream body = this.response.getBody();
83+
InputStream body = getDelegate().getBody();
8684
// Per contract body shouldn't be null, but check anyway..
8785
if (body == null) {
8886
return true;
@@ -111,35 +109,9 @@ public boolean hasEmptyMessageBody() throws IOException {
111109
}
112110

113111

114-
@Override
115-
public HttpHeaders getHeaders() {
116-
return this.response.getHeaders();
117-
}
118-
119112
@Override
120113
public InputStream getBody() throws IOException {
121-
return (this.pushbackInputStream != null ? this.pushbackInputStream : this.response.getBody());
122-
}
123-
124-
@Override
125-
public HttpStatusCode getStatusCode() throws IOException {
126-
return this.response.getStatusCode();
127-
}
128-
129-
@Override
130-
@Deprecated
131-
public int getRawStatusCode() throws IOException {
132-
return this.response.getRawStatusCode();
133-
}
134-
135-
@Override
136-
public String getStatusText() throws IOException {
137-
return this.response.getStatusText();
138-
}
139-
140-
@Override
141-
public void close() {
142-
this.response.close();
114+
return (this.pushbackInputStream != null ? this.pushbackInputStream : getDelegate().getBody());
143115
}
144116

145117
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,29 +28,29 @@
2828
import static org.mockito.Mockito.mock;
2929

3030
/**
31-
* Unit tests for {@link MessageBodyClientHttpResponseWrapper}.
31+
* Unit tests for {@link IntrospectingClientHttpResponse}.
3232
*
3333
* @since 5.3.10
3434
* @author Yin-Jui Liao
3535
*/
36-
class MessageBodyClientHttpResponseWrapperTests {
36+
class IntrospectingClientHttpResponseTests {
3737

3838
private final ClientHttpResponse response = mock(ClientHttpResponse.class);
3939

40-
private final MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response);
40+
private final IntrospectingClientHttpResponse wrappedResponse = new IntrospectingClientHttpResponse(response);
4141

4242

4343
@Test
4444
void messageBodyDoesNotExist() throws Exception {
4545
given(response.getBody()).willReturn(null);
46-
assertThat(responseWrapper.hasEmptyMessageBody()).isTrue();
46+
assertThat(wrappedResponse.hasEmptyMessageBody()).isTrue();
4747
}
4848

4949
@Test
5050
void messageBodyExists() throws Exception {
5151
InputStream stream = new ByteArrayInputStream("content".getBytes());
5252
given(response.getBody()).willReturn(stream);
53-
assertThat(responseWrapper.hasEmptyMessageBody()).isFalse();
53+
assertThat(wrappedResponse.hasEmptyMessageBody()).isFalse();
5454
}
5555

5656
}

0 commit comments

Comments
 (0)