Skip to content

Commit 892fae4

Browse files
authored
Multipart performs blocking call in every instantiation #699 (#716)
* Multipart performs blocking call in every instantiation #699 Signed-off-by: Jorge Bescos Gascon <[email protected]>
1 parent 666ec99 commit 892fae4

File tree

10 files changed

+271
-52
lines changed

10 files changed

+271
-52
lines changed

api/src/main/java/jakarta/mail/BodyPart.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -39,13 +39,6 @@ public abstract class BodyPart implements Part {
3939
*/
4040
protected Multipart parent;
4141

42-
/**
43-
* Instance of stream provider.
44-
*
45-
* @since JavaMail 2.1
46-
*/
47-
protected final StreamProvider streamProvider = StreamProvider.provider();
48-
4942
/**
5043
* Creates a default {@code BodyPart}.
5144
*/
@@ -74,4 +67,14 @@ public Multipart getParent() {
7467
void setParent(Multipart parent) {
7568
this.parent = parent;
7669
}
70+
71+
@Override
72+
public StreamProvider getStreamProvider() throws MessagingException {
73+
if (parent != null) {
74+
return parent.getStreamProvider();
75+
} else {
76+
return Part.super.getStreamProvider();
77+
}
78+
}
79+
7780
}

api/src/main/java/jakarta/mail/Message.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,11 +17,13 @@
1717
package jakarta.mail;
1818

1919
import jakarta.mail.search.SearchTerm;
20+
import jakarta.mail.util.StreamProvider;
2021

2122
import java.io.InvalidObjectException;
2223
import java.io.ObjectStreamException;
2324
import java.io.Serializable;
2425
import java.util.Date;
26+
import java.util.ServiceConfigurationError;
2527

2628
/**
2729
* This class models an email message. This is an abstract class.
@@ -705,4 +707,22 @@ protected void setExpunged(boolean expunged) {
705707
public boolean match(SearchTerm term) throws MessagingException {
706708
return term.match(this);
707709
}
710+
711+
@Override
712+
public StreamProvider getStreamProvider() throws MessagingException {
713+
try {
714+
try {
715+
final Session s = this.session;
716+
if (s != null) {
717+
return s.getStreamProvider();
718+
} else {
719+
return Session.getDefaultInstance(System.getProperties(), null).getStreamProvider();
720+
}
721+
} catch (ServiceConfigurationError sce) {
722+
throw new IllegalStateException(sce);
723+
}
724+
} catch (RuntimeException re) {
725+
throw new NoSuchProviderException("Unable to get " + StreamProvider.class.getName(), re);
726+
}
727+
}
708728
}

api/src/main/java/jakarta/mail/Multipart.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -20,6 +20,7 @@
2020

2121
import java.io.IOException;
2222
import java.io.OutputStream;
23+
import java.util.ServiceConfigurationError;
2324
import java.util.Vector;
2425

2526
/**
@@ -61,13 +62,6 @@ public abstract class Multipart {
6162
*/
6263
protected Part parent;
6364

64-
/**
65-
* Instance of stream provider.
66-
*
67-
* @since JavaMail 2.1
68-
*/
69-
protected final StreamProvider streamProvider = StreamProvider.provider();
70-
7165
/**
7266
* Default constructor. An empty Multipart object is created.
7367
*/
@@ -266,4 +260,32 @@ public synchronized Part getParent() {
266260
public synchronized void setParent(Part parent) {
267261
this.parent = parent;
268262
}
263+
264+
/**
265+
* Obtains the {@link StreamProvider} from the parent, if possible.
266+
* Otherwise it obtains it from
267+
* {@link Session#getDefaultInstance(java.util.Properties, Authenticator)}.
268+
*
269+
* @return the StreamProvider implementation from the session.
270+
* @throws MessagingException if errors.
271+
*
272+
* @since JavaMail 2.2
273+
*/
274+
protected StreamProvider getStreamProvider() throws MessagingException {
275+
Part parent = this.parent;
276+
if (parent != null) {
277+
return parent.getStreamProvider();
278+
} else {
279+
try {
280+
try {
281+
return Session.getDefaultInstance(System.getProperties(), null).getStreamProvider();
282+
} catch (ServiceConfigurationError sce) {
283+
throw new IllegalStateException(sce);
284+
}
285+
} catch (RuntimeException re) {
286+
throw new NoSuchProviderException("Unable to get " + StreamProvider.class.getName(), re);
287+
}
288+
}
289+
}
290+
269291
}

api/src/main/java/jakarta/mail/Part.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,11 +17,13 @@
1717
package jakarta.mail;
1818

1919
import jakarta.activation.DataHandler;
20+
import jakarta.mail.util.StreamProvider;
2021

2122
import java.io.IOException;
2223
import java.io.InputStream;
2324
import java.io.OutputStream;
2425
import java.util.Enumeration;
26+
import java.util.ServiceConfigurationError;
2527

2628
/**
2729
* The <code>Part</code> interface is the common base interface for
@@ -453,4 +455,25 @@ Enumeration<Header> getMatchingHeaders(String[] header_names)
453455
*/
454456
Enumeration<Header> getNonMatchingHeaders(String[] header_names)
455457
throws MessagingException;
458+
459+
/**
460+
* Obtains the {@link StreamProvider}.
461+
* It defaults to {@link Session#getDefaultInstance(java.util.Properties, Authenticator)}.
462+
*
463+
* @return the StreamProvider.
464+
* @throws MessagingException if errors.
465+
*
466+
* @since JavaMail 2.2
467+
*/
468+
default StreamProvider getStreamProvider() throws MessagingException {
469+
try {
470+
try {
471+
return Session.getDefaultInstance(System.getProperties(), null).getStreamProvider();
472+
} catch (ServiceConfigurationError sce) {
473+
throw new IllegalStateException(sce);
474+
}
475+
} catch (RuntimeException re) {
476+
throw new NoSuchProviderException("Unable to get " + StreamProvider.class.getName(), re);
477+
}
478+
}
456479
}

