Search in sources :

Example 1 with InlineAttachment

use of uk.nhs.adaptors.pss.translator.model.InlineAttachment 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)

Example 2 with InlineAttachment

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

the class AttachmentReferenceUpdaterService method updateReferenceToAttachment.

public String updateReferenceToAttachment(List<InboundMessage.Attachment> attachments, String conversationId, String payloadStr) throws ValidationException, AttachmentNotFoundException, InlineAttachmentProcessingException {
    if (conversationId == null || conversationId.isEmpty()) {
        throw new ValidationException("ConversationId cannot be null or empty");
    }
    String resultPayload = payloadStr;
    if (attachments != null) {
        for (InboundMessage.Attachment attachment : attachments) {
            try {
                if (!xmlParseUtilService.parseIsSkeleton(attachment.getDescription())) {
                    InlineAttachment inlineAttachment = new InlineAttachment(attachment);
                    String filename = inlineAttachment.getOriginalFilename();
                    // find "local" reference by finding the following:
                    // "<reference value=\"file://localhost/${filename}\" />"
                    var patternStr = String.format("<reference value=\"file://localhost/%s\" \\/>", filename);
                    Pattern pattern = Pattern.compile(patternStr);
                    Matcher matcher = pattern.matcher(resultPayload);
                    var matchFound = matcher.find();
                    if (matchFound) {
                        // update local ref with external reference
                        String fileLocation = storageManagerService.getFileLocation(filename, conversationId);
                        var replaceStr = String.format("<reference value=\"%s\" />", xmlEscape(fileLocation));
                        resultPayload = matcher.replaceAll(replaceStr);
                    } else {
                        var message = String.format("Could not find file %s in payload", filename);
                        throw new AttachmentNotFoundException(message);
                    }
                }
            } catch (ParseException ex) {
                throw new InlineAttachmentProcessingException("Unable to parse inline attachment description: " + ex.getMessage());
            } catch (AttachmentNotFoundException e) {
                throw new AttachmentNotFoundException(e.getMessage());
            }
        }
    }
    return resultPayload;
}
Also used : Pattern(java.util.regex.Pattern) AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) ValidationException(javax.xml.bind.ValidationException) Matcher(java.util.regex.Matcher) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) InlineAttachment(uk.nhs.adaptors.pss.translator.model.InlineAttachment) ParseException(java.text.ParseException) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)

Aggregations

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