Skip to content

Commit 0a3c315

Browse files
author
synapticloop
committed
updated example zip, fixed static file servant to use handlers
1 parent 22508a6 commit 0a3c315

File tree

7 files changed

+55
-91
lines changed

7 files changed

+55
-91
lines changed

build.xml

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,8 @@
1212
</target>
1313

1414
<target name="clean">
15-
<delete failonerror="false">
16-
<fileset dir="${build.dir}">
17-
<include name="**/*.*" />
18-
</fileset>
19-
</delete>
20-
21-
<delete failonerror="false">
22-
<fileset dir="${dist.dir}">
23-
<include name="**/*.*" />
24-
</fileset>
25-
</delete>
15+
<delete dir="${build.dir}" />
16+
<delete dir="${dist.dir}" />
2617
</target>
2718

2819
<path id="classpath">
@@ -45,23 +36,7 @@
4536
- example with and without templar
4637
-->
4738

48-
<target name="dist" depends="clean,init,dist-routemaster,dist-routemaster-templar,dist-all-in-one" description="build the example, all in one and standalone binary distributable">
49-
<!--
50-
Now zip it all up
51-
-->
52-
<zip destfile="${dist.dir}/${ant.project.name}-example.zip" >
53-
<zipfileset dir="${dist.dir}" prefix="routemaster">
54-
<include name="${ant.project.name}.jar" />
55-
</zipfileset>
56-
<zipfileset dir="${src.dir}" prefix="routemaster">
57-
<include name="mimetypes.properties" />
58-
<include name="routemaster.properties" />
59-
</zipfileset>
60-
<zipfileset dir="${src.html.dir}" prefix="routemaster">
61-
<include name="templar/**/*.*" />
62-
</zipfileset>
63-
</zip>
64-
</target>
39+
<target name="dist" depends="clean,init,dist-routemaster,dist-routemaster-templar,dist-example" description="build the example, all in one and standalone binary distributable" />
6540

6641
<!--
6742
Build the routemaster jar without templar
@@ -99,25 +74,29 @@
9974
<delete file="${build.dir}/routemaster.example.properties" />
10075
</target>
10176

102-
<!--
103-
Build the routemaster jar with templar
104-
-->
105-
<target name="dist-all-in-one" depends="clean,init,compile" description="build the all in one binary distributable">
106-
<copy file="${src.dir}/routemaster.classpath.servant.properties" tofile="${build.dir}/routemaster.properties" />
107-
<copy file="${src.dir}/mimetypes.properties" todir="${build.dir}" />
77+
<target name="dist-example" depends="clean,init,compile" description="build the example zip file">
78+
<!-- Copy over the example templar files -->
79+
<copy file="${src.dir}/routemaster.example.fileservant.properties" tofile="${build.dir}/routemaster.properties" />
80+
<copy file="${src.dir}/mimetypes.example.properties" tofile="${build.dir}/mimetypes.properties" />
10881

109-
<jar destfile="${dist.dir}/${ant.project.name}-all-in-one.jar" manifest="src/main/META-INF/manifest.mf">
110-
<fileset dir="${build.dir}">
111-
<include name="**/*.class" />
112-
</fileset>
113-
<fileset dir="${src.dir}">
114-
<include name="**/*.example.properties" />
115-
</fileset>
116-
<fileset dir="${src.html.dir}">
82+
<!--
83+
Now zip it all up
84+
-->
85+
<zip destfile="${dist.dir}/${ant.project.name}-example.zip" >
86+
87+
<zipfileset dir="${dist.dir}" prefix="routemaster">
88+
<include name="${ant.project.name}-templar.jar" />
89+
</zipfileset>
90+
<zipfileset dir="${build.dir}" prefix="routemaster">
91+
<include name="mimetypes.properties" />
92+
<include name="routemaster.properties" />
93+
</zipfileset>
94+
<zipfileset dir="${src.html.dir}" prefix="routemaster">
11795
<include name="**/*.*" />
118-
</fileset>
119-
<zipgroupfileset dir="lib/runtime" includes="*.jar" />
120-
</jar>
96+
</zipfileset>
97+
</zip>
98+
99+
<!-- delete the files -->
121100
<delete>
122101
<fileset dir="${build.dir}">
123102
<include name="**/*.properties" />

