From 61032d572f77f658fd67c553fccde102053a69eb Mon Sep 17 00:00:00 2001 From: Vincenzo Ingenito <54367888+vincenzo-ingenito@users.noreply.github.com> Date: Mon, 29 Jan 2024 09:44:07 +0100 Subject: [PATCH] fix: Extract cda.xml and CDA.XML --- .../ms/gtw/dispatcher/utility/PDFUtility.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/utility/PDFUtility.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/utility/PDFUtility.java index fc7637c2..dc76cc60 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/utility/PDFUtility.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/utility/PDFUtility.java @@ -39,24 +39,22 @@ import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.AttachmentDTO; import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.BusinessException; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; @Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class PDFUtility { - private PDFUtility() {} - + public static String extractCDAFromAttachments(final byte[] cda, final String cdaAttachmentName) { String out = null; final Map attachments = extractAttachments(cda); if (!attachments.isEmpty()) { - if (attachments.size() == 1) { - out = PDFUtility.detectCharsetAndExtract(attachments.values().iterator().next().getContent()); - } else { - final AttachmentDTO attDTO = attachments.get(cdaAttachmentName); - if (attDTO != null) { - out = PDFUtility.detectCharsetAndExtract(attDTO.getContent()); - } + final AttachmentDTO attDTO = attachments.get(cdaAttachmentName); + if (attDTO != null) { + out = PDFUtility.detectCharsetAndExtract(attDTO.getContent()); } } return out; @@ -76,7 +74,7 @@ private static Map extractAttachments(byte[] bytePDF) { mimeType(entry.getValue().getEmbeddedFile().getSubtype()). content(entry.getValue().getEmbeddedFile().toByteArray()). build(); - out.put(entry.getKey(), att); + out.put(entry.getKey().toLowerCase(), att); } } catch (Exception e) { log.warn("Errore in fase di estrazione allegati da pdf."); @@ -162,7 +160,6 @@ public static boolean isPdf(byte[] pdf) { public static String detectCharsetAndExtract(byte[] bytes) { Charset detectedCharset = StandardCharsets.UTF_8; try { - XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);