11package org .simplejavamail .converter .internal .mimemessage ;
22
3+ import org .simplejavamail .internal .util .NaturalEntryKeyComparator ;
4+
35import javax .activation .DataHandler ;
46import javax .activation .DataSource ;
57import javax .annotation .Nonnull ;
2527import java .io .IOException ;
2628import java .io .InputStream ;
2729import java .io .UnsupportedEncodingException ;
28- import java .util .ArrayList ;
29- import java .util .Arrays ;
30- import java .util .Collections ;
31- import java .util .HashMap ;
32- import java .util .Iterator ;
33- import java .util .List ;
34- import java .util .Map ;
35- import java .util .TreeMap ;
30+ import java .util .*;
31+ import java .util .AbstractMap .SimpleEntry ;
3632
3733import static java .lang .String .format ;
3834import static org .simplejavamail .internal .util .MiscUtil .extractCID ;
@@ -133,13 +129,13 @@ private static void parseMimePartTree(@Nonnull final MimePart currentPart, @Nonn
133129 final DataSource ds = createDataSource (currentPart );
134130 // if the diposition is not provided, for now the part should be treated as inline (later non-embedded inline attachments are moved)
135131 if (Part .ATTACHMENT .equalsIgnoreCase (disposition )) {
136- parsedComponents .attachmentList .put ( parseResourceName ( parseContentID (currentPart ), parseFileName (currentPart )), ds );
132+ parsedComponents .attachmentList .add ( new SimpleEntry <>( parseResourceNameOrUnnamed ( parseContentID (currentPart ), parseFileName (currentPart )), ds ) );
137133 } else if (disposition == null || Part .INLINE .equalsIgnoreCase (disposition )) {
138134 if (parseContentID (currentPart ) != null ) {
139135 parsedComponents .cidMap .put (parseContentID (currentPart ), ds );
140136 } else {
141137 // contentID missing -> treat as standard attachment
142- parsedComponents .attachmentList .put ( parseResourceName ( null , parseFileName (currentPart )), ds );
138+ parsedComponents .attachmentList .add ( new SimpleEntry <>( parseResourceNameOrUnnamed ( null , parseFileName (currentPart )), ds ) );
143139 }
144140 } else {
145141 throw new IllegalStateException ("invalid attachment type" );
@@ -219,7 +215,13 @@ public static String parseDisposition(@Nonnull final MimePart currentPart) {
219215 }
220216
221217 @ Nonnull
222- private static String parseResourceName (@ Nullable final String possibleWrappedContentID , @ Nonnull final String fileName ) {
218+ private static String parseResourceNameOrUnnamed (@ Nullable final String possibleWrappedContentID , @ Nonnull final String fileName ) {
219+ String resourceName = parseResourceName (possibleWrappedContentID , fileName );
220+ return valueNullOrEmpty (resourceName ) ? "unnamed" : resourceName ;
221+ }
222+
223+ @ Nonnull
224+ private static String parseResourceName (@ Nullable String possibleWrappedContentID , @ Nonnull String fileName ) {
223225 if (!valueNullOrEmpty (possibleWrappedContentID )) {
224226 // https://regex101.com/r/46ulb2/1
225227 String unwrappedContentID = possibleWrappedContentID .replaceAll ("^<?(.*?)>?$" , "$1" );
@@ -231,7 +233,7 @@ private static String parseResourceName(@Nullable final String possibleWrappedCo
231233 return fileName ;
232234 }
233235 }
234-
236+
235237 @ SuppressWarnings ("WeakerAccess" )
236238 @ Nonnull
237239 public static List <Header > retrieveAllHeaders (@ Nonnull final MimePart part ) {
@@ -454,14 +456,15 @@ static void moveInvalidEmbeddedResourcesToAttachments(ParsedMimeMessageComponent
454456 Map .Entry <String , DataSource > cidEntry = it .next ();
455457 String cid = extractCID (cidEntry .getKey ());
456458 if (htmlContent == null || !htmlContent .contains ("cid:" + cid )) {
457- parsedComponents .attachmentList .put ( cid , cidEntry .getValue ());
459+ parsedComponents .attachmentList .add ( new SimpleEntry <>( cid , cidEntry .getValue () ));
458460 it .remove ();
459461 }
460462 }
461463 }
462464
463465 public static class ParsedMimeMessageComponents {
464- final Map <String , DataSource > attachmentList = new TreeMap <>();
466+ @ SuppressWarnings ("unchecked" )
467+ final Set <Map .Entry <String , DataSource >> attachmentList = new TreeSet <>(NaturalEntryKeyComparator .INSTANCE );
465468 final Map <String , DataSource > cidMap = new TreeMap <>();
466469 private final Map <String , Object > headers = new HashMap <>();
467470 private final List <InternetAddress > toAddresses = new ArrayList <>();
@@ -481,7 +484,7 @@ public String getMessageId() {
481484 return messageId ;
482485 }
483486
484- public Map <String , DataSource > getAttachmentList () {
487+ public Set < Map . Entry <String , DataSource > > getAttachmentList () {
485488 return attachmentList ;
486489 }
487490
0 commit comments