Skip to content

Commit

Permalink
#346: Add option to parse MimeMessage without fetching attachment dat…
Browse files Browse the repository at this point in the history
…a from server -> Properly return named datasource without fetching all the data if unwanted
  • Loading branch information
bbottema committed Dec 25, 2021
1 parent 4ed07a7 commit d417203
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
Expand Up @@ -361,4 +361,17 @@ public static String randomCid10() {

return buffer.toString().toLowerCase();
}

/**
* @param fullMimeType the mime type from the mail api
* @return The real mime type
*/
@NotNull
public static String parseBaseMimeType(@NotNull final String fullMimeType) {
final int pos = fullMimeType.indexOf(';');
if (pos >= 0) {
return fullMimeType.substring(0, pos);
}
return fullMimeType;
}
}
Expand Up @@ -60,7 +60,7 @@ public OutputStream getOutputStream()
*/
@Override
public String getContentType() {
return dataSource.getContentType();
return MiscUtil.parseBaseMimeType(dataSource.getContentType());
}

/**
Expand Down
Expand Up @@ -4,6 +4,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.simplejavamail.internal.util.MiscUtil;
import org.simplejavamail.internal.util.NamedDataSource;
import org.simplejavamail.internal.util.NaturalEntryKeyComparator;
import org.simplejavamail.internal.util.Preconditions;

Expand Down Expand Up @@ -385,21 +386,14 @@ public static DataHandler retrieveDataHandler(@NotNull final MimePart part) {
private static DataSource createDataSource(@NotNull final MimePart part, final boolean fetchAttachmentData) {
final DataSource dataSource = retrieveDataHandler(part).getDataSource();
final String dataSourceName = parseDataSourceName(part, dataSource);
final String contentType = parseBaseMimeType(dataSource.getContentType());
return createByteArrayDataSource(dataSource, dataSourceName, contentType, fetchAttachmentData);
}

@NotNull
private static ByteArrayDataSource createByteArrayDataSource(DataSource dataSource, String dataSourceName, String contentType, boolean fetchAttachmentData) {
final InputStream is = retrieveInputStream(dataSource);
try {
final ByteArrayDataSource result = fetchAttachmentData
? new ByteArrayDataSource(readContent(is), contentType)
: new ByteArrayDataSource(is, contentType);

if (fetchAttachmentData) {
final String contentType = MiscUtil.parseBaseMimeType(dataSource.getContentType());
final ByteArrayDataSource result = new ByteArrayDataSource(readContent(retrieveInputStream(dataSource)), contentType);
result.setName(dataSourceName);
return result;
} catch (IOException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_GETTING_INPUTSTREAM, e);
} else {
return new NamedDataSource(dataSourceName, dataSource);
}
}

Expand Down Expand Up @@ -436,20 +430,6 @@ private static byte[] readContent(@NotNull final InputStream is) {
}
}

/**
* @param fullMimeType the mime type from the mail api
* @return The real mime type
*/
@NotNull
private static String parseBaseMimeType(@NotNull final String fullMimeType) {
final int pos = fullMimeType.indexOf(';');
if (pos >= 0) {
return fullMimeType.substring(0, pos);
}
return fullMimeType;
}


@SuppressWarnings("WeakerAccess")
@NotNull
public static List<InternetAddress> parseToAddresses(@NotNull final MimeMessage mimeMessage) {
Expand Down

0 comments on commit d417203

Please sign in to comment.