Skip to content

Fix for issue #492: Support UTF8 characters in attributes values for … #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public class DefaultAttributeValidationPolicy implements AttributeValidationPoli

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

//UTF8 Definitions
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

private static final String UTF8_STRING = "(" + UTF8_CHAR + "+)";

//URL Components
private static final String USER = "[" + UCHAR + "\\x3B\\x3F\\x26\\x3D]*"; //UCHAR|;|?|&|=

Expand Down Expand Up @@ -242,13 +247,17 @@ public class DefaultAttributeValidationPolicy implements AttributeValidationPoli

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

private static final String UTF8_ATTRIBUTE_EXPRESSION = "^" + ATTRIBUTE_DESCRIPTION + ATTRIBUTE_SEPARATOR + FILL + UTF8_STRING;

//Pattern Declarations
private static final Pattern ATTRIBUTE_PATTERN = Pattern.compile(ATTRIBUTE_EXPRESSION);

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

private static final Pattern URL_ATTRIBUTE_PATTERN = Pattern.compile(URL_ATTRIBUTE_EXPRESSION);


private static final Pattern UTF8_ATTRIBUTE_PATTERN = Pattern.compile(UTF8_ATTRIBUTE_EXPRESSION);

private boolean ordered = false;

/**
Expand Down Expand Up @@ -279,11 +288,12 @@ public void setOrdered(boolean ordered) {
/**
* Validates attribute contained in the buffer and returns an LdapAttribute.
* <p>
* Ensures attributes meets one of three prescribed patterns for valid attributes:
* Ensures attributes meets one of four prescribed patterns for valid attributes:
* <ol>
* <li>A standard attribute pattern of the form: ATTR_ID[;options]: VALUE</li>
* <li>A Base64 attribute pattern of the form: ATTR_ID[;options]:: BASE64_VALUE</li>
* <li>A url attribute pattern of the form: ATTR_ID[;options]:&lt; URL_VALUE</li>
* <li>A UTF8 attribute pattern of the form: ATTR_ID[;options]: UTF8_VALUE</li>
* </ol>
* <p>
* Upon success an LdapAttribute object is returned.
Expand Down Expand Up @@ -314,6 +324,12 @@ public Attribute parse(String buffer) {
return parseUrlAttribute(matcher);
}

matcher = UTF8_ATTRIBUTE_PATTERN.matcher(buffer);
if (matcher.matches()) {
//Is a UTF8 attribute...
return parseUtf8Attribute(matcher);
}

//default: no match.
throw new InvalidAttributeFormatException("Not a valid attribute: [" + buffer + "]");
}
Expand Down Expand Up @@ -363,5 +379,16 @@ private LdapAttribute parseUrlAttribute(Matcher matcher) {
throw new InvalidAttributeFormatException(e);
}
}


private LdapAttribute parseUtf8Attribute(Matcher matcher) {
String id = matcher.group(1);
String value = matcher.group(3);
List<String> options = Arrays.asList((!StringUtils.hasLength(matcher.group(2)) ? new String[] {} : matcher.group(2).replaceFirst(";","").split(OPTION_SEPARATOR)));

if (options.isEmpty()) {
return new LdapAttribute(id, value, ordered);
} else {
return new LdapAttribute(id, value, options, ordered);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class DefaultAttributeValidationPolicyTest {

private static DefaultAttributeValidationPolicy policy = new DefaultAttributeValidationPolicy();

private static enum AttributeType { STRING, BASE64, URL }
private static enum AttributeType { STRING, BASE64, URL, UTF8 }

private String line;
private String id;
Expand Down Expand Up @@ -105,7 +105,10 @@ public static Collection<Object[]> data() {
{ "url:< prospero://host.dom:1525//pros/name;key=value", "url", "", "prospero://host.dom:1525//pros/name;key=value", AttributeType.URL},
{ "url:< nntp://news.cs.hut.fi/alt.html/239157", "url", "", "nntp://news.cs.hut.fi/alt.html/239157", AttributeType.URL},
{ "url:< wais://vega.lib.ncsu.edu/alawon.src?nren", "url", "", "wais://vega.lib.ncsu.edu/alawon.src?nren", AttributeType.URL},
{ "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}
{ "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},

//UTF8
{ "company: Østfold Akershus", "company", "", "Østfold Akershus", AttributeType.STRING }

});
}
Expand Down Expand Up @@ -159,6 +162,11 @@ public void parseAttribute() {
assertThat(attribute.get() instanceof URI).as("Value is not a URL.").isTrue();
assertThat(attribute.get()).as("Values do not match: ").isEqualTo(url);
break;

case UTF8:
assertThat(attribute.get() instanceof String).as("Value is not a UTF8.").isTrue();
assertThat(attribute.get()).as("Values do not match: ").isEqualTo(value);
break;
}

log.info("Success!");
Expand Down