From 772bc030eea48de7a9be2998693c1f4ab686e249 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 29 Jun 2016 10:41:18 +0200 Subject: [PATCH] BasicAuthorizationInterceptor belongs to http.client.support Issue: SPR-14412 --- .../BasicAuthorizationInterceptor.java | 19 +++++++++++++++---- .../BasicAuthorizationInterceptorTests.java | 5 ++++- 2 files changed, 19 insertions(+), 5 deletions(-) rename spring-web/src/main/java/org/springframework/http/client/{ => support}/BasicAuthorizationInterceptor.java (72%) rename spring-web/src/test/java/org/springframework/http/client/{ => support}/BasicAuthorizationInterceptorTests.java (91%) diff --git a/spring-web/src/main/java/org/springframework/http/client/BasicAuthorizationInterceptor.java b/spring-web/src/main/java/org/springframework/http/client/support/BasicAuthorizationInterceptor.java similarity index 72% rename from spring-web/src/main/java/org/springframework/http/client/BasicAuthorizationInterceptor.java rename to spring-web/src/main/java/org/springframework/http/client/support/BasicAuthorizationInterceptor.java index 3d49b918e7..ebf4a5368a 100644 --- a/spring-web/src/main/java/org/springframework/http/client/BasicAuthorizationInterceptor.java +++ b/spring-web/src/main/java/org/springframework/http/client/support/BasicAuthorizationInterceptor.java @@ -14,12 +14,15 @@ * limitations under the License. */ -package org.springframework.http.client; +package org.springframework.http.client.support; 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; @@ -37,17 +40,25 @@ public class BasicAuthorizationInterceptor implements ClientHttpRequestIntercept private final String password; + + /** + * Create a new interceptor which adds a BASIC authorization header + * for the given username and password. + * @param username the username to use + * @param password the password to use + */ public BasicAuthorizationInterceptor(String username, String password) { Assert.hasLength(username, "Username must not be empty"); this.username = username; - this.password = (password == null ? "" : password); + 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)); + + 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-web/src/test/java/org/springframework/http/client/BasicAuthorizationInterceptorTests.java b/spring-web/src/test/java/org/springframework/http/client/support/BasicAuthorizationInterceptorTests.java similarity index 91% rename from spring-web/src/test/java/org/springframework/http/client/BasicAuthorizationInterceptorTests.java rename to spring-web/src/test/java/org/springframework/http/client/support/BasicAuthorizationInterceptorTests.java index ea1071c04b..ce96460dc0 100644 --- a/spring-web/src/test/java/org/springframework/http/client/BasicAuthorizationInterceptorTests.java +++ b/spring-web/src/test/java/org/springframework/http/client/support/BasicAuthorizationInterceptorTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.http.client; +package org.springframework.http.client.support; import java.net.URI; @@ -24,6 +24,9 @@ import org.junit.rules.ExpectedException; import org.springframework.beans.DirectFieldAccessor; import org.springframework.http.HttpMethod; +import org.springframework.http.client.ClientHttpRequest; +import org.springframework.http.client.ClientHttpRequestExecution; +import org.springframework.http.client.SimpleClientHttpRequestFactory; import static org.junit.Assert.*; import static org.mockito.Mockito.*;