KEYCLOAK-19183
LDAPDn should use a static Pattern instead calling String.split with a regex
This commit is contained in:
parent
c7f8544b0c
commit
0c64d32b9b
1 changed files with 8 additions and 3 deletions
|
@ -22,12 +22,17 @@ import java.util.Collection;
|
||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||||
*/
|
*/
|
||||||
public class LDAPDn {
|
public class LDAPDn {
|
||||||
|
|
||||||
|
private static final Pattern DN_PATTERN = Pattern.compile("(?<!\\\\),");
|
||||||
|
private static final Pattern ENTRY_PATTERN = Pattern.compile("(?<!\\\\)\\+");
|
||||||
|
private static final Pattern SUB_ENTRY_PATTERN = Pattern.compile("(?<!\\\\)=");
|
||||||
|
|
||||||
private final Deque<RDN> entries;
|
private final Deque<RDN> entries;
|
||||||
|
|
||||||
|
@ -49,7 +54,7 @@ public class LDAPDn {
|
||||||
if(dnString.trim().isEmpty())
|
if(dnString.trim().isEmpty())
|
||||||
return dn;
|
return dn;
|
||||||
|
|
||||||
String[] rdns = dnString.split("(?<!\\\\),");
|
String[] rdns = DN_PATTERN.split(dnString);
|
||||||
for (String entryStr : rdns) {
|
for (String entryStr : rdns) {
|
||||||
if (entryStr.indexOf('+') == -1) {
|
if (entryStr.indexOf('+') == -1) {
|
||||||
// This is 99.9% of cases where RDN consists of single key-value pair
|
// This is 99.9% of cases where RDN consists of single key-value pair
|
||||||
|
@ -57,7 +62,7 @@ public class LDAPDn {
|
||||||
dn.addLast(new RDN(subEntry));
|
dn.addLast(new RDN(subEntry));
|
||||||
} else {
|
} else {
|
||||||
// This is 0.1% of cases where RDN consists of more key-value pairs like "uid=foo+cn=bar"
|
// This is 0.1% of cases where RDN consists of more key-value pairs like "uid=foo+cn=bar"
|
||||||
String[] subEntries = entryStr.split("(?<!\\\\)\\+");
|
String[] subEntries = ENTRY_PATTERN.split(entryStr);
|
||||||
RDN entry = new RDN();
|
RDN entry = new RDN();
|
||||||
for (String subEntryStr : subEntries) {
|
for (String subEntryStr : subEntries) {
|
||||||
SubEntry subEntry = parseSingleSubEntry(dn, subEntryStr);
|
SubEntry subEntry = parseSingleSubEntry(dn, subEntryStr);
|
||||||
|
@ -72,7 +77,7 @@ public class LDAPDn {
|
||||||
|
|
||||||
// parse single sub-entry and add it to the "dn" . Assumption is that subentry is something like "uid=bar" and does not contain + character
|
// parse single sub-entry and add it to the "dn" . Assumption is that subentry is something like "uid=bar" and does not contain + character
|
||||||
private static SubEntry parseSingleSubEntry(LDAPDn dn, String subEntryStr) {
|
private static SubEntry parseSingleSubEntry(LDAPDn dn, String subEntryStr) {
|
||||||
String[] rdn = subEntryStr.split("(?<!\\\\)=");
|
String[] rdn = SUB_ENTRY_PATTERN.split(subEntryStr);
|
||||||
if (rdn.length >1) {
|
if (rdn.length >1) {
|
||||||
return new SubEntry(rdn[0].trim(), rdn[1].trim());
|
return new SubEntry(rdn[0].trim(), rdn[1].trim());
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue