Skip to content

Commit cc8e2df

Browse files
committed
Add favicon, update archetype, add static file route and fix unit test
1 parent b050bac commit cc8e2df

8 files changed

Lines changed: 84 additions & 29 deletions

File tree

‎jooby-archetype/src/main/resources/archetype-resources/public/welcome.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<html>
22
<head>
33
<title>${artifactId}</title>
4+
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
45
<link rel="stylesheet" href="assets/css/style.css">
56
</head>
67
<body>

‎jooby-archetype/src/main/resources/archetype-resources/src/main/java/App.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
public class App extends Jooby {
99

1010
{
11+
get("/favicon.ico");
12+
1113
assets("/assets/**");
1214

1315
get("/", file("welcome.html"));

‎jooby-tests/src/test/java/org/jooby/DateTimeFormatterFeature.java‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.time.ZoneId;
88
import java.time.format.DateTimeFormatter;
99
import java.util.Date;
10+
import java.util.TimeZone;
1011

1112
import javax.inject.Inject;
1213
import javax.inject.Named;
@@ -31,19 +32,25 @@ public static class Resource {
3132

3233
private String dateFormat;
3334

35+
private ZoneId zoneId;
36+
3437
@Inject
3538
public Resource(final DateTimeFormatter formatter, final ZoneId zoneId,
3639
@Named("application.dateFormat") final String dateFormat) {
37-
assertEquals(ZoneId.of("America/Argentina/Buenos_Aires"), zoneId);
40+
assertEquals(ZoneId.of("GMT"), zoneId);
41+
assertEquals(zoneId, formatter.getZone());
3842
this.formatter = requireNonNull(formatter, "def formatter is required.");
43+
this.zoneId = zoneId;
3944
this.dateFormat = requireNonNull(dateFormat, "The dateFormat is required.");
4045
}
4146

4247
@GET
4348
@Path("/")
4449
public String formatter(final long time, final org.jooby.Request req) {
4550
Date date = new Date(time);
46-
String sdate = new SimpleDateFormat(dateFormat, req.locale()).format(date);
51+
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, req.locale());
52+
sdf.setTimeZone(TimeZone.getTimeZone(zoneId));
53+
String sdate = sdf.format(date);
4754
String newsdate = formatter.format(formatter.parse(sdate));
4855
return sdate + "|" + newsdate;
4956
}
@@ -53,15 +60,14 @@ public String formatter(final long time, final org.jooby.Request req) {
5360
use(ConfigFactory
5461
.empty()
5562
.withValue("application.lang", ConfigValueFactory.fromAnyRef("en_US"))
56-
.withValue("application.tz",
57-
ConfigValueFactory.fromAnyRef("America/Argentina/Buenos_Aires")));
63+
.withValue("application.tz", ConfigValueFactory.fromAnyRef("GMT")));
5864
use(Resource.class);
5965
}
6066

6167
@Test
6268
public void dateFormat() throws Exception {
6369
long time = 1412824189989l;
64-
assertEquals("10/9/14 12:09 AM|10/9/14 12:09 AM",
70+
assertEquals("10/9/14 3:09 AM|10/9/14 3:09 AM",
6571
execute(GET(uri("/?time=" + time)), (response) -> {
6672
assertEquals(200, response.getStatusLine().getStatusCode());
6773
}));

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

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static java.util.Objects.requireNonNull;
2222

2323
import java.io.File;
24-
import java.io.InputStream;
2524
import java.lang.reflect.Type;
2625
import java.nio.charset.Charset;
2726
import java.text.DecimalFormat;
@@ -44,6 +43,7 @@
4443
import org.jooby.internal.AssetFormatter;
4544
import org.jooby.internal.AssetRoute;
4645
import org.jooby.internal.FallbackBodyConverter;
46+
import org.jooby.internal.RoutePattern;
4747
import org.jooby.internal.Server;
4848
import org.jooby.internal.TypeConverters;
4949
import org.jooby.internal.jetty.Jetty;
@@ -565,6 +565,27 @@ public abstract void configure(@Nonnull Mode mode, @Nonnull Config config,
565565
return handler(new Route.Definition("*", path, handler));
566566
}
567567

568+
/**
569+
* Serve a static file from classpath:
570+
*
571+
* <pre>
572+
* get("/favicon.ico");
573+
* </pre>
574+
*
575+
* This method is a shorcut for:
576+
*
577+
* <pre>
578+
* get("/favicon.ico", file("/favicon.ico");
579+
* </pre>
580+
*
581+
* @param path A path pattern.
582+
* @param handler A handler to execute.
583+
* @return A new route definition.
584+
*/
585+
public @Nonnull Route.Definition get(final String path) {
586+
return handler(new Route.Definition("GET", path, file(path)));
587+
}
588+
568589
/**
569590
* Define an in-line route that supports HTTP GET method:
570591
*
@@ -1145,20 +1166,52 @@ public Route.Handler redirect(final String location) {
11451166
* <pre>
11461167
* {
11471168
* // serve the welcome.html from classpath root
1148-
* get("/", file(MediaType.html, "welcome.html");
1169+
* get("/", file("welcome.html");
11491170
* }
11501171
* </pre>
11511172
*
11521173
* @param location Absolute classpath location.
11531174
* @return A new route handler.
11541175
*/
11551176
public Route.Handler file(@Nonnull final String location) {
1177+
return staticFile(location);
1178+
}
1179+
1180+
/**
1181+
* Serve a single file from classpath.
1182+
* Usage:
1183+
*
1184+
* <pre>
1185+
* {
1186+
* // serve the welcome.html from classpath root
1187+
* get("/", file(MediaType.html, "welcome.html");
1188+
* }
1189+
* </pre>
1190+
*
1191+
* @param location Absolute classpath location.
1192+
* @return A new route handler.
1193+
*/
1194+
private static Route.Handler staticFile(@Nonnull final String location) {
11561195
requireNonNull(location, "A location is required.");
1196+
String path = RoutePattern.normalize(location);
11571197
return (req, res) -> {
1158-
InputStream in = getClass().getClassLoader().getResourceAsStream(location);
1159-
MediaTypeProvider typedb = req.getInstance(MediaTypeProvider.class);
1160-
res.type(typedb.forPath(location));
1161-
res.send(in);
1198+
req.getInstance(AssetRoute.class).handle(new Request.Forwarding(req) {
1199+
1200+
@Override
1201+
public String path() {
1202+
return path;
1203+
}
1204+
1205+
@Override
1206+
public Route route() {
1207+
return new Route.Forwarding(super.route()) {
1208+
@Override
1209+
public String path() {
1210+
return path;
1211+
}
1212+
};
1213+
}
1214+
}, res);
11621215
};
11631216
}
11641217

‎jooby/src/main/java/org/jooby/internal/AssetRoute.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.util.Date;
2424

25+
import javax.inject.Singleton;
26+
2527
import org.jooby.Asset;
2628
import org.jooby.Request;
2729
import org.jooby.Response;
@@ -30,6 +32,7 @@
3032

3133
import com.google.inject.Inject;
3234

35+
@Singleton
3336
public class AssetRoute implements Route.Handler {
3437

3538
private AssetProvider provider;
@@ -40,22 +43,22 @@ public AssetRoute(final AssetProvider provider) {
4043
}
4144

4245
@Override
43-
public void handle(final Request request, final Response response) throws Exception {
44-
Asset resource = provider.get(request.path());
46+
public void handle(final Request req, final Response res) throws Exception {
47+
Asset resource = provider.get(req.path());
4548

4649
long lastModified = resource.lastModified();
4750

4851
// Handle if modified since
4952
if (lastModified > 0) {
50-
long ifModified = request.header("If-Modified-Since").toOptional(Long.class).orElse(-1l);
53+
long ifModified = req.header("If-Modified-Since").toOptional(Long.class).orElse(-1l);
5154
if (ifModified > 0 && lastModified / 1000 <= ifModified / 1000) {
52-
response.status(Status.NOT_MODIFIED);
55+
res.status(Status.NOT_MODIFIED);
5356
return;
5457
}
55-
response.header("Last-Modified", new Date(lastModified));
58+
res.header("Last-Modified", new Date(lastModified));
5659
}
57-
response.type(resource.type());
58-
response.send(resource);
60+
res.type(resource.type());
61+
res.send(resource);
5962
}
6063

6164
}

‎jooby/src/main/java/org/jooby/internal/RouteHandler.java‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,6 @@ private List<Route> routes(final Verb verb, final String path, final MediaType t
230230
final List<MediaType> accept) {
231231
List<Route> routes = findRoutes(verb, path, type, accept);
232232

233-
routes.add(RouteImpl.fromStatus((req, res, chain) -> {
234-
if (!res.committed()) {
235-
Err ex = handle406or415(verb, path, type, accept);
236-
if (ex != null) {
237-
throw ex;
238-
}
239-
}
240-
chain.next(req, res);
241-
}, verb, path, Status.OK, accept));
242-
243233
// 406 or 415
244234
routes.add(RouteImpl.fromStatus((req, res, chain) -> {
245235
if (!res.committed()) {

‎jooby/src/main/java/org/jooby/internal/RoutePattern.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private static String quote(final String s, final int start, final int end) {
121121
return Pattern.quote(s.substring(start, end));
122122
}
123123

124-
private static String normalize(final String pattern) {
124+
public static String normalize(final String pattern) {
125125
String normalized = SLASH.matcher(pattern).replaceAll("/");
126126
StringBuilder buffer = new StringBuilder();
127127
if (normalized.equals("/")) {
2.19 KB
Binary file not shown.

0 commit comments

Comments
 (0)