Skip to content

Commit ff0b616

Browse files
committed
small refactor/improvements
* make AssetHandler public * make some of the internal handlers to public package * get rid of CdnHandler, code was merge it inside AssetHandler * allow to turn off etag support * add Asset.resource() which has access to the URL/asset
1 parent d039d46 commit ff0b616

30 files changed

Lines changed: 600 additions & 269 deletions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.jooby;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
import com.typesafe.config.ConfigFactory;
7+
import com.typesafe.config.ConfigValueFactory;
8+
9+
public class AssetETagOffFeature extends ServerFeature {
10+
11+
{
12+
use(ConfigFactory.empty().withValue("assets.etag", ConfigValueFactory.fromAnyRef(false)));
13+
assets("/assets/**");
14+
}
15+
16+
@Test
17+
public void etag() throws Exception {
18+
request()
19+
.get("/assets/file.js")
20+
.expect(200)
21+
.header("Content-Type", "application/javascript;charset=UTF-8")
22+
.header("Content-Length", 15)
23+
.header("ETag", (String) null);
24+
}
25+
26+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.jooby;
2+
3+
import org.jooby.handlers.AssetHandler;
4+
import org.jooby.test.ServerFeature;
5+
import org.junit.Test;
6+
7+
public class AssetHandlerFeature extends ServerFeature {
8+
9+
{
10+
assets("/assets/**", new AssetHandler("/", getClass()));
11+
}
12+
13+
@Test
14+
public void jsAsset() throws Exception {
15+
request()
16+
.get("/assets/file.js")
17+
.expect(200)
18+
.header("Content-Type", "application/javascript;charset=UTF-8")
19+
.header("Content-Length", 15)
20+
.header("Last-Modified", lastModified -> {
21+
request()
22+
.get("/assets/file.js")
23+
.header("If-Modified-Since", lastModified)
24+
.expect(304)
25+
.empty();
26+
});
27+
}
28+
29+
@Test
30+
public void cssAsset() throws Exception {
31+
request()
32+
.get("/assets/file.css")
33+
.expect(200)
34+
.header("Content-Type", "text/css;charset=UTF-8")
35+
.header("Content-Length", 22)
36+
.header("Last-Modified", lastModified -> {
37+
request()
38+
.get("/assets/file.css")
39+
.header("If-Modified-Since", lastModified)
40+
.expect(304)
41+
.empty();
42+
});
43+
}
44+
45+
@Test
46+
public void imageAsset() throws Exception {
47+
request()
48+
.get("/assets/favicon.ico")
49+
.expect(200)
50+
.header("Content-Type", "image/x-icon")
51+
.header("Content-Length", 2238)
52+
.header("Last-Modified", lastModified -> {
53+
request()
54+
.get("/assets/favicon.ico")
55+
.header("If-Modified-Since", lastModified)
56+
.expect(304)
57+
.empty();
58+
});
59+
}
60+
61+
@Test
62+
public void emptyFile() throws Exception {
63+
request()
64+
.get("/assets/empty.css")
65+
.expect(200)
66+
.header("Content-Type", "text/css;charset=UTF-8")
67+
.header("Content-Length", 0)
68+
.header("Last-Modified", lastModified -> {
69+
request()
70+
.get("/assets/empty.css")
71+
.header("If-Modified-Since", lastModified)
72+
.expect(304)
73+
.empty();
74+
});
75+
}
76+
77+
@Test
78+
public void largeFile() throws Exception {
79+
request()
80+
.get("/assets/jquery-2.1.4.js")
81+
.expect(200)
82+
.header("Content-Type", "application/javascript;charset=UTF-8")
83+
.header("Content-Length", 247597)
84+
.header("Last-Modified", lastModified -> {
85+
request()
86+
.get("/assets/file.js")
87+
.header("If-Modified-Since", lastModified)
88+
.expect(304)
89+
.empty();
90+
});
91+
}
92+
93+
}

‎coverage-report/src/test/java/org/jooby/CorsCustomFeature.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
import java.util.Optional;
44

5+
import org.jooby.handlers.Cors;
6+
import org.jooby.handlers.CorsHandler;
57
import org.jooby.test.ServerFeature;
68
import org.junit.Test;
79

810
public class CorsCustomFeature extends ServerFeature {
911

1012
{
11-
cors(new Cors().withOrigin("http://foo.com")
13+
use("*", new CorsHandler(new Cors()
14+
.withOrigin("http://foo.com")
1215
.withHeaders("*")
1316
.withoutCreds()
1417
.withExposedHeaders("H1")
15-
.withMaxAge(-1));
18+
.withMaxAge(-1)));
1619

1720
get("/greeting", req -> "Hello " + req.param("name").value("World") + "!");
1821
}

‎coverage-report/src/test/java/org/jooby/CorsDisabledFeature.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22

33
import java.util.Optional;
44

5+
import org.jooby.handlers.Cors;
6+
import org.jooby.handlers.CorsHandler;
57
import org.jooby.test.ServerFeature;
68
import org.junit.Test;
79

810
public class CorsDisabledFeature extends ServerFeature {
911

1012
{
11-
cors(new Cors().withOrigin("http://foo.com")
12-
.withHeaders("*")
13-
.withoutCreds()
14-
.withExposedHeaders("H1")
15-
.withMaxAge(-1)
16-
.disabled());
13+
use("*", new CorsHandler(
14+
new Cors().withOrigin("http://foo.com")
15+
.withHeaders("*")
16+
.withoutCreds()
17+
.withExposedHeaders("H1")
18+
.withMaxAge(-1)
19+
.disabled()));
1720

1821
get("/greeting", req -> "Hello " + req.param("name").value("World") + "!");
1922
}

‎coverage-report/src/test/java/org/jooby/CorsFeature.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import java.util.Optional;
44

5+
import org.jooby.handlers.CorsHandler;
56
import org.jooby.test.ServerFeature;
67
import org.junit.Test;
78

89
public class CorsFeature extends ServerFeature {
910

1011
{
11-
cors();
12+
use("*", new CorsHandler());
1213

1314
get("/greeting", req -> "Hello " + req.param("name").value("World") + "!");
1415
}

‎coverage-report/src/test/java/org/jooby/CsrfFeature.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import static org.junit.Assert.assertEquals;
44

5+
import org.jooby.handlers.CsrfHandler;
56
import org.jooby.test.ServerFeature;
67
import org.junit.Test;
78

89
public class CsrfFeature extends ServerFeature {
910

1011
{
11-
use("*", new Csrf());
12+
use("*", new CsrfHandler());
1213

1314
get("/csrf", req -> req.get("csrf").get());
1415

‎coverage-report/src/test/java/org/jooby/issues/Issue169.java‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ public class Issue169 extends ServerFeature {
1010

1111
{
1212

13-
use (new Jackson());
13+
assets("/assets/**");
1414

15-
get("/file", (req, rsp)-> {
15+
use(new Jackson());
16+
17+
get("/file", (req, rsp) -> {
1618
rsp.download("file.txt", new ByteArrayInputStream("hello".getBytes()));
1719
});
1820

@@ -21,9 +23,22 @@ public class Issue169 extends ServerFeature {
2123
@Test
2224
public void shouldDownloadAFile() throws Exception {
2325
request()
24-
.get("/file")
25-
.expect(200)
26-
.header("Content-Length", 5)
27-
.header("Content-Disposition", "attachment; filename=\"file.txt\"; filename*=utf-8''file.txt");
26+
.get("/file")
27+
.expect(200)
28+
.header("Content-Length", 5)
29+
.header("Content-Disposition",
30+
"attachment; filename=\"file.txt\"; filename*=utf-8''file.txt");
31+
}
32+
33+
@Test
34+
public void shouldGetAnAsset() throws Exception {
35+
request()
36+
.get("/assets/file.css")
37+
.expect("html {\n" +
38+
" border: 0;\n" +
39+
"}\n")
40+
.expect(200)
41+
.header("Content-Type", "text/css;charset=utf-8");
2842
}
43+
2944
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.jooby;
2020

2121
import java.io.InputStream;
22+
import java.net.URL;
2223

2324
import com.google.common.io.BaseEncoding;
2425
import com.google.common.primitives.Longs;
@@ -57,6 +58,11 @@ public interface Asset {
5758
*/
5859
String path();
5960

61+
/**
62+
* @return URL representing the resource.
63+
*/
64+
URL resource();
65+
6066
/**
6167
* @return Generate a weak Etag using the {@link #path()}, {@link #lastModified()} and
6268
* {@link #length()}.
@@ -66,7 +72,7 @@ default String etag() {
6672
b.append("W/\"");
6773

6874
BaseEncoding b64 = BaseEncoding.base64();
69-
int lhash = path().hashCode();
75+
int lhash = resource().hashCode();
7076

7177
b.append(b64.encode(Longs.toByteArray(lastModified() ^ lhash)));
7278
b.append(b64.encode(Longs.toByteArray(length() ^ lhash)));

0 commit comments

Comments
 (0)