From 24dc8811ee201391056a0c5b42e840de4fb59441 Mon Sep 17 00:00:00 2001 From: Ulrik Sandberg Date: Thu, 3 Sep 2009 17:49:12 +0000 Subject: [PATCH] Applied Keiths patch 0.0.2-SNAPSHOT.20090827. --- .../ldap/core/LdapAttribute.java | 29 ++++++++++--------- .../ldap/core/LdapAttributes.java | 23 +++++++++------ .../ldap/ldif/parser/LdifParser.java | 17 ++++++----- .../ldap/ldif/parser/Parser.java | 7 +++++ .../DefaultAttributeValidationPolicy.java | 6 ++-- .../DefaultAttributeValidationPolicyTest.java | 15 ++++++++-- .../ldap/ldif/LdifParserTest.java | 22 +++++++++----- core-tiger/src/test/resources/test.ldif | 18 +++++++++--- 8 files changed, 91 insertions(+), 46 deletions(-) diff --git a/core-tiger/src/main/java/org/springframework/ldap/core/LdapAttribute.java b/core-tiger/src/main/java/org/springframework/ldap/core/LdapAttribute.java index dc00ad4f..d59a0eb9 100644 --- a/core-tiger/src/main/java/org/springframework/ldap/core/LdapAttribute.java +++ b/core-tiger/src/main/java/org/springframework/ldap/core/LdapAttribute.java @@ -76,7 +76,7 @@ public class LdapAttribute extends BasicAttribute { * Creates an attribute with the specified ID whose values may be ordered. * * @param id {@link java.lang.String} ID of the attribute. - * @param ordered {@link java.lang.boolean} indicating whether or not the attributes values are ordered. + * @param ordered boolean indicating whether or not the attributes values are ordered. */ public LdapAttribute(String id, boolean ordered) { super(id, ordered); @@ -87,7 +87,7 @@ public class LdapAttribute extends BasicAttribute { * * @param id {@link java.lang.String} ID of the attribute. * @param options {@link java.util.Collection} of {@link java.lang.String} attribute options. - * @param ordered {@link java.lang.boolean} indicating whether or not the attributes values are ordered. + * @param ordered boolean indicating whether or not the attributes values are ordered. */ public LdapAttribute(String id, Collection options, boolean ordered) { super(id, ordered); @@ -99,7 +99,7 @@ public class LdapAttribute extends BasicAttribute { * * @param id {@link java.lang.String} ID of the attribute. * @param value Attribute value. - * @param ordered {@link java.lang.boolean} indicating whether or not the attributes values are ordered. + * @param ordered boolean indicating whether or not the attributes values are ordered. */ public LdapAttribute(String id, Object value, boolean ordered) { super(id, value, ordered); @@ -111,7 +111,7 @@ public class LdapAttribute extends BasicAttribute { * @param id {@link java.lang.String} ID of the attribute. * @param value Attribute value. * @param options {@link java.util.Collection} of {@link java.lang.String} attribute options. - * @param ordered {@link java.lang.boolean} indicating whether or not the attributes values are ordered. + * @param ordered boolean indicating whether or not the attributes values are ordered. */ public LdapAttribute(String id, Object value, Collection options, boolean ordered) { super(id, value, ordered); @@ -140,7 +140,7 @@ public class LdapAttribute extends BasicAttribute { * Add an option. * * @param option {@link java.lang.String} option. - * @return {@link java.lang.boolean} indication successful addition of option. + * @return boolean indication successful addition of option. */ public boolean addOption(String option) { return this.options.add(option); @@ -150,7 +150,7 @@ public class LdapAttribute extends BasicAttribute { * Add all values in the collection to the options. * * @param options {@link java.util.Collection} of {@link java.lang.String} values. - * @return {@link java.lang.boolean} indication successful addition of options. + * @return boolean indication successful addition of options. */ public boolean addAllOptions(Collection options) { return this.options.addAll(options); @@ -167,7 +167,7 @@ public class LdapAttribute extends BasicAttribute { * Checks for existence of a particular option on the set. * * @param option {@link java.lang.String} option. - * @return {@link java.lang.boolean} indicating result. + * @return boolean indicating result. */ public boolean contains(String option) { return this.options.contains(option); @@ -177,7 +177,7 @@ public class LdapAttribute extends BasicAttribute { * Checks for existence of a series of options on the set. * * @param options {@link java.util.Collection} of {@link java.lang.String} options. - * @return {@link java.lang.boolean} indicating result. + * @return boolean indicating result. */ public boolean containsAll(Collection options) { return this.options.containsAll(options); @@ -186,7 +186,7 @@ public class LdapAttribute extends BasicAttribute { /** * Tests for the presence of options. * - * @return {@link java.lang.boolean} indicating result. + * @return boolean indicating result. */ public boolean hasOptions() { return !options.isEmpty(); @@ -196,7 +196,7 @@ public class LdapAttribute extends BasicAttribute { * Removes an option from the the set. * * @param option {@link java.lang.String} option. - * @return {@link java.lang.boolean} indicating successful removal of option. + * @return boolean indicating successful removal of option. */ public boolean removeOption(String option) { return this.options.remove(options); @@ -205,8 +205,8 @@ public class LdapAttribute extends BasicAttribute { /** * Removes all options listed in the supplied set. * - * @param options {@link java.util.Collection} of {@link java.util.String} options. - * @return {@link java.lang.boolean} indicating successful removal of options. + * @param options {@link java.util.Collection} of {@link java.lang.String} options. + * @return boolean indicating successful removal of options. */ public boolean removeAllOptions(Collection options) { return this.options.removeAll(options); @@ -215,10 +215,11 @@ public class LdapAttribute extends BasicAttribute { /** * Removes any options not on the set of supplied options. * - * @param options {@link java.util.Collection} of {@link java.util.String} options. - * @return {@link java.lang.boolean} indicating successful retention of options. + * @param options {@link java.util.Collection} of {@link java.lang.String} options. + * @return boolean indicating successful retention of options. */ public boolean retainAllOptions(Collection options) { return this.options.retainAll(options); } + } diff --git a/core-tiger/src/main/java/org/springframework/ldap/core/LdapAttributes.java b/core-tiger/src/main/java/org/springframework/ldap/core/LdapAttributes.java index c8b31be0..9f101c9b 100644 --- a/core-tiger/src/main/java/org/springframework/ldap/core/LdapAttributes.java +++ b/core-tiger/src/main/java/org/springframework/ldap/core/LdapAttributes.java @@ -22,6 +22,8 @@ import javax.naming.NamingException; import javax.naming.directory.Attribute; import javax.naming.directory.BasicAttributes; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.ldap.core.DistinguishedName; import sun.misc.BASE64Encoder; @@ -44,6 +46,8 @@ import sun.misc.BASE64Encoder; public class LdapAttributes extends BasicAttributes { private static final long serialVersionUID = 97903297123869138L; + + private static Log log = LogFactory.getLog(LdapAttributes.class); private static final String SAFE_CHAR = "[\\p{ASCII}&&[^\\x00\\x0A\\x0D]]"; //Any ASCII except NUL, LF, and CR @@ -74,7 +78,7 @@ public class LdapAttributes extends BasicAttributes { /** * Constructor for specifying whether or not the object is case sensitive. * - * @param ignoreCase {@link java.lang.boolean} indicator. + * @param ignoreCase boolean indicator. */ public LdapAttributes(boolean ignoreCase) { super(ignoreCase); @@ -84,7 +88,7 @@ public class LdapAttributes extends BasicAttributes { * Creates an LdapAttributes object with the specified DN and case sensitivity setting. * * @param dn The {@link org.springframework.ldap.core.DistinguishedName} to which this object is bound. - * @param ignoreCase {@link java.lang.boolean} indicator. + * @param ignoreCase boolean indicator. */ public LdapAttributes(DistinguishedName dn, boolean ignoreCase) { super(ignoreCase); @@ -118,7 +122,7 @@ public class LdapAttributes extends BasicAttributes { * * @param attrID {@link java.lang.String} ID of the attribute. * @param val Value of the attribute. - * @param ignoreCase {@link java.lang.boolean} indicator. + * @param ignoreCase boolean indicator. */ public LdapAttributes(String attrID, Object val, boolean ignoreCase) { put(new LdapAttribute(attrID, val, ignoreCase)); @@ -130,7 +134,7 @@ public class LdapAttributes extends BasicAttributes { * @param dn The {@link org.springframework.ldap.core.DistinguishedName} to which this object is bound. * @param attrID {@link java.lang.String} ID of the attribute. * @param val Value of the attribute. - * @param ignoreCase {@link java.lang.boolean} indicator. + * @param ignoreCase boolean indicator. */ public LdapAttributes(DistinguishedName dn, String attrID, Object val, boolean ignoreCase) { this.dn = dn; @@ -161,8 +165,9 @@ public class LdapAttributes extends BasicAttributes { * @return {@link java.lang.String} formated to RFC2849 LDIF specifications. */ public String toString() { + StringBuilder sb = new StringBuilder(); + try { - StringBuilder sb = new StringBuilder(); DistinguishedName dn = getDN(); @@ -196,11 +201,11 @@ public class LdapAttributes extends BasicAttributes { } } - return sb.toString(); - } catch (NamingException e) { - e.printStackTrace(); - return ""; + log.error("Error formating attributes for output.", e); + sb = new StringBuilder(); } + + return sb.toString(); } } diff --git a/core-tiger/src/main/java/org/springframework/ldap/ldif/parser/LdifParser.java b/core-tiger/src/main/java/org/springframework/ldap/ldif/parser/LdifParser.java index 381d5b26..2e1fd272 100644 --- a/core-tiger/src/main/java/org/springframework/ldap/ldif/parser/LdifParser.java +++ b/core-tiger/src/main/java/org/springframework/ldap/ldif/parser/LdifParser.java @@ -32,7 +32,6 @@ import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapAttributes; -import org.springframework.ldap.ldif.InvalidAttributeFormatException; import org.springframework.ldap.ldif.InvalidRecordFormatException; import org.springframework.ldap.ldif.support.AttributeValidationPolicy; import org.springframework.ldap.ldif.support.DefaultAttributeValidationPolicy; @@ -77,7 +76,7 @@ import org.springframework.util.Assert; * retrieved. Likewise, the {@link #reset() reset()} method will reset the resource. *

* Objects implementing the {@link javax.naming.directory.Attributes Attributes} interface are required to support a case sensitivity setting - * which controls whether or not the attribute IDs of the object are case sensitive. The {@link #setCaseInsensitive() caseInsensitive} + * which controls whether or not the attribute IDs of the object are case sensitive. The {@link #caseInsensitive caseInsensitive} * setting of the {@link LdifParser LdifParser} is passed to the constructor of any {@link javax.naming.directory.Attributes Attributes} created. The * default value for this setting is true so that case insensitive objects are created. * @@ -206,6 +205,10 @@ public class LdifParser implements Parser, InitializingBean { reader = new BufferedReader(new InputStreamReader(resource.getInputStream())); } + public boolean isReady() throws IOException { + return reader.ready(); + } + public void close() throws IOException { if (resource.isOpen()) reader.close(); @@ -223,7 +226,10 @@ public class LdifParser implements Parser, InitializingBean { public LdapAttributes getRecord() throws IOException { Assert.notNull(reader, "A reader must be obtained: parser not open."); - if (!reader.ready()) return null; + if (!reader.ready()) { + log.debug("Reader not ready!"); + return null; + } LdapAttributes record = new LdapAttributes(caseInsensitive); StringBuilder builder = new StringBuilder(); @@ -290,7 +296,7 @@ public class LdifParser implements Parser, InitializingBean { addAttributeToRecord(builder.toString(), record); if (specification.isSatisfiedBy(record)) { - log.trace("Returning record."); + log.debug("record parsed:\n" + record); return record; } else { @@ -345,9 +351,6 @@ public class LdifParser implements Parser, InitializingBean { log.error(e); } catch (NoSuchElementException e) { log.error(e); - } catch (InvalidAttributeFormatException e) { - log.error(e); - record = null; } } diff --git a/core-tiger/src/main/java/org/springframework/ldap/ldif/parser/Parser.java b/core-tiger/src/main/java/org/springframework/ldap/ldif/parser/Parser.java index ecef6af7..88b1b285 100644 --- a/core-tiger/src/main/java/org/springframework/ldap/ldif/parser/Parser.java +++ b/core-tiger/src/main/java/org/springframework/ldap/ldif/parser/Parser.java @@ -80,4 +80,11 @@ public interface Parser { */ public Attributes getRecord() throws IOException; + /** + * Indicates whether or not the parser is ready to to return results. + * + * @return boolean indicator + * @throws IOException if there is a problem with the underlying resource. + */ + public boolean isReady() throws IOException; } diff --git a/core-tiger/src/main/java/org/springframework/ldap/ldif/support/DefaultAttributeValidationPolicy.java b/core-tiger/src/main/java/org/springframework/ldap/ldif/support/DefaultAttributeValidationPolicy.java index 321255e6..0a7db512 100644 --- a/core-tiger/src/main/java/org/springframework/ldap/ldif/support/DefaultAttributeValidationPolicy.java +++ b/core-tiger/src/main/java/org/springframework/ldap/ldif/support/DefaultAttributeValidationPolicy.java @@ -262,7 +262,7 @@ public class DefaultAttributeValidationPolicy implements AttributeValidationPoli /** * Constructor for indicating whether or not attribute values should be ordered alphabetically. * - * @param ordered {@link java.lang.boolean} value. + * @param ordered value. */ public DefaultAttributeValidationPolicy(boolean ordered) { this.ordered = ordered; @@ -271,7 +271,7 @@ public class DefaultAttributeValidationPolicy implements AttributeValidationPoli /** * Indicates whether or not the attribute values should be ordered alphabetically. * - * @param ordered {@link java.lang.boolean} value. + * @param ordered value. */ public void setOrdered(boolean ordered) { this.ordered = ordered; @@ -316,7 +316,7 @@ public class DefaultAttributeValidationPolicy implements AttributeValidationPoli } //default: no match. - throw new InvalidAttributeFormatException("Not a valid attribute: " + buffer); + throw new InvalidAttributeFormatException("Not a valid attribute: [" + buffer + "]"); } private LdapAttribute parseStringAttribute(Matcher matcher) { diff --git a/core-tiger/src/test/java/org/springframework/ldap/ldif/DefaultAttributeValidationPolicyTest.java b/core-tiger/src/test/java/org/springframework/ldap/ldif/DefaultAttributeValidationPolicyTest.java index fe1c1c28..44c564ae 100644 --- a/core-tiger/src/test/java/org/springframework/ldap/ldif/DefaultAttributeValidationPolicyTest.java +++ b/core-tiger/src/test/java/org/springframework/ldap/ldif/DefaultAttributeValidationPolicyTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.*; import java.net.URI; import java.util.Arrays; import java.util.Collection; +import java.util.List; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; @@ -41,6 +42,12 @@ public class DefaultAttributeValidationPolicyTest { private String value; private AttributeType type; + private List exceptions = Arrays.asList(new String[] { + "description: :A big sailing fan.", + "cn;lang-ja:: 5bCP56yg5Y6fIO.ODreODieODi+ODvA==", + "url:< http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28" + }); + /** * The data set to parse. * @return @@ -59,12 +66,14 @@ public class DefaultAttributeValidationPolicyTest { { "title;lang-en;phonetic: Sales, Director", "title", ";lang-en;phonetic", "Sales, Director", AttributeType.STRING}, { "mail: rogasawara@airius.co.jp", "mail", "", "rogasawara@airius.co.jp", AttributeType.STRING}, { "description: A big sailing fan.", "description", "", "A big sailing fan.", AttributeType.STRING}, + { "description: :A big sailing fan.", "description", "", ":A big sailing fan.", AttributeType.STRING}, //Base64 { "xml:: PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4=", "xml", "", "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4=", AttributeType.BASE64}, { "ou;lang-ja;phonetic:: 44GI44GE44GO44KH44GG44G2", "ou", ";lang-ja;phonetic", "44GI44GE44GO44KH44GG44G2", AttributeType.BASE64 }, { "dn:: dWlkPXJvZ2FzYXdhcmEsb3U95Za25qWt6YOoLG89QWlyaXVz", "dn", "", "dWlkPXJvZ2FzYXdhcmEsb3U95Za25qWt6YOoLG89QWlyaXVz", AttributeType.BASE64 }, { "cn;lang-ja:: 5bCP56yg5Y6fIOODreODieODi+ODvA==", "cn", ";lang-ja", "5bCP56yg5Y6fIOODreODieODi+ODvA==", AttributeType.BASE64 }, + { "cn;lang-ja:: 5bCP56yg5Y6fIO.ODreODieODi+ODvA==", "cn", ";lang-ja", "5bCP56yg5Y6fIO.ODreODieODi+ODvA==", AttributeType.BASE64 }, //Url { "url:< http://www.oracle.com/", "url", "", "http://www.oracle.com/", AttributeType.URL}, @@ -79,7 +88,8 @@ public class DefaultAttributeValidationPolicyTest { { "url:< news:comp.infosystems.www.servers.unix", "url", "", "news:comp.infosystems.www.servers.unix", AttributeType.URL}, { "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:< 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} }); } @@ -138,7 +148,8 @@ public class DefaultAttributeValidationPolicyTest { log.info("Success!"); } catch (Exception e) { - fail("Exception thrown: " + e.getClass().getSimpleName() + " (message: " + e.getMessage() + ")"); + if (!exceptions.contains(line)) + fail("Exception thrown: " + e.getClass().getSimpleName() + " (message: " + e.getMessage() + ")"); } } } diff --git a/core-tiger/src/test/java/org/springframework/ldap/ldif/LdifParserTest.java b/core-tiger/src/test/java/org/springframework/ldap/ldif/LdifParserTest.java index 2ad83eb7..283312b3 100644 --- a/core-tiger/src/test/java/org/springframework/ldap/ldif/LdifParserTest.java +++ b/core-tiger/src/test/java/org/springframework/ldap/ldif/LdifParserTest.java @@ -64,23 +64,31 @@ public class LdifParserTest { */ @Test public void parseLdif() { + int count = 0; + try { LdapAttributes attributes; - int count = 0; while (parser.hasMoreRecords()) { - attributes = parser.getRecord(); - log.info("attributes:\n" + attributes); - if (attributes != null) { - assertTrue("A dn is required.", attributes.getDN() != null); - assertTrue("Object class is required.", attributes.get("objectclass") != null); - count++; + try { + attributes = parser.getRecord(); + log.info("attributes:\n" + attributes); + if (attributes != null) { + assertTrue("A dn is required.", attributes.getDN() != null); + assertTrue("Object class is required.", attributes.get("objectclass") != null); + count++; + } + } catch (InvalidAttributeFormatException e) { + log.error(e); + if (count != 6) fail(); } log.debug("hasMoreRecords: " + parser.hasMoreRecords()); } log.info("record count: " + count); + //assertTrue("An incorrect number of records were parsed.", count == 8); + log.info("Done!"); } catch (IOException e) { diff --git a/core-tiger/src/test/resources/test.ldif b/core-tiger/src/test/resources/test.ldif index 3feb90d4..7ec2867d 100644 --- a/core-tiger/src/test/resources/test.ldif +++ b/core-tiger/src/test/resources/test.ldif @@ -128,7 +128,18 @@ title;lang-en: Sales, Director # -# Example 5: A file containing a reference to an external file +# Example 5: An LDIF file containing an invalid attribute +# + +dn: cn=Harry Jacobs, ou=Product Development, dc=airius, dc=com +objectClass: top +objectClass: person +cn: Harry Jacobs +description: :A big sailing fan. + + +# +# Example 6: A file containing a reference to an external file # dn: cn=Horatio Jensen, ou=Product Testing, dc=airius, dc=com @@ -136,7 +147,6 @@ objectclass: top objectclass: person objectclass: organizationalPerson cn: Horatio Jensen - cn: Horatio N Jensen sn: Jensen uid: hjensen @@ -145,7 +155,7 @@ jpegphoto:< file:///usr/local/directory/photos/hjensen.jpg # -# Example 6: A file containing a series of change records and comments +# Example 7: A file containing a series of change records and comments # # Add a new entry @@ -213,7 +223,7 @@ delete: description # -# Example 7: An LDIF file containing a change record with a control +# Example 8: An LDIF file containing a change record with a control # # Delete an entry. The operation will attach the LDAPv3