Skip to content

Commit d0b98de

Browse files
committed
asset handler should set content-length header (if possible) fix #29
1 parent c8af569 commit d0b98de

10 files changed

Lines changed: 56 additions & 14 deletions

File tree

jooby/src/main/java/org/jooby/Asset.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public interface Asset {
3838
*/
3939
@Nonnull String name();
4040

41+
/**
42+
* @return Asset size (in bytes) or <code>-1</code> if undefined.
43+
*/
44+
long length();
45+
4146
/**
4247
* @return The last modified date if possible or -1 when isn't.
4348
*/

jooby/src/main/java/org/jooby/Response.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public Response charset(final Charset charset) {
189189
}
190190

191191
@Override
192-
public Response length(final int length) {
192+
public Response length(final long length) {
193193
response.length(length);
194194
return this;
195195
}
@@ -586,7 +586,7 @@ default Response cookie(final @Nonnull Cookie.Definition cookie) {
586586
* @return This response.
587587
*/
588588
@Nonnull
589-
Response length(int length);
589+
Response length(long length);
590590

591591
/**
592592
* @return Get the response type.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public void handle(final Request req, final Response rsp, final Route.Chain chai
4848
}
4949
rsp.header("Last-Modified", new Date(lastModified));
5050
}
51+
long length = resource.length();
52+
if (length >= 0) {
53+
rsp.length(length);
54+
}
5155
rsp.type(resource.type());
5256
rsp.send(resource);
5357
}

jooby/src/main/java/org/jooby/internal/FileAsset.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public InputStream stream() throws IOException {
4949
return new FileInputStream(file);
5050
}
5151

52+
@Override
53+
public long length() {
54+
return file.length();
55+
}
56+
5257
@Override
5358
public long lastModified() {
5459
return file.lastModified();

jooby/src/main/java/org/jooby/internal/URLAsset.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.InputStream;
2525
import java.net.URL;
2626
import java.net.URLConnection;
27+
import java.util.Optional;
2728

2829
import org.jooby.Asset;
2930
import org.jooby.MediaType;
@@ -36,12 +37,17 @@ class URLAsset implements Asset {
3637

3738
private MediaType mediaType;
3839

39-
private long lastModified;
40+
private long lastModified = -1;
41+
42+
private long length = -1;
4043

4144
public URLAsset(final URL url, final MediaType mediaType) throws IOException {
4245
this.url = requireNonNull(url, "An url is required.");
4346
this.mediaType = requireNonNull(mediaType, "A mediaType is required.");
44-
this.lastModified = lastModified(url);
47+
Optional.ofNullable(lastModified(url)).ifPresent(md -> {
48+
this.length = md[0];
49+
this.lastModified = md[1];
50+
});
4551
}
4652

4753
@Override
@@ -51,6 +57,11 @@ public String name() {
5157
return path.substring(slash + 1);
5258
}
5359

60+
@Override
61+
public long length() {
62+
return length;
63+
}
64+
5465
@Override
5566
public InputStream stream() throws IOException {
5667
return url.openStream();
@@ -71,13 +82,13 @@ public String toString() {
7182
return name() + "(" + type() + ")";
7283
}
7384

74-
private static long lastModified(final URL resource) throws IOException {
85+
private static long[] lastModified(final URL resource) throws IOException {
7586
URLConnection uc = null;
7687
try {
7788
uc = resource.openConnection();
78-
return uc.getLastModified();
89+
return new long[]{uc.getContentLengthLong(), uc.getLastModified() };
7990
} catch (IOException ex) {
80-
return -1;
91+
return null;
8192
} finally {
8293
if (uc != null) {
8394
// http://stackoverflow.com/questions/2057351/how-do-i-get-the-last-modification-time-of

jooby/src/main/java/org/jooby/internal/undertow/UndertowResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public Response charset(final Charset charset) {
227227
}
228228

229229
@Override
230-
public Response length(final int length) {
230+
public Response length(final long length) {
231231
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, length);
232232
return this;
233233
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,7 @@ public void assets() throws Exception {
19311931
expect(rsp.header(eq("Last-Modified"), unit.capture(java.util.Date.class)))
19321932
.andReturn(rsp);
19331933
expect(rsp.type(MediaType.js)).andReturn(rsp);
1934+
expect(rsp.length(20)).andReturn(rsp);
19341935
rsp.send(unit.capture(Asset.class));
19351936
})
19361937
.run(unit -> {
@@ -2146,6 +2147,7 @@ public void staticFile() throws Exception {
21462147
expect(rsp.header(eq("Last-Modified"), unit.capture(java.util.Date.class)))
21472148
.andReturn(rsp);
21482149
expect(rsp.type(MediaType.js)).andReturn(rsp);
2150+
expect(rsp.length(20)).andReturn(rsp);
21492151
rsp.send(unit.capture(Asset.class));
21502152
})
21512153
.run(

jooby/src/test/java/org/jooby/ResponseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public Response charset(final Charset charset) {
100100
}
101101

102102
@Override
103-
public Response length(final int length) {
103+
public Response length(final long length) {
104104
throw new UnsupportedOperationException();
105105
}
106106

jooby/src/test/java/org/jooby/internal/FileAssetTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public void lastModified() {
3232
.lastModified() > 0);
3333
}
3434

35+
@Test
36+
public void length() throws IOException {
37+
assertEquals(15, new FileAsset(new File("src/test/resources/assets/file.js"),
38+
MediaType.js).length());
39+
}
40+
41+
3542
@Test
3643
public void type() {
3744
assertEquals(MediaType.js,

jooby/src/test/java/org/jooby/internal/URLAssetTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,28 @@ public void toStr() throws IOException {
3030

3131
@Test
3232
public void lastModified() throws IOException {
33-
assertTrue(
34-
new URLAsset(new File("src/test/resources/assets/file.js").toURI().toURL(), MediaType.js)
35-
.lastModified() > 0);
33+
assertTrue(new URLAsset(new File("src/test/resources/assets/file.js").toURI().toURL(),
34+
MediaType.js)
35+
.lastModified() > 0);
36+
}
37+
38+
@Test
39+
public void length() throws IOException {
40+
assertEquals(15, new URLAsset(new File("src/test/resources/assets/file.js").toURI().toURL(),
41+
MediaType.js).length());
3642
}
3743

3844
@Test
3945
public void type() throws IOException {
4046
assertEquals(MediaType.js,
41-
new URLAsset(new File("src/test/resources/assets/file.js").toURI().toURL(), MediaType.js).type());
47+
new URLAsset(new File("src/test/resources/assets/file.js").toURI().toURL(), MediaType.js)
48+
.type());
4249
}
4350

4451
@Test
4552
public void stream() throws IOException {
46-
InputStream stream = new URLAsset(new File("src/test/resources/assets/file.js").toURI().toURL(), MediaType.js)
53+
InputStream stream = new URLAsset(
54+
new File("src/test/resources/assets/file.js").toURI().toURL(), MediaType.js)
4755
.stream();
4856
assertEquals("function () {}\n", new String(ByteStreams.toByteArray(stream)));
4957
stream.close();

0 commit comments

Comments
 (0)