Search in sources :

Example 1 with InlineAttachmentProcessingException

use of uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException 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 InlineAttachmentProcessingException

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

the class COPCMessageHandler method checkAndMergeFileParts.

public void checkAndMergeFileParts(InboundMessage inboundMessage, String conversationId) throws SAXException, AttachmentLogException, ValidationException, InlineAttachmentProcessingException {
    Document ebXmlDocument = xPathService.parseDocumentFromXml(inboundMessage.getEbXML());
    var inboundMessageId = xPathService.getNodeValue(ebXmlDocument, MESSAGE_ID_PATH);
    var currentAttachmentLog = patientAttachmentLogService.findAttachmentLog(inboundMessageId, conversationId);
    if (currentAttachmentLog == null) {
        throw new AttachmentLogException("Given COPC message is missing an attachment log");
    }
    // if a message has arrived early, it will not have a parent ID so we can cancel early.
    if (currentAttachmentLog.getParentMid() == null) {
        return;
    }
    var conversationAttachmentLogs = patientAttachmentLogService.findAttachmentLogs(conversationId);
    var attachmentLogFragments = conversationAttachmentLogs.stream().sorted(Comparator.comparingInt(PatientAttachmentLog::getOrderNum)).filter(log -> !(log.getParentMid() == null) && log.getParentMid().equals(currentAttachmentLog.getParentMid())).toList();
    var parentLogMessageId = attachmentLogFragments.size() == 1 ? currentAttachmentLog.getMid() : currentAttachmentLog.getParentMid();
    attachmentLogFragments = conversationAttachmentLogs.stream().sorted(Comparator.comparingInt(PatientAttachmentLog::getOrderNum)).filter(log -> !(log.getParentMid() == null) && log.getParentMid().equals(parentLogMessageId)).toList();
    var allFragmentsHaveUploaded = attachmentLogFragments.stream().allMatch(PatientAttachmentLog::getUploaded);
    if (allFragmentsHaveUploaded) {
        String payload = attachmentHandlerService.buildSingleFileStringFromPatientAttachmentLogs(attachmentLogFragments, conversationId);
        var parentLogFile = conversationAttachmentLogs.stream().filter(log -> log.getMid().equals(parentLogMessageId)).findAny().orElse(null);
        var mergedLargeAttachment = createNewLargeAttachmentInList(parentLogFile, payload);
        attachmentHandlerService.storeAttachments(mergedLargeAttachment, conversationId);
        var updatedLog = PatientAttachmentLog.builder().mid(parentLogFile.getMid()).uploaded(true).build();
        patientAttachmentLogService.updateAttachmentLog(updatedLog, conversationId);
        attachmentLogFragments.forEach((PatientAttachmentLog log) -> {
            attachmentHandlerService.removeAttachment(log.getFilename(), conversationId);
            patientAttachmentLogService.deleteAttachmentLog(log.getMid(), conversationId);
        });
    }
}
Also used : Arrays(java.util.Arrays) AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Autowired(org.springframework.beans.factory.annotation.Autowired) NackAckPreparationService(uk.nhs.adaptors.pss.translator.service.NackAckPreparationService) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) COPCIN000001UK01Message(org.hl7.v3.COPCIN000001UK01Message) ArrayList(java.util.ArrayList) BundleMappingException(uk.nhs.adaptors.pss.translator.exception.BundleMappingException) Document(org.w3c.dom.Document) ParseException(java.text.ParseException) PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) InboundMessageMergingService(uk.nhs.adaptors.pss.translator.service.InboundMessageMergingService) PatientAttachmentLogService(uk.nhs.adaptors.connector.service.PatientAttachmentLogService) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference) AttachmentHandlerService(uk.nhs.adaptors.pss.translator.service.AttachmentHandlerService) XPathService(uk.nhs.adaptors.pss.translator.service.XPathService) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AttachmentLogException(uk.nhs.adaptors.pss.translator.exception.AttachmentLogException) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) JAXBException(javax.xml.bind.JAXBException) List(java.util.List) Component(org.springframework.stereotype.Component) Slf4j(lombok.extern.slf4j.Slf4j) SAXException(org.xml.sax.SAXException) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException) EHR_EXTRACT_CANNOT_BE_PROCESSED(uk.nhs.adaptors.pss.translator.model.NACKReason.EHR_EXTRACT_CANNOT_BE_PROCESSED) ValidationException(javax.xml.bind.ValidationException) PatientMigrationRequestDao(uk.nhs.adaptors.connector.dao.PatientMigrationRequestDao) Comparator(java.util.Comparator) XmlParseUtilService(uk.nhs.adaptors.pss.translator.util.XmlParseUtilService) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) Document(org.w3c.dom.Document) AttachmentLogException(uk.nhs.adaptors.pss.translator.exception.AttachmentLogException)

Example 3 with InlineAttachmentProcessingException

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

the class COPCMessageHandler method handleMessage.

