Skip to content

Commit 3c7d3f9

Browse files
committed
Add String to File conversion support
Add `String` -> `File` support to the `ApplicationConversionService` that can support both simple filename as well as file URLs. This allows Spring Boot application to work in a similar way to vanilla Spring applications where Spring's `FileEditor` provides similar support. Closes gh-16931
1 parent 61873fb commit 3c7d3f9

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/ApplicationConversionService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public static void addApplicationConverters(ConverterRegistry registry) {
115115
registry.addConverter(new DurationToNumberConverter());
116116
registry.addConverter(new StringToDataSizeConverter());
117117
registry.addConverter(new NumberToDataSizeConverter());
118+
registry.addConverter(new StringToFileConverter());
118119
registry.addConverterFactory(new LenientStringToEnumConverterFactory());
119120
registry.addConverterFactory(new LenientBooleanToEnumConverterFactory());
120121
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2019 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+
* https://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+
17+
package org.springframework.boot.convert;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import org.springframework.core.convert.converter.Converter;
23+
import org.springframework.core.io.DefaultResourceLoader;
24+
import org.springframework.core.io.Resource;
25+
import org.springframework.core.io.ResourceLoader;
26+
import org.springframework.util.ResourceUtils;
27+
28+
/**
29+
* {@link Converter} to convert from a {@link String} to a {@link File}. Supports basic
30+
* file conversion as well as file URLs.
31+
*
32+
* @author Phillip Webb
33+
*/
34+
class StringToFileConverter implements Converter<String, File> {
35+
36+
private static final ResourceLoader resourceLoader = new DefaultResourceLoader();
37+
38+
@Override
39+
public File convert(String source) {
40+
if (ResourceUtils.isUrl(source)) {
41+
return getFile(resourceLoader.getResource(source));
42+
}
43+
File file = new File(source);
44+
if (file.isAbsolute()) {
45+
return file;
46+
}
47+
Resource resource = resourceLoader.getResource(source);
48+
if (resource.exists()) {
49+
return getFile(resource);
50+
}
51+
return file;
52+
}
53+
54+
private File getFile(Resource resource) {
55+
try {
56+
return resource.getFile();
57+
}
58+
catch (IOException ex) {
59+
throw new IllegalStateException("Could not retrieve file for " + resource + ": " + ex.getMessage());
60+
}
61+
}
62+
63+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-2019 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+
* https://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+
17+
package org.springframework.boot.convert;
18+
19+
import java.io.File;
20+
import java.util.stream.Stream;
21+
22+
import org.junit.jupiter.api.io.TempDir;
23+
import org.junit.jupiter.params.provider.Arguments;
24+
25+
import org.springframework.core.convert.ConversionService;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link StringToFileConverter}.
31+
*
32+
* @author Phillip Webb
33+
*/
34+
class StringToFileConverterTests {
35+
36+
@TempDir
37+
File temp;
38+
39+
@ConversionServiceTest
40+
void convertWhenSimpleFileReturnsFile(ConversionService conversionService) {
41+
assertThat(convert(conversionService, this.temp.getAbsolutePath() + "/test"))
42+
.isEqualTo(new File(this.temp, "test").getAbsoluteFile());
43+
}
44+
45+
@ConversionServiceTest
46+
void convertWhenFilePrefixedReturnsFile(ConversionService conversionService) {
47+
assertThat(convert(conversionService, "file:" + this.temp.getAbsolutePath() + "/test").getAbsoluteFile())
48+
.isEqualTo(new File(this.temp, "test").getAbsoluteFile());
49+
}
50+
51+
private File convert(ConversionService conversionService, String source) {
52+
return conversionService.convert(source, File.class);
53+
}
54+
55+
static Stream<? extends Arguments> conversionServices() {
56+
return ConversionServiceArguments
57+
.with((conversionService) -> conversionService.addConverter(new StringToFileConverter()));
58+
}
59+
60+
}

0 commit comments

Comments
 (0)