Skip to content

Commit e7ac731

Browse files
committed
Rename StaticContent and add Builder interface
1 parent 670910c commit e7ac731

File tree

8 files changed

+164
-129
lines changed

8 files changed

+164
-129
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package io.avaje.jex.staticcontent;
2+
3+
import java.net.URLConnection;
4+
import java.util.function.Predicate;
5+
6+
import io.avaje.jex.Context;
7+
import io.avaje.jex.spi.JexPlugin;
8+
9+
/**
10+
* Static content resource handler.
11+
* <pre>{@code
12+
*
13+
* var staticContent = StaticContent.createFile("src/test/resources/public")
14+
* .directoryIndex("index.html")
15+
* .preCompress()
16+
* .build()
17+
*
18+
* Jex.create()
19+
* .plugin(staticContent)
20+
* .port(8080)
21+
* .start();
22+
*
23+
* }</pre>
24+
*/
25+
public sealed interface StaticContent extends JexPlugin
26+
permits StaticResourceHandlerBuilder {
27+
28+
/**
29+
* Create and return a new static content class path configuration.
30+
*
31+
* @param resourceRoot The file to serve, or the directory the files are located in.
32+
*/
33+
static Builder createCP(String resourceRoot) {
34+
return StaticResourceHandlerBuilder.builder(resourceRoot);
35+
}
36+
37+
/**
38+
* Create and return a new static content class path configuration with the
39+
* `/public` directory as the root.
40+
*/
41+
static Builder createCP() {
42+
return StaticResourceHandlerBuilder.builder("/public/");
43+
}
44+
45+
/**
46+
* Create and return a new static content configuration for a File.
47+
*
48+
* @param resourceRoot The path of the file to serve, or the directory the files are located in.
49+
*/
50+
static Builder createFile(String resourceRoot) {
51+
return StaticResourceHandlerBuilder.builder(resourceRoot).file();
52+
}
53+
54+
/**
55+
* Builder for StaticContent.
56+
*/
57+
sealed interface Builder
58+
permits StaticResourceHandlerBuilder {
59+
60+
/**
61+
* Sets the HTTP path for the static resource handler.
62+
*
63+
* @param path the HTTP path prefix
64+
* @return the updated configuration
65+
*/
66+
Builder httpPath(String path);
67+
68+
/**
69+
* Sets the index file to be served when a directory is requests.
70+
*
71+
* @param directoryIndex the index file
72+
* @return the updated configuration
73+
*/
74+
Builder directoryIndex(String directoryIndex);
75+
76+
/**
77+
* Sent resources will be pre-compressed and cached in memory when this is enabled
78+
*
79+
* @return the updated configuration
80+
*/
81+
Builder preCompress();
82+
83+
/**
84+
* Sets a custom resource loader for loading class/module path resources. This is normally used
85+
* when running the application on the module path when files cannot be discovered.
86+
*
87+
* <p>Example usage: {@code service.resourceLoader(ClassResourceLoader.create(getClass())) }
88+
*
89+
* @param resourceLoader the custom resource loader
90+
* @return the updated configuration
91+
*/
92+
Builder resourceLoader(ClassResourceLoader resourceLoader);
93+
94+
/**
95+
* Adds a new MIME type mapping to the configuration. (Default: uses {@link
96+
* URLConnection#getFileNameMap()}
97+
*
98+
* @param ext the file extension (e.g., "html", "css", "js")
99+
* @param mimeType the corresponding MIME type (e.g., "text/html", "text/css",
100+
* "application/javascript")
101+
* @return the updated configuration
102+
*/
103+
Builder putMimeTypeMapping(String ext, String mimeType);
104+
105+
/**
106+
* Adds a new response header to the configuration.
107+
*
108+
* @param key the header name
109+
* @param value the header value
110+
* @return the updated configuration
111+
*/
112+
Builder putResponseHeader(String key, String value);
113+
114+
/**
115+
* Sets a predicate to filter files based on the request context.
116+
*
117+
* @param skipFilePredicate the predicate to use
118+
* @return the updated configuration
119+
*/
120+
Builder skipFilePredicate(Predicate<Context> skipFilePredicate);
121+
122+
/**
123+
* Build and return the StaticContent.
124+
*/
125+
StaticContent build();
126+
}
127+
}

avaje-jex-static-content/src/main/java/io/avaje/jex/staticcontent/StaticContentService.java

Lines changed: 0 additions & 100 deletions
This file was deleted.

avaje-jex-static-content/src/main/java/io/avaje/jex/staticcontent/StaticResourceHandlerBuilder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import io.avaje.jex.Jex;
1313
import io.avaje.jex.compression.CompressionConfig;
1414

