Skip to content

Commit a54321b

Browse files
committed
router: change assets return type from Route to AssetHandler fix #3504
1 parent 3c3215c commit a54321b

File tree

4 files changed

+40
-57
lines changed

4 files changed

+40
-57
lines changed

docs/asciidoc/static-files.adoc

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,19 @@ control these headers programmatically:
134134
[source, java, role="primary"]
135135
----
136136
{
137-
AssetSource www = AssetSource.create(Paths.get("www"));
138-
assets("/static/*", new AssetHandler(www)
137+
assets("/static/*", Paths.get("www"))
139138
.setLastModified(false)
140-
.setEtag(false)
141-
);
139+
.setEtag(false);
142140
}
143141
----
144142

145143
.Kotlin
146144
[source, kotlin, role="secondary"]
147145
----
148146
{
149-
val www = AssetSource.create(Paths.get("www"))
150-
assets("/static/*", AssetHandler(www)
147+
assets("/static/*", Paths.get("www"))
151148
.setLastModified(false)
152149
.setEtag(false)
153-
);
154150
}
155151
----
156152

@@ -160,21 +156,17 @@ The `maxAge` option set a `Cache-Control` header:
160156
[source, java, role="primary"]
161157
----
162158
{
163-
AssetSource www = AssetSource.create(Paths.get("www"));
164-
assets("/static/*", new AssetHandler(www)
159+
assets("/static/*", Paths.get("www"))
165160
.setMaxAge(Duration.ofDays(365))
166-
);
167161
}
168162
----
169163

170164
.Kotlin
171165
[source, kotlin, role="secondary"]
172166
----
173167
{
174-
val www = AssetSource.create(Paths.get("www"))
175-
assets("/static/*", AssetHandler(www)
168+
assets("/static/*", Paths.get("www"))
176169
.setMaxAge(Duration.ofDays(365))
177-
);
178170
}
179171
----
180172

@@ -188,8 +180,7 @@ specify a function via javadoc:AssetHandler[cacheControl, java.util.Function]:
188180
[source, java, role="primary"]
189181
----
190182
{
191-
AssetSource www = AssetSource.create(Paths.get("www"));
192-
assets("/static/*", new AssetHandler(www)
183+
assets("/static/*", Paths.get("www"))
193184
.cacheControl(path -> {
194185
if (path.endsWith("dont-cache-me.html")) {
195186
return CacheControl.noCache(); // disable caching
@@ -200,16 +191,15 @@ specify a function via javadoc:AssetHandler[cacheControl, java.util.Function]:
200191
} else {
201192
return CacheControl.defaults(); // AssetHandler defaults
202193
}
203-
}));
194+
});
204195
}
205196
----
206197

207198
.Kotlin
208199
[source, kotlin, role="secondary"]
209200
----
210201
{
211-
val www = AssetSource.create(Paths.get("www"))
212-
assets("/static/*", AssetHandler(www)
202+
assets("/static/*", Paths.get("www"))
213203
.cacheControl {
214204
when {
215205
it.endsWith("dont-cache-me.html") -> CacheControl.noCache() // disable caching
@@ -218,7 +208,7 @@ specify a function via javadoc:AssetHandler[cacheControl, java.util.Function]:
218208
.setMaxAge(Duration.ofDays(365))
219209
else -> CacheControl.defaults() // AssetHandler defaults
220210
}
221-
})
211+
}
222212
}
223213
----
224214

