Search in sources :

Example 1 with ContainerException

use of org.hippoecm.hst.core.container.ContainerException in project hippo by NHS-digital-website.

the class S3ConnectorValve method invoke.

public void invoke(final ValveContext context) throws ContainerException {
    // We're retrieving the service at the last possible moment to ensure that
    // we always use the latest instance of it as the service may ge re-registered
    // without restarting the application due to config changes in the Console.
    final PooledS3Connector s3Connector = serviceProvider.getService(PooledS3Connector.class);
    final String s3Reference = context.getServletRequest().getParameter("s3Reference");
    s3Connector.download(s3Reference, s3File -> {
        final HttpServletResponse response = context.getServletResponse();
        if (context.getRequestContext().isChannelManagerPreviewRequest()) {
            String fileName = context.getServletRequest().getParameter("fileName");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            response.setHeader("Content-Length", String.valueOf(s3File.getLength()));
            response.setStatus(HttpServletResponse.SC_ACCEPTED);
            try (OutputStream responseOutputStream = response.getOutputStream();
                InputStream s3InputStream = s3File.getContent()) {
                IOUtils.copyLarge(s3InputStream, responseOutputStream);
            } catch (final Exception ex) {
                throw new RuntimeException("Failed to download content from S3: " + fileName + ": " + s3Reference, ex);
            }
        } else {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        }
    });
}
Also used : PooledS3Connector(uk.nhs.digital.externalstorage.s3.PooledS3Connector) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ContainerException(org.hippoecm.hst.core.container.ContainerException)

Aggregations

InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ContainerException (org.hippoecm.hst.core.container.ContainerException)1 PooledS3Connector (uk.nhs.digital.externalstorage.s3.PooledS3Connector)1