Skip to content

Commit

Permalink
fix: Extract cda.xml and CDA.XML
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzo-ingenito committed Jan 29, 2024
1 parent 247a053 commit 61032d5
Showing 1 changed file with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, AttachmentDTO> 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;
Expand All @@ -76,7 +74,7 @@ private static Map<String, AttachmentDTO> 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.");
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 61032d5

Please sign in to comment.