Skip to content

Commit d86eb35

Browse files
committed
#508: provide consistent api regarding Session parameter and use Session.getInstance instead of Session.getDefaultInstance to avoid tainting global state. Also fixed a bug emlToEmailBuilder used emlToMimeMessage
1 parent 687ac7f commit d86eb35

File tree

1 file changed

+95
-19
lines changed

1 file changed

+95
-19
lines changed

modules/simple-java-mail/src/main/java/org/simplejavamail/converter/EmailConverter.java

Lines changed: 95 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,19 @@ public static Email emlToEmail(@NotNull final InputStream emlInputStream) {
290290
}
291291

292292
/**
293-
* Delegates to {@link #emlToEmailBuilder(InputStream, Pkcs12Config)} with the full string value read from the given <code>InputStream</code>.
293+
* Delegates to {@link #emlToEmail(InputStream, Pkcs12Config, Session)} using a dummy {@link Session} instance.
294294
*/
295295
@NotNull
296296
public static Email emlToEmail(@NotNull final InputStream emlInputStream, @Nullable final Pkcs12Config pkcs12Config) {
297-
return emlToEmailBuilder(emlInputStream, pkcs12Config).buildEmail();
297+
return emlToEmail(emlInputStream, pkcs12Config, createDummySession());
298+
}
299+
300+
/**
301+
* Delegates to {@link #emlToEmailBuilder(InputStream, Pkcs12Config)} with the full string value read from the given <code>InputStream</code>.
302+
*/
303+
@NotNull
304+
public static Email emlToEmail(@NotNull final InputStream emlInputStream, @Nullable final Pkcs12Config pkcs12Config, @NotNull final Session session) {
305+
return emlToEmailBuilder(emlInputStream, pkcs12Config, session).buildEmail();
298306
}
299307

300308
/**
@@ -306,11 +314,19 @@ public static Email emlToEmail(@NotNull final String eml) {
306314
}
307315

308316
/**
309-
* Delegates to {@link #emlToEmailBuilder(String, Pkcs12Config)}.
317+
* Delegates to {@link #emlToEmail(String, Pkcs12Config, Session)} using a dummy {@link Session} instance.
310318
*/
311319
@NotNull
312320
public static Email emlToEmail(@NotNull final String eml, @Nullable final Pkcs12Config pkcs12Config) {
313-
return emlToEmailBuilder(eml, pkcs12Config).buildEmail();
321+
return emlToEmail(eml, pkcs12Config, createDummySession());
322+
}
323+
324+
/**
325+
* Delegates to {@link #emlToEmailBuilder(String, Pkcs12Config, Session)}.
326+
*/
327+
@NotNull
328+
public static Email emlToEmail(@NotNull final String eml, @Nullable final Pkcs12Config pkcs12Config, @NotNull final Session session) {
329+
return emlToEmailBuilder(eml, pkcs12Config, session).buildEmail();
314330
}
315331

316332
/**
@@ -322,65 +338,125 @@ public static Email emlToEmail(@NotNull final File emlFile) {
322338
}
323339

324340
/**
325-
* Delegates to {@link #emlToEmailBuilder(File, Pkcs12Config)}.
341+
* Delegates to {@link #emlToEmail(File, Pkcs12Config, Session)} using a dummy {@link Session} instance.
326342
*/
327343
@NotNull
328344
public static Email emlToEmail(@NotNull final File emlFile, @Nullable final Pkcs12Config pkcs12Config) {
329-
return emlToEmailBuilder(emlFile, pkcs12Config).buildEmail();
345+
return emlToEmail(emlFile, pkcs12Config, createDummySession());
330346
}
331347

332348
/**
333-
* Delegates to {@link #emlToEmailBuilder(File, Pkcs12Config)}.
349+
* Delegates to {@link #emlToEmailBuilder(File, Pkcs12Config, Session)}.
350+
*/
351+
@NotNull
352+
public static Email emlToEmail(@NotNull final File emlFile, @Nullable final Pkcs12Config pkcs12Config, @NotNull final Session session) {
353+
return emlToEmailBuilder(emlFile, pkcs12Config, session).buildEmail();
354+
}
355+
356+
/**
357+
* Delegates to {@link #emlToEmailBuilder(File, Session)} using a dummy {@link Session} instance.
334358
*/
335359
@NotNull
336360
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final File emlFile) {
337-
return emlToEmailBuilder(emlFile, null);
361+
return emlToEmailBuilder(emlFile, createDummySession());
338362
}
339363

340364
/**
341-
* Delegates to {@link #emlToMimeMessage(File)} and then {@link #mimeMessageToEmailBuilder(MimeMessage, Pkcs12Config)}.
365+
* Delegates to {@link #emlToEmailBuilder(File, Pkcs12Config, Session)}.
366+
*/
367+
@NotNull
368+
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final File emlFile, @NotNull final Session session) {
369+
return emlToEmailBuilder(emlFile, null, session);
370+
}
371+
372+
/**
373+
* Delegates to {@link #emlToEmailBuilder(File, Pkcs12Config, Session)} using a dummy {@link Session} instance.
342374
*/
343375
@NotNull
344376
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final File emlFile, @Nullable final Pkcs12Config pkcs12Config) {
345-
return mimeMessageToEmailBuilder(emlToMimeMessage(emlFile), pkcs12Config);
377+
return emlToEmailBuilder(emlFile, pkcs12Config, createDummySession());
346378
}
347379

