Skip to content

Instantly share code, notes, and snippets.

@jexp
Created April 24, 2015 17:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jexp/af4d0430ddf3c90f229c to your computer and use it in GitHub Desktop.
Save jexp/af4d0430ddf3c90f229c to your computer and use it in GitHub Desktop.
Neo4j Server Extension that serves files from a webapp directory of the JAR
@Path("/")
public class StaticWebResource {
@GET
@Path("{file:(?i).+\\.(png|jpg|jpeg|svg|gif|html?|js|css|txt)}")
public Response file(@PathParam("file") String file) throws IOException {
InputStream fileStream = findFileStream(file);
if (fileStream == null) return Response.status(Response.Status.NOT_FOUND).build();
else return Response.ok(fileStream, mediaType(file)).build();
}
private InputStream findFileStream(String file) throws IOException {
URL fileUrl = ClassLoader.getSystemResource("webapp/" + file);
if (fileUrl==null) return null;
return fileUrl.openStream();
}
public String mediaType(String file) {
int dot = file.lastIndexOf(".");
if (dot == -1) return MediaType.TEXT_PLAIN;
String ext = file.substring(dot + 1).toLowerCase();
switch (ext) {
case "png":
return "image/png";
case "js":
return "text/javascript";
case "css":
return "text/css";
case "html":
return MediaType.TEXT_HTML;
default:
return MediaType.TEXT_PLAIN;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment