Skip to content

Commit

Permalink
router: change assets return type from Route to AssetHandler fix #3504
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Aug 15, 2024
1 parent 3c3215c commit a54321b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 57 deletions.
44 changes: 16 additions & 28 deletions docs/asciidoc/static-files.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,19 @@ control these headers programmatically:
[source, java, role="primary"]
----
{
AssetSource www = AssetSource.create(Paths.get("www"));
assets("/static/*", new AssetHandler(www)
assets("/static/*", Paths.get("www"))
.setLastModified(false)
.setEtag(false)
);
.setEtag(false);
}
----

.Kotlin
[source, kotlin, role="secondary"]
----
{
val www = AssetSource.create(Paths.get("www"))
assets("/static/*", AssetHandler(www)
assets("/static/*", Paths.get("www"))
.setLastModified(false)
.setEtag(false)
);
}
----

Expand All @@ -160,21 +156,17 @@ The `maxAge` option set a `Cache-Control` header:
[source, java, role="primary"]
----
{
AssetSource www = AssetSource.create(Paths.get("www"));
assets("/static/*", new AssetHandler(www)
assets("/static/*", Paths.get("www"))
.setMaxAge(Duration.ofDays(365))
);
}
----

.Kotlin
[source, kotlin, role="secondary"]
----
{
val www = AssetSource.create(Paths.get("www"))
assets("/static/*", AssetHandler(www)
assets("/static/*", Paths.get("www"))
.setMaxAge(Duration.ofDays(365))
);
}
----

Expand All @@ -188,8 +180,7 @@ specify a function via javadoc:AssetHandler[cacheControl, java.util.Function]:
[source, java, role="primary"]
----
{
AssetSource www = AssetSource.create(Paths.get("www"));
assets("/static/*", new AssetHandler(www)
assets("/static/*", Paths.get("www"))
.cacheControl(path -> {
if (path.endsWith("dont-cache-me.html")) {
return CacheControl.noCache(); // disable caching
Expand All @@ -200,16 +191,15 @@ specify a function via javadoc:AssetHandler[cacheControl, java.util.Function]:
} else {
return CacheControl.defaults(); // AssetHandler defaults
}
}));
});
}
----

.Kotlin
[source, kotlin, role="secondary"]
----
{
val www = AssetSource.create(Paths.get("www"))
assets("/static/*", AssetHandler(www)
assets("/static/*", Paths.get("www"))
.cacheControl {
when {
it.endsWith("dont-cache-me.html") -> CacheControl.noCache() // disable caching
Expand All @@ -218,7 +208,7 @@ specify a function via javadoc:AssetHandler[cacheControl, java.util.Function]:
.setMaxAge(Duration.ofDays(365))
else -> CacheControl.defaults() // AssetHandler defaults
}
})
}
}
----

Expand All @@ -230,11 +220,10 @@ an exception or generating any other content you want:
[source, java, role="primary"]
----
{
AssetSource www = AssetSource.create(Paths.get("www"));
assets("/static/*", new AssetHandler(www)
assets("/static/*", Paths.get("www"))
.notFound(ctx -> {
throw new MyAssetException();
}));
});
error(MyAssetException.class, (ctx, cause, code) -> {
// render MyAssetException as you want
Expand All @@ -246,13 +235,12 @@ an exception or generating any other content you want:
[source, kotlin, role="secondary"]
----
{
val www = AssetSource.create(Paths.get("www"))
assets("/static/*", AssetHandler(www)
.notFound { _ ->
assets("/static/*", Paths.get("www"))
.notFound { _ ->
throw MyAssetException()
})
error(MyAssetException::class) {
}
error(MyAssetException::class) {
// render MyAssetException as you want
}
}
}
----
13 changes: 7 additions & 6 deletions jooby/src/main/java/io/jooby/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ default Object execute(@NonNull Context context) {
* @param source File system directory.
* @return A route.
*/
default @NonNull Route assets(@NonNull String pattern, @NonNull Path source) {
default @NonNull AssetHandler assets(@NonNull String pattern, @NonNull Path source) {
return assets(pattern, AssetSource.create(source));
}

Expand All @@ -722,9 +722,9 @@ default Object execute(@NonNull Context context) {
*
* @param pattern Path pattern.
* @param source File-System folder when exists, or fallback to a classpath folder.
* @return A route.
* @return AssetHandler.
*/
default @NonNull Route assets(@NonNull String pattern, @NonNull String source) {
default @NonNull AssetHandler assets(@NonNull String pattern, @NonNull String source) {
Path path =
Stream.of(source.split("/"))
.reduce(Paths.get(System.getProperty("user.dir")), Path::resolve, Path::resolve);
Expand All @@ -742,7 +742,7 @@ default Object execute(@NonNull Context context) {
* @param sources additional Asset sources.
* @return A route.
*/
default @NonNull Route assets(
default @NonNull AssetHandler assets(
@NonNull String pattern, @NonNull AssetSource source, @NonNull AssetSource... sources) {
AssetSource[] allSources;
if (sources.length == 0) {
Expand All @@ -762,8 +762,9 @@ default Object execute(@NonNull Context context) {
* @param handler Asset handler.
* @return A route.
*/
default @NonNull Route assets(@NonNull String pattern, @NonNull AssetHandler handler) {
return route(GET, pattern, handler);
default @NonNull AssetHandler assets(@NonNull String pattern, @NonNull AssetHandler handler) {
route(GET, pattern, handler);
return handler;
}

/**
Expand Down
14 changes: 5 additions & 9 deletions tests/src/test/java/io/jooby/i3501/Issue3501.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

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

import io.jooby.handler.AssetHandler;
import io.jooby.handler.AssetSource;
import io.jooby.junit.ServerTest;
import io.jooby.junit.ServerTestRunner;

Expand All @@ -19,13 +17,11 @@ public void assetHandlerShouldGenerateCustom404Response(ServerTestRunner runner)
runner
.define(
app -> {
app.assets(
"/issue3501/*",
new AssetHandler(AssetSource.create(getClass().getClassLoader(), "/static"))
.notFound(
ctx -> {
throw new UnsupportedOperationException();
}));
app.assets("/issue3501/*", "/static")
.notFound(
ctx -> {
throw new UnsupportedOperationException();
});

app.error(
UnsupportedOperationException.class,
Expand Down
26 changes: 12 additions & 14 deletions tests/src/test/java/io/jooby/test/FeaturedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2625,7 +2625,7 @@ public void staticAssetsCaching(ServerTestRunner runner) {
runner
.define(
app -> {
app.assets("/www/?*", new AssetHandler(source).setNoCache());
app.assets("/www/?*", source).setNoCache();
})
.ready(
client -> {
Expand All @@ -2642,19 +2642,17 @@ public void staticAssetsCaching(ServerTestRunner runner) {
runner
.define(
app -> {
app.assets(
"/www/?*",
new AssetHandler(source)
.cacheControl(
path -> {
if (path.endsWith("about.html")) {
return CacheControl.noCache();
} else if (path.equals("foo.js")) {
return CacheControl.defaults().setETag(false);
} else {
return CacheControl.defaults();
}
}));
app.assets("/www/?*", source)
.cacheControl(
path -> {
if (path.endsWith("about.html")) {
return CacheControl.noCache();
} else if (path.equals("foo.js")) {
return CacheControl.defaults().setETag(false);
} else {
return CacheControl.defaults();
}
});
})
.ready(
client -> {
Expand Down

0 comments on commit a54321b

Please sign in to comment.