Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit e3a7648

Browse files
committed
Change maxAge from long to Duration in AssetHandler
Issue jooby-project#356
1 parent d0083c4 commit e3a7648

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

coverage-report/src/test/java/org/jooby/issues/Issue356.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.jooby.issues;
22

3+
import java.time.Duration;
4+
35
import org.jooby.handlers.AssetHandler;
46
import org.jooby.test.ServerFeature;
57

@@ -13,16 +15,16 @@
1315
public class Issue356 extends ServerFeature {
1416
{
1517
assets("/assets/file.css", new AssetHandler("/")
16-
.etag(false).lastModified(false).maxAge(86400));
18+
.etag(false).lastModified(false).maxAge(Duration.ofDays(1)));
1719

1820
assets("/assets/favicon.ico", new AssetHandler("/")
19-
.etag(false).lastModified(true).maxAge(1209600));
21+
.etag(false).lastModified(true).maxAge(Duration.ofDays(14)));
2022

2123
assets("/assets/file.js", new AssetHandler("/")
22-
.etag(true).lastModified(false).maxAge(172800));
24+
.etag(true).lastModified(false).maxAge(Duration.ofDays(2)));
2325

2426
assets("/assets/empty.css", new AssetHandler("/")
25-
.etag(true).lastModified(true).maxAge(604800));
27+
.etag(true).lastModified(true).maxAge(Duration.ofDays(7)));
2628
}
2729

2830
@Test

jooby-assets/src/main/java/org/jooby/assets/Assets.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*/
1919
package org.jooby.assets;
2020

21-
import java.util.concurrent.TimeUnit;
21+
import java.time.Duration;
22+
import java.util.Optional;
2223

2324
import org.jooby.Env;
2425
import org.jooby.Jooby;
@@ -266,8 +267,11 @@ public void configure(final Env env, final Config config, final Binder binder) {
266267
: new AssetHandler("/")
267268
.etag(conf.getBoolean("assets.etag"))
268269
.cdn(conf.getString("assets.cdn"))
269-
.lastModified(conf.getBoolean("assets.lastModified"))
270-
.maxAge(conf.getDuration("assets.cache.maxAge", TimeUnit.SECONDS));
270+
.lastModified(conf.getBoolean("assets.lastModified"));
271+
272+
if (conf.hasPath("assets.cache.maxAge")) {
273+
handler.maxAge(Duration.parse(conf.getString("assets.cache.maxAge")));
274+
}
271275

272276
compiler.patterns().forEach(
273277
pattern -> routes.addBinding()

jooby-assets/src/test/java/org/jooby/assets/AssetsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void configuredist() throws Exception {
182182
.withValue("assets.etag", ConfigValueFactory.fromAnyRef(true))
183183
.withValue("application.path", ConfigValueFactory.fromAnyRef("/"))
184184
.withValue("assets.cdn", ConfigValueFactory.fromAnyRef(""))
185-
.withValue("assets.cache.maxAge", ConfigValueFactory.fromAnyRef("365d"))
185+
.withValue("assets.cache.maxAge", ConfigValueFactory.fromAnyRef("P365D"))
186186
.withValue("assets.lastModified", ConfigValueFactory.fromAnyRef(true))
187187
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(false));
188188
new MockUnit(Env.class, Config.class, Binder.class, Request.class, Response.class,

jooby/src/main/java/org/jooby/handlers/AssetHandler.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import java.net.URL;
2626
import java.net.URLClassLoader;
2727
import java.text.MessageFormat;
28+
import java.time.Duration;
2829
import java.util.Date;
2930
import java.util.Map;
31+
import java.util.Optional;
3032

3133
import org.jooby.Asset;
3234
import org.jooby.Jooby;
@@ -92,7 +94,7 @@ public class AssetHandler implements Route.Handler {
9294

9395
private boolean etag = true;
9496

95-
private long maxAge = -1;
97+
private Optional<Duration> maxAgeOpt = Optional.empty();
9698

9799
private boolean lastModified = true;
98100

@@ -184,12 +186,21 @@ public AssetHandler cdn(final String cdn) {
184186
return this;
185187
}
186188

189+
/**
190+
* @param maxAge Set the cache header max-age value.
191+
* @return This handler.
192+
*/
193+
public AssetHandler maxAge(final Duration maxAge) {
194+
this.maxAgeOpt = Optional.of(maxAge);
195+
return this;
196+
}
197+
187198
/**
188199
* @param maxAge Set the cache header max-age value in seconds.
189200
* @return This handler.
190201
*/
191202
public AssetHandler maxAge(final long maxAge) {
192-
this.maxAge = maxAge;
203+
this.maxAgeOpt = Optional.of(Duration.ofSeconds(maxAge));
193204
return this;
194205
}
195206

@@ -253,9 +264,9 @@ private void doHandle(final Request req, final Response rsp, final Asset asset)
253264
}
254265

255266
// cache max-age
256-
if (maxAge > 0) {
257-
rsp.header("Cache-Control", "max-age=" + maxAge);
258-
}
267+
maxAgeOpt.ifPresent(d -> {
268+
rsp.header("Cache-Control", "max-age=" + d.getSeconds());
269+
});
259270

260271
send(req, rsp, asset);
261272
}

jooby/src/main/resources/org/jooby/jooby.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ assets {
153153

154154
charset = ${application.charset}
155155

156-
cache.maxAge = -1
156+
# Either null or a string in the ISO 8601 format according to `java.time.Duration.parse(CharSequence)`.
157+
cache.maxAge = null
157158

158159
}
159160

0 commit comments

Comments
 (0)