Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.