Skip to content

Commit eafb9bd

Browse files
mdurawajansupol
authored andcommitted
Replaced internal Base64 by java.util.Base64 (#3803) (#3829)
* Replaced core-common.src.main.java.org.glassfish.jersey.internal.util.Base64 with java.util.Base64 * fixed conversion to String after decoding of Base64 encode message Signed-off-by: Michael Durawa <michael.durawa@boehringer-ingelheim.com>
1 parent 4125a33 commit eafb9bd

File tree

10 files changed

+39
-224
lines changed

10 files changed

+39
-224
lines changed

connectors/jdk-connector/src/main/java/org/glassfish/jersey/jdk/connector/internal/ProxyBasicAuthenticator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package org.glassfish.jersey.jdk.connector.internal;
1818

1919
import java.nio.charset.Charset;
20+
import java.util.Base64;
2021

21-
import org.glassfish.jersey.internal.util.Base64;
2222

2323
/**
2424
* @author Ondrej Kosatka (ondrej.kosatka at oracle.com)
@@ -46,6 +46,6 @@ static String generateAuthorizationHeader(String userName, String password) thro
4646
System.arraycopy(prefix, 0, usernamePassword, 0, prefix.length);
4747
System.arraycopy(passwordBytes, 0, usernamePassword, prefix.length, passwordBytes.length);
4848

49-
return "Basic " + Base64.encodeAsString(usernamePassword);
49+
return "Basic " + Base64.getEncoder().encodeToString(usernamePassword);
5050
}
5151
}

connectors/jdk-connector/src/test/java/org/glassfish/jersey/jdk/connector/internal/ProxyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.OutputStream;
2121
import java.nio.charset.Charset;
22+
import java.util.Base64;
2223
import java.util.regex.Matcher;
2324
import java.util.regex.Pattern;
2425

@@ -31,7 +32,6 @@
3132

3233
import org.glassfish.jersey.client.ClientConfig;
3334
import org.glassfish.jersey.client.ClientProperties;
34-
import org.glassfish.jersey.internal.util.Base64;
3535
import org.glassfish.jersey.jdk.connector.JdkConnectorProperties;
3636
import org.glassfish.jersey.jdk.connector.JdkConnectorProvider;
3737
import org.glassfish.jersey.server.ResourceConfig;
@@ -227,7 +227,7 @@ private boolean verifyBasicAuthorizationHeader(Response response, String authori
227227
response.setStatus(400);
228228
return false;
229229
}
230-
String decoded = new String(Base64.decode(authorizationHeader.substring(6).getBytes()),
230+
String decoded = new String(Base64.getDecoder().decode(authorizationHeader.substring(6).getBytes()),
231231
CHARACTER_SET);
232232
final String[] split = decoded.split(":");
233233
final String username = split[0];

core-client/src/main/java/org/glassfish/jersey/client/authentication/BasicAuthenticator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
package org.glassfish.jersey.client.authentication;
1818

19+
import java.util.Base64;
20+
1921
import javax.ws.rs.client.ClientRequestContext;
2022
import javax.ws.rs.client.ClientResponseContext;
2123
import javax.ws.rs.core.HttpHeaders;
2224

2325
import org.glassfish.jersey.client.internal.LocalizationMessages;
24-
import org.glassfish.jersey.internal.util.Base64;
2526

2627
/**
2728
* Implementation of Basic Http Authentication method (RFC 2617).
@@ -61,7 +62,7 @@ private String calculateAuthentication(HttpAuthenticationFilter.Credentials cred
6162
System.arraycopy(prefix, 0, usernamePassword, 0, prefix.length);
6263
System.arraycopy(password, 0, usernamePassword, prefix.length, password.length);
6364

64-
return "Basic " + Base64.encodeAsString(usernamePassword);
65+
return "Basic " + Base64.getEncoder().encodeToString(usernamePassword);
6566
}
6667

6768
/**

core-client/src/test/java/org/glassfish/jersey/client/filter/HttpBasicAuthFilterTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.glassfish.jersey.client.filter;
1818

1919

20+
import java.util.Base64;
2021
import java.util.concurrent.Future;
2122

2223
import javax.ws.rs.client.Client;
@@ -34,7 +35,7 @@
3435
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
3536
import org.glassfish.jersey.client.spi.Connector;
3637
import org.glassfish.jersey.client.spi.ConnectorProvider;
37-
import org.glassfish.jersey.internal.util.Base64;
38+
3839

3940
import org.junit.Test;
4041
import static org.junit.Assert.assertEquals;
@@ -53,7 +54,8 @@ public void testGet() {
5354
Invocation.Builder invBuilder = client.target(UriBuilder.fromUri("/").build()).request();
5455
Response r = invBuilder.get();
5556

56-
assertEquals("Basic " + Base64.encodeAsString("Uzivatelske jmeno:Heslo"), r.getHeaderString(HttpHeaders.AUTHORIZATION));
57+
assertEquals("Basic " + Base64.getEncoder().encodeToString("Uzivatelske jmeno:Heslo".getBytes()),
58+
r.getHeaderString(HttpHeaders.AUTHORIZATION));
5759
}
5860

5961
@Test
@@ -63,7 +65,7 @@ public void testBlankUsernamePassword() {
6365
Invocation.Builder invBuilder = client.target(UriBuilder.fromUri("/").build()).request();
6466
Response r = invBuilder.get();
6567

66-
assertEquals("Basic " + Base64.encodeAsString(":"), r.getHeaderString(HttpHeaders.AUTHORIZATION));
68+
assertEquals("Basic " + Base64.getEncoder().encodeToString(":".getBytes()), r.getHeaderString(HttpHeaders.AUTHORIZATION));
6769
}
6870

6971
private static class TestConnector implements Connector, ConnectorProvider {

core-common/src/main/java/org/glassfish/jersey/internal/util/Base64.java

Lines changed: 0 additions & 189 deletions
This file was deleted.

core-common/src/test/java/org/glassfish/jersey/internal/util/Base64Test.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.glassfish.jersey.internal.util;
1818

1919
import java.util.Arrays;
20+
import java.util.Base64;
2021

2122
import org.junit.Test;
2223
import static org.junit.Assert.assertEquals;
@@ -48,57 +49,57 @@ public class Base64Test {
4849
@Test
4950
public void testEncodeString() throws Exception {
5051
for (int i = 0; i < decoded.length; i++) {
51-
assertEquals(encoded[i], new String(Base64.encode(decoded[i].getBytes("ASCII")), "ASCII"));
52+
assertEquals(encoded[i], new String(Base64.getEncoder().encode(decoded[i].getBytes("ASCII")), "ASCII"));
5253
}
5354
}
5455

5556
@Test
5657
public void testDecodeString() throws Exception {
5758
for (int i = 0; i < encoded.length; i++) {
58-
assertEquals(decoded[i], new String(Base64.decode(encoded[i].getBytes("ASCII")), "ASCII"));
59+
assertEquals(decoded[i], new String(Base64.getDecoder().decode(encoded[i].getBytes("ASCII")), "ASCII"));
5960
}
6061
}
6162

6263
@Test
6364
public void testRoundtripLengthMod3Equals0() {
6465
byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8};
65-
byte[] result = Base64.decode(Base64.encode(data));
66+
byte[] result = Base64.getDecoder().decode(Base64.getEncoder().encode(data));
6667
assertTrue("failed to roundtrip value to base64", Arrays.equals(data, result));
6768
}
6869

6970
@Test
7071
public void testRoundtripLengthMod3Equals1() {
7172
byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
72-
byte[] result = Base64.decode(Base64.encode(data));
73+
byte[] result = Base64.getDecoder().decode(Base64.getEncoder().encode(data));
7374
assertTrue("failed to roundtrip value to base64", Arrays.equals(data, result));
7475
}
7576

7677
@Test
7778
public void testRoundtripLengthMod3Equals2() {
7879
byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
79-
byte[] result = Base64.decode(Base64.encode(data));
80+
byte[] result = Base64.getDecoder().decode(Base64.getEncoder().encode(data));
8081
assertTrue("failed to roundtrip value to base64", Arrays.equals(data, result));
8182
}
8283

8384
@Test
8485
public void testRoundtripOneByteGreaterThan127() {
8586
byte[] data = {(byte) 128};
8687
try {
87-
byte[] result = Base64.decode(Base64.encode(data));
88-
fail();
89-
} catch (Exception e) {
88+
byte[] result = Base64.getDecoder().decode(Base64.getEncoder().encode(data));
9089
// ok
90+
} catch (Exception e) {
91+
fail();
9192
}
9293
}
9394

9495
@Test
9596
public void testRoundtripAssortedValues() {
9697
byte[] data = {0, 1, 63, 64, 65, (byte) 127, (byte) 128, (byte) 1299, (byte) 254, (byte) 255};
9798
try {
98-
Base64.decode(Base64.encode(data));
99-
fail();
100-
} catch (Exception e) {
99+
Base64.getDecoder().decode(Base64.getEncoder().encode(data));
101100
// ok
101+
} catch (Exception e) {
102+
fail();
102103
}
103104
}
104105

@@ -109,10 +110,10 @@ public void testEncodeByteArray() {
109110
data[i] = (byte) (255 - i);
110111
}
111112
try {
112-
new String(Base64.encode(data));
113-
fail();
114-
} catch (Exception e) {
113+
new String(Base64.getEncoder().encode(data));
115114
// ok
115+
} catch (Exception e) {
116+
fail();
116117
}
117118
}
118119

@@ -122,7 +123,7 @@ public void testDecodeString2() {
122123
+ "+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn"
123124
+ "+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6"
124125
+ "/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";
125-
byte[] result = Base64.decode(data.getBytes());
126+
byte[] result = Base64.getDecoder().decode(data.getBytes());
126127

127128
assertEquals("incorrect length", result.length, 256);
128129
for (int i = 0; i < 256; ++i) {

0 commit comments

Comments
 (0)