Skip to content

Commit 990c534

Browse files
authored
Remove commons-codec dependency (#563)
* Replace string utf8 helpers * Use guava for base64 encoding/decoding * Remove commons-codec dependencies * Fix urlsafe string encoding * Add tests for null edge cases for StringUtils
1 parent 3d7f17c commit 990c534

File tree

6 files changed

+41
-112
lines changed

6 files changed

+41
-112
lines changed

google-http-client-assembly/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
<configuration>
8080
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
8181
<outputDirectory>${project.build.directory}/libs</outputDirectory>
82-
<excludeArtifactIds>android,opengl-api,xmlParserAPIs,commons-codec,json</excludeArtifactIds>
82+
<excludeArtifactIds>android,opengl-api,xmlParserAPIs,json</excludeArtifactIds>
8383
</configuration>
8484
</execution>
8585
<execution>
@@ -92,7 +92,7 @@
9292
<classifier>sources</classifier>
9393
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
9494
<outputDirectory>${project.build.directory}/libs-sources</outputDirectory>
95-
<excludeArtifactIds>android,opengl-api,xmlParserAPIs,commons-codec,json</excludeArtifactIds>
95+
<excludeArtifactIds>android,opengl-api,xmlParserAPIs,json</excludeArtifactIds>
9696
</configuration>
9797
</execution>
9898
</executions>

google-http-client/pom.xml

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<links>
2424
<link>http://download.oracle.com/javase/7/docs/api/</link>
2525
<link>https://google.github.io/guava/releases/${project.guava.version}/api/docs/</link>
26-
<link>https://commons.apache.org/proper/commons-codec/archives/${project.commons-codec.version}/apidocs/</link>
2726
</links>
2827
<doctitle>${project.name} ${project.version}</doctitle>
2928
<windowtitle>${project.artifactId} ${project.version}</windowtitle>
@@ -41,68 +40,6 @@
4140
</execution>
4241
</executions>
4342
</plugin>
44-
<!-- Use jarjar to include a private copy of Apache Commons Codec library
45-
to avoid a runtime dependency conflict with Android which includes
46-
an older version of that library, as well as Guava JDK5. -->
47-
<plugin>
48-
<groupId>org.sonatype.plugins</groupId>
49-
<artifactId>jarjar-maven-plugin</artifactId>
50-
<version>1.9</version>
51-
<executions>
52-
<execution>
53-
<phase>package</phase>
54-
<goals>
55-
<goal>jarjar</goal>
56-
</goals>
57-
<configuration>
58-
<includes>
59-
<include>commons-codec:commons-codec</include>
60-
<include>com.google.guava:guava</include>
61-
</includes>
62-
<rules>
63-
<rule>
64-
<pattern>org.apache.commons.codec.**</pattern>
65-
<result>com.google.api.client.repackaged.org.apache.commons.codec.@1</result>
66-
</rule>
67-
<rule>
68-
<pattern>com.google.common.**</pattern>
69-
<result>com.google.api.client.repackaged.com.google.common.@1</result>
70-
</rule>
71-
<keep>
72-
<pattern>com.google.api.client.**</pattern>
73-
</keep>
74-
</rules>
75-
</configuration>
76-
</execution>
77-
</executions>
78-
</plugin>
79-
<plugin>
80-
<artifactId>maven-antrun-plugin</artifactId>
81-
<version>1.8</version>
82-
<executions>
83-
<!-- Remove extraneous files from the jarjar'ed Apache Commons Codec
84-
Library. -->
85-
<execution>
86-
<id>scrub</id>
87-
<phase>package</phase>
88-
<goals>
89-
<goal>run</goal>
90-
</goals>
91-
<configuration>
92-
<target>
93-
<jar destfile="target/scrubbed.jar" filesetmanifest="merge">
94-
<zipfileset src="target/google-http-client-${project.version}.jar">
95-
<exclude name="**/org/apache/commons/codec/language/bm/*.txt"/>
96-
<exclude name="META-INF/*.txt"/>
97-
<exclude name="META-INF/maven/**"/>
98-
</zipfileset>
99-
</jar>
100-
<move file="target/scrubbed.jar" tofile="target/google-http-client-${project.version}.jar"/>
101-
</target>
102-
</configuration>
103-
</execution>
104-
</executions>
105-
</plugin>
10643
<plugin>
10744
<artifactId>maven-jar-plugin</artifactId>
10845
<configuration>
@@ -165,10 +102,6 @@
165102
<artifactId>mockito-all</artifactId>
166103
<scope>test</scope>
167104
</dependency>
168-
<dependency>
169-
<groupId>commons-codec</groupId>
170-
<artifactId>commons-codec</artifactId>
171-
</dependency>
172105
<dependency>
173106
<groupId>com.google.j2objc</groupId>
174107
<artifactId>j2objc-annotations</artifactId>

google-http-client/src/main/java/com/google/api/client/util/Base64.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@
1414

1515
package com.google.api.client.util;
1616

17+
import com.google.common.io.BaseEncoding;
18+
import com.google.common.io.BaseEncoding.DecodingException;
19+
1720
/**
18-
* Proxy for version 1.6 (or newer) of the Apache Commons Codec
19-
* {@link org.apache.commons.codec.binary.Base64} implementation.
20-
*
21-
* <p>
22-
* This is needed in order to support platforms like Android which already include an older version
23-
* of the Apache Commons Codec (Android includes version 1.3). To avoid a dependency library
24-
* conflict, this library includes a reduced private copy of version 1.6 (or newer) of the Apache
25-
* Commons Codec (using a tool like jarjar).
26-
* </p>
21+
* Proxy for handling Base64 encoding/decoding.
2722
*
2823
* @since 1.8
2924
* @author Yaniv Inbar
@@ -36,21 +31,19 @@ public class Base64 {
3631
* @param binaryData binary data to encode or {@code null} for {@code null} result
3732
* @return byte[] containing Base64 characters in their UTF-8 representation or {@code null} for
3833
* {@code null} input
39-
* @see org.apache.commons.codec.binary.Base64#encodeBase64(byte[])
4034
*/
4135
public static byte[] encodeBase64(byte[] binaryData) {
42-
return org.apache.commons.codec.binary.Base64.encodeBase64(binaryData);
36+
return StringUtils.getBytesUtf8(encodeBase64String(binaryData));
4337
}
4438

4539
/**
4640
* Encodes binary data using the base64 algorithm but does not chunk the output.
4741
*
4842
* @param binaryData binary data to encode or {@code null} for {@code null} result
4943
* @return String containing Base64 characters or {@code null} for {@code null} input
50-
* @see org.apache.commons.codec.binary.Base64#encodeBase64String(byte[])
5144
*/
5245
public static String encodeBase64String(byte[] binaryData) {
53-
return org.apache.commons.codec.binary.Base64.encodeBase64String(binaryData);
46+
return BaseEncoding.base64().encode(binaryData);
5447
}
5548

5649

@@ -61,10 +54,9 @@ public static String encodeBase64String(byte[] binaryData) {
6154
* @param binaryData binary data to encode or {@code null} for {@code null} result
6255
* @return byte[] containing Base64 characters in their UTF-8 representation or {@code null} for
6356
* {@code null} input
64-
* @see org.apache.commons.codec.binary.Base64#encodeBase64URLSafe(byte[])
6557
*/
6658
public static byte[] encodeBase64URLSafe(byte[] binaryData) {
67-
return org.apache.commons.codec.binary.Base64.encodeBase64URLSafe(binaryData);
59+
return StringUtils.getBytesUtf8(encodeBase64URLSafeString(binaryData));
6860
}
6961

7062
/**
@@ -73,32 +65,38 @@ public static byte[] encodeBase64URLSafe(byte[] binaryData) {
7365
*
7466
* @param binaryData binary data to encode or {@code null} for {@code null} result
7567
* @return String containing Base64 characters or {@code null} for {@code null} input
76-
* @see org.apache.commons.codec.binary.Base64#encodeBase64URLSafeString(byte[])
7768
*/
7869
public static String encodeBase64URLSafeString(byte[] binaryData) {
79-
return org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(binaryData);
70+
return BaseEncoding.base64Url().omitPadding().encode(binaryData);
8071
}
8172

8273
/**
83-
* Decodes Base64 data into octets.
74+
* Decodes Base64 data into octets. Note that this method handles both URL-safe and
75+
* non-URL-safe base 64 encoded inputs.
8476
*
8577
* @param base64Data Byte array containing Base64 data or {@code null} for {@code null} result
8678
* @return Array containing decoded data or {@code null} for {@code null} input
87-
* @see org.apache.commons.codec.binary.Base64#decodeBase64(byte[])
8879
*/
8980
public static byte[] decodeBase64(byte[] base64Data) {
90-
return org.apache.commons.codec.binary.Base64.decodeBase64(base64Data);
81+
return decodeBase64(StringUtils.newStringUtf8(base64Data));
9182
}
9283

9384
/**
94-
* Decodes a Base64 String into octets.
85+
* Decodes a Base64 String into octets. Note that this method handles both URL-safe and
86+
* non-URL-safe base 64 encoded strings.
9587
*
9688
* @param base64String String containing Base64 data or {@code null} for {@code null} result
9789
* @return Array containing decoded data or {@code null} for {@code null} input
98-
* @see org.apache.commons.codec.binary.Base64#decodeBase64(String)
9990
*/
10091
public static byte[] decodeBase64(String base64String) {
101-
return org.apache.commons.codec.binary.Base64.decodeBase64(base64String);
92+
try {
93+
return BaseEncoding.base64().decode(base64String);
94+
} catch (IllegalArgumentException e) {
95+
if (e.getCause() instanceof DecodingException) {
96+
return BaseEncoding.base64Url().decode(base64String);
97+
}
98+
throw e;
99+
}
102100
}
103101

104102
private Base64() {

google-http-client/src/main/java/com/google/api/client/util/StringUtils.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@
1515
package com.google.api.client.util;
1616

1717
import java.io.UnsupportedEncodingException;
18+
import java.nio.charset.StandardCharsets;
1819

1920

2021
/**
2122
* Utilities for strings.
2223
*
23-
* <p>
24-
* Some of these methods are a proxy for version 1.6 (or newer) of the Apache Commons Codec
25-
* {@link StringUtils} implementation. This is needed in order to support platforms like Android
26-
* which already include an older version of the Apache Commons Codec (Android includes version
27-
* 1.3). To avoid a dependency library conflict, this library includes a reduced private copy of
28-
* version 1.6 (or newer) of the Apache Commons Codec (using a tool like jarjar).
29-
* </p>
30-
*
3124
* @since 1.8
3225
* @author Yaniv Inbar
3326
*/
@@ -48,13 +41,15 @@ public class StringUtils {
4841
* @return encoded bytes, or <code>null</code> if the input string was <code>null</code>
4942
* @throws IllegalStateException Thrown when the charset is missing, which should be never
5043
* according the the Java specification.
51-
* @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html"
44+
* @see <a href="http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html"
5245
* >Standard charsets</a>
53-
* @see org.apache.commons.codec.binary.StringUtils#getBytesUtf8(String)
5446
* @since 1.8
5547
*/
5648
public static byte[] getBytesUtf8(String string) {
57-
return org.apache.commons.codec.binary.StringUtils.getBytesUtf8(string);
49+
if (string == null) {
50+
return null;
51+
}
52+
return string.getBytes(StandardCharsets.UTF_8);
5853
}
5954

6055
/**
@@ -66,11 +61,13 @@ public static byte[] getBytesUtf8(String string) {
6661
* charset, or <code>null</code> if the input byte array was <code>null</code>.
6762
* @throws IllegalStateException Thrown when a {@link UnsupportedEncodingException} is caught,
6863
* which should never happen since the charset is required.
69-
* @see org.apache.commons.codec.binary.StringUtils#newStringUtf8(byte[])
7064
* @since 1.8
7165
*/
7266
public static String newStringUtf8(byte[] bytes) {
73-
return org.apache.commons.codec.binary.StringUtils.newStringUtf8(bytes);
67+
if (bytes == null) {
68+
return null;
69+
}
70+
return new String(bytes, StandardCharsets.UTF_8);
7471
}
7572

7673
private StringUtils() {

google-http-client/src/test/java/com/google/api/client/util/StringUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ public void testToBytesUtf8() {
4040
Assert.assertArrayEquals(SAMPLE_UTF8, StringUtils.getBytesUtf8(SAMPLE));
4141
}
4242

43+
public void testToBytesUtf8Null() {
44+
assertNull(StringUtils.getBytesUtf8(null));
45+
}
46+
4347
public void testFromBytesUtf8() {
4448
assertEquals(SAMPLE, StringUtils.newStringUtf8(SAMPLE_UTF8));
4549
}
50+
51+
public void testFromBytesUtf8Null() {
52+
assertNull(StringUtils.newStringUtf8(null));
53+
}
4654
}

pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@
150150
<artifactId>httpclient</artifactId>
151151
<version>${project.httpclient.version}</version>
152152
</dependency>
153-
<dependency>
154-
<groupId>commons-codec</groupId>
155-
<artifactId>commons-codec</artifactId>
156-
<version>${project.commons-codec.version}</version>
157-
</dependency>
158153
<dependency>
159154
<groupId>com.google.guava</groupId>
160155
<artifactId>guava</artifactId>
@@ -436,7 +431,6 @@
436431
<link>http://fasterxml.github.com/jackson-core/javadoc/${project.jackson-core2.version}/</link>
437432
<link>https://www.javadoc.io/doc/com.google.code.gson/gson/${project.gson.version}</link>
438433
<link>https://google.github.io/guava/releases/${project.guava.version}/api/docs/</link>
439-
<link>https://commons.apache.org/proper/commons-codec/archives/${project.commons-codec.version}/apidocs/</link>
440434
</links>
441435
<doctitle>Google HTTP Client Library for Java ${project.version}</doctitle>
442436
<excludePackageNames>com.google.api.client.findbugs:com.google.api.client.test.:com.google.api.services</excludePackageNames>
@@ -575,7 +569,6 @@
575569
<project.guava.version>26.0-android</project.guava.version>
576570
<project.xpp3.version>1.1.4c</project.xpp3.version>
577571
<project.commons-logging.version>1.1.1</project.commons-logging.version>
578-
<project.commons-codec.version>1.11</project.commons-codec.version>
579572
<project.httpclient.version>4.5.5</project.httpclient.version>
580573
<project.jdo2-api.version>2.3-eb</project.jdo2-api.version>
581574
<project.datanucleus-core.version>3.2.2</project.datanucleus-core.version>

0 commit comments

Comments
 (0)