Skip to content

Commit dc22044

Browse files
mp911deodrotbohm
authored andcommitted
DATAREST-1198 - Add Converter to convert String to javax.naming.ldap.LdapName.
We now provide and configure a converter for String to LdapName conversion so Spring Data LDAP can be used with Spring Data REST without further configuration. Original pull request: #290.
1 parent 8966907 commit dc22044

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.rest.core;
17+
18+
import javax.naming.InvalidNameException;
19+
import javax.naming.Name;
20+
import javax.naming.ldap.LdapName;
21+
22+
import org.springframework.core.convert.converter.Converter;
23+
24+
/**
25+
* {@link Converter} to convert a {@link String} to a {@link LdapName}.
26+
*
27+
* @author Mark Paluch
28+
* @since 3.1
29+
*/
30+
public enum StringToLdapNameConverter implements Converter<String, Name> {
31+
32+
INSTANCE;
33+
34+
/*
35+
* (non-Javadoc)
36+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
37+
*/
38+
@Override
39+
public LdapName convert(String source) {
40+
41+
try {
42+
return new LdapName(source);
43+
} catch (InvalidNameException e) {
44+
throw new IllegalArgumentException(String.format("Cannot create LdapName for '%s'!", source), e);
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.rest.core;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import javax.naming.InvalidNameException;
21+
import javax.naming.Name;
22+
import javax.naming.ldap.LdapName;
23+
24+
import org.junit.Test;
25+
import org.springframework.core.convert.support.DefaultConversionService;
26+
27+
/**
28+
* Unit tests for {@link StringToLdapNameConverter}.
29+
*
30+
* @author Mark Paluch
31+
*/
32+
public class StringToLdapNameConverterUnitTests {
33+
34+
@Test // DATAREST-1198
35+
public void shouldCreateLdapName() throws InvalidNameException {
36+
37+
LdapName converted = StringToLdapNameConverter.INSTANCE.convert("dc=foo");
38+
39+
assertThat(converted).isEqualTo(new LdapName("dc=foo"));
40+
}
41+
42+
@Test // DATAREST-1198
43+
public void failedConversionShouldThrowIAE() {
44+
45+
assertThatThrownBy(() -> StringToLdapNameConverter.INSTANCE.convert("foo"))
46+
.isInstanceOf(IllegalArgumentException.class);
47+
}
48+
49+
@Test // DATAREST-1198
50+
public void shouldConvertStringInNameViaConversionService() {
51+
52+
DefaultConversionService conversionService = new DefaultConversionService();
53+
conversionService.addConverter(StringToLdapNameConverter.INSTANCE);
54+
55+
Name converted = conversionService.convert("dc=foo", Name.class);
56+
57+
assertThat(converted).isInstanceOf(LdapName.class);
58+
}
59+
}

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.rest.webmvc.config;
1717

18+
import javax.naming.Name;
1819
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Collection;
@@ -43,6 +44,7 @@
4344
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
4445
import org.springframework.core.Ordered;
4546
import org.springframework.core.convert.ConversionService;
47+
import org.springframework.core.convert.converter.Converter;
4648
import org.springframework.core.io.ClassPathResource;
4749
import org.springframework.data.auditing.AuditableBeanWrapperFactory;
4850
import org.springframework.data.auditing.MappingAuditableBeanWrapperFactory;
@@ -57,6 +59,7 @@
5759
import org.springframework.data.repository.support.DefaultRepositoryInvokerFactory;
5860
import org.springframework.data.repository.support.Repositories;
5961
import org.springframework.data.repository.support.RepositoryInvokerFactory;
62+
import org.springframework.data.rest.core.StringToLdapNameConverter;
6063
import org.springframework.data.rest.core.UriToEntityConverter;
6164
import org.springframework.data.rest.core.config.MetadataConfiguration;
6265
import org.springframework.data.rest.core.config.Projection;
@@ -247,6 +250,7 @@ public DefaultFormattingConversionService defaultConversionService() {
247250
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
248251
// Add Spring Data Commons formatters
249252
conversionService.addConverter(uriToEntityConverter(conversionService));
253+
conversionService.addConverter(stringToNameConverter());
250254
addFormatters(conversionService);
251255

252256
configurerDelegate.configureConversionService(conversionService);
@@ -673,6 +677,10 @@ protected UriToEntityConverter uriToEntityConverter(ConversionService conversion
673677
return new UriToEntityConverter(persistentEntities(), repositoryInvokerFactory(conversionService), repositories());
674678
}
675679

680+
protected Converter<String, ? extends Name> stringToNameConverter() {
681+
return StringToLdapNameConverter.INSTANCE;
682+
}
683+
676684
@Bean
677685
public ExcerptProjector excerptProjector() {
678686

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import javax.naming.Name;
21+
import javax.naming.ldap.LdapName;
2022
import java.util.Collection;
2123
import java.util.Date;
2224
import java.util.List;
@@ -64,6 +66,7 @@
6466
* Integration tests for basic application bootstrapping (general configuration related checks).
6567
*
6668
* @author Oliver Gierke
69+
* @author Mark Paluch
6770
*/
6871
public class RepositoryRestMvConfigurationIntegrationTests {
6972

@@ -185,6 +188,15 @@ public void hasConvertersForPointAndDistance() {
185188
assertThat(service.canConvert(Distance.class, String.class)).isTrue();
186189
}
187190

191+
@Test // DATAREST-1198
192+
public void hasConvertersForNamAndLdapName() {
193+
194+
ConversionService service = context.getBean("defaultConversionService", ConversionService.class);
195+
196+
assertThat(service.canConvert(String.class, Name.class)).isTrue();
197+
assertThat(service.canConvert(String.class, LdapName.class)).isTrue();
198+
}
199+
188200
@Test // DATAREST-686
189201
public void defaultsEncodingForMessageSourceToUtfEight() {
190202

0 commit comments

Comments
 (0)