From 0a3c315d2c683a790d7c7bfca37203779faec8af Mon Sep 17 00:00:00 2001 From: synapticloop Date: Thu, 3 Sep 2015 14:41:06 +0100 Subject: [PATCH] updated example zip, fixed static file servant to use handlers --- build.xml | 69 +++++++------------ src/main/java/mimetypes.properties | 30 -------- ...outemaster.example.fileservant.properties} | 6 +- .../servant/HandlerTemplarServant.java | 4 +- .../RouteMasterRestTemplarServant.java | 16 ++--- .../nanohttpd/servant/StaticFileServant.java | 19 ++++- .../nanohttpd/utils/MimeTypeMapper.java | 2 +- 7 files changed, 55 insertions(+), 91 deletions(-) delete mode 100644 src/main/java/mimetypes.properties rename src/main/java/{routemaster.classpath.servant.properties => routemaster.example.fileservant.properties} (93%) diff --git a/build.xml b/build.xml index 966d467..3d76569 100644 --- a/build.xml +++ b/build.xml @@ -12,17 +12,8 @@ - - - - - - - - - - - + + @@ -45,23 +36,7 @@ - example with and without templar --> - - - - - - - - - - - - - - - + - - - + + + + - - - - - - - - + + + + + + + + + + + - - - + + + + diff --git a/src/main/java/mimetypes.properties b/src/main/java/mimetypes.properties deleted file mode 100644 index d780400..0000000 --- a/src/main/java/mimetypes.properties +++ /dev/null @@ -1,30 +0,0 @@ -# This file is a list of all of the mimetypes that are available to the -# -# synapticloop.nanohttpd.servant.StaticFileServant -# -css=text/css -htm=text/html -html=text/html -xml=text/xml -java=text/x-java-source, text/java -md=text/plain -txt=text/plain -asc=text/plain -gif=image/gif -jpg=image/jpeg -jpeg=image/jpeg -png=image/png -mp3=audio/mpeg -m3u=audio/mpeg-url -mp4=video/mp4 -ogv=video/ogg -flv=video/x-flv -mov=video/quicktime -swf=application/x-shockwave-flash -js=application/javascript -pdf=application/pdf -doc=application/msword -ogg=application/x-ogg -zip=application/octet-stream -exe=application/octet-stream -class=application/octet-stream \ No newline at end of file diff --git a/src/main/java/routemaster.classpath.servant.properties b/src/main/java/routemaster.example.fileservant.properties similarity index 93% rename from src/main/java/routemaster.classpath.servant.properties rename to src/main/java/routemaster.example.fileservant.properties index 137889b..8b467cf 100644 --- a/src/main/java/routemaster.classpath.servant.properties +++ b/src/main/java/routemaster.example.fileservant.properties @@ -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.' @@ -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 diff --git a/src/main/java/synapticloop/nanohttpd/example/servant/HandlerTemplarServant.java b/src/main/java/synapticloop/nanohttpd/example/servant/HandlerTemplarServant.java index 7e641d1..a79d139 100644 --- a/src/main/java/synapticloop/nanohttpd/example/servant/HandlerTemplarServant.java +++ b/src/main/java/synapticloop/nanohttpd/example/servant/HandlerTemplarServant.java @@ -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); } @@ -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())); diff --git a/src/main/java/synapticloop/nanohttpd/example/servant/RouteMasterRestTemplarServant.java b/src/main/java/synapticloop/nanohttpd/example/servant/RouteMasterRestTemplarServant.java index e23b6c0..1b0534c 100644 --- a/src/main/java/synapticloop/nanohttpd/example/servant/RouteMasterRestTemplarServant.java +++ b/src/main/java/synapticloop/nanohttpd/example/servant/RouteMasterRestTemplarServant.java @@ -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 params) { @@ -38,7 +38,7 @@ public Response doGet(File rootDir, IHTTPSession httpSession, Map routerMap = router.getRouterMap(); Collection values = routerMap.values(); for (Iterator 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) { @@ -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); diff --git a/src/main/java/synapticloop/nanohttpd/servant/StaticFileServant.java b/src/main/java/synapticloop/nanohttpd/servant/StaticFileServant.java index da0d415..eca4f00 100644 --- a/src/main/java/synapticloop/nanohttpd/servant/StaticFileServant.java +++ b/src/main/java/synapticloop/nanohttpd/servant/StaticFileServant.java @@ -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; @@ -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 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))); @@ -81,6 +93,7 @@ private static Response serveFile(File file, Map header, String String mimeType = NanoHTTPD.MIME_HTML; Response res = null; + if(MimeTypeMapper.getMimeTypes().containsKey(extension)) { mimeType = MimeTypeMapper.getMimeTypes().get(extension); } diff --git a/src/main/java/synapticloop/nanohttpd/utils/MimeTypeMapper.java b/src/main/java/synapticloop/nanohttpd/utils/MimeTypeMapper.java index 1fa2b6c..f74c3cc 100644 --- a/src/main/java/synapticloop/nanohttpd/utils/MimeTypeMapper.java +++ b/src/main/java/synapticloop/nanohttpd/utils/MimeTypeMapper.java @@ -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 {