From 92fb0762fbc315e7805ba2d601dac24d5632504b Mon Sep 17 00:00:00 2001 From: j-dimension Date: Sat, 16 Nov 2024 22:01:02 +0100 Subject: [PATCH] fixed mail parsing error related to inline images. close #2669 --- .github/workflows/main.yml | 2 +- .../utils/convert/MimeMessageObject.java | 26 +++++++++++++------ .../client/utils/convert/ParserUtil.java | 20 -------------- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a2e3f4ad7..6b5e418e3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -111,7 +111,7 @@ jobs: -Dsonar.organization=jlawyerorg -Dsonar.projectKey=jlawyerorg_j-lawyer-org -Dsonar.projectName=j-lawyer-org - -Dsonar.projectVersion=2.6.0 + -Dsonar.projectVersion=3.1-dev -Dsonar.java.binaries=. -Dsonar.java.source=11 diff --git a/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/MimeMessageObject.java b/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/MimeMessageObject.java index 344c81142..3f2a4351b 100644 --- a/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/MimeMessageObject.java +++ b/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/MimeMessageObject.java @@ -140,14 +140,24 @@ private Map getInlinedImage(Part part) throws Excepti walkMimeStructure(part, 0, (p, level) -> { String[] header = p.getHeader(CONTENT_ID); if (p.isMimeType(IMAGE_TYPE) && nonNull(header)) { - if (p.getContent() != null && !(p.getContent() instanceof BASE64DecoderStream)) { - log.warn("inline image has content of instance " + p.getContent().getClass().getName()); - } else { - try { - String imageBase64 = Base64.getEncoder().encodeToString(IOUtils.toByteArray((BASE64DecoderStream) p.getContent())); - result.put(header[0], new MimeMessageObject(imageBase64, new ContentType(p.getContentType()))); - } catch (Throwable t) { - log.error("unable to get content of inline image - skipping", t); + + Object contentObject=null; + try { + contentObject=p.getContent(); + } catch (Throwable t) { + log.error("Unable to extract content", t); + } + + if (contentObject != null) { + if (!(contentObject instanceof BASE64DecoderStream)) { + log.warn("inline image has content of instance " + contentObject.getClass().getName()); + } else { + try { + String imageBase64 = Base64.getEncoder().encodeToString(IOUtils.toByteArray((BASE64DecoderStream) contentObject)); + result.put(header[0], new MimeMessageObject(imageBase64, new ContentType(p.getContentType()))); + } catch (Throwable t) { + log.error("unable to get content of inline image - skipping", t); + } } } diff --git a/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/ParserUtil.java b/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/ParserUtil.java index 696c62b9f..40f71e704 100644 --- a/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/ParserUtil.java +++ b/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/ParserUtil.java @@ -1,35 +1,15 @@ package com.jdimension.jlawyer.client.utils.convert; -import static com.jdimension.jlawyer.client.utils.convert.Helper.DEFAULT_HTML_NAME; -import static com.jdimension.jlawyer.client.utils.convert.Helper.DEFAULT_PDF_NAME; import static com.jdimension.jlawyer.client.utils.convert.Helper.EMAIL_HEADER_ID; -import static com.jdimension.jlawyer.client.utils.convert.Helper.HEADER_TEMPLATE_CONTAINER; -import static com.jdimension.jlawyer.client.utils.convert.Helper.TEMP_DIR; -import static com.jdimension.jlawyer.client.utils.convert.Helper.UNKNOWN; -import static com.jdimension.jlawyer.client.utils.convert.Helper.readTemplate; -import static com.jdimension.jlawyer.client.utils.convert.Helper.writeToFile; import com.lowagie.text.DocumentException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; -//import org.simplejavamail.api.email.AttachmentResource; -//import org.simplejavamail.converter.EmailConverter; import javax.mail.MessagingException; -import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.List; -import static java.nio.charset.StandardCharsets.UTF_8; -import java.util.ArrayList; -import static java.util.Objects.isNull; -import static java.util.Optional.ofNullable; -import static org.apache.commons.collections4.CollectionUtils.isEmpty; -import static org.apache.commons.lang3.StringUtils.isBlank; import static org.apache.commons.lang3.StringUtils.isNotBlank; /**