Skip to content

Commit a6d9361

Browse files
committed
SslOptions.x509() method doesn't allow for absolute paths fix jooby-project#2303
1 parent 7138c83 commit a6d9361

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

jooby/src/main/java/io/jooby/SslOptions.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.io.IOException;
1212
import java.io.InputStream;
1313
import java.nio.file.Files;
14+
import java.nio.file.InvalidPathException;
15+
import java.nio.file.Path;
1416
import java.nio.file.Paths;
1517
import java.util.Arrays;
1618
import java.util.List;
@@ -233,8 +235,21 @@ public String getType() {
233235
*/
234236
public @Nonnull InputStream getResource(@Nonnull ClassLoader loader, @Nonnull String path)
235237
throws IOException {
236-
InputStream resource = Stream
237-
.of(Paths.get(path), Paths.get(System.getProperty("user.dir"), path))
238+
Path filepath = Paths.get(path);
239+
Stream<Path> paths;
240+
if (Files.exists(filepath)) {
241+
// absolute file:
242+
paths = Stream.of(filepath);
243+
} else {
244+
try {
245+
// Try relative to current dir:
246+
paths = Stream.of(Paths.get(System.getProperty("user.dir"), path));
247+
} catch (InvalidPathException cause) {
248+
// Try with classloader:
249+
paths = Stream.empty();
250+
}
251+
}
252+
InputStream resource = paths
238253
.map(it -> it.normalize().toAbsolutePath())
239254
.filter(Files::exists)
240255
.findFirst()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.jooby;
2+
3+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
12+
import org.junit.jupiter.api.Assertions;
13+
import org.junit.jupiter.api.Test;
14+
15+
public class Issue2303 {
16+
17+
@Test
18+
public void shouldLoadAbsoluteFile() throws IOException {
19+
String crt = file("ssl", "test.crt");
20+
String key = file("ssl", "test.key");
21+
SslOptions ssl = SslOptions.x509(crt, key);
22+
assertNotNull(ssl);
23+
withResource(ssl, ssl.getCert(), Assertions::assertNotNull);
24+
withResource(ssl, ssl.getPrivateKey(), Assertions::assertNotNull);
25+
}
26+
27+
private void withResource(SslOptions ssl, String file,
28+
SneakyThrows.Consumer<InputStream> consumer) throws IOException {
29+
try (InputStream crtFile = ssl.getResource(getClass().getClassLoader(), file)) {
30+
consumer.accept(crtFile);
31+
}
32+
}
33+
34+
private String file(String... path) {
35+
Path basedir = Paths.get(System.getProperty("user.dir"));
36+
if (Files.exists(basedir.resolve("jooby"))) {
37+
// maven vs IDE
38+
basedir = basedir.resolve("jooby");
39+
}
40+
Path result = basedir.resolve("src").resolve("test").resolve("resources");
41+
for (String segment : path) {
42+
result = result.resolve(segment);
43+
}
44+
result = result.normalize().toAbsolutePath();
45+
assertTrue(Files.exists(result), result.toString());
46+
return result.toString();
47+
}
48+
}

0 commit comments

Comments
 (0)