Skip to content

Commit d21ea08

Browse files
committed
Finished migration to Java 1.8 (and initial minor cleanup)
1 parent b9bec5d commit d21ea08

File tree

8 files changed

+29
-82
lines changed

8 files changed

+29
-82
lines changed

pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.bbottema</groupId>
88
<artifactId>standard-project-parent</artifactId>
9-
<version>1.0.18</version>
9+
<version>1.0.24</version>
1010
</parent>
1111

1212
<groupId>org.simplejavamail</groupId>
@@ -19,8 +19,6 @@
1919
<inceptionYear>2021</inceptionYear>
2020

2121
<properties>
22-
<java.version>1.8</java.version>
23-
<se.eris.notnull.instrument>false</se.eris.notnull.instrument>
2422
<automaticModuleName>com.github.bbottema.java-utils-mail-smime</automaticModuleName>
2523
<!-- license plugin, see possible types here: -->
2624
<!-- https://github.com/mathieucarbou/license-maven-plugin/tree/master/license-maven-plugin/src/main/resources/com/mycila/maven/plugin/license/templates -->

src/main/java/net/markenwerk/utils/mail/smime/MimeUtil.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package net.markenwerk.utils.mail.smime;
22

33
import jakarta.mail.MessagingException;
4-
import jakarta.mail.Session;
54
import jakarta.mail.internet.MimeBodyPart;
6-
import jakarta.mail.internet.MimeMessage;
75
import java.io.ByteArrayInputStream;
86
import java.io.ByteArrayOutputStream;
97
import java.io.IOException;
@@ -20,18 +18,7 @@ final class MimeUtil {
2018

2119
private MimeUtil() {
2220
}
23-
24-
/**
25-
* Translates a {@link MimeMessage} into its MIME-canonical form.
26-
*/
27-
static MimeMessage canonicalize(Session session, MimeMessage mimeMessage) throws MessagingException, IOException {
28-
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
29-
OutputStream out = new MimeCanonicalOutputStream(buffer);
30-
mimeMessage.writeTo(out);
31-
out.close();
32-
return new MimeMessage(session, new ByteArrayInputStream(buffer.toByteArray()));
33-
}
34-
21+
3522
/**
3623
* Translates a {@link MimeBodyPart} into its MIME-canonical form.
3724
*/
@@ -50,7 +37,7 @@ static MimeBodyPart canonicalize(MimeBodyPart mimeBodyPart) throws MessagingExce
5037
private static class MimeCanonicalOutputStream extends java.io.FilterOutputStream {
5138

5239
int lastReadByte = -1;
53-
byte[] crlf = new byte[] { (byte) '\r', (byte) '\n' };
40+
final byte[] crlf = new byte[] { (byte) '\r', (byte) '\n' };
5441

5542
public MimeCanonicalOutputStream(java.io.OutputStream os) {
5643
super(os);
@@ -68,11 +55,11 @@ public void write(int b) throws java.io.IOException {
6855
lastReadByte = b;
6956
}
7057

71-
public void write(byte b[]) throws java.io.IOException {
58+
public void write(byte[] b) throws java.io.IOException {
7259
write(b, 0, b.length);
7360
}
7461

75-
public void write(byte b[], int off, int len) throws java.io.IOException {
62+
public void write(byte[] b, int off, int len) throws java.io.IOException {
7663
int start = off;
7764

7865
len = off + len;

src/main/java/net/markenwerk/utils/mail/smime/SmimeException.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,5 @@ public SmimeException(String message, Throwable cause) {
3434
public SmimeException(String message) {
3535
super(message);
3636
}
37-
38-
/**
39-
* Create a new {@code SmimeException} with the given cause.
40-
*
41-
* @param cause
42-
* The causing {@link Exception} wrapped by this
43-
* {@code SmimeException}.
44-
*/
45-
public SmimeException(Throwable cause) {
46-
super(cause);
47-
}
48-
37+
4938
}

src/main/java/net/markenwerk/utils/mail/smime/SmimeKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public List<String> getAssociatedAddresses() {
8585
}
8686

8787
private void extractAssociatedAddresses() {
88-
List<String> addresses = new ArrayList<String>();
88+
List<String> addresses = new ArrayList<>();
8989
try {
9090
X509Certificate certificate = getCertificate();
9191
if (null != certificate) {

src/main/java/net/markenwerk/utils/mail/smime/SmimeKeyStore.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
*/
2222
public class SmimeKeyStore {
2323

24-
private KeyStore keyStore = null;
24+
private final KeyStore keyStore;
2525

2626
/**
27-
* Creates a new {@code SmimeKeyStore} by loading a PKCS12 keystore from a
27+
* Creates a new {@code SmimeKeyStore} by loading a PKCS12 keystore from
2828
* the given input stream.
2929
*
3030
* <p>
@@ -41,7 +41,7 @@ public SmimeKeyStore(InputStream stream, char[] password) {
4141
}
4242

4343
/**
44-
* Creates a new {@code SmimeKeyStore} by loading a PKCS12 keystore from a
44+
* Creates a new {@code SmimeKeyStore} by loading a PKCS12 keystore from
4545
* the given input stream.
4646
*
4747
* <p>
@@ -166,7 +166,7 @@ private X509Certificate[] copy(Certificate[] certificateChain) {
166166
public Set<String> getPrivateKeyAliases() {
167167
try {
168168
Enumeration<String> aliases = keyStore.aliases();
169-
Set<String> aliasSet = new HashSet<String>();
169+
Set<String> aliasSet = new HashSet<>();
170170
while (aliases.hasMoreElements()) {
171171
String alias = aliases.nextElement();
172172
if (keyStore.isKeyEntry(alias))

src/main/java/net/markenwerk/utils/mail/smime/SmimeState.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public enum SmimeState {
3535
* Indicates that the {@link MimePart} or {@link MimeMultipart} is neither
3636
* S/MIME encrypted nor S/MIME signed.
3737
*/
38-
NEITHER;
39-
38+
NEITHER
39+
4040
}

src/main/java/net/markenwerk/utils/mail/smime/SmimeUtil.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import jakarta.mail.internet.MimePart;
5454
import java.io.IOException;
5555
import java.math.BigInteger;
56-
import java.security.GeneralSecurityException;
5756
import java.security.PrivateKey;
5857
import java.security.PublicKey;
5958
import java.security.Security;
@@ -84,6 +83,7 @@ public final class SmimeUtil {
8483
}
8584
}
8685

86+
@SuppressWarnings("unused")
8787
private SmimeUtil() {
8888
}
8989

@@ -143,23 +143,20 @@ public static MimeBodyPart encrypt(MimeBodyPart mimeBodyPart, X509Certificate ce
143143
try {
144144
SMIMEEnvelopedGenerator generator = prepareGenerator(certificate);
145145
OutputEncryptor encryptor = prepareEncryptor();
146-
147-
MimeBodyPart encryptedMimeBodyPart = generator.generate(mimeBodyPart, encryptor);
148-
return encryptedMimeBodyPart;
146+
147+
return generator.generate(mimeBodyPart, encryptor);
149148

150149
} catch (Exception e) {
151150
throw handledException(e);
152151
}
153152
}
154153

155154
private static void copyHeaders(MimeBodyPart fromBodyPart, MimeMessage toMessage) throws MessagingException {
156-
@SuppressWarnings("unchecked")
157155
Enumeration<Header> headers = fromBodyPart.getAllHeaders();
158156
copyHeaders(headers, toMessage);
159157
}
160158

161159
private static void copyHeaders(MimeMessage fromMessage, MimeMessage toMessage) throws MessagingException {
162-
@SuppressWarnings("unchecked")
163160
Enumeration<Header> headers = fromMessage.getAllHeaders();
164161
copyHeaders(headers, toMessage);
165162
}
@@ -186,7 +183,7 @@ private static OutputEncryptor prepareEncryptor() throws CMSException {
186183
}
187184

188185
/**
189-
* Decrypts a S/MIME encrypted MIME message and yields a new MIME message.
186+
* Decrypts an S/MIME encrypted MIME message and yields a new MIME message.
190187
*
191188
* @param session
192189
* The {@link Session} that is used in conjunction with the
@@ -215,7 +212,7 @@ public static MimeMessage decrypt(Session session, MimeMessage mimeMessage, Smim
215212
}
216213

217214
/**
218-
* Decrypts a S/MIME encrypted MIME body part and yields a new MIME body
215+
* Decrypts an S/MIME encrypted MIME body part and yields a new MIME body
219216
* part.
220217
*
221218
* @param mimeBodyPart
@@ -235,7 +232,7 @@ public static MimeBodyPart decrypt(MimeBodyPart mimeBodyPart, SmimeKey smimeKey)
235232
}
236233

237234
/**
238-
* Decrypts a S/MIME encrypted MIME multipart and yields a new MIME body
235+
* Decrypts an S/MIME encrypted MIME multipart and yields a new MIME body
239236
* part.
240237
*
241238
* @param mimeMultipart
@@ -274,7 +271,6 @@ private static byte[] decryptContent(SMIMEEnveloped smimeEnveloped, SmimeKey smi
274271
}
275272

276273
private static void copyHeaderLines(MimeMessage fromMessage, MimeMessage toMessage) throws MessagingException {
277-
@SuppressWarnings("unchecked")
278274
Enumeration<String> headerLines = fromMessage.getAllHeaderLines();
279275
while (headerLines.hasMoreElements()) {
280276
String nextElement = headerLines.nextElement();
@@ -327,8 +323,7 @@ private static SignerInfoGenerator getInfoGenerator(SmimeKey smimeKey) throws Op
327323

328324
PrivateKey privateKey = smimeKey.getPrivateKey();
329325
X509Certificate certificate = smimeKey.getCertificate();
330-
SignerInfoGenerator infoGenerator = builder.build("SHA256withRSA", privateKey, certificate);
331-
return infoGenerator;
326+
return builder.build("SHA256withRSA", privateKey, certificate);
332327
}
333328

334329
private static ASN1EncodableVector getSignedAttributes(SmimeKey smimeKey) {
@@ -351,19 +346,18 @@ private static IssuerAndSerialNumber getIssuerAndSerialNumber(SmimeKey smimeKey)
351346
X509Certificate certificate = smimeKey.getCertificate();
352347
BigInteger serialNumber = certificate.getSerialNumber();
353348
X500Name issuerName = new X500Name(certificate.getIssuerDN().getName());
354-
IssuerAndSerialNumber issuerAndSerialNumber = new IssuerAndSerialNumber(issuerName, serialNumber);
355-
return issuerAndSerialNumber;
349+
return new IssuerAndSerialNumber(issuerName, serialNumber);
356350
}
357351

358352
private static JcaCertStore getCertificateStore(SmimeKey smimeKey) throws CertificateEncodingException {
359353
Certificate[] certificateChain = smimeKey.getCertificateChain();
360354
X509Certificate certificate = smimeKey.getCertificate();
361355

362-
List<Certificate> certificateList = null;
356+
final List<Certificate> certificateList;
363357
if (certificateChain != null && certificateChain.length > 0) {
364358
certificateList = Arrays.asList(certificateChain);
365359
} else {
366-
certificateList = new ArrayList<Certificate>();
360+
certificateList = new ArrayList<>();
367361
certificateList.add(certificate);
368362
}
369363
return new JcaCertStore(certificateList);
@@ -383,6 +377,7 @@ private static JcaCertStore getCertificateStore(SmimeKey smimeKey) throws Certif
383377
* @return The new S/MIME signed {@link MimeMessage} or {@link SMTPMessage}.
384378
*/
385379
public static <T extends MimeMessage> T sign(Session session, T mimeMessage, SmimeKey smimeKey) {
380+
//noinspection unchecked
386381
return (mimeMessage instanceof SMTPMessage)
387382
? sign(mimeMessage, (T) new SMTPMessage(session), smimeKey)
388383
: sign(mimeMessage, (T) new MimeMessage(session), smimeKey);
@@ -411,7 +406,7 @@ private static MimeBodyPart extractMimeBodyPart(MimeMessage mimeMessage) throws
411406
}
412407

413408
/**
414-
* Checks the signature on a S/MIME signed MIME multipart.
409+
* Checks the signature on an S/MIME signed MIME multipart.
415410
*
416411
* @param mimeMultipart
417412
* The {@link MimeMultipart} to be checked.
@@ -427,7 +422,7 @@ public static boolean checkSignature(MimeMultipart mimeMultipart) {
427422
}
428423

429424
/**
430-
* Checks the signature on a S/MIME signed MIME part (i.e. MIME message).
425+
* Checks the signature on an S/MIME signed MIME part (i.e. MIME message).
431426
*
432427
* @param mimePart
433428
* The {@link MimePart} to be checked.
@@ -451,8 +446,7 @@ public static boolean checkSignature(MimePart mimePart) {
451446
/**
452447
* Checks a SMIMESigned to make sure that the signature matches.
453448
*/
454-
private static boolean checkSignature(SMIMESigned smimeSigned) throws MessagingException, IOException,
455-
GeneralSecurityException {
449+
private static boolean checkSignature(SMIMESigned smimeSigned) {
456450
try {
457451
boolean returnValue = true;
458452

@@ -547,7 +541,7 @@ private static SignerInformationVerifier getVerifier(X509Certificate certificate
547541
}
548542

549543
/**
550-
* Returns the signed MIME body part of a S/MIME signed MIME multipart.
544+
* Returns the signed MIME body part of an S/MIME signed MIME multipart.
551545
*
552546
* @param mimeMultipart
553547
* The {@link MimeMultipart} to be stripped off.
@@ -563,7 +557,7 @@ public static MimeBodyPart getSignedContent(MimeMultipart mimeMultipart) {
563557
}
564558

565559
/**
566-
* Returns the signed MIME body part of a S/MIME signed MIME part (i.e. MIME
560+
* Returns the signed MIME body part of an S/MIME signed MIME part (i.e. MIME
567561
* message).
568562
*
569563
* @param mimePart

src/main/java/net/markenwerk/utils/mail/smime/UpdatableMimeBodyPart.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import jakarta.mail.MessagingException;
44
import jakarta.mail.internet.MimeBodyPart;
5-
import java.io.InputStream;
65

76
/**
87
* A {@link MimeBodyPart} that exposes the method {@code updateHeaders()} with
@@ -14,26 +13,6 @@
1413
*/
1514
class UpdatableMimeBodyPart extends MimeBodyPart {
1615

17-
/**
18-
* Create a new {@code UpdatableMimeBodyPart}.
19-
*/
20-
public UpdatableMimeBodyPart() {
21-
super();
22-
}
23-
24-
/**
25-
* Create a new {@code UpdatableMimeBodyPart} by reading and parsing the
26-
* data from the specified input stream.
27-
*
28-
* @param in
29-
* The {@link InputStream} to be read.
30-
* @throws MessagingException
31-
* If the {@code MimeBodyPart} couldn't be read.
32-
*/
33-
public UpdatableMimeBodyPart(InputStream in) throws MessagingException {
34-
super(in);
35-
}
36-
3716
/**
3817
* Calls updateHeaders().
3918
*/

0 commit comments

Comments
 (0)