Search in sources :

Example 1 with PooledS3Connector

use of uk.nhs.digital.externalstorage.s3.PooledS3Connector in project hippo by NHS-digital-website.

the class ResourceUploadPlugin method handleUpload.

/**
 * Handles the file upload from the form.
 *
 * @param upload the {@link FileUpload} containing the upload information
 */
private void handleUpload(FileUpload upload) throws FileUploadViolationException {
    final PooledS3Connector s3Connector = HippoServiceRegistry.getService(PooledS3Connector.class);
    String fileName = upload.getClientFileName();
    String mimeType = upload.getContentType();
    try {
        final S3ObjectMetadata s3ObjectMetadata = s3Connector.upload(wrapCheckedException(upload::getInputStream), fileName, mimeType);
        JcrNodeModel nodeModel = (JcrNodeModel) this.getDefaultModel();
        Node node = nodeModel.getNode();
        try {
            setResourceProperties(node, s3ObjectMetadata);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    } catch (Exception ex) {
        log.error("Cannot upload resource", ex);
        throw new FileUploadViolationException(ex.getMessage());
    }
}
Also used : PooledS3Connector(uk.nhs.digital.externalstorage.s3.PooledS3Connector) FileUploadViolationException(org.hippoecm.frontend.plugins.jquery.upload.FileUploadViolationException) Node(javax.jcr.Node) S3ObjectMetadata(uk.nhs.digital.externalstorage.s3.S3ObjectMetadata) JcrNodeModel(org.hippoecm.frontend.model.JcrNodeModel) FileUploadViolationException(org.hippoecm.frontend.plugins.jquery.upload.FileUploadViolationException) RepositoryException(javax.jcr.RepositoryException) PooledS3Connector.wrapCheckedException(uk.nhs.digital.externalstorage.s3.PooledS3Connector.wrapCheckedException)

Example 2 with PooledS3Connector

use of uk.nhs.digital.externalstorage.s3.PooledS3Connector in project hippo by NHS-digital-website.

the class ExternalFileCopyTask method processResourceNodes.

@Override
protected void processResourceNodes(final PooledS3Connector s3, final NodeIterator resourceNodes) throws RepositoryException, WorkflowException {
    final Node copiedDocumentFolderNode = copiedDocumentFolder.getNode(getWorkflowContext().getInternalWorkflowSession());
    final Node copiedDocumentHandleNode = copiedDocumentFolderNode.getNode(copiedDocumentName);
    for (final NodeIterator resourceNodeIterator = findResourceNodes(copiedDocumentHandleNode); resourceNodeIterator.hasNext(); ) {
        final Node copiedResourceNode = resourceNodeIterator.nextNode();
        final String oldReference = copiedResourceNode.getProperty(PROPERTY_EXTERNAL_STORAGE_REFERENCE).getString();
        final S3ObjectMetadata s3ObjectMetadata = s3.copyFile(oldReference, copiedResourceNode.getProperty(PROPERTY_EXTERNAL_STORAGE_FILE_NAME).getString());
        s3.unpublishResource(s3ObjectMetadata.getReference());
        copiedResourceNode.setProperty(PROPERTY_EXTERNAL_STORAGE_REFERENCE, s3ObjectMetadata.getReference());
        copiedResourceNode.setProperty(PROPERTY_EXTERNAL_STORAGE_PUBLIC_URL, s3ObjectMetadata.getUrl());
        log.debug("Copied external resource {} as private {}", oldReference, s3ObjectMetadata);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) S3ObjectMetadata(uk.nhs.digital.externalstorage.s3.S3ObjectMetadata)

Example 3 with PooledS3Connector

use of uk.nhs.digital.externalstorage.s3.PooledS3Connector in project hippo by NHS-digital-website.

the class ImageDisplayPlugin method createFileLink.

private ResourceLink createFileLink(S3NodeMetadata metadata) throws RepositoryException {
    S3FileResourceStream s3FileResourceStream = new S3FileResourceStream(metadata);
    ResourceStreamResource resourceStreamResource = new ResourceStreamResource(s3FileResourceStream) {

        @Override
        public void respond(final Attributes attributes) {
            final PooledS3Connector s3ConnectorService = HippoServiceRegistry.getService(PooledS3Connector.class);
            s3ConnectorService.download(metadata.getReference(), s3File -> {
                s3FileResourceStream.setS3File(s3File);
                super.respond(attributes);
            });
        }
    };
    resourceStreamResource.setCacheDuration(Duration.NONE);
    resourceStreamResource.setFileName(metadata.getFileName());
    resourceStreamResource.setContentDisposition(ContentDisposition.ATTACHMENT);
    ResourceLink filelink = new ResourceLink("link", resourceStreamResource);
    filelink.add(new Label("filename", new Model<>(metadata.getFileName())));
    return filelink;
}
Also used : PooledS3Connector(uk.nhs.digital.externalstorage.s3.PooledS3Connector) ResourceStreamResource(org.apache.wicket.request.resource.ResourceStreamResource) ResourceLink(org.apache.wicket.markup.html.link.ResourceLink) Label(org.apache.wicket.markup.html.basic.Label) IModel(org.apache.wicket.model.IModel) Model(org.apache.wicket.model.Model)

Example 4 with PooledS3Connector

use of uk.nhs.digital.externalstorage.s3.PooledS3Connector 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

PooledS3Connector (uk.nhs.digital.externalstorage.s3.PooledS3Connector)3 Node (javax.jcr.Node)2 S3ObjectMetadata (uk.nhs.digital.externalstorage.s3.S3ObjectMetadata)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 NodeIterator (javax.jcr.NodeIterator)1 RepositoryException (javax.jcr.RepositoryException)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Label (org.apache.wicket.markup.html.basic.Label)1 ResourceLink (org.apache.wicket.markup.html.link.ResourceLink)1 IModel (org.apache.wicket.model.IModel)1 Model (org.apache.wicket.model.Model)1 ResourceStreamResource (org.apache.wicket.request.resource.ResourceStreamResource)1 JcrNodeModel (org.hippoecm.frontend.model.JcrNodeModel)1 FileUploadViolationException (org.hippoecm.frontend.plugins.jquery.upload.FileUploadViolationException)1 ContainerException (org.hippoecm.hst.core.container.ContainerException)1 PooledS3Connector.wrapCheckedException (uk.nhs.digital.externalstorage.s3.PooledS3Connector.wrapCheckedException)1