Skip to content

Commit 177b4bb

Browse files
author
barna10
committed
Fix for issue #492: Support UTF8 characters in attributes values for LDIF's DefaultAttributeValidationPolicy in order to comply with RFC2849
1 parent dc4fe89 commit 177b4bb

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public class DefaultAttributeValidationPolicy implements AttributeValidationPoli
120120

121121
private static final String BASE64_STRING = "(" + BASE64_CHAR + "*)";
122122

123+
//UTF8 Definitions
124+
private static final String UTF8_CHAR = "[\\p{L}|[0-9]|\\s]"; // \p{L} for any kind of letter from any language, including spaces and digits
125+
126+
private static final String UTF8_STRING = "(" + UTF8_CHAR + "+)";
127+
123128
//URL Components
124129
private static final String USER = "[" + UCHAR + "\\x3B\\x3F\\x26\\x3D]*"; //UCHAR|;|?|&|=
125130

@@ -242,13 +247,17 @@ public class DefaultAttributeValidationPolicy implements AttributeValidationPoli
242247

243248
private static final String URL_ATTRIBUTE_EXPRESSION = "^" + ATTRIBUTE_DESCRIPTION + ATTRIBUTE_SEPARATOR + URL_INDICATOR + FILL + URL + "$"; //URL
244249

250+
private static final String UTF8_ATTRIBUTE_EXPRESSION = "^" + ATTRIBUTE_DESCRIPTION + ATTRIBUTE_SEPARATOR + FILL + UTF8_STRING;
251+
245252
//Pattern Declarations
246253
private static final Pattern ATTRIBUTE_PATTERN = Pattern.compile(ATTRIBUTE_EXPRESSION);
247254

248255
private static final Pattern BASE64_ATTRIBUTE_PATTERN = Pattern.compile(BASE64_ATTRIBUTE_EXPRESSION);
249256

250257
private static final Pattern URL_ATTRIBUTE_PATTERN = Pattern.compile(URL_ATTRIBUTE_EXPRESSION);
251-
258+
259+
private static final Pattern UTF8_ATTRIBUTE_PATTERN = Pattern.compile(UTF8_ATTRIBUTE_EXPRESSION);
260+
252261
private boolean ordered = false;
253262

254263
/**
@@ -279,11 +288,12 @@ public void setOrdered(boolean ordered) {
279288
/**
280289
* Validates attribute contained in the buffer and returns an LdapAttribute.
281290
* <p>
282-
* Ensures attributes meets one of three prescribed patterns for valid attributes:
291+
* Ensures attributes meets one of four prescribed patterns for valid attributes:
283292
* <ol>
284293
* <li>A standard attribute pattern of the form: ATTR_ID[;options]: VALUE</li>
285294
* <li>A Base64 attribute pattern of the form: ATTR_ID[;options]:: BASE64_VALUE</li>
286295
* <li>A url attribute pattern of the form: ATTR_ID[;options]:&lt; URL_VALUE</li>
296+
* <li>A UTF8 attribute pattern of the form: ATTR_ID[;options]: UTF8_VALUE</li>
287297
* </ol>
288298
* <p>
289299
* Upon success an LdapAttribute object is returned.
@@ -314,6 +324,12 @@ public Attribute parse(String buffer) {
314324
return parseUrlAttribute(matcher);
315325
}
316326

327+
matcher = UTF8_ATTRIBUTE_PATTERN.matcher(buffer);
328+
if (matcher.matches()) {
329+
//Is a UTF8 attribute...
330+
return parseUtf8Attribute(matcher);
331+
}
332+
317333
//default: no match.
318334
throw new InvalidAttributeFormatException("Not a valid attribute: [" + buffer + "]");
319335
}
@@ -363,5 +379,16 @@ private LdapAttribute parseUrlAttribute(Matcher matcher) {
363379
throw new InvalidAttributeFormatException(e);
364380
}
365381
}
366-
382+
383+
private LdapAttribute parseUtf8Attribute(Matcher matcher) {
384+
String id = matcher.group(1);
385+
String value = matcher.group(3);
386+
List<String> options = Arrays.asList((!StringUtils.hasLength(matcher.group(2)) ? new String[] {} : matcher.group(2).replaceFirst(";","").split(OPTION_SEPARATOR)));
387+
388+
if (options.isEmpty()) {
389+
return new LdapAttribute(id, value, ordered);
390+
} else {
391+
return new LdapAttribute(id, value, options, ordered);
392+
}
393+
}
367394
}

ldif/ldif-core/src/test/java/org/springframework/ldap/ldif/DefaultAttributeValidationPolicyTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class DefaultAttributeValidationPolicyTest {
5050

5151
private static DefaultAttributeValidationPolicy policy = new DefaultAttributeValidationPolicy();
5252

53-
private static enum AttributeType { STRING, BASE64, URL }
53+
private static enum AttributeType { STRING, BASE64, URL, UTF8 }
5454

5555
private String line;
5656
private String id;
@@ -105,7 +105,10 @@ public static Collection<Object[]> data() {
105105
{ "url:< prospero://host.dom:1525//pros/name;key=value", "url", "", "prospero://host.dom:1525//pros/name;key=value", AttributeType.URL},
106106
{ "url:< nntp://news.cs.hut.fi/alt.html/239157", "url", "", "nntp://news.cs.hut.fi/alt.html/239157", AttributeType.URL},
107107
{ "url:< wais://vega.lib.ncsu.edu/alawon.src?nren", "url", "", "wais://vega.lib.ncsu.edu/alawon.src?nren", AttributeType.URL},
108-
{ "url:< http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28", "url", "", "http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28", AttributeType.URL}
108+
{ "url:< http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28", "url", "", "http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28", AttributeType.URL},
109+
110+
//UTF8
111+
{ "company: Østfold Akershus", "company", "", "Østfold Akershus", AttributeType.STRING }
109112

110113
});
111114
}
@@ -159,6 +162,11 @@ public void parseAttribute() {
159162
assertThat(attribute.get() instanceof URI).as("Value is not a URL.").isTrue();
160163
assertThat(attribute.get()).as("Values do not match: ").isEqualTo(url);
161164
break;
165+
166+
case UTF8:
167+
assertThat(attribute.get() instanceof String).as("Value is not a UTF8.").isTrue();
168+
assertThat(attribute.get()).as("Values do not match: ").isEqualTo(value);
169+
break;
162170
}
163171

164172
log.info("Success!");

0 commit comments

Comments
 (0)