From fd6fc30eead2f509b10c422c77a9f15b579f0fcf Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Thu, 31 Mar 2016 12:33:45 -0500 Subject: [PATCH] Add RequestPostProcessor Fixes gh-81 --- .../reactive/DefaultHttpRequestBuilder.java | 16 ++++++ .../client/reactive/RequestPostProcessor.java | 36 +++++++++++++ .../DefaultHttpRequestBuilderTests.java | 52 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 spring-web-reactive/src/main/java/org/springframework/web/client/reactive/RequestPostProcessor.java create mode 100644 spring-web-reactive/src/test/java/org/springframework/web/client/reactive/DefaultHttpRequestBuilderTests.java diff --git a/spring-web-reactive/src/main/java/org/springframework/web/client/reactive/DefaultHttpRequestBuilder.java b/spring-web-reactive/src/main/java/org/springframework/web/client/reactive/DefaultHttpRequestBuilder.java index c6e19a414c..05fb554f92 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/client/reactive/DefaultHttpRequestBuilder.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/client/reactive/DefaultHttpRequestBuilder.java @@ -38,6 +38,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.client.reactive.ClientHttpRequest; import org.springframework.http.client.reactive.ClientHttpRequestFactory; +import org.springframework.util.Assert; import org.springframework.web.client.RestClientException; /** @@ -137,6 +138,21 @@ public class DefaultHttpRequestBuilder implements HttpRequestBuilder { return this; } + /** + * Allows performing more complex operations with a strategy. For example, a + * {@link RequestPostProcessor} implementation might accept the arguments of + * username and password and set an HTTP Basic authentication header. + * + * @param postProcessor the {@link RequestPostProcessor} to use. Cannot be null. + * + * @return this instance for further modifications. + */ + public DefaultHttpRequestBuilder apply(RequestPostProcessor postProcessor) { + Assert.notNull(postProcessor, "`postProcessor` is required"); + postProcessor.postProcess(this); + return this; + } + public ClientHttpRequest build(ClientHttpRequestFactory factory) { ClientHttpRequest request = factory.createRequest(this.httpMethod, this.url, this.httpHeaders); request.getHeaders().putAll(this.httpHeaders); diff --git a/spring-web-reactive/src/main/java/org/springframework/web/client/reactive/RequestPostProcessor.java b/spring-web-reactive/src/main/java/org/springframework/web/client/reactive/RequestPostProcessor.java new file mode 100644 index 0000000000..ac8ec9783e --- /dev/null +++ b/spring-web-reactive/src/main/java/org/springframework/web/client/reactive/RequestPostProcessor.java @@ -0,0 +1,36 @@ +/* + * Copyright 2002-2016 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.web.client.reactive; + +/** + * Allows post processing the {@link DefaultHttpRequestBuilder} for strategy for + * performing more complex operations. + * + * @author Rob Winch + * @see DefaultHttpRequestBuilder#apply(RequestPostProcessor) + */ +public interface RequestPostProcessor { + + /** + * Implementations can modify the {@link DefaultHttpRequestBuilder} passed + * in. + * + * @param toPostProcess + * the {@link DefaultHttpRequestBuilder} to be modified. + */ + void postProcess(DefaultHttpRequestBuilder toPostProcess); +} diff --git a/spring-web-reactive/src/test/java/org/springframework/web/client/reactive/DefaultHttpRequestBuilderTests.java b/spring-web-reactive/src/test/java/org/springframework/web/client/reactive/DefaultHttpRequestBuilderTests.java new file mode 100644 index 0000000000..112c62d6d7 --- /dev/null +++ b/spring-web-reactive/src/test/java/org/springframework/web/client/reactive/DefaultHttpRequestBuilderTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2002-2016 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.web.client.reactive; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.http.HttpMethod; + +/** + * + * @author Rob Winch + * + */ +public class DefaultHttpRequestBuilderTests { + private DefaultHttpRequestBuilder builder; + + @Before + public void setup() { + builder = new DefaultHttpRequestBuilder(HttpMethod.GET, "https://example.com/foo"); + } + + @Test + public void apply() { + RequestPostProcessor postProcessor = mock(RequestPostProcessor.class); + + builder.apply(postProcessor); + + verify(postProcessor).postProcess(builder); + } + + @Test(expected = IllegalArgumentException.class) + public void applyNullPostProcessorThrowsIllegalArgumentException() { + builder.apply(null); + } +}