|
21 | 21 | import static java.util.Objects.requireNonNull; |
22 | 22 |
|
23 | 23 | import java.io.File; |
24 | | -import java.io.InputStream; |
25 | 24 | import java.lang.reflect.Type; |
26 | 25 | import java.nio.charset.Charset; |
27 | 26 | import java.text.DecimalFormat; |
|
44 | 43 | import org.jooby.internal.AssetFormatter; |
45 | 44 | import org.jooby.internal.AssetRoute; |
46 | 45 | import org.jooby.internal.FallbackBodyConverter; |
| 46 | +import org.jooby.internal.RoutePattern; |
47 | 47 | import org.jooby.internal.Server; |
48 | 48 | import org.jooby.internal.TypeConverters; |
49 | 49 | import org.jooby.internal.jetty.Jetty; |
@@ -565,6 +565,27 @@ public abstract void configure(@Nonnull Mode mode, @Nonnull Config config, |
565 | 565 | return handler(new Route.Definition("*", path, handler)); |
566 | 566 | } |
567 | 567 |
|
| 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 | + |
568 | 589 | /** |
569 | 590 | * Define an in-line route that supports HTTP GET method: |
570 | 591 | * |
@@ -1145,20 +1166,52 @@ public Route.Handler redirect(final String location) { |
1145 | 1166 | * <pre> |
1146 | 1167 | * { |
1147 | 1168 | * // serve the welcome.html from classpath root |
1148 | | - * get("/", file(MediaType.html, "welcome.html"); |
| 1169 | + * get("/", file("welcome.html"); |
1149 | 1170 | * } |
1150 | 1171 | * </pre> |
1151 | 1172 | * |
1152 | 1173 | * @param location Absolute classpath location. |
1153 | 1174 | * @return A new route handler. |
1154 | 1175 | */ |
1155 | 1176 | 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) { |
1156 | 1195 | requireNonNull(location, "A location is required."); |
| 1196 | + String path = RoutePattern.normalize(location); |
1157 | 1197 | 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); |
1162 | 1215 | }; |
1163 | 1216 | } |
1164 | 1217 |
|
|
0 commit comments