From 300387d995124e443f9bd4357e589ed13598dcfe Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Thu, 15 Oct 2015 11:54:51 +0900 Subject: [PATCH] Replace Base64Encoder with Base64Utils Replace the custom Base64Encoder class with Spring's Base64Utils which since 4.2 can encode bytes using Java 8's encoder, commons-digest or Java 6's JAX-B encoder. Closes gh-4189 --- .../devtools/livereload/Base64Encoder.java | 63 ------------------- .../boot/devtools/livereload/Connection.java | 3 +- .../livereload/Base64EncoderTests.java | 53 ---------------- .../boot/test/Base64Encoder.java | 63 ------------------- .../boot/test/TestRestTemplate.java | 7 ++- .../boot/test/Base64EncoderTests.java | 53 ---------------- 6 files changed, 8 insertions(+), 234 deletions(-) delete mode 100644 spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Base64Encoder.java delete mode 100644 spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/Base64EncoderTests.java delete mode 100644 spring-boot/src/main/java/org/springframework/boot/test/Base64Encoder.java delete mode 100644 spring-boot/src/test/java/org/springframework/boot/test/Base64EncoderTests.java diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Base64Encoder.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Base64Encoder.java deleted file mode 100644 index 65f37db04c..0000000000 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Base64Encoder.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2012-2015 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.devtools.livereload; - -import java.nio.charset.Charset; - -/** - * Simple Base64 Encoder. - * - * @author Phillip Webb - */ -final class Base64Encoder { - - private static final Charset UTF_8 = Charset.forName("UTF-8"); - - private static final String ALPHABET_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - + "abcdefghijklmnopqrstuvwxyz0123456789+/"; - - static final byte[] ALPHABET = ALPHABET_CHARS.getBytes(UTF_8); - - private static final byte EQUALS_SIGN = '='; - - private Base64Encoder() { - } - - public static String encode(String string) { - return encode(string.getBytes(UTF_8)); - } - - public static String encode(byte[] bytes) { - byte[] encoded = new byte[bytes.length / 3 * 4 + (bytes.length % 3 == 0 ? 0 : 4)]; - for (int i = 0; i < bytes.length; i += 3) { - encodeBlock(bytes, i, Math.min((bytes.length - i), 3), encoded, i / 3 * 4); - } - return new String(encoded, UTF_8); - } - - private static void encodeBlock(byte[] src, int srcPos, int blockLen, byte[] dest, - int destPos) { - int inBuff = (blockLen > 0 ? ((src[srcPos] << 24) >>> 8) : 0) - | (blockLen > 1 ? ((src[srcPos + 1] << 24) >>> 16) : 0) - | (blockLen > 2 ? ((src[srcPos + 2] << 24) >>> 24) : 0); - for (int i = 0; i < 4; i++) { - dest[destPos + i] = (i > blockLen ? EQUALS_SIGN - : ALPHABET[(inBuff >>> (6 * (3 - i))) & 0x3f]); - } - } - -} diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Connection.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Connection.java index e1aa3d16d5..41d13b0018 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Connection.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Connection.java @@ -28,6 +28,7 @@ import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.util.Base64Utils; /** * A {@link LiveReloadServer} connection. @@ -149,7 +150,7 @@ class Connection { String response = matcher.group(1).trim() + WEBSOCKET_GUID; MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); messageDigest.update(response.getBytes(), 0, response.length()); - return Base64Encoder.encode(messageDigest.digest()); + return Base64Utils.encodeToString(messageDigest.digest()); } /** diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/Base64EncoderTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/Base64EncoderTests.java deleted file mode 100644 index fd88c9329b..0000000000 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/Base64EncoderTests.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2012-2015 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.devtools.livereload; - -import org.junit.Test; - -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; - -/** - * Tests for {@link Base64Encoder}. - * - * @author Phillip Webb - */ -public class Base64EncoderTests { - - private static final String TEXT = "Man is distinguished, not only by his reason, " - + "but by this singular passion from other animals, which is a lust of the " - + "mind, that by a perseverance of delight in the continued and indefatigable " - + "generation of knowledge, exceeds the short vehemence of any carnal pleasure."; - - private static final String ENCODED = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5I" - + "GhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbm" - + "ltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmF" - + "uY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVy" - + "YXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55I" - + "GNhcm5hbCBwbGVhc3VyZS4="; - - @Test - public void encodeText() { - assertThat(Base64Encoder.encode(TEXT), equalTo(ENCODED)); - assertThat(Base64Encoder.encode("pleasure."), equalTo("cGxlYXN1cmUu")); - assertThat(Base64Encoder.encode("leasure."), equalTo("bGVhc3VyZS4=")); - assertThat(Base64Encoder.encode("easure."), equalTo("ZWFzdXJlLg==")); - assertThat(Base64Encoder.encode("asure."), equalTo("YXN1cmUu")); - assertThat(Base64Encoder.encode("sure."), equalTo("c3VyZS4=")); - } - -} diff --git a/spring-boot/src/main/java/org/springframework/boot/test/Base64Encoder.java b/spring-boot/src/main/java/org/springframework/boot/test/Base64Encoder.java deleted file mode 100644 index f9eb63dfb3..0000000000 --- a/spring-boot/src/main/java/org/springframework/boot/test/Base64Encoder.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2012-2015 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.test; - -import java.nio.charset.Charset; - -/** - * Simple Base64 Encoder. - * - * @author Phillip Webb - */ -final class Base64Encoder { - - private static final Charset UTF_8 = Charset.forName("UTF-8"); - - private static final String ALPHABET_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - + "abcdefghijklmnopqrstuvwxyz0123456789+/"; - - static final byte[] ALPHABET = ALPHABET_CHARS.getBytes(UTF_8); - - private static final byte EQUALS_SIGN = '='; - - private Base64Encoder() { - } - - public static String encode(String string) { - return encode(string.getBytes(UTF_8)); - } - - public static String encode(byte[] bytes) { - byte[] encoded = new byte[bytes.length / 3 * 4 + (bytes.length % 3 == 0 ? 0 : 4)]; - for (int i = 0; i < bytes.length; i += 3) { - encodeBlock(bytes, i, Math.min((bytes.length - i), 3), encoded, i / 3 * 4); - } - return new String(encoded, UTF_8); - } - - private static void encodeBlock(byte[] src, int srcPos, int blockLen, byte[] dest, - int destPos) { - int inBuff = (blockLen > 0 ? ((src[srcPos] << 24) >>> 8) : 0) - | (blockLen > 1 ? ((src[srcPos + 1] << 24) >>> 16) : 0) - | (blockLen > 2 ? ((src[srcPos + 2] << 24) >>> 24) : 0); - for (int i = 0; i < 4; i++) { - dest[destPos + i] = (i > blockLen ? EQUALS_SIGN - : ALPHABET[(inBuff >>> (6 * (3 - i))) & 0x3f]); - } - } - -} diff --git a/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java b/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java index fef8469621..22faefa9cd 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java @@ -18,6 +18,7 @@ package org.springframework.boot.test; import java.io.IOException; import java.net.URI; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -37,6 +38,7 @@ 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; @@ -52,6 +54,8 @@ import org.springframework.web.client.RestTemplate; */ public class TestRestTemplate extends RestTemplate { + private static final Charset UTF_8 = Charset.forName("UTF-8"); + /** * Create a new {@link TestRestTemplate} instance. * @param httpClientOptions client options to use if the Apache HTTP Client is used @@ -124,7 +128,8 @@ public class TestRestTemplate extends RestTemplate { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { - String token = Base64Encoder.encode(this.username + ":" + this.password); + 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/test/java/org/springframework/boot/test/Base64EncoderTests.java b/spring-boot/src/test/java/org/springframework/boot/test/Base64EncoderTests.java deleted file mode 100644 index a250066c43..0000000000 --- a/spring-boot/src/test/java/org/springframework/boot/test/Base64EncoderTests.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2012-2015 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.test; - -import org.junit.Test; - -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; - -/** - * Tests for {@link Base64Encoder}. - * - * @author Phillip Webb - */ -public class Base64EncoderTests { - - private static final String TEXT = "Man is distinguished, not only by his reason, " - + "but by this singular passion from other animals, which is a lust of the " - + "mind, that by a perseverance of delight in the continued and indefatigable " - + "generation of knowledge, exceeds the short vehemence of any carnal pleasure."; - - private static final String ENCODED = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5I" - + "GhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbm" - + "ltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmF" - + "uY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVy" - + "YXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55I" - + "GNhcm5hbCBwbGVhc3VyZS4="; - - @Test - public void encodeText() { - assertThat(Base64Encoder.encode(TEXT), equalTo(ENCODED)); - assertThat(Base64Encoder.encode("pleasure."), equalTo("cGxlYXN1cmUu")); - assertThat(Base64Encoder.encode("leasure."), equalTo("bGVhc3VyZS4=")); - assertThat(Base64Encoder.encode("easure."), equalTo("ZWFzdXJlLg==")); - assertThat(Base64Encoder.encode("asure."), equalTo("YXN1cmUu")); - assertThat(Base64Encoder.encode("sure."), equalTo("c3VyZS4=")); - } - -}