org.springframework.ldap.filter.HardcodedFilter Maven / Gradle / Ivy
/*
* Copyright 2005-2013 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
*
* https://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 hard coded 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)))
*
* 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.
* <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;
}
public StringBuffer encode(StringBuffer buff) {
if (!StringUtils.hasLength(this.filter)) {
return buff;
}
buff.append(this.filter);
return buff;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
HardcodedFilter that = (HardcodedFilter) o;
if ((this.filter != null) ? !this.filter.equals(that.filter) : that.filter != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
return (this.filter != null) ? this.filter.hashCode() : 0;
}
}