From e6d645cd7ef84cbe8014a61bde0b5242e3c09007 Mon Sep 17 00:00:00 2001 From: Cloud User Date: Mon, 27 Nov 2023 18:26:23 +0000 Subject: [PATCH] replaced apache commons base64 encoder/decoder with native java updated encoding/decoding to use java native implementation rather than apache commons --- .../webservice/query/util/OptionallyEncodedString.java | 9 ++++----- .../datawave/webservice/query/util/TypedValueTest.java | 10 +++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main/java/datawave/webservice/query/util/OptionallyEncodedString.java b/src/main/java/datawave/webservice/query/util/OptionallyEncodedString.java index 6f023c6..74b7d1a 100644 --- a/src/main/java/datawave/webservice/query/util/OptionallyEncodedString.java +++ b/src/main/java/datawave/webservice/query/util/OptionallyEncodedString.java @@ -4,6 +4,7 @@ import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +import java.util.Base64; import javax.xml.bind.annotation.XmlAccessOrder; import javax.xml.bind.annotation.XmlAccessType; @@ -12,8 +13,6 @@ import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlValue; -import org.apache.commons.codec.binary.Base64; - /** * A JAXB holder class for strings that could possibly contain invalid XML characters. If any invalid XML characters are found, the string will be base64 * encoded. @@ -74,7 +73,7 @@ public String getValue() { try { incoming = value.getBytes("UTF-8"); - byte[] decodedBytes = Base64.decodeBase64(incoming); + byte[] decodedBytes = Base64.getDecoder().decode(incoming); decoded = new String(decodedBytes, Charset.forName("UTF-8")); } catch (UnsupportedEncodingException e) { // Should never happen with UTF-8!!! (but if it does we will be @@ -91,7 +90,7 @@ public byte[] getValueAsBytes() { try { byte[] incoming = value.getBytes("UTF-8"); if (this.base64Encoded != null && this.base64Encoded.equals(Boolean.TRUE)) { - return Base64.decodeBase64(incoming); + return Base64.getDecoder().decode(incoming); } else { return incoming; } @@ -112,7 +111,7 @@ public void setValue(String value) { if (XMLUtil.isValidXML(value)) { this.value = value; } else { - this.value = new String(Base64.encodeBase64(value.getBytes(UTF_8)), UTF_8); + this.value = new String(Base64.getEncoder().encode(value.getBytes(UTF_8)), UTF_8); this.base64Encoded = true; } } diff --git a/src/test/java/datawave/webservice/query/util/TypedValueTest.java b/src/test/java/datawave/webservice/query/util/TypedValueTest.java index 54ec5f5..a0edc1f 100644 --- a/src/test/java/datawave/webservice/query/util/TypedValueTest.java +++ b/src/test/java/datawave/webservice/query/util/TypedValueTest.java @@ -10,13 +10,13 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.text.MessageFormat; +import java.util.Base64; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import javax.xml.bind.JAXBContext; -import org.apache.commons.codec.binary.Base64; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -61,7 +61,7 @@ public void testPlainString() throws Exception { public void testEncodedStringString() throws Exception { TypedValue value = new TypedValue("encoded\0String"); String actual = serialize(value); - assertEquals(expected64("xs:string", new String(Base64.encodeBase64("encoded\0String".getBytes()))), actual); + assertEquals(expected64("xs:string", new String(Base64.getEncoder().encode("encoded\0String".getBytes()))), actual); assertTrue(value.isBase64Encoded()); assertEquals("xs:string", value.getType()); } @@ -71,7 +71,7 @@ public void testEncodedNoOpTypeString() throws Exception { BaseType type = new NoOpType("encoded\0String"); TypedValue value = new TypedValue(type); String actual = serialize(value); - assertEquals(expected64("xs:string", new String(Base64.encodeBase64("encoded\0String".getBytes()))), actual); + assertEquals(expected64("xs:string", new String(Base64.getEncoder().encode("encoded\0String".getBytes()))), actual); assertTrue(value.isBase64Encoded()); assertEquals("xs:string", value.getType()); } @@ -81,7 +81,7 @@ public void testEncodedTypeString() throws Exception { BaseType type = new LcNoDiacriticsType("encoded\0String"); TypedValue value = new TypedValue(type); String actual = serialize(value); - assertEquals(expected64("xs:string", new String(Base64.encodeBase64("encoded\0String".getBytes()))), actual); + assertEquals(expected64("xs:string", new String(Base64.getEncoder().encode("encoded\0String".getBytes()))), actual); assertTrue(value.isBase64Encoded()); assertEquals("xs:string", value.getType()); } @@ -171,7 +171,7 @@ public void testByte() throws Exception { public void testByteArray() throws Exception { TypedValue value = new TypedValue(new byte[] {(byte) 1, (byte) 2, (byte) 3, (byte) 42}); String actual = serialize(value); - assertEquals(expected("xs:base64Binary", new String(Base64.encodeBase64((byte[]) value.getValue()))), actual); + assertEquals(expected("xs:base64Binary", new String(Base64.getEncoder().encode((byte[]) value.getValue()))), actual); assertFalse(value.isBase64Encoded()); assertEquals("xs:base64Binary", value.getType()); }