src/main/java/mimetypes.properties

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/main/java/routemaster.classpath.servant.properties renamed to src/main/java/routemaster.example.fileservant.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ option.error.404=/error/404.html
1717
# routes __must__ extend
1818
# synapticloop.nanohttpd.router.Routable
1919

20-
route./*=synapticloop.nanohttpd.servant.ClasspathFileServant
20+
route./*=synapticloop.nanohttpd.servant.StaticFileServant
2121

2222
# This route will list all of the handlers
23-
route./handlers/=synapticloop.nanohttpd.example.servant.HandlerServant
23+
route./handlers/=synapticloop.nanohttpd.example.servant.HandlerTemplarServant
2424

2525
#
2626
# If you wish to define a restful route, start the property name with a 'rest.'
@@ -32,7 +32,7 @@ route./handlers/=synapticloop.nanohttpd.example.servant.HandlerServant
3232
# rest routes __must__ extend
3333
# synapticloop.nanohttpd.router.RestRoutable
3434

35-
rest./rest/%method%/=synapticloop.nanohttpd.example.servant.RouteMasterRestServant
35+
rest./rest/%method%/=synapticloop.nanohttpd.example.servant.RouteMasterRestTemplarServant
3636

3737
# This is a simple rest servant that shows the various http methods being called
3838
# with mapped and unmapped parameters. Any parameters that form part of the

src/main/java/synapticloop/nanohttpd/example/servant/HandlerTemplarServant.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
public class HandlerTemplarServant extends Routable {
2020

21+
private static final String HANDLER_SNIPPET_TEMPLAR = "/templar/handler-snippet.templar";
22+
2123
public HandlerTemplarServant(String routeContext) {
2224
super(routeContext);
2325
}
@@ -38,7 +40,7 @@ public Response serve(File rootDir, IHTTPSession httpSession) {
3840
templarContext.add("handler", plugin.getName());
3941

4042
try {
41-
Parser parser = TemplarHelper.getParser("/templar/handler-snippet.templar");
43+
Parser parser = TemplarHelper.getParser(rootDir.getAbsolutePath() + HANDLER_SNIPPET_TEMPLAR);
4244
content.append(parser.render(templarContext));
4345
} catch (ParseException pex) {
4446
return(HttpUtils.internalServerErrorResponse(pex.getMessage()));

src/main/java/synapticloop/nanohttpd/example/servant/RouteMasterRestTemplarServant.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
public class RouteMasterRestTemplarServant extends RestRoutable {
2525

26-
private static final String ROUTER_SNIPPET_TEMPLAR = "src/main/html/templar/router-snippet.templar";
26+
private static final String ROUTER_SNIPPET_TEMPLAR = "/templar/router-snippet.templar";
2727
private static final Logger LOGGER = Logger.getLogger(RouteMasterRestServant.class.getName());
2828

2929
public RouteMasterRestTemplarServant(String routeContext, List<String> params) {
@@ -38,7 +38,7 @@ public Response doGet(File rootDir, IHTTPSession httpSession, Map<String, String
3838
if(method != null) {
3939
if("routes".equals(method)) {
4040
Router router = RouteMaster.getRouter();
41-
printRouter(content, router);
41+
printRouter(rootDir, content, router);
4242
return(HttpUtils.okResponse(content.toString()));
4343
} else if ("cache".equals(method)) {
4444
printCache(content);
@@ -62,29 +62,29 @@ private void printCache(StringBuilder content) {
6262
}
6363
}
6464

65-
private void printRouter(StringBuilder content, Router router) {
65+
private void printRouter(File rootDir, StringBuilder content, Router router) {
6666
// now get all of the other routes
6767

6868
Routable defaultRoute = router.getDefaultRoute();
6969
if(null != defaultRoute) {
70-
printRoutable(content, router, defaultRoute, false);
70+
printRoutable(rootDir, content, router, defaultRoute, false);
7171
}
7272

7373

7474
Routable wildcardRoute = router.getWildcardRoute();
7575
if(null != wildcardRoute) {
76-
printRoutable(content, router, wildcardRoute, true);
76+
printRoutable(rootDir, content, router, wildcardRoute, true);
7777
}
7878

7979
Map<String,Router> routerMap = router.getRouterMap();
8080
Collection<Router> values = routerMap.values();
8181
for (Iterator<Router> iterator = values.iterator(); iterator.hasNext();) {
8282
Router subRouter = iterator.next();
83-
printRouter(content, subRouter);
83+
printRouter(rootDir, content, subRouter);
8484
}
8585
}
8686

87-
private void printRoutable(StringBuilder content, Router router, Routable routable, boolean isWildcard) {
87+
private void printRoutable(File rootDir, StringBuilder content, Router router, Routable routable, boolean isWildcard) {
8888
TemplarContext templarContext = new TemplarContext();
8989

9090
if(routable instanceof RestRoutable) {
@@ -99,7 +99,7 @@ private void printRoutable(StringBuilder content, Router router, Routable routab
9999
}
100100
templarContext.add("class", routable.getClass().getCanonicalName());
101101
try {
102-
Parser parser = TemplarHelper.getParser(ROUTER_SNIPPET_TEMPLAR);
102+
Parser parser = TemplarHelper.getParser(rootDir.getAbsolutePath() + ROUTER_SNIPPET_TEMPLAR);
103103
content.append(parser.render(templarContext));
104104
} catch (ParseException pex) {
105105
LOGGER.log(Level.SEVERE, "Could not parse '" + ROUTER_SNIPPET_TEMPLAR + "'.", pex);

src/main/java/synapticloop/nanohttpd/servant/StaticFileServant.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Set;
88
import java.util.logging.Logger;
99

10+
import synapticloop.nanohttpd.handler.Handler;
1011
import synapticloop.nanohttpd.router.Routable;
1112
import synapticloop.nanohttpd.router.RouteMaster;
1213
import synapticloop.nanohttpd.utils.HttpUtils;
@@ -46,13 +47,24 @@ public Response serve(File rootDir, IHTTPSession httpSession) {
4647
if(null != indexFile) {
4748
file = indexFile;
4849
}
49-
} else {
50-
// is a file -
5150
}
5251

5352
String absolutePath = file.getAbsolutePath();
53+
// at this point we have a file and we now need to check whether we need a handler
54+
55+
int lastIndexOf = absolutePath.lastIndexOf(".");
56+
String extension = absolutePath.substring(lastIndexOf + 1);
57+
58+
Map<String, Handler> handlerCache = RouteMaster.getHandlerCache();
59+
if(handlerCache.containsKey(extension)) {
60+
Handler handler = handlerCache.get(extension);
61+
if(handler.canServeUri(file.getName(), rootDir)) {
62+
return(handler.serveFile(uri, httpSession.getHeaders(), httpSession, file));
63+
}
64+
}
65+
66+
// at this point - we haven't been handled by a handler - need to serve the file
5467
if(file.exists() && file.canRead()) {
55-
int lastIndexOf = absolutePath.lastIndexOf(".");
5668
if(lastIndexOf != -1) {
5769
// have a file here
5870
return(serveFile(file, httpSession.getHeaders(), absolutePath.substring(lastIndexOf + 1)));
@@ -81,6 +93,7 @@ private static Response serveFile(File file, Map<String, String> header, String
8193
String mimeType = NanoHTTPD.MIME_HTML;
8294
Response res = null;
8395

96+
8497
if(MimeTypeMapper.getMimeTypes().containsKey(extension)) {
8598
mimeType = MimeTypeMapper.getMimeTypes().get(extension);
8699
}

src/main/java/synapticloop/nanohttpd/utils/MimeTypeMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private MimeTypeMapper() {}
2727

2828
// maybe it is in the current working directory
2929

30-
File mimetypesFile = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + MIMETYPES_PROPERTIES);
30+
File mimetypesFile = new File("./" + MIMETYPES_PROPERTIES);
3131
if(null == inputStream) {
3232
if(mimetypesFile.exists() && mimetypesFile.canRead()) {
3333
try {

0 commit comments

Comments
 (0)