Search in sources :

Example 1 with FilePathData

use of uk.nhs.digital.arc.util.FilePathData in project hippo by NHS-digital-website.

the class S3StorageManager method uploadFileToS3.

public S3ObjectMetadata uploadFileToS3(String docbase, String sourceFilePath) {
    FilePathData sourceFilePathData = new FilePathData(docbase, sourceFilePath);
    String targetFileName = sourceFilePathData.getFilename();
    S3ObjectMetadata metaData = null;
    if (sourceFilePathData.isS3Protocol()) {
        String sourceBucketName = sourceFilePathData.getS3Bucketname();
        String noBucketSourceFilePath = sourceFilePathData.getFilePathNoBucket();
        if (getS3Connector().doesObjectExist(sourceBucketName, noBucketSourceFilePath)) {
            metaData = getS3Connector().copyFileFromOtherBucket(noBucketSourceFilePath, sourceBucketName, targetFileName);
        }
    }
    return metaData;
}
Also used : FilePathData(uk.nhs.digital.arc.util.FilePathData) S3ObjectMetadata(uk.nhs.digital.externalstorage.s3.S3ObjectMetadata)

Example 2 with FilePathData

use of uk.nhs.digital.arc.util.FilePathData in project hippo by NHS-digital-website.

the class PubSysChartsectionTransformer method getChartDataAsJsonFromContentOfResource.

/**
 * Since we only have one concrete implementation of {@link uk.nhs.digital.arc.storage.ArcStorageManager}, which is {@link uk.nhs.digital.arc.storage.S3StorageManager}
 * then we are really only considering S3 based data this point in time, although it could be implemented easily enough to locate its data
 * from an alternate source using a descendent of the {@link uk.nhs.digital.arc.storage.ArcStorageManager} interface
 *
 * @param sectionNode is the node to which we attach the storage meta data once loaded
 */
private void getChartDataAsJsonFromContentOfResource(ContentNode sectionNode) {
    FilePathData sourceFilePathData = new FilePathData(docbase, chartSection.getDataFileReq());
    ArcFileData metaData = storageManager.getFileMetaData(sourceFilePathData);
    if (sourceFilePathData.isS3Protocol()) {
        try {
            Binary binaryData = getJsonDataAndCreateBinaryObjectFromProvidedFile(sectionNode, metaData.getDelegateStream());
            ContentNode newNode = new ContentNode(PUBLICATIONSYSTEM_DATAFILE, PUBLICATIONSYSTEM_RESOURCE);
            this.addFileRelatedProperties(newNode, new BinaryValue(IOUtils.toByteArray(binaryData.getStream())), metaData.getContentType(), sourceFilePathData.getFilename());
            sectionNode.addNode(newNode);
        } catch (IOException | RepositoryException exception) {
            exception.printStackTrace();
        }
    }
}
Also used : ArcFileData(uk.nhs.digital.arc.storage.ArcFileData) FilePathData(uk.nhs.digital.arc.util.FilePathData) BinaryValue(org.onehippo.forge.content.pojo.model.BinaryValue) RepositoryException(javax.jcr.RepositoryException) Binary(javax.jcr.Binary) IOException(java.io.IOException) ContentNode(org.onehippo.forge.content.pojo.model.ContentNode)

Example 3 with FilePathData

use of uk.nhs.digital.arc.util.FilePathData in project hippo by NHS-digital-website.

the class ManifestProcessor method readWrapperFromFile.

/**
 * The wrapper is the main manifest and contains references to documents that we want to create
 * It also has a docbase which is used when referencing files that are not fully qualified inside the segment data
 * @return is a {@link ManifestProcessingSummary} with details of all the pages created along with any error conditions
 * that have been encountered
 * @throws IOException if we are unable to locate the manifest
 */
public ManifestProcessingSummary readWrapperFromFile() throws IOException {
    FilePathData manifestFileData = new FilePathData(manifestFile);
    ManifestProcessingSummary messageSummary = new ManifestProcessingSummary();
    // At this point, we only deal with files located in an S3 bucket
    if (manifestFileData.isS3Protocol() && storageManager.fileExists(manifestFile)) {
        InputStream in = storageManager.getFileInputStream(manifestFileData);
        List<String> createdPages = new ArrayList<>();
        List<String> existingPages = new ArrayList<>();
        String reportRootFolder = getReportRootFolder();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        ManifestWrapper wrapper = objectMapper.readValue(in, ManifestWrapper.class);
        // start adjusting it
        if (!preview) {
            existingPages = getExistingPages();
        }
        String calculatedDocbase = getDocbaseUsingManifestLocationIfRequired(wrapper.getDocumentBase(), manifestFileData);
        if (calculatedDocbase == null || wrapper.getPages() == null || wrapper.getPages().size() == 0) {
            ProcessOutcome outcome;
            if (calculatedDocbase == null) {
                outcome = new ProcessOutcome("Could not locate a docbase in the file: " + manifestFile, ProcessOutcome.ERROR);
            } else {
                outcome = new ProcessOutcome("Could not locate any page sections in the file: " + manifestFile, ProcessOutcome.ERROR);
            }
            messageSummary.addIndividualProcessOutcome(outcome);
        } else {
            // FOr each page, we send it to the processing method and record the outcome
            wrapper.getPages().stream().forEach(page -> {
                try {
                    messageSummary.addIndividualProcessOutcome(loadAndProcessPage(calculatedDocbase, page, objectMapper));
                    createdPages.add(reportRootFolder + "/" + page.getPageName());
                } catch (IOException e) {
                    LOGGER.error("Error processing the individual page", e);
                }
            });
            // files from S3 that are no longer referenced (since their referencing JCR nodes wil have long been deleted)
            if (!preview) {
                removeOrphanedPages(existingPages, createdPages);
            }
        }
    } else {
        messageSummary.addIndividualProcessOutcome(new ProcessOutcome("Could not locate manifest file in the location: " + manifestFile, ProcessOutcome.ERROR));
    }
    return messageSummary;
}
Also used : InputStream(java.io.InputStream) ManifestWrapper(uk.nhs.digital.arc.json.ManifestWrapper) ArrayList(java.util.ArrayList) FilePathData(uk.nhs.digital.arc.util.FilePathData) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with FilePathData

