Can't seem to figure out how to serve a favicon.ico
. JPG, GIF, PNG, HTML, CSS work without problems.
Here's my resource:
@Path("/{fileName: .+(?:png|jpg|gif)}")
@Produces({"image/png, image/jpg, image/gif"})
public class MimeImages {
@GET
public Response getFullImage(@PathParam("fileName") String fileName) throws IOException {
String sImgType = "jpg";
if(fileName.toLowerCase().endsWith(".png")) {
sImgType = "png";
} else if(fileName.toLowerCase().endsWith(".gif")) {
sImgType = "gif";
}
URL urlToResource = getClass().getResource("/com/test/web/" + fileName);
BufferedImage image = ImageIO.read(urlToResource);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, sImgType, baos);
byte[] imageData = baos.toByteArray();
return Response.ok(new ByteArrayInputStream(imageData)).build();
}
}
And:
@Path("/{fileName: .*(?!png|jpg|gif|mp3)}")
@Produces({"text/html, text/plain, text/css"})
public class MimeHtml {
@GET
public Response getPage(@PathParam("fileName") String fileName) throws IOException {
fileName = fileName.equals("") ? "index.htm" : fileName;
URL urlToResource = getClass().getResource("/com/test/web/" + fileName);
return Response.ok(read(urlToResource.openConnection().getInputStream())).build();
}
private String read(InputStream stream) throws IOException {
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(
stream, StandardCharsets.UTF_8))) {
return buffer.lines().collect(Collectors.joining("\n"));
}
}
}
Server:
public class WebServer {
private HttpServer webServer;
public void start() throws IOException {
System.out.println("Starting WebServer\n");
webServer = createHttpServer();
webServer.start();
System.out.println(String.format("\nWeb Server started:" + "%sapplication.wadl\n", getURI()));
}
public void stop() {
webServer.stop(0);
}
public static HttpServer createHttpServer() throws IOException {
ResourceConfig rc = new PackagesResourceConfig("com.test");
return HttpServerFactory.create(getURI(), rc);
}
private static URI getURI() {
return UriBuilder.fromUri("http://localhost/").port(4444).build();
}
}
I've tried Path'ing them as image and HTML but can't get it to serve an icon.
If I do /favicon.ico
I just get a byte dump in the browser.