Module with embedded jetty server for websockets break Ignition GAN connections

Don't use the router. Just add a servlet. Handle each request. Simple.

:man_shrugging:

Ok I see... :+1:

or I could perhaps try to change the mime in the route handler:
response.setContentType(...);

Just set the content type to null in your route mounting implementation, then override it in the actual handler.

for files llike mp3 or pdf, bytes have to be converted in String in the route handler ?

I have tried:

return com.inductiveautomation.ignition.common.Base64.encodeBytes(FileUtilities.readFileAsBytes(filePath));

but the response is not readable by the browser ???

what is the way to encode bytes ? is there any httpServletResponse.setCharacterEncoding(xxx); necessary ?

RTM:
The solution was in the doc: :man_facepalming:

Called when a the route this handler is attached to is matched in an incoming HTTP request.
Returns:
Return an object if your handler should result in a string body returned. Optionally transform the object if it is not already a string by adding a ResponseRenderer to your mounted route. Return null if you have handled the response yourself by directly manipulating the HttpServletResponse.

httpServletResponse.setContentType("application/pdf");
byte[] bytes = FileUtilities.readFileAsBytes(filePath);
httpServletResponse.setContentLength(bytes.length);
httpServletResponse.getOutputStream().write(bytes);
return null;

As an aside, this is a big no-no memory usage wise. You're allocating a byte array (on the heap) for the entire file you're serving, only to immediately discard it when you're writing.

Much better to use a file method that returns an input stream and transfer it, preferably using some buffering mechanism. Apache commons IOUtils should be exposed for your usage in a third party module:

        try (InputStream inputStream = Files.newInputStream(Path.of("somePath")); 
             ServletOutputStream servletOutputStream = res.getOutputStream()) {
            // buffered:
            org.apache.commons.io.IOUtils.copy(inputStream, servletOutputStream);
            // jdk native:
            // inputStream.transferTo(servletOutputStream);
        }
3 Likes