Fix for LDAP-28: added HardcodedFilter to enable hardcoded filters in configuration files. Also included FilterEditor, which creates instances of

HardcodedFilter for Filter properties.

Changed name of index.xml in docbook to spring-ldap-reference.xml.
This commit is contained in:
Mattias Arthursson
2008-06-19 11:57:33 +00:00
parent 1fc4824a66
commit 246452b6a8
10 changed files with 194 additions and 4 deletions

View File

@@ -23,7 +23,7 @@ package org.springframework.ldap.filter;
* AndFilter filter = new AndFilter();
* filter.and(new EqualsFilter("objectclass", "person");
* filter.and(new EqualsFilter("cn", "Some CN");
* System.out.println(filter.ecode());
* System.out.println(filter.encode());
* </pre>
*
* would result in: <code>(&amp;(objectclass=person)(cn=Some CN))</code>

View File

@@ -21,7 +21,7 @@ package org.springframework.ldap.filter;
*
* <pre>
* EqualsFilter filter = new EqualsFilter(&quot;cn&quot;, &quot;Some CN&quot;);
* System.out.println(filter.ecode());
* System.out.println(filter.encode());
* </pre>
*
* would result in:

View File

@@ -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));
}
}

View File

@@ -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.
*
* <pre>
* Filter filter = new HardcodedFilter(&quot;(&amp;(objectClass=user)(!(objectClass=computer)))&quot;);
* System.out.println(filter.toString());
* </pre>
*
* would result in:
* <code>(&amp;(objectClass=user)(!(objectClass=computer)))</code>
* <p>
* <b>Note 1</b>: 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. &quot;&amp;&quot; needs to be encoded as &quot;&amp;amp;&quot;, e.g.
* <pre>
* &lt;bean class="MyClass"&gt;
* &lt;property name="filter" value="(&amp;amp;(objectClass=user)(!(objectClass=computer)))" /&gt;
* &lt;/bean&gt;
* </pre>
* </p>
* <p>
* <b>Note 2</b>: 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.
* </p>
* @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;
}
}

View File

@@ -23,7 +23,7 @@ package org.springframework.ldap.filter;
* AndFilter filter = new AndFilter();
* filter.or(new EqualsFilter(&quot;objectclass&quot;, &quot;person&quot;);
* filter.or(new EqualsFilter(&quot;objectclass&quot;, &quot;organizationalUnit&quot;);
* System.out.println(filter.ecode());
* System.out.println(filter.encode());
* </pre>
*
* would result in:

View File

@@ -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;
}
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.ldap.filter.DummyFilterConsumer">
<property name="filter"
value="(&amp;(objectclass=person)(!(objectclass=computer))" />
</bean>
</beans>

View File

@@ -104,7 +104,7 @@
</dependency>
</dependencies>
<configuration>
<includes>index.xml</includes>
<includes>spring-ldap-reference.xml</includes>
<chunkedOutput>true</chunkedOutput>
<xincludeSupported>true</xincludeSupported>
<htmlStylesheet>css/html.css</htmlStylesheet>