Skip to content

Commit

Permalink
updated example zip, fixed static file servant to use handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
synapticloop committed Sep 3, 2015
1 parent 22508a6 commit 0a3c315
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 91 deletions.
69 changes: 24 additions & 45 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,8 @@
</target>

<target name="clean">
<delete failonerror="false">
<fileset dir="${build.dir}">
<include name="**/*.*" />
</fileset>
</delete>

<delete failonerror="false">
<fileset dir="${dist.dir}">
<include name="**/*.*" />
</fileset>
</delete>
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>

<path id="classpath">
Expand All @@ -45,23 +36,7 @@
- example with and without templar
-->

<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">
<!--
Now zip it all up
-->
<zip destfile="${dist.dir}/${ant.project.name}-example.zip" >
<zipfileset dir="${dist.dir}" prefix="routemaster">
<include name="${ant.project.name}.jar" />
</zipfileset>
<zipfileset dir="${src.dir}" prefix="routemaster">
<include name="mimetypes.properties" />
<include name="routemaster.properties" />
</zipfileset>
<zipfileset dir="${src.html.dir}" prefix="routemaster">
<include name="templar/**/*.*" />
</zipfileset>
</zip>
</target>
<target name="dist" depends="clean,init,dist-routemaster,dist-routemaster-templar,dist-example" description="build the example, all in one and standalone binary distributable" />

<!--
Build the routemaster jar without templar
Expand Down Expand Up @@ -99,25 +74,29 @@
<delete file="${build.dir}/routemaster.example.properties" />
</target>

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

<jar destfile="${dist.dir}/${ant.project.name}-all-in-one.jar" manifest="src/main/META-INF/manifest.mf">
<fileset dir="${build.dir}">
<include name="**/*.class" />
</fileset>
<fileset dir="${src.dir}">
<include name="**/*.example.properties" />
</fileset>
<fileset dir="${src.html.dir}">
<!--
Now zip it all up
-->
<zip destfile="${dist.dir}/${ant.project.name}-example.zip" >

<zipfileset dir="${dist.dir}" prefix="routemaster">
<include name="${ant.project.name}-templar.jar" />
</zipfileset>
<zipfileset dir="${build.dir}" prefix="routemaster">
<include name="mimetypes.properties" />
<include name="routemaster.properties" />
</zipfileset>
<zipfileset dir="${src.html.dir}" prefix="routemaster">
<include name="**/*.*" />
</fileset>
<zipgroupfileset dir="lib/runtime" includes="*.jar" />
</jar>
</zipfileset>
</zip>

<!-- delete the files -->
<delete>
<fileset dir="${build.dir}">
<include name="**/*.properties" />
Expand Down
30 changes: 0 additions & 30 deletions src/main/java/mimetypes.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ option.error.404=/error/404.html
# routes __must__ extend
# synapticloop.nanohttpd.router.Routable