public void handleMessage(InboundMessage inboundMessage, String conversationId) throws JAXBException, InlineAttachmentProcessingException, SAXException, AttachmentLogException, AttachmentNotFoundException, BundleMappingException, JsonProcessingException {
    COPCIN000001UK01Message payload = unmarshallString(inboundMessage.getPayload(), COPCIN000001UK01Message.class);
    PatientMigrationRequest migrationRequest = migrationRequestDao.getMigrationRequest(conversationId);
    try {
        Document ebXmlDocument = getEbXmlDocument(inboundMessage);
        String messageId = xPathService.getNodeValue(ebXmlDocument, MESSAGE_ID_PATH);
        PatientAttachmentLog patientAttachmentLog = patientAttachmentLogService.findAttachmentLog(messageId, conversationId);
        // If there is no PatientAttachmentLog for this message then we have receieved a message out of order
        if (patientAttachmentLog == null) {
            addLogForEarlyFragmentAndStore(inboundMessage, conversationId, payload, ebXmlDocument, migrationRequest.getId());
        } else {
            if (isManifestMessage(ebXmlDocument)) {
                extractFragmentsAndLog(migrationRequest, patientAttachmentLog, conversationId, inboundMessage);
            } else {
                storeCOPCAttachment(patientAttachmentLog, inboundMessage, conversationId);
                patientAttachmentLog.setUploaded(true);
                patientAttachmentLogService.updateAttachmentLog(patientAttachmentLog, conversationId);
            }
        }
        nackAckPreparationService.sendAckMessage(payload, conversationId, migrationRequest.getLosingPracticeOdsCode());
        checkAndMergeFileParts(inboundMessage, conversationId);
        // merge and uncompress large EHR message
        if (inboundMessageMergingService.canMergeCompleteBundle(conversationId)) {
            inboundMessageMergingService.mergeAndBundleMessage(conversationId);
        }
    } catch (ParseException | InlineAttachmentProcessingException | ValidationException | SAXException e) {
        LOGGER.error("failed to parse COPC_IN000001UK01 ebxml: " + "failed to extract \"mid:\" from xlink:href, before sending the continue message", e);
        nackAckPreparationService.sendNackMessage(EHR_EXTRACT_CANNOT_BE_PROCESSED, payload, conversationId);
    }
}
Also used : PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) ValidationException(javax.xml.bind.ValidationException) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) ParseException(java.text.ParseException) Document(org.w3c.dom.Document) COPCIN000001UK01Message(org.hl7.v3.COPCIN000001UK01Message) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException) SAXException(org.xml.sax.SAXException)

Example 4 with InlineAttachmentProcessingException

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

the class EhrExtractMessageHandler method handleMessage.

public void handleMessage(InboundMessage inboundMessage, String conversationId) throws JAXBException, JsonProcessingException, InlineAttachmentProcessingException, BundleMappingException, AttachmentNotFoundException, ParseException, SAXException {
    RCMRIN030000UK06Message payload = unmarshallString(inboundMessage.getPayload(), RCMRIN030000UK06Message.class);
    PatientMigrationRequest migrationRequest = migrationRequestDao.getMigrationRequest(conversationId);
    MigrationStatusLog migrationStatusLog = migrationStatusLogService.getLatestMigrationStatusLog(conversationId);
    migrationStatusLogService.addMigrationStatusLog(EHR_EXTRACT_RECEIVED, conversationId);
    try {
        Document ebXmlDocument = getEbXmlDocument(inboundMessage);
        String messageId = xPathService.getNodeValue(ebXmlDocument, MESSAGE_ID_PATH);
        boolean hasExternalAttachment = !(inboundMessage.getExternalAttachments() == null || inboundMessage.getExternalAttachments().isEmpty());
        // Manage attachments against the EHR message
        var attachments = inboundMessage.getAttachments();
        if (attachments != null) {
            attachmentHandlerService.storeAttachments(attachments, conversationId);
            for (var i = 0; i < attachments.size(); i++) {
                var attachment = attachments.get(i);
                PatientAttachmentLog newAttachmentLog = buildPatientAttachmentLogFromAttachment(messageId, migrationRequest, attachment);
                patientAttachmentLogService.addAttachmentLog(newAttachmentLog);
            }
        }
        if (!hasExternalAttachment) {
            var fileUpdatedPayload = attachmentReferenceUpdaterService.updateReferenceToAttachment(inboundMessage.getAttachments(), conversationId, inboundMessage.getPayload());
            inboundMessage.setPayload(fileUpdatedPayload);
            payload = unmarshallString(inboundMessage.getPayload(), RCMRIN030000UK06Message.class);
            var bundle = bundleMapperService.mapToBundle(payload, migrationRequest.getLosingPracticeOdsCode());
            migrationStatusLogService.updatePatientMigrationRequestAndAddMigrationStatusLog(conversationId, fhirParser.encodeToJson(bundle), objectMapper.writeValueAsString(inboundMessage), EHR_EXTRACT_TRANSLATED);
            nackAckPreparationService.sendAckMessage(payload, conversationId);
        }
        // sending continue message
        if (hasExternalAttachment) {
            String patientNhsNumber = XmlParseUtilService.parseNhsNumber(payload);
            for (InboundMessage.ExternalAttachment externalAttachment : inboundMessage.getExternalAttachments()) {
                PatientAttachmentLog patientAttachmentLog;
                // save COPC_UK01 messages
                patientAttachmentLog = buildPatientAttachmentLogFromExternalAttachment(migrationRequest, externalAttachment);
                patientAttachmentLogService.addAttachmentLog(patientAttachmentLog);
            }
            migrationStatusLogService.updatePatientMigrationRequestAndAddMigrationStatusLog(conversationId, null, objectMapper.writeValueAsString(inboundMessage), EHR_EXTRACT_TRANSLATED);
            sendContinueRequest(payload, conversationId, patientNhsNumber, migrationRequest.getWinningPracticeOdsCode(), migrationStatusLog.getDate().toInstant());
        }
    } catch (BundleMappingException | DataFormatException | JsonProcessingException | InlineAttachmentProcessingException | AttachmentNotFoundException | SAXException | StorageException ex) {
        nackAckPreparationService.sendNackMessage(EHR_EXTRACT_CANNOT_BE_PROCESSED, payload, conversationId);
        throw ex;
    } catch (ParseException ex) {
        throw ex;
    }
}
Also used : AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) DataFormatException(ca.uhn.fhir.parser.DataFormatException) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) MigrationStatusLog(uk.nhs.adaptors.connector.model.MigrationStatusLog) BundleMappingException(uk.nhs.adaptors.pss.translator.exception.BundleMappingException) ParseException(java.text.ParseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) StorageException(uk.nhs.adaptors.pss.translator.storage.StorageException) RCMRIN030000UK06Message(org.hl7.v3.RCMRIN030000UK06Message) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)