use of uk.nhs.digital.arc.util.FilePathData in project hippo by NHS-digital-website.

the class ManifestProcessor method loadAndProcessPage.

/**
 * Each segment is processed here and the outcome recorded
 * @param docbase the base for all S3 files and the manifest itself
 * @param page the item we are about to process
 * @param objectMapper the object mapper is used to transform json data into objects. It's useful to keep one instance of this only
 * @return the outcome of the process
 * @throws IOException should we encounter an exception during processing
 */
private ProcessOutcome loadAndProcessPage(String docbase, Page page, ObjectMapper objectMapper) throws IOException {
    FilePathData filePathData = new FilePathData(docbase, page.getPageRef());
    ProcessOutcome processOutcome = new ProcessOutcome("Now checking the manifest segment '" + page.getPageRef() + "', which is used by the page '" + page.getPageName() + "' ...\n");
    // We only deal with S3 objects that exist at the moment
    if (filePathData.isS3Protocol() && storageManager.fileExists(filePathData)) {
        InputStream in = storageManager.getFileInputStream(filePathData);
        try {
            final Class jsonDataClass = JsonClassFactory.getJsonDataClassFromDocumentType(page.getDocumentType());
            final ArcDoctype jsonObject = (ArcDoctype) objectMapper.readValue(in, jsonDataClass);
            if (!preview) {
                addPublicationSystemDocument(docbase, page.getDocumentType(), page.getPageName(), jsonObject);
            } else {
                checkValidityOfUrls(docbase, jsonObject.getAllReferencedExternalUrls(), processOutcome);
            }
            if (!processOutcome.isInError()) {
                processOutcome.addMessageLine("... parsed OK\n\n");
            } else {
                processOutcome.addErrorMessageLine("... errors above will stop processing from continuing. Please adjust file locations where necessary\n\n");
            }
        } catch (Exception e) {
            processOutcome.addErrorMessageLine("\nError encountered during parsing:\n");
            processOutcome.addErrorMessageLine(ExceptionUtils.getStackTrace(e) + "\n");
        }
    } else {
        processOutcome.addErrorMessageLine("** Could not find the segment: " + filePathData.getFilePath() + " - check docbase and filename form a valid path\n");
    }
    return processOutcome;
}
Also used : InputStream(java.io.InputStream) FilePathData(uk.nhs.digital.arc.util.FilePathData) ArcDoctype(uk.nhs.digital.arc.json.ArcDoctype) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Example 5 with FilePathData

use of uk.nhs.digital.arc.util.FilePathData in project hippo by NHS-digital-website.

the class PubSysImagesectionTransformer method getImageDataFromS3File.

private void getImageDataFromS3File(ContentNode sectionNode) {
    FilePathData sourceFilePathUtils = new FilePathData(docbase, imageSection.getImageReq());
    ArcFileData metadata = storageManager.getFileMetaData(sourceFilePathUtils);
    try {
        ContentNode newNode = new ContentNode(PUBLICATIONSYSTEM_IMAGE, PUBLICATIONSYSTEM_RESOURCE);
        this.addFileRelatedProperties(newNode, new BinaryValue(IOUtils.toByteArray(metadata.getDelegateStream())), metadata.getContentType(), sourceFilePathUtils.getFilename());
        sectionNode.addNode(newNode);
    } catch (IOException e) {
        LOGGER.error("Error attempting to create image data", e);
    }
}
Also used : ArcFileData(uk.nhs.digital.arc.storage.ArcFileData) FilePathData(uk.nhs.digital.arc.util.FilePathData) BinaryValue(org.onehippo.forge.content.pojo.model.BinaryValue) IOException(java.io.IOException) ContentNode(org.onehippo.forge.content.pojo.model.ContentNode)

Aggregations

FilePathData (uk.nhs.digital.arc.util.FilePathData)6 IOException (java.io.IOException)4 InputStream (java.io.InputStream)3 RepositoryException (javax.jcr.RepositoryException)2 BinaryValue (org.onehippo.forge.content.pojo.model.BinaryValue)2 ContentNode (org.onehippo.forge.content.pojo.model.ContentNode)2 ArcFileData (uk.nhs.digital.arc.storage.ArcFileData)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BufferedInputStream (java.io.BufferedInputStream)1 ArrayList (java.util.ArrayList)1 Binary (javax.jcr.Binary)1 Test (org.junit.Test)1 ArcDoctype (uk.nhs.digital.arc.json.ArcDoctype)1 ManifestWrapper (uk.nhs.digital.arc.json.ManifestWrapper)1 S3ObjectMetadata (uk.nhs.digital.externalstorage.s3.S3ObjectMetadata)1