15-
final class StaticResourceHandlerBuilder implements StaticContentService {
15+
final class StaticResourceHandlerBuilder implements StaticContent.Builder, StaticContent {
1616

1717
private static final String FAILED_TO_LOCATE_FILE = "Failed to locate file: ";
1818
private static final String DIRECTORY_INDEX_FAILURE =
@@ -43,6 +43,11 @@ public void apply(Jex jex) {
4343
jex.get(path, createHandler(jex.config().compression()));
4444
}
4545

46+
@Override
47+
public StaticContent build() {
48+
return this;
49+
}
50+
4651
ExchangeHandler createHandler(CompressionConfig compress) {
4752
path =
4853
Objects.requireNonNull(path)

avaje-jex-static-content/src/main/java/io/avaje/jex/staticcontent/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Static Content API - see {@link io.avaje.jex.staticcontent.StaticContentService}.
2+
* Static Content API - see {@link io.avaje.jex.staticcontent.StaticContent}.
33
*
44
* <pre>{@code
55
* var staticContent = StaticContentService.createCP("/public").httpPath("/").directoryIndex("index.html");

avaje-jex-static-content/src/main/java/module-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Defines the Static Content API for serving static resources with Jex - see {@link io.avaje.jex.staticcontent.StaticContentService}.
2+
* Defines the Static Content API for serving static resources with Jex - see {@link StaticContent}.
33
*
44
* <pre>{@code
55
* var staticContent = StaticContentService.createCP("/public").httpPath("/").directoryIndex("index.html");

avaje-jex-static-content/src/test/java/io/avaje/jex/staticcontent/CompressedStaticFileTest.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,30 @@ static TestPair init() {
1919

2020
final Jex app =
2121
Jex.create()
22-
.plugin(defaultCP().httpPath("/index"))
23-
.plugin(defaultFile().httpPath("/indexFile"))
24-
.plugin(defaultCP().httpPath("/indexWild/*"))
25-
.plugin(defaultFile().httpPath("/indexWildFile/*"))
26-
.plugin(defaultCP().httpPath("/sus/"))
27-
.plugin(defaultFile().httpPath("/susFile/*"))
28-
.plugin(StaticContentService.createCP("/logback.xml").httpPath("/single"))
22+
.plugin(defaultCP().httpPath("/index").build())
23+
.plugin(defaultFile().httpPath("/indexFile").build())
24+
.plugin(defaultCP().httpPath("/indexWild/*").build())
25+
.plugin(defaultFile().httpPath("/indexWildFile/*").build())
26+
.plugin(defaultCP().httpPath("/sus/").build())
27+
.plugin(defaultFile().httpPath("/susFile/*").build())
28+
.plugin(StaticContent.createCP("/logback.xml").httpPath("/single").build())
2929
.plugin(
30-
StaticContentService.createFile("src/test/resources/logback.xml")
31-
.httpPath("/singleFile"));
30+
StaticContent.createFile("src/test/resources/logback.xml")
31+
.httpPath("/singleFile").build());
3232

3333
return TestPair.create(app);
3434
}
3535

36-
private static StaticContentService defaultFile() {
37-
return StaticContentService.createFile("src/test/resources/public")
36+
private static StaticContent.Builder defaultFile() {
37+
return StaticContent.createFile("src/test/resources/public")
3838
.directoryIndex("index.html")
3939
.preCompress();
4040
}
4141

42-
private static StaticContentService defaultCP() {
43-
return StaticContentService.createCP("/public").directoryIndex("index.html").preCompress();
42+
private static StaticContent.Builder defaultCP() {
43+
return StaticContent.createCP("/public")
44+
.directoryIndex("index.html")
45+
.preCompress();
4446
}
4547

4648
@AfterAll

avaje-jex-static-content/src/test/java/io/avaje/jex/staticcontent/StaticFileTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,27 @@ static TestPair init() {
1919

2020
final Jex app =
2121
Jex.create()
22-
.plugin(defaultCP().httpPath("/index"))
23-
.plugin(defaultFile().httpPath("/indexFile"))
24-
.plugin(defaultCP().httpPath("/indexWild/*"))
25-
.plugin(defaultFile().httpPath("/indexWildFile/*"))
26-
.plugin(defaultCP().httpPath("/sus/"))
27-
.plugin(defaultFile().httpPath("/susFile/*"))
28-
.plugin(StaticContentService.createCP("/logback.xml").httpPath("/single"))
22+
.plugin(defaultCP().httpPath("/index").build())
23+
.plugin(defaultFile().httpPath("/indexFile").build())
24+
.plugin(defaultCP().httpPath("/indexWild/*").build())
25+
.plugin(defaultFile().httpPath("/indexWildFile/*").build())
26+
.plugin(defaultCP().httpPath("/sus/").build())
27+
.plugin(defaultFile().httpPath("/susFile/*").build())
28+
.plugin(StaticContent.createCP("/logback.xml").httpPath("/single").build())
2929
.plugin(
30-
StaticContentService.createFile("src/test/resources/logback.xml")
31-
.httpPath("/singleFile"));
30+
StaticContent.createFile("src/test/resources/logback.xml")
31+
.httpPath("/singleFile").build());
3232

3333
return TestPair.create(app);
3434
}
3535

36-
private static StaticContentService defaultFile() {
37-
return StaticContentService.createFile("src/test/resources/public")
36+
private static StaticContent.Builder defaultFile() {
37+
return StaticContent.createFile("src/test/resources/public")
3838
.directoryIndex("index.html");
3939
}
4040

41-
private static StaticContentService defaultCP() {
42-
return StaticContentService.createCP("/public").directoryIndex("index.html");
41+
private static StaticContent.Builder defaultCP() {
42+
return StaticContent.createCP("/public").directoryIndex("index.html");
4343
}
4444

4545
@AfterAll

avaje-jex/src/main/java/io/avaje/jex/Jex.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public sealed interface Jex permits DJex {
2929
* Create Jex.
3030
*
3131
* <pre>{@code
32+
*
3233
* final Jex.Server app = Jex.create()
3334
* .routing(routing -> routing
3435
* .get("/", ctx -> ctx.text("hello world"))

0 commit comments

Comments
 (0)