route./*=synapticloop.nanohttpd.servant.ClasspathFileServant
route./*=synapticloop.nanohttpd.servant.StaticFileServant

# This route will list all of the handlers
route./handlers/=synapticloop.nanohttpd.example.servant.HandlerServant
route./handlers/=synapticloop.nanohttpd.example.servant.HandlerTemplarServant

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

rest./rest/%method%/=synapticloop.nanohttpd.example.servant.RouteMasterRestServant
rest./rest/%method%/=synapticloop.nanohttpd.example.servant.RouteMasterRestTemplarServant

# This is a simple rest servant that shows the various http methods being called
# with mapped and unmapped parameters. Any parameters that form part of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

public class HandlerTemplarServant extends Routable {

private static final String HANDLER_SNIPPET_TEMPLAR = "/templar/handler-snippet.templar";

public HandlerTemplarServant(String routeContext) {
super(routeContext);
}
Expand All @@ -38,7 +40,7 @@ public Response serve(File rootDir, IHTTPSession httpSession) {
templarContext.add("handler", plugin.getName());

try {
Parser parser = TemplarHelper.getParser("/templar/handler-snippet.templar");
Parser parser = TemplarHelper.getParser(rootDir.getAbsolutePath() + HANDLER_SNIPPET_TEMPLAR);
content.append(parser.render(templarContext));
} catch (ParseException pex) {
return(HttpUtils.internalServerErrorResponse(pex.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class RouteMasterRestTemplarServant extends RestRoutable {

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

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

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

Routable defaultRoute = router.getDefaultRoute();
if(null != defaultRoute) {
printRoutable(content, router, defaultRoute, false);
printRoutable(rootDir, content, router, defaultRoute, false);
}


Routable wildcardRoute = router.getWildcardRoute();
if(null != wildcardRoute) {
printRoutable(content, router, wildcardRoute, true);
printRoutable(rootDir, content, router, wildcardRoute, true);
}

Map<String,Router> routerMap = router.getRouterMap();
Collection<Router> values = routerMap.values();
for (Iterator<Router> iterator = values.iterator(); iterator.hasNext();) {
Router subRouter = iterator.next();
printRouter(content, subRouter);
printRouter(rootDir, content, subRouter);
}
}

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

if(routable instanceof RestRoutable) {
Expand All @@ -99,7 +99,7 @@ private void printRoutable(StringBuilder content, Router router, Routable routab
}
templarContext.add("class", routable.getClass().getCanonicalName());
try {
Parser parser = TemplarHelper.getParser(ROUTER_SNIPPET_TEMPLAR);
Parser parser = TemplarHelper.getParser(rootDir.getAbsolutePath() + ROUTER_SNIPPET_TEMPLAR);
content.append(parser.render(templarContext));
} catch (ParseException pex) {
LOGGER.log(Level.SEVERE, "Could not parse '" + ROUTER_SNIPPET_TEMPLAR + "'.", pex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Set;
import java.util.logging.Logger;

import synapticloop.nanohttpd.handler.Handler;
import synapticloop.nanohttpd.router.Routable;
import synapticloop.nanohttpd.router.RouteMaster;
import synapticloop.nanohttpd.utils.HttpUtils;
Expand Down Expand Up @@ -46,13 +47,24 @@ public Response serve(File rootDir, IHTTPSession httpSession) {
if(null != indexFile) {
file = indexFile;
}
} else {
// is a file -
}

String absolutePath = file.getAbsolutePath();
// at this point we have a file and we now need to check whether we need a handler

int lastIndexOf = absolutePath.lastIndexOf(".");
String extension = absolutePath.substring(lastIndexOf + 1);

Map<String, Handler> handlerCache = RouteMaster.getHandlerCache();
if(handlerCache.containsKey(extension)) {
Handler handler = handlerCache.get(extension);
if(handler.canServeUri(file.getName(), rootDir)) {
return(handler.serveFile(uri, httpSession.getHeaders(), httpSession, file));
}
}

// at this point - we haven't been handled by a handler - need to serve the file
if(file.exists() && file.canRead()) {
int lastIndexOf = absolutePath.lastIndexOf(".");
if(lastIndexOf != -1) {
// have a file here
return(serveFile(file, httpSession.getHeaders(), absolutePath.substring(lastIndexOf + 1)));
Expand Down Expand Up @@ -81,6 +93,7 @@ private static Response serveFile(File file, Map<String, String> header, String
String mimeType = NanoHTTPD.MIME_HTML;
Response res = null;


if(MimeTypeMapper.getMimeTypes().containsKey(extension)) {
mimeType = MimeTypeMapper.getMimeTypes().get(extension);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private MimeTypeMapper() {}

// maybe it is in the current working directory

File mimetypesFile = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + MIMETYPES_PROPERTIES);
File mimetypesFile = new File("./" + MIMETYPES_PROPERTIES);
if(null == inputStream) {
if(mimetypesFile.exists() && mimetypesFile.canRead()) {
try {
Expand Down

0 comments on commit 0a3c315

Please sign in to comment.