1

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.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
John Smith
  • 3,493
  • 3
  • 25
  • 52

1 Answers1

0

Ok. I figured this out and I believe Cassio's answer image/x-icon may work as well but haven't tried it. I got content/unknown from debugging a webserver but it does work.

UPDATE: I tried image/x-icon. This also works and probably is the better answer.

@Path("/{fileName: .*ico}")
@Produces({"image/x-icon"})
public class MimeFavIcon {
    @GET
    public Response getPage(@PathParam("fileName") String fileName) throws IOException {
        fileName = fileName.equals("") ? "index.htm" : fileName;
        URL urlToResource = getClass().getResource("/com/daford/web/" + fileName);
        URLConnection conn = urlToResource.openConnection();
        InputStream inConnectionReader = conn.getInputStream();
        int size = conn.getContentLength();
        byte[] imageData = new byte[size];
        //noinspection ResultOfMethodCallIgnored
        inConnectionReader.read(imageData, 0, size);
        return Response.ok(new ByteArrayInputStream(imageData)).build();

    }
}
John Smith
  • 3,493
  • 3
  • 25
  • 52
  • My problem was related to reading/writing image file which could be solved following this: https://stackoverflow.com/questions/9204287/how-to-return-a-png-image-from-jersey-rest-service-method-to-the-browser – Cleber Jorge Amaral Oct 23 '19 at 01:43