Skip to content

Commit 71b021c

Browse files
committed
Add decorators for ClientHttpRequest & Response
1 parent d59dc97 commit 71b021c

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2002-2017 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+
* http://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+
package org.springframework.http.client.reactive;
17+
18+
import java.net.URI;
19+
import java.util.function.Supplier;
20+
21+
import org.reactivestreams.Publisher;
22+
import reactor.core.publisher.Mono;
23+
24+
import org.springframework.core.io.buffer.DataBuffer;
25+
import org.springframework.core.io.buffer.DataBufferFactory;
26+
import org.springframework.http.HttpCookie;
27+
import org.springframework.http.HttpHeaders;
28+
import org.springframework.http.HttpMethod;
29+
import org.springframework.util.Assert;
30+
import org.springframework.util.MultiValueMap;
31+
32+
/**
33+
* Wraps another {@link ClientHttpRequest} and delegates all methods to it.
34+
* Sub-classes can override specific methods selectively.
35+
*
36+
* @author Rossen Stoyanchev
37+
* @since 5.0
38+
*/
39+
public class ClientHttpRequestDecorator implements ClientHttpRequest {
40+
41+
private final ClientHttpRequest delegate;
42+
43+
44+
public ClientHttpRequestDecorator(ClientHttpRequest delegate) {
45+
Assert.notNull(delegate, "ClientHttpRequest delegate is required.");
46+
this.delegate = delegate;
47+
}
48+
49+
50+
public ClientHttpRequest getDelegate() {
51+
return this.delegate;
52+
}
53+
54+
55+
// ClientHttpRequest delegation methods...
56+
57+
@Override
58+
public HttpMethod getMethod() {
59+
return this.delegate.getMethod();
60+
}
61+
62+
@Override
63+
public URI getURI() {
64+
return this.delegate.getURI();
65+
}
66+
67+
@Override
68+
public HttpHeaders getHeaders() {
69+
return this.delegate.getHeaders();
70+
}
71+
72+
@Override
73+
public MultiValueMap<String, HttpCookie> getCookies() {
74+
return this.delegate.getCookies();
75+
}
76+
77+
@Override
78+
public DataBufferFactory bufferFactory() {
79+
return this.delegate.bufferFactory();
80+
}
81+
82+
@Override
83+
public void beforeCommit(Supplier<? extends Mono<Void>> action) {
84+
this.delegate.beforeCommit(action);
85+
}
86+
87+
@Override
88+
public boolean isCommitted() {
89+
return this.delegate.isCommitted();
90+
}
91+
92+
@Override
93+
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
94+
return this.delegate.writeWith(body);
95+
}
96+
97+
@Override
98+
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
99+
return this.delegate.writeAndFlushWith(body);
100+
}
101+
102+
@Override
103+
public Mono<Void> setComplete() {
104+
return this.delegate.setComplete();
105+
}
106+
107+
108+
@Override
109+
public String toString() {
110+
return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]";
111+
}
112+
113+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2002-2017 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+
* http://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+
package org.springframework.http.client.reactive;
17+
18+
import reactor.core.publisher.Flux;
19+
20+
import org.springframework.core.io.buffer.DataBuffer;
21+
import org.springframework.http.HttpHeaders;
22+
import org.springframework.http.HttpStatus;
23+
import org.springframework.http.ResponseCookie;
24+
import org.springframework.util.Assert;
25+
import org.springframework.util.MultiValueMap;
26+
27+
/**
28+
* Wraps another {@link ClientHttpResponse} and delegates all methods to it.
29+
* Sub-classes can override specific methods selectively.
30+
*
31+
* @author Rossen Stoyanchev
32+
* @since 5.0
33+
*/
34+
public 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+
public ClientHttpResponse getDelegate() {
46+
return this.delegate;
47+
}
48+
49+
50+
// ServerHttpResponse delegation methods...
51+
52+
53+
@Override
54+
public HttpStatus getStatusCode() {
55+
return this.delegate.getStatusCode();
56+
}
57+
58+
@Override
59+
public HttpHeaders getHeaders() {
60+
return this.delegate.getHeaders();
61+
}
62+
63+
@Override
64+
public MultiValueMap<String, ResponseCookie> getCookies() {
65+
return this.delegate.getCookies();
66+
}
67+
68+
@Override
69+
public Flux<DataBuffer> getBody() {
70+
return this.delegate.getBody();
71+
}
72+
73+
74+
@Override
75+
public String toString() {
76+
return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]";
77+
}
78+
79+
}

spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponseDecorator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,10 @@ public Mono<Void> setComplete() {
114114
return getDelegate().setComplete();
115115
}
116116

117+
118+
@Override
119+
public String toString() {
120+
return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]";
121+
}
122+
117123
}

0 commit comments

Comments
 (0)