api/src/main/java/jakarta/mail/internet/MimeBodyPart.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -30,7 +30,6 @@
3030
import jakarta.mail.Multipart;
3131
import jakarta.mail.Part;
3232
import jakarta.mail.util.LineOutputStream;
33-
import jakarta.mail.util.StreamProvider;
3433
import jakarta.mail.util.StreamProvider.EncoderTypes;
3534

3635
import java.io.BufferedInputStream;
@@ -1641,7 +1640,7 @@ static void writeTo(MimePart part, OutputStream os, String[] ignoreList)
16411640
} else {
16421641
Map<String, Object> params = new HashMap<>();
16431642
params.put("allowutf8", allowutf8);
1644-
los = StreamProvider.provider().outputLineStream(os, allowutf8);
1643+
los = part.getStreamProvider().outputLineStream(os, allowutf8);
16451644
}
16461645

16471646
// First, write out the header

api/src/main/java/jakarta/mail/internet/MimeMessage.java

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -29,7 +29,6 @@
2929
import jakarta.mail.Multipart;
3030
import jakarta.mail.Session;
3131
import jakarta.mail.util.LineOutputStream;
32-
import jakarta.mail.util.StreamProvider;
3332

3433
import java.io.BufferedInputStream;
3534
import java.io.ByteArrayInputStream;
@@ -45,8 +44,6 @@
4544
import java.util.Enumeration;
4645
import java.util.List;
4746
import java.util.Properties;
48-
import java.util.ServiceConfigurationError;
49-
5047

5148
/**
5249
* This class represents a MIME style email message. It implements
@@ -245,7 +242,7 @@ public MimeMessage(MimeMessage source) throws MessagingException {
245242
strict = source.strict;
246243
source.writeTo(bos);
247244
bos.close();
248-
try (InputStream bis = provider().inputSharedByteArray(bos.toByteArray())) {
245+
try (InputStream bis = getStreamProvider().inputSharedByteArray(bos.toByteArray())) {
249246
parse(bis);
250247
}
251248
saved = true;
@@ -1410,7 +1407,7 @@ protected InputStream getContentStream() throws MessagingException {
14101407
if (contentStream != null)
14111408
return ((SharedInputStream) contentStream).newStream(0, -1);
14121409
if (content != null) {
1413-
return provider().inputSharedByteArray(content);
1410+
return getStreamProvider().inputSharedByteArray(content);
14141411
}
14151412
throw new MessagingException("No MimeMessage content");
14161413
}
@@ -1917,7 +1914,7 @@ public void writeTo(OutputStream os, String[] ignoreList)
19171914
// Else, the content is untouched, so we can just output it
19181915
// First, write out the header
19191916
Enumeration<String> hdrLines = getNonMatchingHeaderLines(ignoreList);
1920-
LineOutputStream los = provider().outputLineStream(os, allowutf8);
1917+
LineOutputStream los = getStreamProvider().outputLineStream(os, allowutf8);
19211918
while (hdrLines.hasMoreElements())
19221919
los.writeln(hdrLines.nextElement());
19231920

@@ -2322,23 +2319,4 @@ protected MimeMessage createMimeMessage(Session session)
23222319
throws MessagingException {
23232320
return new MimeMessage(session);
23242321
}
2325-
2326-
private StreamProvider provider() throws MessagingException {
2327-
try {
2328-
try {
2329-
final Session s = this.session;
2330-
if (s != null) {
2331-
return s.getStreamProvider();
2332-
} else {
2333-
return Session.getDefaultInstance(System.getProperties(),
2334-
null).getStreamProvider();
2335-
}
2336-
} catch (ServiceConfigurationError sce) {
2337-
throw new IllegalStateException(sce);
2338-
}
2339-
} catch (RuntimeException re) {
2340-
throw new MessagingException("Unable to get "
2341-
+ StreamProvider.class.getName(), re);
2342-
}
2343-
}
23442322
}

api/src/main/java/jakarta/mail/internet/MimeMultipart.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -520,7 +520,7 @@ public synchronized void writeTo(OutputStream os)
520520

521521
String boundary = "--" +
522522
(new ContentType(contentType)).getParameter("boundary");
523-
LineOutputStream los = streamProvider.outputLineStream(os, false);
523+
LineOutputStream los = getStreamProvider().outputLineStream(os, false);
524524
// if there's a preamble, write it out
525525
if (preamble != null) {
526526
byte[] pb = MimeUtility.getBytes(preamble);
@@ -601,7 +601,7 @@ protected synchronized void parse() throws MessagingException {
601601

602602
try {
603603
// Skip and save the preamble
604-
LineInputStream lin = streamProvider.inputLineStream(in, false);
604+
LineInputStream lin = getStreamProvider().inputLineStream(in, false);
605605
StringBuilder preamblesb = null;
606606
String line;
607607
while ((line = lin.readLine()) != null) {

api/src/main/java/jakarta/mail/internet/PreencodedMimeBodyPart.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -78,7 +78,7 @@ public void writeTo(OutputStream os)
7878
if (os instanceof LineOutputStream) {
7979
los = (LineOutputStream) os;
8080
} else {
81-
los = streamProvider.outputLineStream(os, false);
81+
los = getStreamProvider().outputLineStream(os, false);
8282
}
8383

8484
// First, write out the header

0 commit comments

Comments
 (0)