Example 5 with InlineAttachmentProcessingException

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

the class MhsQueueMessageHandler method handleMessage.

public boolean handleMessage(Message message) {
    try {
        InboundMessage inboundMessage = readMessage(message);
        Document ebXmlDocument = xPathService.parseDocumentFromXml(inboundMessage.getEbXML());
        String conversationId = xPathService.getNodeValue(ebXmlDocument, CONVERSATION_ID_PATH);
        applyConversationId(conversationId);
        String interactionId = xPathService.getNodeValue(ebXmlDocument, INTERACTION_ID_PATH);
        if (ACKNOWLEDGEMENT_INTERACTION_ID.equals(interactionId)) {
            acknowledgmentMessageHandler.handleMessage(inboundMessage, conversationId);
        } else if (EHR_EXTRACT_INTERACTION_ID.equals(interactionId)) {
            ehrExtractMessageHandler.handleMessage(inboundMessage, conversationId);
        } else if (CONTINUE_ATTACHMENT_INTERACTION_ID.equals(interactionId)) {
            continueMessageHandler.handleMessage(inboundMessage, conversationId);
        } else {
            LOGGER.info("Handling message with [{}] interaction id not implemented", interactionId);
        }
        return true;
    } catch (JMSException | JAXBException | SAXException e) {
        LOGGER.error("Unable to read the content of the inbound MHS message", e);
        return false;
    } catch (JsonProcessingException | DataFormatException e) {
        LOGGER.error("Unable to parse messages", e);
        return false;
    } catch (InlineAttachmentProcessingException | AttachmentLogException e) {
        LOGGER.error("Unable to process inline attachments", e);
        return false;
    } catch (AttachmentNotFoundException e) {
        LOGGER.error("Unable to find attachment reference inbound message", e);
        return false;
    } catch (BundleMappingException e) {
        LOGGER.error("Unable to map EHR Extract to FHIR bundle", e);
        return false;
    } catch (ParseException e) {
        LOGGER.error("Unable to parse Ebxml References", e);
        return false;
    }
// catch (SkeletonEhrProcessingException e) {
// LOGGER.error("Unable to process EhrExtract", e);
// return false;
// }
}
Also used : AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) JAXBException(javax.xml.bind.JAXBException) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) JMSException(javax.jms.JMSException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) AttachmentLogException(uk.nhs.adaptors.pss.translator.exception.AttachmentLogException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) BundleMappingException(uk.nhs.adaptors.pss.translator.exception.BundleMappingException) ParseException(java.text.ParseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)

Aggregations

InlineAttachmentProcessingException (uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)9 ParseException (java.text.ParseException)7 InboundMessage (uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage)7 ValidationException (javax.xml.bind.ValidationException)6 SAXException (org.xml.sax.SAXException)6 PatientMigrationRequest (uk.nhs.adaptors.connector.model.PatientMigrationRequest)6 AttachmentNotFoundException (uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 Document (org.w3c.dom.Document)5 BundleMappingException (uk.nhs.adaptors.pss.translator.exception.BundleMappingException)5 XmlUnmarshallUtil.unmarshallString (uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString)5 JAXBException (javax.xml.bind.JAXBException)4 PatientAttachmentLog (uk.nhs.adaptors.connector.model.PatientAttachmentLog)4 COPCIN000001UK01Message (org.hl7.v3.COPCIN000001UK01Message)3 AttachmentLogException (uk.nhs.adaptors.pss.translator.exception.AttachmentLogException)3 DataFormatException (ca.uhn.fhir.parser.DataFormatException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Comparator (java.util.Comparator)2 List (java.util.List)2