Skip to content

Commit d417203

Browse files
committed
#346: Add option to parse MimeMessage without fetching attachment data from server -> Properly return named datasource without fetching all the data if unwanted
1 parent 4ed07a7 commit d417203

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

modules/core-module/src/main/java/org/simplejavamail/internal/util/MiscUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,17 @@ public static String randomCid10() {
361361

362362
return buffer.toString().toLowerCase();
363363
}
364+
365+
/**
366+
* @param fullMimeType the mime type from the mail api
367+
* @return The real mime type
368+
*/
369+
@NotNull
370+
public static String parseBaseMimeType(@NotNull final String fullMimeType) {
371+
final int pos = fullMimeType.indexOf(';');
372+
if (pos >= 0) {
373+
return fullMimeType.substring(0, pos);
374+
}
375+
return fullMimeType;
376+
}
364377
}

modules/core-module/src/main/java/org/simplejavamail/internal/util/NamedDataSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public OutputStream getOutputStream()
6060
*/
6161
@Override
6262
public String getContentType() {
63-
return dataSource.getContentType();
63+
return MiscUtil.parseBaseMimeType(dataSource.getContentType());
6464
}
6565

6666
/**

modules/simple-java-mail/src/main/java/org/simplejavamail/converter/internal/mimemessage/MimeMessageParser.java

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.jetbrains.annotations.NotNull;
55
import org.jetbrains.annotations.Nullable;
66
import org.simplejavamail.internal.util.MiscUtil;
7+
import org.simplejavamail.internal.util.NamedDataSource;
78
import org.simplejavamail.internal.util.NaturalEntryKeyComparator;
89
import org.simplejavamail.internal.util.Preconditions;
910

@@ -385,21 +386,14 @@ public static DataHandler retrieveDataHandler(@NotNull final MimePart part) {
385386
private static DataSource createDataSource(@NotNull final MimePart part, final boolean fetchAttachmentData) {
386387
final DataSource dataSource = retrieveDataHandler(part).getDataSource();
387388
final String dataSourceName = parseDataSourceName(part, dataSource);
388-
final String contentType = parseBaseMimeType(dataSource.getContentType());
389-
return createByteArrayDataSource(dataSource, dataSourceName, contentType, fetchAttachmentData);
390-
}
391-
392-
@NotNull
393-
private static ByteArrayDataSource createByteArrayDataSource(DataSource dataSource, String dataSourceName, String contentType, boolean fetchAttachmentData) {
394-
final InputStream is = retrieveInputStream(dataSource);
395-
try {
396-
final ByteArrayDataSource result = fetchAttachmentData
397-
? new ByteArrayDataSource(readContent(is), contentType)
398-
: new ByteArrayDataSource(is, contentType);
389+
390+
if (fetchAttachmentData) {
391+
final String contentType = MiscUtil.parseBaseMimeType(dataSource.getContentType());
392+
final ByteArrayDataSource result = new ByteArrayDataSource(readContent(retrieveInputStream(dataSource)), contentType);
399393
result.setName(dataSourceName);
400394
return result;
401-
} catch (IOException e) {
402-
throw new MimeMessageParseException(MimeMessageParseException.ERROR_GETTING_INPUTSTREAM, e);
395+
} else {
396+
return new NamedDataSource(dataSourceName, dataSource);
403397
}
404398
}
405399

@@ -436,20 +430,6 @@ private static byte[] readContent(@NotNull final InputStream is) {
436430
}
437431
}
438432

439-
/**
440-
* @param fullMimeType the mime type from the mail api
441-
* @return The real mime type
442-
*/
443-
@NotNull
444-
private static String parseBaseMimeType(@NotNull final String fullMimeType) {
445-
final int pos = fullMimeType.indexOf(';');
446-
if (pos >= 0) {
447-
return fullMimeType.substring(0, pos);
448-
}
449-
return fullMimeType;
450-
}
451-
452-
453433
@SuppressWarnings("WeakerAccess")
454434
@NotNull
455435
public static List<InternetAddress> parseToAddresses(@NotNull final MimeMessage mimeMessage) {

0 commit comments

Comments
 (0)