@@ -230,11 +220,10 @@ an exception or generating any other content you want:
230220
[source, java, role="primary"]
231221
----
232222
{
233-
AssetSource www = AssetSource.create(Paths.get("www"));
234-
assets("/static/*", new AssetHandler(www)
223+
assets("/static/*", Paths.get("www"))
235224
.notFound(ctx -> {
236225
throw new MyAssetException();
237-
}));
226+
});
238227
239228
error(MyAssetException.class, (ctx, cause, code) -> {
240229
// render MyAssetException as you want
@@ -246,13 +235,12 @@ an exception or generating any other content you want:
246235
[source, kotlin, role="secondary"]
247236
----
248237
{
249-
val www = AssetSource.create(Paths.get("www"))
250-
assets("/static/*", AssetHandler(www)
251-
.notFound { _ ->
238+
assets("/static/*", Paths.get("www"))
239+
.notFound { _ ->
252240
throw MyAssetException()
253-
})
254-
error(MyAssetException::class) {
241+
}
242+
error(MyAssetException::class) {
255243
// render MyAssetException as you want
256-
}
244+
}
257245
}
258246
----

jooby/src/main/java/io/jooby/Router.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ default Object execute(@NonNull Context context) {
708708
* @param source File system directory.
709709
* @return A route.
710710
*/
711-
default @NonNull Route assets(@NonNull String pattern, @NonNull Path source) {
711+
default @NonNull AssetHandler assets(@NonNull String pattern, @NonNull Path source) {
712712
return assets(pattern, AssetSource.create(source));
713713
}
714714

@@ -722,9 +722,9 @@ default Object execute(@NonNull Context context) {
722722
*
723723
* @param pattern Path pattern.
724724
* @param source File-System folder when exists, or fallback to a classpath folder.
725-
* @return A route.
725+
* @return AssetHandler.
726726
*/
727-
default @NonNull Route assets(@NonNull String pattern, @NonNull String source) {
727+
default @NonNull AssetHandler assets(@NonNull String pattern, @NonNull String source) {
728728
Path path =
729729
Stream.of(source.split("/"))
730730
.reduce(Paths.get(System.getProperty("user.dir")), Path::resolve, Path::resolve);
@@ -742,7 +742,7 @@ default Object execute(@NonNull Context context) {
742742
* @param sources additional Asset sources.
743743
* @return A route.
744744
*/
745-
default @NonNull Route assets(
745+
default @NonNull AssetHandler assets(
746746
@NonNull String pattern, @NonNull AssetSource source, @NonNull AssetSource... sources) {
747747
AssetSource[] allSources;
748748
if (sources.length == 0) {
@@ -762,8 +762,9 @@ default Object execute(@NonNull Context context) {
762762
* @param handler Asset handler.
763763
* @return A route.
764764
*/
765-
default @NonNull Route assets(@NonNull String pattern, @NonNull AssetHandler handler) {
766-
return route(GET, pattern, handler);
765+
default @NonNull AssetHandler assets(@NonNull String pattern, @NonNull AssetHandler handler) {
766+
route(GET, pattern, handler);
767+
return handler;
767768
}
768769

769770
/**

tests/src/test/java/io/jooby/i3501/Issue3501.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import static org.junit.jupiter.api.Assertions.assertEquals;
99

10-
import io.jooby.handler.AssetHandler;
11-
import io.jooby.handler.AssetSource;
1210
import io.jooby.junit.ServerTest;
1311
import io.jooby.junit.ServerTestRunner;
1412

@@ -19,13 +17,11 @@ public void assetHandlerShouldGenerateCustom404Response(ServerTestRunner runner)
1917
runner
2018
.define(
2119
app -> {
22-
app.assets(
23-
"/issue3501/*",
24-
new AssetHandler(AssetSource.create(getClass().getClassLoader(), "/static"))
25-
.notFound(
26-
ctx -> {
27-
throw new UnsupportedOperationException();
28-
}));
20+
app.assets("/issue3501/*", "/static")
21+
.notFound(
22+
ctx -> {
23+
throw new UnsupportedOperationException();
24+
});
2925

3026
app.error(
3127
UnsupportedOperationException.class,

tests/src/test/java/io/jooby/test/FeaturedTest.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,7 @@ public void staticAssetsCaching(ServerTestRunner runner) {
26252625
runner
26262626
.define(
26272627
app -> {
2628-
app.assets("/www/?*", new AssetHandler(source).setNoCache());
2628+
app.assets("/www/?*", source).setNoCache();
26292629
})
26302630
.ready(
26312631
client -> {
@@ -2642,19 +2642,17 @@ public void staticAssetsCaching(ServerTestRunner runner) {
26422642
runner
26432643
.define(
26442644
app -> {
2645-
app.assets(
2646-
"/www/?*",
2647-
new AssetHandler(source)
2648-
.cacheControl(
2649-
path -> {
2650-
if (path.endsWith("about.html")) {
2651-
return CacheControl.noCache();
2652-
} else if (path.equals("foo.js")) {
2653-
return CacheControl.defaults().setETag(false);
2654-
} else {
2655-
return CacheControl.defaults();
2656-
}
2657-
}));
2645+
app.assets("/www/?*", source)
2646+
.cacheControl(
2647+
path -> {
2648+
if (path.endsWith("about.html")) {
2649+
return CacheControl.noCache();
2650+
} else if (path.equals("foo.js")) {
2651+
return CacheControl.defaults().setETag(false);
2652+
} else {
2653+
return CacheControl.defaults();
2654+
}
2655+
});
26582656
})
26592657
.ready(
26602658
client -> {

0 commit comments

Comments
 (0)