From 71b021c7cca87a6ca1d71532961dd082cf50c66a Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 20 Feb 2017 18:26:15 -0500 Subject: [PATCH] Add decorators for ClientHttpRequest & Response --- .../reactive/ClientHttpRequestDecorator.java | 113 ++++++++++++++++++ .../reactive/ClientHttpResponseDecorator.java | 79 ++++++++++++ .../reactive/ServerHttpResponseDecorator.java | 6 + 3 files changed, 198 insertions(+) create mode 100644 spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpRequestDecorator.java create mode 100644 spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponseDecorator.java diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpRequestDecorator.java b/spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpRequestDecorator.java new file mode 100644 index 00000000000..7258d982f84 --- /dev/null +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpRequestDecorator.java @@ -0,0 +1,113 @@ +/* + * Copyright 2002-2017 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.http.client.reactive; + +import java.net.URI; +import java.util.function.Supplier; + +import org.reactivestreams.Publisher; +import reactor.core.publisher.Mono; + +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.io.buffer.DataBufferFactory; +import org.springframework.http.HttpCookie; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.util.Assert; +import org.springframework.util.MultiValueMap; + +/** + * Wraps another {@link ClientHttpRequest} and delegates all methods to it. + * Sub-classes can override specific methods selectively. + * + * @author Rossen Stoyanchev + * @since 5.0 + */ +public class ClientHttpRequestDecorator implements ClientHttpRequest { + + private final ClientHttpRequest delegate; + + + public ClientHttpRequestDecorator(ClientHttpRequest delegate) { + Assert.notNull(delegate, "ClientHttpRequest delegate is required."); + this.delegate = delegate; + } + + + public ClientHttpRequest getDelegate() { + return this.delegate; + } + + + // ClientHttpRequest delegation methods... + + @Override + public HttpMethod getMethod() { + return this.delegate.getMethod(); + } + + @Override + public URI getURI() { + return this.delegate.getURI(); + } + + @Override + public HttpHeaders getHeaders() { + return this.delegate.getHeaders(); + } + + @Override + public MultiValueMap getCookies() { + return this.delegate.getCookies(); + } + + @Override + public DataBufferFactory bufferFactory() { + return this.delegate.bufferFactory(); + } + + @Override + public void beforeCommit(Supplier> action) { + this.delegate.beforeCommit(action); + } + + @Override + public boolean isCommitted() { + return this.delegate.isCommitted(); + } + + @Override + public Mono writeWith(Publisher body) { + return this.delegate.writeWith(body); + } + + @Override + public Mono writeAndFlushWith(Publisher> body) { + return this.delegate.writeAndFlushWith(body); + } + + @Override + public Mono setComplete() { + return this.delegate.setComplete(); + } + + + @Override + public String toString() { + return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]"; + } + +} diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponseDecorator.java b/spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponseDecorator.java new file mode 100644 index 00000000000..e9716f8248f --- /dev/null +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponseDecorator.java @@ -0,0 +1,79 @@ +/* + * Copyright 2002-2017 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.http.client.reactive; + +import reactor.core.publisher.Flux; + +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseCookie; +import org.springframework.util.Assert; +import org.springframework.util.MultiValueMap; + +/** + * Wraps another {@link ClientHttpResponse} and delegates all methods to it. + * Sub-classes can override specific methods selectively. + * + * @author Rossen Stoyanchev + * @since 5.0 + */ +public class ClientHttpResponseDecorator implements ClientHttpResponse { + + private final ClientHttpResponse delegate; + + + public ClientHttpResponseDecorator(ClientHttpResponse delegate) { + Assert.notNull(delegate, "ClientHttpResponse delegate is required."); + this.delegate = delegate; + } + + + public ClientHttpResponse getDelegate() { + return this.delegate; + } + + + // ServerHttpResponse delegation methods... + + + @Override + public HttpStatus getStatusCode() { + return this.delegate.getStatusCode(); + } + + @Override + public HttpHeaders getHeaders() { + return this.delegate.getHeaders(); + } + + @Override + public MultiValueMap getCookies() { + return this.delegate.getCookies(); + } + + @Override + public Flux getBody() { + return this.delegate.getBody(); + } + + + @Override + public String toString() { + return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]"; + } + +} diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponseDecorator.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponseDecorator.java index 0450ceb3a65..4d99573d225 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponseDecorator.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponseDecorator.java @@ -114,4 +114,10 @@ public class ServerHttpResponseDecorator implements ServerHttpResponse { return getDelegate().setComplete(); } + + @Override + public String toString() { + return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]"; + } + }