Skip to content

Commit 9516edd

Browse files
committed
Isolate DatatypeConverter Usage
Closes gh-590
1 parent 5efce56 commit 9516edd

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

core/src/main/java/org/springframework/ldap/support/LdapEncoder.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,11 @@
1616

1717
package org.springframework.ldap.support;
1818

19+
import java.util.Base64;
20+
1921
import org.springframework.ldap.BadLdapGrammarException;
2022
import org.springframework.util.Assert;
23+
import org.springframework.util.ClassUtils;
2124

2225
import javax.xml.bind.DatatypeConverter;
2326

@@ -248,7 +251,7 @@ public static String printBase64Binary(byte[] val) {
248251

249252
Assert.notNull(val, "val must not be null!");
250253

251-
String encoded = DatatypeConverter.printBase64Binary(val);
254+
String encoded = encode(val);
252255

253256
int length = encoded.length();
254257
StringBuilder sb = new StringBuilder(length + length / RFC2849_MAX_BASE64_CHARS_PER_LINE);
@@ -293,6 +296,32 @@ public static byte[] parseBase64Binary(String val) {
293296
sb.append(c);
294297
}
295298

296-
return DatatypeConverter.parseBase64Binary(sb.toString());
299+
return decode(sb.toString());
300+
}
301+
302+
private static String encode(byte[] decoded) {
303+
if (ClassUtils.isPresent("java.util.Base64", null)) {
304+
return java.util.Base64.getEncoder().encodeToString(decoded);
305+
} else {
306+
return Base64Converter.encode(decoded);
307+
}
308+
}
309+
310+
private static byte[] decode(String encoded) {
311+
if (ClassUtils.isPresent("java.util.Base64", null)) {
312+
return java.util.Base64.getDecoder().decode(encoded);
313+
} else {
314+
return Base64Converter.decode(encoded);
315+
}
316+
}
317+
318+
private static class Base64Converter {
319+
static byte[] decode(String string) {
320+
return DatatypeConverter.parseBase64Binary(string);
321+
}
322+
323+
static String encode(byte[] bytes) {
324+
return DatatypeConverter.printBase64Binary(bytes);
325+
}
297326
}
298327
}

ldif/ldif-core/src/main/java/org/springframework/ldap/ldif/support/DefaultAttributeValidationPolicy.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2013 the original author or authors.
2+
* Copyright 2005-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,8 +23,6 @@
2323
import org.springframework.util.StringUtils;
2424

2525
import javax.naming.directory.Attribute;
26-
import javax.xml.bind.DatatypeConverter;
27-
import java.io.IOException;
2826
import java.net.URI;
2927
import java.net.URISyntaxException;
3028
import java.util.Arrays;

0 commit comments

Comments
 (0)