Search in sources :

Example 1 with StorageDataUploadWrapper

use of uk.nhs.adaptors.pss.translator.storage.StorageDataUploadWrapper in project nia-patient-switching-standard-adaptor by NHSDigital.

the class AttachmentHandlerService method storeAttachments.

public void storeAttachments(List<InboundMessage.Attachment> attachments, String conversationId) throws ValidationException, InlineAttachmentProcessingException {
    if (!StringUtils.hasText(conversationId)) {
        throw new ValidationException("ConversationId cannot be null or empty");
    }
    if (attachments != null) {
        for (InboundMessage.Attachment attachment : attachments) {
            try {
                InlineAttachment inlineAttachment = new InlineAttachment(attachment);
                byte[] decodedPayload = Base64.getMimeDecoder().decode(inlineAttachment.getPayload());
                byte[] payload;
                if (inlineAttachment.isCompressed()) {
                    GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(decodedPayload));
                    payload = inputStream.readAllBytes();
                } else {
                    payload = decodedPayload;
                }
                StorageDataUploadWrapper dataWrapper = new StorageDataUploadWrapper(attachment.getContentType(), conversationId, payload);
                String filename = inlineAttachment.getOriginalFilename();
                storageManagerService.uploadFile(filename, dataWrapper, conversationId);
            } catch (StorageException ex) {
                throw new InlineAttachmentProcessingException("Unable to upload inline attachment to storage: " + ex.getMessage());
            } catch (IOException ex) {
                throw new InlineAttachmentProcessingException("Unable to decompress attachment: " + ex.getMessage());
            } catch (ParseException ex) {
                throw new InlineAttachmentProcessingException("Unable to parse inline attachment description: " + ex.getMessage());
            }
        }
    }
}
Also used : ValidationException(javax.xml.bind.ValidationException) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) StorageDataUploadWrapper(uk.nhs.adaptors.pss.translator.storage.StorageDataUploadWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) InlineAttachment(uk.nhs.adaptors.pss.translator.model.InlineAttachment) ParseException(java.text.ParseException) StorageException(uk.nhs.adaptors.pss.translator.storage.StorageException) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ValidationException (javax.xml.bind.ValidationException)1 InlineAttachmentProcessingException (uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)1 InboundMessage (uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage)1 InlineAttachment (uk.nhs.adaptors.pss.translator.model.InlineAttachment)1 StorageDataUploadWrapper (uk.nhs.adaptors.pss.translator.storage.StorageDataUploadWrapper)1 StorageException (uk.nhs.adaptors.pss.translator.storage.StorageException)1