From 246452b6a8884cdc3655ad2fe9c62607fbb510ee Mon Sep 17 00:00:00 2001
From: Mattias Arthursson
+ * Note 1: If the definition is in XML you will need to properly encode any special characters so that they are valid in an XML file,
+ * e.g. "&" needs to be encoded as "&", e.g.
+ * (&(objectclass=person)(cn=Some CN))
diff --git a/mvn-build/core/src/main/java/org/springframework/ldap/filter/EqualsFilter.java b/mvn-build/core/src/main/java/org/springframework/ldap/filter/EqualsFilter.java
index e452be35..7a2453c4 100644
--- a/mvn-build/core/src/main/java/org/springframework/ldap/filter/EqualsFilter.java
+++ b/mvn-build/core/src/main/java/org/springframework/ldap/filter/EqualsFilter.java
@@ -21,7 +21,7 @@ package org.springframework.ldap.filter;
*
*
* EqualsFilter filter = new EqualsFilter("cn", "Some CN");
- * System.out.println(filter.ecode());
+ * System.out.println(filter.encode());
*
*
* would result in:
diff --git a/mvn-build/core/src/main/java/org/springframework/ldap/filter/FilterEditor.java b/mvn-build/core/src/main/java/org/springframework/ldap/filter/FilterEditor.java
new file mode 100644
index 00000000..6931b5bb
--- /dev/null
+++ b/mvn-build/core/src/main/java/org/springframework/ldap/filter/FilterEditor.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2005-2008 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.ldap.filter;
+
+import java.beans.PropertyEditorSupport;
+
+/**
+ * Property editor for {@link Filter} instances. Creates {@link HardcodedFilter}
+ * instances.
+ *
+ * @author Mathieu Larchet
+ */
+public class FilterEditor extends PropertyEditorSupport {
+ public void setAsText(String text) throws IllegalArgumentException {
+ setValue(new HardcodedFilter(text));
+ }
+}
diff --git a/mvn-build/core/src/main/java/org/springframework/ldap/filter/HardcodedFilter.java b/mvn-build/core/src/main/java/org/springframework/ldap/filter/HardcodedFilter.java
new file mode 100644
index 00000000..0cc37823
--- /dev/null
+++ b/mvn-build/core/src/main/java/org/springframework/ldap/filter/HardcodedFilter.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2005-2008 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.ldap.filter;
+
+import org.springframework.util.StringUtils;
+
+/**
+ * Allows hardcoded parts to be included in a search filter. Particularly useful
+ * if some filters are specified in configuration files and these should be
+ * combined with other ones.
+ *
+ *
+ * Filter filter = new HardcodedFilter("(&(objectClass=user)(!(objectClass=computer)))");
+ * System.out.println(filter.toString());
+ *
+ *
+ * would result in:
+ * (&(objectClass=user)(!(objectClass=computer)))
+ *
+ * <bean class="MyClass">
+ * <property name="filter" value="(&(objectClass=user)(!(objectClass=computer)))" />
+ * </bean>
+ *
+ *
+ * Note 2: There will be no validation to ensure that the supplied filter is + * valid. Using this implementation to build filters from user input is strongly + * discouraged. + *
+ * @author Justen Stepka + * @author Mathieu Larchet + */ +public class HardcodedFilter extends AbstractFilter { + + private String filter; + + /** + * The hardcoded string to be used for this filter. + * @param filter the hardcoded filter string. + */ + public HardcodedFilter(String filter) { + this.filter = filter; + } + + /* + * (non-Javadoc) + * @see org.springframework.ldap.filter.AbstractFilter#encode(java.lang.StringBuffer) + */ + public StringBuffer encode(StringBuffer buff) { + if (!StringUtils.hasLength(filter)) { + return buff; + } + + buff.append(filter); + return buff; + } +} diff --git a/mvn-build/core/src/main/java/org/springframework/ldap/filter/OrFilter.java b/mvn-build/core/src/main/java/org/springframework/ldap/filter/OrFilter.java index b0369a70..85d9ae6b 100644 --- a/mvn-build/core/src/main/java/org/springframework/ldap/filter/OrFilter.java +++ b/mvn-build/core/src/main/java/org/springframework/ldap/filter/OrFilter.java @@ -23,7 +23,7 @@ package org.springframework.ldap.filter; * AndFilter filter = new AndFilter(); * filter.or(new EqualsFilter("objectclass", "person"); * filter.or(new EqualsFilter("objectclass", "organizationalUnit"); - * System.out.println(filter.ecode()); + * System.out.println(filter.encode()); * * * would result in: diff --git a/mvn-build/integration-tests/src/main/java/org/springframework/ldap/filter/DummyFilterConsumer.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/filter/DummyFilterConsumer.java new file mode 100644 index 00000000..1b518751 --- /dev/null +++ b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/filter/DummyFilterConsumer.java @@ -0,0 +1,34 @@ +/* + * Copyright 2005-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ldap.filter; + +/** + * Dummy class to test Filter property editor. + * + * @author Mattias Hellborg Arthursson + */ +public class DummyFilterConsumer { + + private Filter filter; + + public Filter getFilter() { + return filter; + } + + public void setFilter(Filter filter) { + this.filter = filter; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/filter/HardcodedFilterIntegrationTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/filter/HardcodedFilterIntegrationTest.java new file mode 100644 index 00000000..3113bc45 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/filter/HardcodedFilterIntegrationTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2005-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ldap.filter; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +/** + * @author Mattias Hellborg Arthursson + */ +@ContextConfiguration(locations = { "/conf/hardcodedFilterTestContext.xml" }) +public class HardcodedFilterIntegrationTest extends AbstractJUnit4SpringContextTests { + + @Autowired + private DummyFilterConsumer dummyFilterConsumer; + + @Test + public void verifyThatFilterEditorWorks() { + Filter filter = dummyFilterConsumer.getFilter(); + assertTrue(filter instanceof HardcodedFilter); + assertEquals("(&(objectclass=person)(!(objectclass=computer))", filter.toString()); + } + +} diff --git a/mvn-build/integration-tests/src/test/resources/conf/hardcodedFilterTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/hardcodedFilterTestContext.xml new file mode 100644 index 00000000..1f02fed7 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/hardcodedFilterTestContext.xml @@ -0,0 +1,11 @@ + +