Created
April 24, 2015 17:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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