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());
}
}
}
}
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;
}
Aggregations