348380
/**
349-
* Delegates to {@link #emlToEmailBuilder(InputStream, Pkcs12Config)}.
381+
* Delegates to {@link #emlToEmailBuilder(InputStream, Pkcs12Config, Session)}.
382+
*/
383+
@NotNull
384+
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final File emlFile, @Nullable final Pkcs12Config pkcs12Config, @NotNull final Session session) {
385+
try {
386+
return emlToEmailBuilder(new FileInputStream(checkNonEmptyArgument(emlFile, "emlFile")), pkcs12Config, session);
387+
} catch (final FileNotFoundException e) {
388+
throw new EmailConverterException(format(EmailConverterException.PARSE_ERROR_EML_FROM_FILE, e.getMessage()), e);
389+
}
390+
}
391+
392+
/**
393+
* Delegates to {@link #emlToEmailBuilder(InputStream, Session)} using a dummy {@link Session} instance.
350394
*/
351395
@NotNull
352396
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final InputStream emlInputStream) {
353-
return emlToEmailBuilder(emlInputStream, null);
397+
return emlToEmailBuilder(emlInputStream, createDummySession());
354398
}
355399

356400
/**
357-
* Delegates to {@link #emlToEmail(String)} with the full string value read from the given <code>InputStream</code>.
401+
* Delegates to {@link #emlToEmailBuilder(InputStream, Pkcs12Config, Session)}.
402+
*/
403+
@NotNull
404+
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final InputStream emlInputStream, @NotNull final Session session) {
405+
return emlToEmailBuilder(emlInputStream, null, session);
406+
}
407+
408+
/**
409+
* Delegates to {@link #emlToEmailBuilder(InputStream, Pkcs12Config, Session)} using a dummy {@link Session} instance.
358410
*/
359411
@NotNull
360412
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final InputStream emlInputStream, @Nullable final Pkcs12Config pkcs12Config) {
413+
return emlToEmailBuilder(emlInputStream, pkcs12Config, createDummySession());
414+
}
415+
416+
/**
417+
* Delegates to {@link #emlToEmailBuilder(String, Pkcs12Config, Session)} with the full string value read from the given <code>InputStream</code>.
418+
*/
419+
@NotNull
420+
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final InputStream emlInputStream, @Nullable final Pkcs12Config pkcs12Config, @NotNull final Session session) {
361421
try {
362422
String emlStr = readInputStreamToString(checkNonEmptyArgument(emlInputStream, "emlInputStream"), UTF_8);
363-
return emlToEmailBuilder(emlStr, pkcs12Config);
423+
return emlToEmailBuilder(emlStr, pkcs12Config, session);
364424
} catch (IOException e) {
365425
throw new EmailConverterException(EmailConverterException.ERROR_READING_EML_INPUTSTREAM, e);
366426
}
367427
}
368428

369429
/**
370-
* Delegates to {@link #emlToEmailBuilder(String, Pkcs12Config)}.
430+
* Delegates to {@link #emlToEmailBuilder(String, Session)} using a dummy {@link Session} instance.
371431
*/
372432
@NotNull
373433
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final String eml) {
374-
return emlToEmailBuilder(eml, null);
434+
return emlToEmailBuilder(eml, createDummySession());
435+
}
436+
437+
/**
438+
* Delegates to {@link #emlToEmailBuilder(String, Pkcs12Config, Session)}.
439+
*/
440+
@NotNull
441+
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final String eml, @NotNull final Session session) {
442+
return emlToEmailBuilder(eml, null, session);
443+
}
444+
445+
/**
446+
* Delegates to {@link #emlToEmailBuilder(String, Pkcs12Config, Session)} using a dummy {@link Session} instance.
447+
*/
448+
@NotNull
449+
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final String eml, @Nullable final Pkcs12Config pkcs12Config) {
450+
return emlToEmailBuilder(eml, pkcs12Config, createDummySession());
375451
}
376452

377453
/**
378454
* Delegates to {@link #emlToMimeMessage(String, Session)} using a dummy {@link Session} instance and passes the result to {@link
379455
* #mimeMessageToEmailBuilder(MimeMessage, Pkcs12Config)}.
380456
*/
381457
@NotNull
382-
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final String eml, @Nullable final Pkcs12Config pkcs12Config) {
383-
final MimeMessage mimeMessage = emlToMimeMessage(checkNonEmptyArgument(eml, "eml"), createDummySession());
458+
public static EmailPopulatingBuilder emlToEmailBuilder(@NotNull final String eml, @Nullable final Pkcs12Config pkcs12Config, @NotNull final Session session) {
459+
final MimeMessage mimeMessage = emlToMimeMessage(checkNonEmptyArgument(eml, "eml"), session);
384460
return mimeMessageToEmailBuilder(mimeMessage, pkcs12Config);
385461
}
386462

@@ -698,7 +774,7 @@ private static EmailPopulatingBuilder buildEmailFromMimeMessage(@NotNull final E
698774
}
699775

700776
private static Session createDummySession() {
701-
return Session.getDefaultInstance(new Properties());
777+
return Session.getInstance(new Properties());
702778
}
703779

704780
}

0 commit comments

Comments
 (0)