From f54bec835d6f88694f0b1dcf31c5119eac50de59 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 29 Jun 2016 08:07:30 +0200 Subject: [PATCH] Move BasicAuthorizationInterceptor `BasicAuthorizationInterceptor` is now available in the core framework and this commit uses that instead of the outdated copy in Boot. Closes gh-6237 --- .../boot/test/TestRestTemplate.java | 27 +------ .../test/web/client/TestRestTemplate.java | 2 +- .../client/BasicAuthorizationInterceptor.java | 58 --------------- .../boot/web/client/RestTemplateBuilder.java | 1 + .../BasicAuthorizationInterceptorTests.java | 73 ------------------- .../web/client/RestTemplateBuilderTests.java | 1 + 6 files changed, 4 insertions(+), 158 deletions(-) delete mode 100644 spring-boot/src/main/java/org/springframework/boot/web/client/BasicAuthorizationInterceptor.java delete mode 100644 spring-boot/src/test/java/org/springframework/boot/web/client/BasicAuthorizationInterceptorTests.java diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/TestRestTemplate.java b/spring-boot-test/src/main/java/org/springframework/boot/test/TestRestTemplate.java index 9a734cf0df..78519f8ead 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/TestRestTemplate.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/TestRestTemplate.java @@ -32,13 +32,11 @@ import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.protocol.HttpContext; import org.springframework.http.HttpMethod; -import org.springframework.http.HttpRequest; -import org.springframework.http.client.ClientHttpRequestExecution; +import org.springframework.http.client.BasicAuthorizationInterceptor; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.InterceptingClientHttpRequestFactory; -import org.springframework.util.Base64Utils; import org.springframework.util.ClassUtils; import org.springframework.web.client.DefaultResponseErrorHandler; import org.springframework.web.client.RestTemplate; @@ -116,29 +114,6 @@ public class TestRestTemplate extends RestTemplate { } - private static class BasicAuthorizationInterceptor - implements ClientHttpRequestInterceptor { - - private final String username; - - private final String password; - - BasicAuthorizationInterceptor(String username, String password) { - this.username = username; - this.password = (password == null ? "" : password); - } - - @Override - public ClientHttpResponse intercept(HttpRequest request, byte[] body, - ClientHttpRequestExecution execution) throws IOException { - String token = Base64Utils.encodeToString( - (this.username + ":" + this.password).getBytes(UTF_8)); - request.getHeaders().add("Authorization", "Basic " + token); - return execution.execute(request, body); - } - - } - /** * {@link HttpComponentsClientHttpRequestFactory} to apply customizations. */ diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java b/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java index a4982dafac..af99ce3ee5 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java @@ -36,13 +36,13 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.protocol.HttpContext; import org.apache.http.ssl.SSLContextBuilder; -import org.springframework.boot.web.client.BasicAuthorizationInterceptor; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; +import org.springframework.http.client.BasicAuthorizationInterceptor; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; diff --git a/spring-boot/src/main/java/org/springframework/boot/web/client/BasicAuthorizationInterceptor.java b/spring-boot/src/main/java/org/springframework/boot/web/client/BasicAuthorizationInterceptor.java deleted file mode 100644 index e994245d08..0000000000 --- a/spring-boot/src/main/java/org/springframework/boot/web/client/BasicAuthorizationInterceptor.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2012-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.boot.web.client; - -import java.io.IOException; -import java.nio.charset.Charset; - -import org.springframework.http.HttpRequest; -import org.springframework.http.client.ClientHttpRequestExecution; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.util.Assert; -import org.springframework.util.Base64Utils; - -/** - * {@link ClientHttpRequestInterceptor} to apply a BASIC authorization header. - * - * @author Phillip Webb - * @since 1.4.0 - */ -public class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor { - - private static final Charset UTF_8 = Charset.forName("UTF-8"); - - private final String username; - - private final String password; - - public BasicAuthorizationInterceptor(String username, String password) { - Assert.hasLength(username, "Username must not be empty"); - this.username = username; - this.password = (password == null ? "" : password); - } - - @Override - public ClientHttpResponse intercept(HttpRequest request, byte[] body, - ClientHttpRequestExecution execution) throws IOException { - String token = Base64Utils - .encodeToString((this.username + ":" + this.password).getBytes(UTF_8)); - request.getHeaders().add("Authorization", "Basic " + token); - return execution.execute(request, body); - } - -} diff --git a/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java b/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java index e2cf15540a..a4ec5043ec 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Set; import org.springframework.beans.BeanUtils; +import org.springframework.http.client.BasicAuthorizationInterceptor; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.converter.HttpMessageConverter; diff --git a/spring-boot/src/test/java/org/springframework/boot/web/client/BasicAuthorizationInterceptorTests.java b/spring-boot/src/test/java/org/springframework/boot/web/client/BasicAuthorizationInterceptorTests.java deleted file mode 100644 index c9536aebc0..0000000000 --- a/spring-boot/src/test/java/org/springframework/boot/web/client/BasicAuthorizationInterceptorTests.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2012-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.boot.web.client; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import org.springframework.http.client.ClientHttpRequestExecution; -import org.springframework.mock.http.client.MockClientHttpRequest; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -/** - * Tests for {@link BasicAuthorizationInterceptor}. - * - * @author Phillip Webb - */ -public class BasicAuthorizationInterceptorTests { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void createWhenUsernameIsNullShouldThrowException() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Username must not be empty"); - new BasicAuthorizationInterceptor(null, "password"); - } - - @Test - public void createWhenUsernameIsEmptyShouldThrowException() throws Exception { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Username must not be empty"); - new BasicAuthorizationInterceptor("", "password"); - } - - @Test - public void createWhenPasswordIsNullShouldUseEmptyPassword() throws Exception { - BasicAuthorizationInterceptor interceptor = new BasicAuthorizationInterceptor( - "username", null); - assertThat(interceptor).extracting("password").containsExactly(""); - } - - @Test - public void interceptShouldAddHeader() throws Exception { - MockClientHttpRequest request = new MockClientHttpRequest(); - ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); - byte[] body = new byte[] {}; - new BasicAuthorizationInterceptor("spring", "boot").intercept(request, body, - execution); - verify(execution).execute(request, body); - assertThat(request.getHeaders().getFirst("Authorization")) - .isEqualTo("Basic c3ByaW5nOmJvb3Q="); - } - -} diff --git a/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java b/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java index b7992d40b4..53ed3bbfbe 100644 --- a/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java @@ -26,6 +26,7 @@ import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.springframework.http.client.BasicAuthorizationInterceptor; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;