Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish migrating java util base64 to apache commons #1376

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version seems to have issues hence I updated it.

<version>5.4</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/adyen/httpclient/AdyenHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.adyen.Config;
import com.adyen.constants.ApiConstants;
import com.adyen.model.RequestOptions;
import java.util.Base64;
import org.apache.commons.codec.binary.Base64;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPatch;
Expand Down Expand Up @@ -244,7 +244,7 @@ private void setApiKey(HttpUriRequest httpUriRequest, String apiKey) {
private void setBasicAuthentication(HttpUriRequest httpUriRequest, String username, String password) {
// set basic authentication
String authString = username + ":" + password;
byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);

httpUriRequest.addHeader("Authorization", "Basic " + authStringEnc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.util.Base64;
import org.apache.commons.codec.binary.Base64;
import java.lang.reflect.Type;

public class ByteArrayToBase64TypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return Base64.getDecoder().decode(json.getAsString().getBytes());
return Base64.decodeBase64(json.getAsString().getBytes());
}

public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(new String(Base64.getEncoder().encode(src)));
return new JsonPrimitive(new String(Base64.encodeBase64(src)));
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/adyen/util/HMACValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import com.adyen.model.notification.Amount;
import com.adyen.model.notification.NotificationRequestItem;
import java.util.Base64;
import org.apache.commons.codec.binary.Base64;
import javax.xml.bind.DatatypeConverter;

import javax.crypto.Mac;
Expand Down Expand Up @@ -60,7 +60,7 @@ public String calculateHMAC(String data, String key) throws IllegalArgumentExcep
byte[] rawHmac = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));

// Base64-encode the hmac
return new String(Base64.getEncoder().encode(rawHmac));
return new String(Base64.encodeBase64(rawHmac));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Missing data or key.");
} catch (Exception e) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/adyen/LegalEntityManagementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.junit.Test;

import java.io.IOException;
import java.util.Base64;
import org.apache.commons.codec.binary.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -320,7 +320,7 @@ public void TestBase64EncodedResponseToByteArray() throws Exception {
" \"type\": \"bankStatement\"\n" +
"}");
Document response = service.updateDocument("SE322KT223222D5FJ7TJN2986", request);
assertEquals("Thisisanbase64encodedstring", new String(Base64.getDecoder().decode(response.getAttachments().get(0).getContent())));
assertEquals("Thisisanbase64encodedstring", new String(Base64.decodeBase64(response.getAttachments().get(0).getContent())));
}

@Test
Expand Down
7 changes: 2 additions & 5 deletions src/test/java/com/adyen/PaymentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,18 @@
import com.adyen.service.PaymentApi;
import com.adyen.service.exception.ApiException;
import com.adyen.util.DateUtil;
import com.fasterxml.jackson.databind.JavaType;
import com.google.gson.reflect.TypeToken;
import okio.ByteString;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.Base64;
import org.apache.commons.codec.binary.Base64;

import static com.adyen.constants.ApiConstants.AdditionalData.*;
import static com.adyen.constants.ApiConstants.SelectedBrand.BOLETO_SANTANDER;
Expand Down Expand Up @@ -454,7 +451,7 @@ public void TestByteArrayDeserialization() throws Exception {
byte[] actualDeserializedBytes = JSON.getMapper().readValue(serializedBytesWithQuotes, byte[].class);

// Assert
assertEquals(expectedBytesAsString, new String(Base64.getDecoder().decode(actualDeserializedBytes)));
assertEquals(expectedBytesAsString, new String(Base64.decodeBase64(actualDeserializedBytes)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import java.util.Base64;import org.junit.Test;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

Expand All @@ -35,7 +36,7 @@ public class ByteArrayToStringAdapterTest {
@Test
public void testSerialize() {
ByteArrayToStringAdapter adapter = new ByteArrayToStringAdapter();
byte[] base64Bytes = Base64.getEncoder().encode("Bytes-To-Be-Encoded".getBytes());
byte[] base64Bytes = Base64.encodeBase64("Bytes-To-Be-Encoded".getBytes());

JsonElement serializedElement = adapter.serialize(base64Bytes, null, null);
assertEquals("Qnl0ZXMtVG8tQmUtRW5jb2RlZA==", serializedElement.getAsString());
Expand All @@ -44,7 +45,7 @@ public void testSerialize() {
@Test
public void testDeserialize() {
ByteArrayToStringAdapter adapter = new ByteArrayToStringAdapter();
byte[] base64Bytes = Base64.getEncoder().encode("Bytes-To-Be-Encoded".getBytes());
byte[] base64Bytes = Base64.encodeBase64("Bytes-To-Be-Encoded".getBytes());

JsonElement element = new JsonPrimitive("Qnl0ZXMtVG8tQmUtRW5jb2RlZA==");
byte[] deserializedBytes = adapter.deserialize(element, null, null);
Expand Down