Skip to content

Commit 23ada63

Browse files
committed
assets handler: automatically configure on startup fix jooby-project#512
1 parent e1bb249 commit 23ada63

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Issue356 extends ServerFeature {
2323
.etag(true).lastModified(false).maxAge(Duration.ofDays(2)));
2424

2525
assets("/assets/empty.css", new AssetHandler("/")
26-
.etag(true).lastModified(true).maxAge(Duration.ofDays(7)));
26+
.etag(true).lastModified(true).maxAge("7d"));
2727
}
2828

2929
@Test

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
*/
1919
package org.jooby.assets;
2020

21-
import java.time.Duration;
22-
import java.util.concurrent.TimeUnit;
23-
2421
import org.jooby.Env;
2522
import org.jooby.Jooby;
2623
import org.jooby.Router;
@@ -333,10 +330,7 @@ public void configure(final Env env, final Config config, final Binder binder) {
333330
.cdn(conf.getString("assets.cdn"))
334331
.lastModified(conf.getBoolean("assets.lastModified"));
335332

336-
if (conf.hasPath("assets.cache.maxAge")) {
337-
handler.maxAge(Duration
338-
.ofSeconds(conf.getDuration("assets.cache.maxAge", TimeUnit.SECONDS)));
339-
}
333+
handler.maxAge(conf.getString("assets.cache.maxAge"));
340334

341335
compiler.patterns().forEach(pattern -> routes.get(pattern, handler));
342336

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public class AssetsTest {
4141
public void configure() throws Exception {
4242
Config conf = ConfigFactory.empty()
4343
.withValue("application.path", ConfigValueFactory.fromAnyRef("/path"))
44-
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(false));
44+
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(false))
45+
.withValue("assets.cache.maxAge", ConfigValueFactory.fromAnyRef(-1));
4546
new MockUnit(Env.class, Binder.class, Request.class, Response.class,
4647
Route.Chain.class).expect(unit -> {
4748
AssetCompiler compiler = unit.constructor(AssetCompiler.class)
@@ -95,7 +96,8 @@ public void configure() throws Exception {
9596
public void configureWithWatch() throws Exception {
9697
Config conf = ConfigFactory.empty()
9798
.withValue("application.path", ConfigValueFactory.fromAnyRef("/"))
98-
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(true));
99+
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(true))
100+
.withValue("assets.cache.maxAge", ConfigValueFactory.fromAnyRef(-1));
99101
new MockUnit(Env.class, Binder.class, Request.class, Response.class,
100102
Route.Chain.class).expect(unit -> {
101103
AssetCompiler compiler = unit.constructor(AssetCompiler.class)
@@ -161,7 +163,8 @@ public void configureWithWatch() throws Exception {
161163
public void configureWithoutWatch() throws Exception {
162164
Config conf = ConfigFactory.empty()
163165
.withValue("application.path", ConfigValueFactory.fromAnyRef("/"))
164-
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(false));
166+
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(false))
167+
.withValue("assets.cache.maxAge", ConfigValueFactory.fromAnyRef(-1));
165168
new MockUnit(Env.class, Binder.class, Request.class, Response.class,
166169
Route.Chain.class).expect(unit -> {
167170
AssetCompiler compiler = unit.constructor(AssetCompiler.class)

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,8 @@ public Route.Definition assets(final String path, final String location) {
17121712
handler
17131713
.cdn(conf.getString("assets.cdn"))
17141714
.lastModified(conf.getBoolean("assets.lastModified"))
1715-
.etag(conf.getBoolean("assets.etag"));
1715+
.etag(conf.getBoolean("assets.etag"))
1716+
.maxAge(conf.getString("assets.cache.maxAge"));
17161717
});
17171718
return assets(path, handler);
17181719
}

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.time.Duration;
2929
import java.util.Date;
3030
import java.util.Map;
31-
import java.util.Optional;
3231

3332
import org.jooby.Asset;
3433
import org.jooby.Jooby;
@@ -40,9 +39,12 @@
4039
import org.jooby.internal.URLAsset;
4140

4241
import com.google.common.base.Strings;
42+
import com.typesafe.config.ConfigFactory;
43+
import com.typesafe.config.ConfigValueFactory;
4344

4445
import javaslang.Function1;
4546
import javaslang.Function2;
47+
import javaslang.control.Try;
4648

4749
/**
4850
* Serve static resources, via {@link Jooby#assets(String)} or variants.
@@ -94,7 +96,7 @@ public class AssetHandler implements Route.Handler {
9496

9597
private boolean etag = true;
9698

97-
private Optional<Duration> maxAge = Optional.empty();
99+
private long maxAge = -1;
98100

99101
private boolean lastModified = true;
100102

@@ -191,16 +193,33 @@ public AssetHandler cdn(final String cdn) {
191193
* @return This handler.
192194
*/
193195
public AssetHandler maxAge(final Duration maxAge) {
194-
this.maxAge = Optional.of(maxAge);
195-
return this;
196+
return maxAge(maxAge.getSeconds());
196197
}
197198

198199
/**
199200
* @param maxAge Set the cache header max-age value in seconds.
200201
* @return This handler.
201202
*/
202203
public AssetHandler maxAge(final long maxAge) {
203-
return maxAge(Duration.ofSeconds(maxAge));
204+
this.maxAge = maxAge;
205+
return this;
206+
}
207+
208+
/**
209+
* Parse value as {@link Duration}. If the value is already a number then it uses as seconds.
210+
* Otherwise, it parse expressions like: 8m, 1h, 365d, etc...
211+
*
212+
* @param maxAge Set the cache header max-age value in seconds.
213+
* @return This handler.
214+
*/
215+
public AssetHandler maxAge(final String maxAge) {
216+
Try.of(() -> Long.parseLong(maxAge))
217+
.recover(x -> ConfigFactory.empty()
218+
.withValue("v", ConfigValueFactory.fromAnyRef(maxAge))
219+
.getDuration("v")
220+
.getSeconds())
221+
.onSuccess(this::maxAge);
222+
return this;
204223
}
205224

206225
@Override
@@ -263,9 +282,9 @@ private void doHandle(final Request req, final Response rsp, final Asset asset)
263282
}
264283

265284
// cache max-age
266-
maxAge.ifPresent(d -> {
267-
rsp.header("Cache-Control", "max-age=" + d.getSeconds());
268-
});
285+
if (maxAge > 0) {
286+
rsp.header("Cache-Control", "max-age=" + maxAge);
287+
}
269288

270289
send(req, rsp, asset);
271290
}

jooby/src/test/java/org/jooby/JoobyTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,7 @@ public void assets() throws Exception {
20912091
expect(conf.getString("assets.cdn")).andReturn("").times(2);
20922092
expect(conf.getBoolean("assets.lastModified")).andReturn(true).times(2);
20932093
expect(conf.getBoolean("assets.etag")).andReturn(true).times(2);
2094+
expect(conf.getString("assets.cache.maxAge")).andReturn("-1").times(2);
20942095

20952096
Injector injector = unit.get(Injector.class);
20962097
expect(injector.getInstance(Key.get(Config.class))).andReturn(conf).times(2);

0 commit comments

Comments
 (0)