Initial import.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
/**
|
||||
* Ldap specific extensions to CrudRepository.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface LdapRepository<T> extends CrudRepository<T, Name> {
|
||||
|
||||
/**
|
||||
* Find one entry matching the specified query.
|
||||
*
|
||||
* @param ldapQuery the query specification.
|
||||
* @return the found entry or <code>null</code> if no matching entry was found.
|
||||
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one entry matches the query.
|
||||
*/
|
||||
T findOne(LdapQuery ldapQuery);
|
||||
|
||||
/**
|
||||
* Find all entries matching the specified query.
|
||||
*
|
||||
* @param ldapQuery the query specification.
|
||||
* @return the entries matching the query.
|
||||
*/
|
||||
Iterable<T> findAll(LdapQuery ldapQuery);
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.ldap.query.SearchScope;
|
||||
|
||||
/**
|
||||
* Annotation for use in {@link org.springframework.data.ldap.repository.LdapRepository} declarations to create
|
||||
* automatic query methods based on statically defined queries.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Query {
|
||||
|
||||
/**
|
||||
* Search base, to be used as input to
|
||||
* {@link org.springframework.ldap.query.LdapQueryBuilder#base(javax.naming.Name)}.
|
||||
*
|
||||
* @return the search base, default is {@link org.springframework.ldap.support.LdapUtils#emptyLdapName()}
|
||||
*/
|
||||
String base() default "";
|
||||
|
||||
/**
|
||||
* The filter format string, to be used as input to
|
||||
* {@link org.springframework.ldap.query.LdapQueryBuilder#filter(String, Object...)}.
|
||||
*
|
||||
* @return search filter, must be specified.
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* Search scope, to be used as input to
|
||||
* {@link org.springframework.ldap.query.LdapQueryBuilder#searchScope(org.springframework.ldap.query.SearchScope)}.
|
||||
*
|
||||
* @return the search scope.
|
||||
*/
|
||||
SearchScope searchScope() default SearchScope.SUBTREE;
|
||||
|
||||
/**
|
||||
* Time limit, to be used as input to {@link org.springframework.ldap.query.LdapQueryBuilder#timeLimit(int)}.
|
||||
*
|
||||
* @return the time limit.
|
||||
*/
|
||||
int timeLimit() default 0;
|
||||
|
||||
/**
|
||||
* Count limit, to be used as input to {@link org.springframework.ldap.query.LdapQueryBuilder#countLimit(int)}.
|
||||
*
|
||||
* @return the count limit.
|
||||
*/
|
||||
int countLimit() default 0;
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.config;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.data.ldap.repository.support.LdapRepositoryFactoryBean;
|
||||
|
||||
/**
|
||||
* Annotation to activate Ldap repositories. If no base package is configured through either {@link #value()},
|
||||
* {@link #basePackages()} or {@link #basePackageClasses()} it will trigger scanning of the package of annotated class.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@Import(LdapRepositoriesRegistrar.class)
|
||||
public @interface EnableLdapRepositories {
|
||||
|
||||
/**
|
||||
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:
|
||||
* {@code @EnableLdapRepositories("org.my.pkg")} instead of {@code @EnableLdapRepositories(basePackages="org.my.pkg")}.
|
||||
*/
|
||||
String[] value() default {};
|
||||
|
||||
/**
|
||||
* Base packages to scan for annotated components. {@link #value()} is an alias for (and mutually exclusive with) this
|
||||
* attribute. Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names.
|
||||
*/
|
||||
String[] basePackages() default {};
|
||||
|
||||
/**
|
||||
* Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The
|
||||
* package of each class specified will be scanned. Consider creating a special no-op marker class or interface in
|
||||
* each package that serves no purpose other than being referenced by this attribute.
|
||||
*/
|
||||
Class<?>[] basePackageClasses() default {};
|
||||
|
||||
/**
|
||||
* Specifies which types are eligible for component scanning. Further narrows the set of candidate components from
|
||||
* everything in {@link #basePackages()} to everything in the base packages that matches the given filter or filters.
|
||||
*/
|
||||
Filter[] includeFilters() default {};
|
||||
|
||||
/**
|
||||
* Specifies which types are not eligible for component scanning.
|
||||
*/
|
||||
Filter[] excludeFilters() default {};
|
||||
|
||||
/**
|
||||
* Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So
|
||||
* for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning
|
||||
* for {@code PersonRepositoryImpl}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String repositoryImplementationPostfix() default "";
|
||||
|
||||
/**
|
||||
* Configures the location of where to find the Spring Data named queries properties file. Will default to
|
||||
* {@code META-INFO/mongo-named-queries.properties}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String namedQueriesLocation() default "";
|
||||
|
||||
/**
|
||||
* Returns the key of the {@link org.springframework.data.repository.query.QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
|
||||
* {@link org.springframework.data.repository.query.QueryLookupStrategy.Key#CREATE_IF_NOT_FOUND}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND;
|
||||
|
||||
/**
|
||||
* Returns the {@link org.springframework.beans.factory.FactoryBean} class to be used for each repository instance. Defaults to
|
||||
* {@link org.springframework.data.ldap.repository.support.LdapRepositoryFactoryBean}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Class<?> repositoryFactoryBeanClass() default LdapRepositoryFactoryBean.class;
|
||||
|
||||
/**
|
||||
* Configures the name of the {@link org.springframework.ldap.core.LdapTemplate} bean to be used with the repositories detected.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String ldapTemplateRef() default "ldapTemplate";
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.config;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
import org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
|
||||
|
||||
/**
|
||||
* LDAP-specific {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar}.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
class LdapRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
|
||||
|
||||
@Override
|
||||
protected Class<? extends Annotation> getAnnotation() {
|
||||
return EnableLdapRepositories.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RepositoryConfigurationExtension getExtension() {
|
||||
return new LdapRepositoryConfigurationExtension();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.config;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.data.ldap.repository.LdapRepository;
|
||||
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
|
||||
import org.springframework.data.repository.config.XmlRepositoryConfigurationSource;
|
||||
import org.springframework.ldap.odm.annotations.Entry;
|
||||
import org.springframework.data.ldap.repository.support.LdapRepositoryFactoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* {@link RepositoryConfigurationExtension} for LDAP.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Mark Paluch
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdapRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
|
||||
|
||||
private static final String ATT_LDAP_TEMPLATE_REF = "ldap-template-ref";
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModuleName()
|
||||
*/
|
||||
@Override
|
||||
public String getModuleName() {
|
||||
return "LDAP";
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModulePrefix()
|
||||
*/
|
||||
@Override
|
||||
protected String getModulePrefix() {
|
||||
return "ldap";
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getRepositoryFactoryClassName()
|
||||
*/
|
||||
public String getRepositoryFactoryClassName() {
|
||||
return LdapRepositoryFactoryBean.class.getName();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getIdentifyingAnnotations()
|
||||
*/
|
||||
@Override
|
||||
protected Collection<Class<? extends Annotation>> getIdentifyingAnnotations() {
|
||||
return Collections.<Class<? extends Annotation>> singleton(Entry.class);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getIdentifyingTypes()
|
||||
*/
|
||||
@Override
|
||||
protected Collection<Class<?>> getIdentifyingTypes() {
|
||||
return Collections.<Class<?>> singleton(LdapRepository.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource config) {
|
||||
|
||||
Element element = config.getElement();
|
||||
String ldapTemplateRef = element.getAttribute(ATT_LDAP_TEMPLATE_REF);
|
||||
if (!StringUtils.hasText(ldapTemplateRef)) {
|
||||
ldapTemplateRef = "ldapTemplate";
|
||||
}
|
||||
|
||||
builder.addPropertyReference("ldapOperations", ldapTemplateRef);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) {
|
||||
|
||||
AnnotationAttributes attributes = config.getAttributes();
|
||||
|
||||
builder.addPropertyReference("ldapOperations", attributes.getString("ldapTemplateRef"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.query;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
abstract class AbstractLdapRepositoryQuery implements RepositoryQuery {
|
||||
|
||||
private final LdapQueryMethod queryMethod;
|
||||
private final Class<?> clazz;
|
||||
private final LdapOperations ldapOperations;
|
||||
|
||||
public AbstractLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> clazz, LdapOperations ldapOperations) {
|
||||
this.queryMethod = queryMethod;
|
||||
this.clazz = clazz;
|
||||
this.ldapOperations = ldapOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Object execute(Object[] parameters) {
|
||||
|
||||
LdapQuery query = createQuery(parameters);
|
||||
|
||||
if (queryMethod.isCollectionQuery()) {
|
||||
return ldapOperations.find(query, clazz);
|
||||
} else {
|
||||
try {
|
||||
return ldapOperations.findOne(query, clazz);
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract LdapQuery createQuery(Object[] parameters);
|
||||
|
||||
Class<?> getClazz() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final QueryMethod getQueryMethod() {
|
||||
return queryMethod;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.query;
|
||||
|
||||
import static org.springframework.ldap.query.LdapQueryBuilder.*;
|
||||
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
import org.springframework.data.ldap.repository.Query;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Handles queries for repository methods annotated with {@link org.springframework.data.ldap.repository.Query}.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
|
||||
|
||||
private final Query queryAnnotation;
|
||||
|
||||
/**
|
||||
* Construct a new instance.
|
||||
*
|
||||
* @param queryMethod the QueryMethod.
|
||||
* @param clazz the managed class.
|
||||
* @param ldapOperations the LdapOperations instance to use.
|
||||
*/
|
||||
public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> clazz, LdapOperations ldapOperations) {
|
||||
|
||||
super(queryMethod, clazz, ldapOperations);
|
||||
|
||||
queryAnnotation = queryMethod.getQueryAnnotation();
|
||||
|
||||
Assert.notNull(queryMethod, "Annotation must be present");
|
||||
Assert.hasLength(queryAnnotation.value(), "Query filter must be specified");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LdapQuery createQuery(Object[] parameters) {
|
||||
|
||||
return query().base(queryAnnotation.base()) //
|
||||
.searchScope(queryAnnotation.searchScope()) //
|
||||
.countLimit(queryAnnotation.countLimit()) //
|
||||
.timeLimit(queryAnnotation.timeLimit()) //
|
||||
.filter(queryAnnotation.value(), parameters);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.query;
|
||||
|
||||
import static org.springframework.ldap.query.LdapQueryBuilder.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
import org.springframework.data.repository.query.ParametersParameterAccessor;
|
||||
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
|
||||
import org.springframework.data.repository.query.parser.Part;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
import org.springframework.ldap.odm.annotations.Entry;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
import org.springframework.ldap.query.ConditionCriteria;
|
||||
import org.springframework.ldap.query.ContainerCriteria;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
/**
|
||||
* Creator of dynamic queries based on method names.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdapQueryCreator extends AbstractQueryCreator<LdapQuery, ContainerCriteria> {
|
||||
|
||||
private final Class<?> clazz;
|
||||
private final ObjectDirectoryMapper mapper;
|
||||
|
||||
/**
|
||||
* Construct a new instance.
|
||||
*/
|
||||
public LdapQueryCreator(PartTree tree, Parameters<?, ?> parameters, Class<?> clazz, ObjectDirectoryMapper mapper,
|
||||
Object[] values) {
|
||||
|
||||
super(tree, new ParametersParameterAccessor(parameters, values));
|
||||
|
||||
this.clazz = clazz;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ContainerCriteria create(Part part, Iterator<Object> iterator) {
|
||||
|
||||
String base = clazz.getAnnotation(Entry.class).base();
|
||||
ConditionCriteria criteria = query().base(base).where(getAttribute(part));
|
||||
|
||||
return appendCondition(part, iterator, criteria);
|
||||
}
|
||||
|
||||
private ContainerCriteria appendCondition(Part part, Iterator<Object> iterator, ConditionCriteria criteria) {
|
||||
|
||||
Part.Type type = part.getType();
|
||||
|
||||
String value = null;
|
||||
if (iterator.hasNext()) {
|
||||
value = iterator.next().toString();
|
||||
}
|
||||
switch (type) {
|
||||
case NEGATING_SIMPLE_PROPERTY:
|
||||
return criteria.not().is(value);
|
||||
case SIMPLE_PROPERTY:
|
||||
return criteria.is(value);
|
||||
case STARTING_WITH:
|
||||
return criteria.like(value + "*");
|
||||
case ENDING_WITH:
|
||||
return criteria.like("*" + value);
|
||||
case CONTAINING:
|
||||
return criteria.like("*" + value + "*");
|
||||
case LIKE:
|
||||
return criteria.like(value);
|
||||
case NOT_LIKE:
|
||||
return criteria.not().like(value);
|
||||
case GREATER_THAN_EQUAL:
|
||||
return criteria.gte(value);
|
||||
case LESS_THAN_EQUAL:
|
||||
return criteria.lte(value);
|
||||
case IS_NOT_NULL:
|
||||
return criteria.isPresent();
|
||||
case IS_NULL:
|
||||
return criteria.not().isPresent();
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(String.format("%s queries are not supported for LDAP repositories", type));
|
||||
}
|
||||
|
||||
private String getAttribute(Part part) {
|
||||
PropertyPath path = part.getProperty();
|
||||
if (path.hasNext()) {
|
||||
throw new IllegalArgumentException("Nested properties are not supported");
|
||||
}
|
||||
|
||||
return mapper.attributeFor(clazz, path.getSegment());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ContainerCriteria and(Part part, ContainerCriteria base, Iterator<Object> iterator) {
|
||||
ConditionCriteria criteria = base.and(getAttribute(part));
|
||||
|
||||
return appendCondition(part, iterator, criteria);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ContainerCriteria or(ContainerCriteria base, ContainerCriteria criteria) {
|
||||
return base.or(criteria);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LdapQuery complete(ContainerCriteria criteria, Sort sort) {
|
||||
return criteria;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.query;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.data.projection.ProjectionFactory;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.data.ldap.repository.Query;
|
||||
|
||||
/**
|
||||
* QueryMethod for Ldap Queries.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdapQueryMethod extends QueryMethod {
|
||||
|
||||
private final Method method;
|
||||
|
||||
/**
|
||||
* Creates a new LdapQueryMethod from the given parameters.
|
||||
*
|
||||
* @param method must not be {@literal null}
|
||||
* @param metadata must not be {@literal null}
|
||||
*/
|
||||
public LdapQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory) {
|
||||
|
||||
super(method, metadata, factory);
|
||||
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the target method is annotated with {@link org.springframework.data.ldap.repository.Query}.
|
||||
*
|
||||
* @return <code>true</code> if the target method is annotated with {@link org.springframework.data.ldap.repository.Query},
|
||||
* <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean hasQueryAnnotation() {
|
||||
return getQueryAnnotation() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link org.springframework.data.ldap.repository.Query} annotation of the target method (if any).
|
||||
*
|
||||
* @return the {@link org.springframework.data.ldap.repository.Query} annotation of the target method if present, or
|
||||
* <code>null</code> otherwise.
|
||||
*/
|
||||
Query getQueryAnnotation() {
|
||||
return AnnotationUtils.getAnnotation(method, Query.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.query;
|
||||
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class PartTreeLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
|
||||
|
||||
private final PartTree partTree;
|
||||
private final Parameters<?, ?> parameters;
|
||||
private final ObjectDirectoryMapper objectDirectoryMapper;
|
||||
|
||||
public PartTreeLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> clazz, LdapOperations ldapOperations) {
|
||||
|
||||
super(queryMethod, clazz, ldapOperations);
|
||||
|
||||
partTree = new PartTree(queryMethod.getName(), clazz);
|
||||
parameters = queryMethod.getParameters();
|
||||
objectDirectoryMapper = ldapOperations.getObjectDirectoryMapper();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LdapQuery createQuery(Object[] actualParameters) {
|
||||
|
||||
org.springframework.data.ldap.repository.query.LdapQueryCreator queryCreator = new LdapQueryCreator(partTree,
|
||||
this.parameters, getClazz(), objectDirectoryMapper, actualParameters);
|
||||
return queryCreator.createQuery();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.support;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
|
||||
import org.springframework.ldap.odm.annotations.Id;
|
||||
|
||||
import com.querydsl.apt.DefaultConfiguration;
|
||||
|
||||
/**
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
class DefaultLdapAnnotationProcessorConfiguration extends DefaultConfiguration {
|
||||
|
||||
public DefaultLdapAnnotationProcessorConfiguration(
|
||||
RoundEnvironment roundEnv,
|
||||
Map<String, String> options,
|
||||
Collection<String> keywords,
|
||||
Class<? extends Annotation> entitiesAnn,
|
||||
Class<? extends Annotation> entityAnn,
|
||||
Class<? extends Annotation> superTypeAnn,
|
||||
Class<? extends Annotation> embeddableAnn,
|
||||
Class<? extends Annotation> embeddedAnn,
|
||||
Class<? extends Annotation> skipAnn) {
|
||||
|
||||
super(roundEnv, options, keywords, entitiesAnn, entityAnn, superTypeAnn, embeddableAnn, embeddedAnn, skipAnn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBlockedField(VariableElement field) {
|
||||
return super.isBlockedField(field) || field.getAnnotation(Id.class) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidField(VariableElement field) {
|
||||
return super.isValidField(field) && field.getAnnotation(Id.class) == null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.support;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.annotation.processing.SupportedAnnotationTypes;
|
||||
import javax.annotation.processing.SupportedSourceVersion;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.tools.Diagnostic;
|
||||
|
||||
import org.springframework.ldap.odm.annotations.Entry;
|
||||
import org.springframework.ldap.odm.annotations.Transient;
|
||||
|
||||
import com.querydsl.apt.AbstractQuerydslProcessor;
|
||||
import com.querydsl.apt.Configuration;
|
||||
import com.querydsl.apt.DefaultConfiguration;
|
||||
import com.querydsl.core.annotations.QueryEntities;
|
||||
|
||||
/**
|
||||
* QueryDSL Annotation Processor to generate QueryDSL classes for entity classes annotated with {@link Entry}.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
@SupportedAnnotationTypes("org.springframework.ldap.odm.annotations.*")
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_6)
|
||||
public class LdapAnnotationProcessor extends AbstractQuerydslProcessor {
|
||||
|
||||
@Override
|
||||
protected Configuration createConfiguration(RoundEnvironment roundEnv) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Running " + getClass().getSimpleName());
|
||||
|
||||
DefaultConfiguration configuration = new DefaultLdapAnnotationProcessorConfiguration(roundEnv,
|
||||
processingEnv.getOptions(), Collections.<String> emptySet(), QueryEntities.class, Entry.class, null, null, null,
|
||||
Transient.class);
|
||||
configuration.setUseFields(true);
|
||||
configuration.setUseGetters(false);
|
||||
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.support;
|
||||
|
||||
import static org.springframework.data.querydsl.QueryDslUtils.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.data.projection.ProjectionFactory;
|
||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.data.repository.query.EvaluationContextProvider;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.data.ldap.repository.query.AnnotatedLdapRepositoryQuery;
|
||||
import org.springframework.data.ldap.repository.query.LdapQueryMethod;
|
||||
import org.springframework.data.ldap.repository.query.PartTreeLdapRepositoryQuery;
|
||||
import org.springframework.data.ldap.repository.support.SimpleLdapRepository;
|
||||
|
||||
/**
|
||||
* Factory to create {@link org.springframework.data.ldap.repository.LdapRepository} instances.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdapRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
private final LdapOperations ldapOperations;
|
||||
|
||||
public LdapRepositoryFactory(LdapOperations ldapOperations) {
|
||||
this.ldapOperations = ldapOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, ID extends Serializable> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata) {
|
||||
|
||||
if (!isQueryDslRepository(metadata.getRepositoryInterface())) {
|
||||
return new org.springframework.data.ldap.repository.support.SimpleLdapRepository(ldapOperations,
|
||||
ldapOperations.getObjectDirectoryMapper(), metadata.getDomainType());
|
||||
}
|
||||
|
||||
return new QueryDslLdapRepository(ldapOperations, ldapOperations.getObjectDirectoryMapper(),
|
||||
metadata.getDomainType());
|
||||
}
|
||||
|
||||
protected Object getTargetRepository(RepositoryInformation metadata) {
|
||||
return getTargetRepository((RepositoryMetadata) metadata);
|
||||
}
|
||||
|
||||
private static boolean isQueryDslRepository(Class<?> repositoryInterface) {
|
||||
return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
|
||||
if (!isQueryDslRepository(metadata.getRepositoryInterface())) {
|
||||
return SimpleLdapRepository.class;
|
||||
} else {
|
||||
return QueryDslLdapRepository.class;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) {
|
||||
return new LdapQueryLookupStrategy();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected QueryLookupStrategy getQueryLookupStrategy(Key key, EvaluationContextProvider evaluationContextProvider) {
|
||||
return new LdapQueryLookupStrategy();
|
||||
}
|
||||
|
||||
private final class LdapQueryLookupStrategy implements QueryLookupStrategy {
|
||||
|
||||
@Override
|
||||
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
|
||||
NamedQueries namedQueries) {
|
||||
LdapQueryMethod queryMethod = new LdapQueryMethod(method, metadata, factory);
|
||||
Class<?> domainType = metadata.getDomainType();
|
||||
|
||||
if (queryMethod.hasQueryAnnotation()) {
|
||||
return new AnnotatedLdapRepositoryQuery(queryMethod, domainType, ldapOperations);
|
||||
} else {
|
||||
return new PartTreeLdapRepositoryQuery(queryMethod, domainType, ldapOperations);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.support;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.data.ldap.repository.support.LdapRepositoryFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.beans.factory.FactoryBean} to create
|
||||
* {@link org.springframework.data.ldap.repository.LdapRepository} instances.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdapRepositoryFactoryBean<T extends Repository<S, Name>, S>
|
||||
extends RepositoryFactoryBeanSupport<T, S, Name> {
|
||||
|
||||
private LdapOperations ldapOperations;
|
||||
|
||||
public void setLdapOperations(LdapOperations ldapOperations) {
|
||||
this.ldapOperations = ldapOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RepositoryFactorySupport createRepositoryFactory() {
|
||||
return new LdapRepositoryFactory(ldapOperations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
super.afterPropertiesSet();
|
||||
Assert.notNull(ldapOperations, "LdapOperations must be set");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.support;
|
||||
|
||||
import org.springframework.ldap.filter.AndFilter;
|
||||
import org.springframework.ldap.filter.EqualsFilter;
|
||||
import org.springframework.ldap.filter.Filter;
|
||||
import org.springframework.ldap.filter.GreaterThanOrEqualsFilter;
|
||||
import org.springframework.ldap.filter.LessThanOrEqualsFilter;
|
||||
import org.springframework.ldap.filter.LikeFilter;
|
||||
import org.springframework.ldap.filter.NotFilter;
|
||||
import org.springframework.ldap.filter.OrFilter;
|
||||
import org.springframework.ldap.filter.PresentFilter;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
|
||||
import com.querydsl.core.types.*;
|
||||
|
||||
/**
|
||||
* Helper class for generating LDAP filters from QueryDSL Expressions.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
class LdapSerializer implements Visitor<Object, Void> {
|
||||
|
||||
private final ObjectDirectoryMapper odm;
|
||||
private final Class<?> clazz;
|
||||
|
||||
public LdapSerializer(ObjectDirectoryMapper odm, Class<?> clazz) {
|
||||
|
||||
this.odm = odm;
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public Filter handle(Expression<?> expression) {
|
||||
return (Filter) expression.accept(this, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(Constant<?> expr, Void context) {
|
||||
return expr.getConstant().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(FactoryExpression<?> expr, Void context) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(Operation<?> expr, Void context) {
|
||||
|
||||
Operator operator = expr.getOperator();
|
||||
|
||||
if (operator == Ops.EQ) {
|
||||
return new EqualsFilter(attribute(expr), value(expr));
|
||||
} else if (operator == Ops.AND) {
|
||||
return new AndFilter().and(handle(expr.getArg(0))).and(handle(expr.getArg(1)));
|
||||
} else if (operator == Ops.OR) {
|
||||
return new OrFilter().or(handle(expr.getArg(0))).or(handle(expr.getArg(1)));
|
||||
} else if (operator == Ops.NOT) {
|
||||
return new NotFilter(handle(expr.getArg(0)));
|
||||
} else if (operator == Ops.LIKE) {
|
||||
return new LikeFilter(attribute(expr), value(expr));
|
||||
} else if (operator == Ops.STARTS_WITH || operator == Ops.STARTS_WITH_IC) {
|
||||
return new LikeFilter(attribute(expr), value(expr) + "*");
|
||||
} else if (operator == Ops.ENDS_WITH || operator == Ops.ENDS_WITH_IC) {
|
||||
return new LikeFilter(attribute(expr), "*" + value(expr));
|
||||
} else if (operator == Ops.STRING_CONTAINS || operator == Ops.STRING_CONTAINS_IC) {
|
||||
return new LikeFilter(attribute(expr), "*" + value(expr) + "*");
|
||||
} else if (operator == Ops.IS_NOT_NULL) {
|
||||
return new PresentFilter(attribute(expr));
|
||||
} else if (operator == Ops.IS_NULL) {
|
||||
return new NotFilter(new PresentFilter(attribute(expr)));
|
||||
} else if (operator == Ops.GOE) {
|
||||
return new GreaterThanOrEqualsFilter(attribute(expr), value(expr));
|
||||
} else if (operator == Ops.LOE) {
|
||||
return new LessThanOrEqualsFilter(attribute(expr), value(expr));
|
||||
}
|
||||
|
||||
throw new UnsupportedOperationException("Unsupported operator " + operator.toString());
|
||||
}
|
||||
|
||||
private String value(Operation<?> expr) {
|
||||
return (String) expr.getArg(1).accept(this, null);
|
||||
}
|
||||
|
||||
private String attribute(Operation<?> expr) {
|
||||
return odm.attributeFor(clazz, (String) expr.getArg(0).accept(this, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ParamExpression<?> expr, Void context) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(Path<?> expr, Void context) {
|
||||
return expr.getMetadata().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(SubQueryExpression<?> expr, Void context) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(TemplateExpression<?> expr, Void context) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.support;
|
||||
|
||||
import static org.springframework.ldap.query.LdapQueryBuilder.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
|
||||
import com.querydsl.core.DefaultQueryMetadata;
|
||||
import com.querydsl.core.FilteredClause;
|
||||
import com.querydsl.core.support.QueryMixin;
|
||||
import com.querydsl.core.types.EntityPath;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
|
||||
/**
|
||||
* Spring LDAP specific {@link FilteredClause} implementation.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
public class QueryDslLdapQuery<K> implements FilteredClause<QueryDslLdapQuery<K>> {
|
||||
|
||||
private final LdapOperations ldapOperations;
|
||||
private final Class<? extends K> clazz;
|
||||
|
||||
private QueryMixin<QueryDslLdapQuery<K>> queryMixin = new QueryMixin<QueryDslLdapQuery<K>>(this,
|
||||
new DefaultQueryMetadata().noValidate());
|
||||
|
||||
private final LdapSerializer filterGenerator;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public QueryDslLdapQuery(LdapOperations ldapOperations, EntityPath<K> entityPath) {
|
||||
this(ldapOperations, (Class<K>) entityPath.getType());
|
||||
}
|
||||
|
||||
public QueryDslLdapQuery(LdapOperations ldapOperations, Class<K> clazz) {
|
||||
|
||||
this.ldapOperations = ldapOperations;
|
||||
this.clazz = clazz;
|
||||
this.filterGenerator = new LdapSerializer(ldapOperations.getObjectDirectoryMapper(), clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryDslLdapQuery<K> where(Predicate... o) {
|
||||
return queryMixin.where(o);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<K> list() {
|
||||
return (List<K>) ldapOperations.find(buildQuery(), clazz);
|
||||
}
|
||||
|
||||
public K uniqueResult() {
|
||||
return ldapOperations.findOne(buildQuery(), clazz);
|
||||
}
|
||||
|
||||
LdapQuery buildQuery() {
|
||||
return query().filter(filterGenerator.handle(queryMixin.getMetadata().getWhere()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.support;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
import org.springframework.data.ldap.repository.support.QueryDslLdapQuery;
|
||||
import org.springframework.data.ldap.repository.support.SimpleLdapRepository;
|
||||
|
||||
import com.querydsl.core.types.OrderSpecifier;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
|
||||
/**
|
||||
* Base repository implementation for QueryDSL support.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Eddu Melendez
|
||||
* @since 2.0
|
||||
*/
|
||||
public class QueryDslLdapRepository<T> extends SimpleLdapRepository<T> implements QueryDslPredicateExecutor<T> {
|
||||
|
||||
public QueryDslLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> clazz) {
|
||||
super(ldapOperations, odm, clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne(Predicate predicate) {
|
||||
return queryFor(predicate).uniqueResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll(Predicate predicate) {
|
||||
return queryFor(predicate).list();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count(Predicate predicate) {
|
||||
return findAll(predicate).size();
|
||||
}
|
||||
|
||||
public boolean exists(Predicate predicate) {
|
||||
return count(predicate) > 0;
|
||||
}
|
||||
|
||||
public Iterable<T> findAll(Predicate predicate, Sort sort) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private QueryDslLdapQuery<T> queryFor(Predicate predicate) {
|
||||
return new QueryDslLdapQuery<T>(getLdapOperations(), getClazz()).where(predicate);
|
||||
}
|
||||
|
||||
public Iterable<T> findAll(OrderSpecifier<?>... orders) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> findAll(Predicate predicate, Pageable pageable) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.ldap.repository.support;
|
||||
|
||||
import static org.springframework.ldap.query.LdapQueryBuilder.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.data.domain.Persistable;
|
||||
import org.springframework.ldap.NameNotFoundException;
|
||||
import org.springframework.ldap.core.LdapOperations;
|
||||
import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler;
|
||||
import org.springframework.ldap.filter.Filter;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
import org.springframework.ldap.query.LdapQuery;
|
||||
import org.springframework.data.ldap.repository.LdapRepository;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base repository implementation for LDAP.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @since 2.0
|
||||
*/
|
||||
public class SimpleLdapRepository<T> implements LdapRepository<T> {
|
||||
|
||||
private static final String OBJECTCLASS_ATTRIBUTE = "objectclass";
|
||||
private final LdapOperations ldapOperations;
|
||||
private final ObjectDirectoryMapper odm;
|
||||
private final Class<T> clazz;
|
||||
|
||||
public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> clazz) {
|
||||
|
||||
this.ldapOperations = ldapOperations;
|
||||
this.odm = odm;
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
protected LdapOperations getLdapOperations() {
|
||||
return ldapOperations;
|
||||
}
|
||||
|
||||
protected Class<T> getClazz() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
|
||||
Filter filter = odm.filterFor(clazz, null);
|
||||
CountNameClassPairCallbackHandler callback = new CountNameClassPairCallbackHandler();
|
||||
LdapQuery query = query().attributes(OBJECTCLASS_ATTRIBUTE).filter(filter);
|
||||
ldapOperations.search(query, callback);
|
||||
|
||||
return callback.getNoOfRows();
|
||||
}
|
||||
|
||||
private <S extends T> boolean isNew(S entity, Name id) {
|
||||
|
||||
if (entity instanceof Persistable) {
|
||||
Persistable<?> persistable = (Persistable<?>) entity;
|
||||
return persistable.isNew();
|
||||
} else {
|
||||
return id == null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> S save(S entity) {
|
||||
|
||||
Assert.notNull(entity, "Entity must not be null");
|
||||
|
||||
Name declaredId = odm.getId(entity);
|
||||
|
||||
if (isNew(entity, declaredId)) {
|
||||
ldapOperations.create(entity);
|
||||
} else {
|
||||
ldapOperations.update(entity);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> Iterable<S> save(Iterable<S> entities) {
|
||||
|
||||
return new TransformingIterable<S, S>(entities, new Function<S, S>() {
|
||||
@Override
|
||||
public S transform(S entry) {
|
||||
return save(entry);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne(Name name) {
|
||||
|
||||
Assert.notNull(name, "Id must not be null");
|
||||
try {
|
||||
return ldapOperations.findByDn(name, clazz);
|
||||
} catch (NameNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll(LdapQuery ldapQuery) {
|
||||
|
||||
Assert.notNull(ldapQuery, "LdapQuery must not be null");
|
||||
return ldapOperations.find(ldapQuery, clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne(LdapQuery ldapQuery) {
|
||||
Assert.notNull(ldapQuery, "LdapQuery must not be null");
|
||||
try {
|
||||
return ldapOperations.findOne(ldapQuery, clazz);
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(Name name) {
|
||||
|
||||
Assert.notNull(name, "Id must not be null");
|
||||
|
||||
return findOne(name) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
return ldapOperations.findAll(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll(final Iterable<Name> names) {
|
||||
|
||||
Iterable<T> found = new TransformingIterable<Name, T>(names, new Function<Name, T>() {
|
||||
@Override
|
||||
public T transform(Name name) {
|
||||
return findOne(name);
|
||||
}
|
||||
});
|
||||
|
||||
LinkedList<T> list = new LinkedList<T>();
|
||||
for (T entry : found) {
|
||||
if (entry != null) {
|
||||
list.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Name name) {
|
||||
|
||||
Assert.notNull(name, "Id must not be null");
|
||||
|
||||
ldapOperations.unbind(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(T entity) {
|
||||
|
||||
Assert.notNull(entity, "Entity must not be null");
|
||||
|
||||
ldapOperations.delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Iterable<? extends T> entities) {
|
||||
|
||||
for (T entity : entities) {
|
||||
delete(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
delete(findAll());
|
||||
}
|
||||
|
||||
private static final class TransformingIterable<F, T> implements Iterable<T> {
|
||||
|
||||
private final Iterable<F> target;
|
||||
private final Function<F, T> function;
|
||||
|
||||
private TransformingIterable(Iterable<F> target, Function<F, T> function) {
|
||||
this.target = target;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
|
||||
final Iterator<F> targetIterator = target.iterator();
|
||||
return new Iterator<T>() {
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return targetIterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
return function.transform(targetIterator.next());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Remove is not supported for this iterator");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private interface Function<F, T> {
|
||||
|
||||
T transform(F entry);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user