-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
251 additions
and
11 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/java/org/springframework/data/ldap/repository/LdapEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2024 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.data.ldap.repository; | ||
|
||
/** | ||
* Allows plugging in custom encoding for {@link LdapParam}. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @since 3.5.0 | ||
*/ | ||
public interface LdapEncoder { | ||
|
||
/** | ||
* Escape a value for use in a filter. | ||
* @param value the value to escape. | ||
* @return a properly escaped representation of the supplied value. | ||
*/ | ||
String filterEncode(String value); | ||
|
||
/** | ||
* Default implementation of {@link LdapEncoder} that uses | ||
* {@link org.springframework.ldap.support.LdapEncoder}. | ||
*/ | ||
class DefaultLdapEncoder implements LdapEncoder { | ||
|
||
@Override | ||
public String filterEncode(String value) { | ||
return org.springframework.ldap.support.LdapEncoder.filterEncode(value); | ||
} | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/org/springframework/data/ldap/repository/LdapParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2016-2024 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.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.core.annotation.AliasFor; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
/** | ||
* A {@link Param} alias that allows passing of custom {@link LdapEncoder}. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @since 3.5.0 | ||
*/ | ||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface LdapParam { | ||
|
||
@AliasFor(annotation = Param.class) | ||
String value(); | ||
|
||
/** | ||
* {@link LdapEncoder} to instantiate to encode query parameters. | ||
* | ||
* @return {@link LdapEncoder} class | ||
*/ | ||
Class<? extends LdapEncoder> encoder() default LdapEncoder.DefaultLdapEncoder.class; | ||
} |
105 changes: 105 additions & 0 deletions
105
src/main/java/org/springframework/data/ldap/repository/query/LdapParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2024 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.data.ldap.repository.query; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.data.geo.Distance; | ||
import org.springframework.data.ldap.repository.LdapEncoder; | ||
import org.springframework.data.ldap.repository.LdapParam; | ||
import org.springframework.data.repository.query.Parameter; | ||
import org.springframework.data.repository.query.Parameters; | ||
import org.springframework.data.repository.query.ParametersSource; | ||
import org.springframework.data.util.TypeInformation; | ||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* Custom extension of {@link Parameters} discovering additional | ||
* | ||
* @author Marcin Grzejszczak | ||
* @since 3.5.0 | ||
*/ | ||
public class LdapParameters extends Parameters<LdapParameters, LdapParameters.LdapParameter> { | ||
|
||
private final TypeInformation<?> domainType; | ||
|
||
/** | ||
* Creates a new {@link LdapParameters} instance from the given {@link Method} and {@link LdapQueryMethod}. | ||
* | ||
* @param parametersSource must not be {@literal null}. | ||
*/ | ||
public LdapParameters(ParametersSource parametersSource) { | ||
|
||
super(parametersSource, methodParameter -> new LdapParameter(methodParameter, | ||
parametersSource.getDomainTypeInformation())); | ||
|
||
this.domainType = parametersSource.getDomainTypeInformation(); | ||
} | ||
|
||
private LdapParameters(List<LdapParameter> parameters, TypeInformation<?> domainType) { | ||
|
||
super(parameters); | ||
this.domainType = domainType; | ||
} | ||
|
||
@Override | ||
protected LdapParameters createFrom(List<LdapParameter> parameters) { | ||
return new LdapParameters(parameters, this.domainType); | ||
} | ||
|
||
@Nullable | ||
LdapEncoder encoderForParameterWithName(String parameterName) { | ||
for (LdapParameter parameter : this) { | ||
if (parameterName.equals(parameter.getName().orElse(null))) { | ||
LdapParam ldapParam = parameter.parameter.getParameterAnnotation(LdapParam.class); | ||
if (ldapParam == null) { | ||
return null; | ||
} | ||
Class<? extends LdapEncoder> encoder = ldapParam.encoder(); | ||
try { | ||
return encoder.getDeclaredConstructor().newInstance(); | ||
} catch (Exception e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Custom {@link Parameter} implementation adding parameters of type {@link Distance} to the special ones. | ||
* | ||
* @author Marcin Grzejszczak | ||
*/ | ||
protected static class LdapParameter extends Parameter { | ||
|
||
private final MethodParameter parameter; | ||
|
||
/** | ||
* Creates a new {@link LdapParameter}. | ||
* | ||
* @param parameter must not be {@literal null}. | ||
* @param domainType must not be {@literal null}. | ||
*/ | ||
LdapParameter(MethodParameter parameter, TypeInformation<?> domainType) { | ||
super(parameter, domainType); | ||
this.parameter = parameter; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters