Search in sources :

Example 6 with InlineAttachmentProcessingException

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

the class EhrExtractMessageHandlerTest method When_HandleMessage_WithStoreAttachmentsThrows_Expect_InlineAttachmentProcessingException.

@Test
public void When_HandleMessage_WithStoreAttachmentsThrows_Expect_InlineAttachmentProcessingException() throws JAXBException, InlineAttachmentProcessingException {
    InboundMessage inboundMessage = new InboundMessage();
    Bundle bundle = new Bundle();
    bundle.setId("Test");
    inboundMessage.setPayload(readInboundMessagePayloadFromFile());
    inboundMessage.setAttachments(new ArrayList<>());
    PatientMigrationRequest migrationRequest = PatientMigrationRequest.builder().losingPracticeOdsCode(LOSING_ODE_CODE).winningPracticeOdsCode(WINNING_ODE_CODE).build();
    when(migrationRequestDao.getMigrationRequest(CONVERSATION_ID)).thenReturn(migrationRequest);
    doThrow(new InlineAttachmentProcessingException("Test Exception")).when(attachmentHandlerService).storeAttachments(any(), any());
    assertThrows(InlineAttachmentProcessingException.class, () -> ehrExtractMessageHandler.handleMessage(inboundMessage, CONVERSATION_ID));
}
Also used : PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) Bundle(org.hl7.fhir.dstu3.model.Bundle) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException) Test(org.junit.jupiter.api.Test)

Example 7 with InlineAttachmentProcessingException

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

Example 8 with InlineAttachmentProcessingException

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

the class InboundMessageMergingService method mergeAndBundleMessage.

public void mergeAndBundleMessage(String conversationId) throws JAXBException, JsonProcessingException {
    if (!StringUtils.hasText(conversationId)) {
        throw new ValidationException(CONVERSATION_ID_HAS_NOT_BEEN_GIVEN);
    }
    PatientMigrationRequest migrationRequest = migrationRequestDao.getMigrationRequest(conversationId);
    var inboundMessage = objectMapper.readValue(migrationRequest.getInboundMessage(), InboundMessage.class);
    RCMRIN030000UK06Message payload = unmarshallString(inboundMessage.getPayload(), RCMRIN030000UK06Message.class);
    try {
        var attachmentLogs = getUndeletedLogsForConversation(conversationId);
        var attachmentsContainSkeletonMessage = attachmentLogs.stream().anyMatch(log -> log.getSkeleton().equals(true));
        if (attachmentsContainSkeletonMessage) {
            findAndReplaceSkeleton(attachmentLogs, inboundMessage, conversationId);
        }
        // process attachments
        var bypassPayloadLoadingArray = new String[attachmentLogs.size()];
        Arrays.fill(bypassPayloadLoadingArray, "");
        var messageAttachments = attachmentHandlerService.buildInboundAttachmentsFromAttachmentLogs(attachmentLogs, Arrays.asList(bypassPayloadLoadingArray), conversationId);
        var newPayloadStr = attachmentReferenceUpdaterService.updateReferenceToAttachment(messageAttachments, conversationId, inboundMessage.getPayload());
        // process bundle
        inboundMessage.setPayload(newPayloadStr);
        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);
    } catch (InlineAttachmentProcessingException | SAXException | TransformerException | BundleMappingException | JAXBException | AttachmentNotFoundException | JsonProcessingException e) {
        LOGGER.error("failed to merge Large Message Parts", e);
        nackAckPreparationService.sendNackMessage(EHR_EXTRACT_CANNOT_BE_PROCESSED, payload, conversationId);
    }
}
Also used : AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) ValidationException(javax.xml.bind.ValidationException) JAXBException(javax.xml.bind.JAXBException) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) SAXException(org.xml.sax.SAXException) PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) BundleMappingException(uk.nhs.adaptors.pss.translator.exception.BundleMappingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TransformerException(javax.xml.transform.TransformerException) RCMRIN030000UK06Message(org.hl7.v3.RCMRIN030000UK06Message) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)

Example 9 with InlineAttachmentProcessingException

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

the class COPCMessageHandler method extractFragmentsAndLog.

private void extractFragmentsAndLog(PatientMigrationRequest migrationRequest, PatientAttachmentLog parentAttachmentLog, String conversationId, InboundMessage message) throws ParseException, SAXException, ValidationException, InlineAttachmentProcessingException {
    List<EbxmlReference> attachmentReferenceDescription = new ArrayList<>();
    attachmentReferenceDescription.addAll(xmlParseUtilService.getEbxmlAttachmentsData(message));
    // first item is always the message payload reference so skip it
    for (var index = 1; index < attachmentReferenceDescription.size(); index++) {
        var payloadReference = attachmentReferenceDescription.get(index);
        var descriptionString = "";
        var messageId = "";
        var fileUpload = false;
        // in this instance there should only ever be one CID on a fragment index file
        if (payloadReference.getHref().contains("cid:")) {
            messageId = payloadReference.getHref().substring(payloadReference.getHref().indexOf("cid:") + "cid:".length());
            descriptionString = message.getAttachments().get(0).getDescription();
            // upload the file
            attachmentHandlerService.storeAttachments(message.getAttachments(), conversationId);
            fileUpload = true;
        } else {
            var localMessageId = payloadReference.getHref().substring(payloadReference.getHref().indexOf("mid:") + "mid:".length());
            messageId = localMessageId;
            var externalAttachmentResult = message.getExternalAttachments().stream().filter(attachment -> attachment.getMessageId().equals(localMessageId)).findFirst();
            if (externalAttachmentResult == null || externalAttachmentResult.stream().count() != 1) {
                throw new ValidationException("External Attachment in payload header does not match a received External Attachment ID");
            }
            var externalAttachment = externalAttachmentResult.get();
            descriptionString = externalAttachment.getDescription();
        }
        PatientAttachmentLog fragmentLog = patientAttachmentLogService.findAttachmentLog(messageId, conversationId);
        if (fragmentLog != null) {
            updateFragmentLog(fragmentLog, parentAttachmentLog, descriptionString, index - 1, parentAttachmentLog.getLargeAttachment());
            patientAttachmentLogService.updateAttachmentLog(fragmentLog, conversationId);
        } else {
            PatientAttachmentLog newFragmentLog = buildPatientAttachmentLog(messageId, descriptionString, parentAttachmentLog.getMid(), migrationRequest.getId(), index - 1, fileUpload, parentAttachmentLog.getLargeAttachment());
            patientAttachmentLogService.addAttachmentLog(newFragmentLog);
        }
    }
}
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) ValidationException(javax.xml.bind.ValidationException) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) ArrayList(java.util.ArrayList) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference)

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