Search in sources :

Example 1 with StreamResourceWriter

use of com.vaadin.flow.server.StreamResourceWriter in project flow by vaadin.

the class StreamResourceHandlerTest method inputStreamResourceWriterThrows_responseStatusIs500.

@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) {
    // Ignore exception, it's expected. We need to check the status
    }
    Mockito.verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
Also used : MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) VaadinServletResponse(com.vaadin.flow.server.VaadinServletResponse) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) Mockito(org.mockito.Mockito) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStreamFactory(com.vaadin.flow.server.InputStreamFactory) VaadinService(com.vaadin.flow.server.VaadinService) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) StreamResource(com.vaadin.flow.server.StreamResource) ServletContext(javax.servlet.ServletContext) ServiceException(com.vaadin.flow.server.ServiceException) StreamResourceWriter(com.vaadin.flow.server.StreamResourceWriter) Before(org.junit.Before) StreamResource(com.vaadin.flow.server.StreamResource) Test(org.junit.Test)

Example 2 with StreamResourceWriter

use of com.vaadin.flow.server.StreamResourceWriter in project flow by vaadin.

the class StreamResourceHandler method handleRequest.

/**
 * Handle sending for a stream resource request.
 *
 * @param session
 *            session for the request
 * @param request
 *            request to handle
 * @param response
 *            response object to which a response can be written.
 * @param streamResource
 *            stream resource that handles data writer
 *
 * @throws IOException
 *             if an IO error occurred
 */
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());
        streamResource.getHeaders().forEach((name, value) -> response.setHeader(name, value));
        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();
    }
    // don't use here "try resource" syntax sugar because in case there is
    // an exception the {@code outputStream} will be closed before "catch"
    // block which sets the status code and this code will not have any
    // effect being called after closing the stream (see #8740).
    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();
        }
    }
}
Also used : OutputStream(java.io.OutputStream) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) ServletContext(javax.servlet.ServletContext) IOException(java.io.IOException) StreamResourceWriter(com.vaadin.flow.server.StreamResourceWriter) IOException(java.io.IOException)

Aggregations

StreamResourceWriter (com.vaadin.flow.server.StreamResourceWriter)2 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)2 IOException (java.io.IOException)2 ServletContext (javax.servlet.ServletContext)2 InputStreamFactory (com.vaadin.flow.server.InputStreamFactory)1 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)1 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)1 ServiceException (com.vaadin.flow.server.ServiceException)1 StreamResource (com.vaadin.flow.server.StreamResource)1 VaadinService (com.vaadin.flow.server.VaadinService)1 VaadinServletResponse (com.vaadin.flow.server.VaadinServletResponse)1 AlwaysLockedVaadinSession (com.vaadin.tests.util.AlwaysLockedVaadinSession)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 OutputStream (java.io.OutputStream)1 ServletException (javax.servlet.ServletException)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Mockito (org.mockito.Mockito)1