StreamResourceHandler implements Serializable { public void handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response, StreamResource streamResource) throws IOException { StreamResourceWriter writer; session.lock(); try { ServletContext context = ((VaadinServletRequest) request) .getServletContext(); response.setContentType(streamResource.getContentTypeResolver() .apply(streamResource, context)); response.setCacheTime(streamResource.getCacheTime()); writer = streamResource.getWriter(); if (writer == null) { throw new IOException( "Stream resource produces null input stream"); } } catch (Exception exception) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); throw exception; } finally { session.unlock(); } OutputStream outputStream = null; try { outputStream = response.getOutputStream(); writer.accept(outputStream, session); } catch (Exception exception) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); throw exception; } finally { if (outputStream != null) { outputStream.close(); } } }  void handleRequest(VaadinSession session, VaadinRequest request,
            VaadinResponse response, StreamResource streamResource);  }
@Test public void inputStreamFactoryThrowsException_responseStatusIs500() throws IOException { StreamResource res = new StreamResource("readme.md", (InputStreamFactory) () -> { throw new RuntimeException("Simulated"); }); try { handler.handleRequest(session, request, response, res); } catch (RuntimeException ignore) { } Mockito.verify(response) .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }
@Test public void inputStreamResourceWriterThrows_responseStatusIs500() throws IOException { StreamResource res = new StreamResource("readme.md", (StreamResourceWriter) (stream, session) -> { throw new RuntimeException("Simulated"); }); try { handler.handleRequest(session, request, response, res); } catch (RuntimeException ignore) { } Mockito.verify(response) .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }
@Test public void inputStreamResourceWriterIsNull_responseStatusIs500() throws IOException { @SuppressWarnings("serial") StreamResource res = new StreamResource("readme.md", () -> new ByteArrayInputStream(new byte[0])) { @Override public StreamResourceWriter getWriter() { return null; } }; try { handler.handleRequest(session, request, response, res); } catch (IOException ignore) { } Mockito.verify(response) .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }