Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multipart performs blocking call in every instantiation #699 #716

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions api/src/main/java/jakarta/mail/BodyPart.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -39,13 +39,6 @@ public abstract class BodyPart implements Part {
*/
protected Multipart parent;

/**
* Instance of stream provider.
*
* @since JavaMail 2.1
*/
protected final StreamProvider streamProvider = StreamProvider.provider();
jmehrens marked this conversation as resolved.
Show resolved Hide resolved

/**
* Creates a default {@code BodyPart}.
*/
Expand Down Expand Up @@ -74,4 +67,14 @@ public Multipart getParent() {
void setParent(Multipart parent) {
this.parent = parent;
}

@Override
public StreamProvider getStreamProvider() throws MessagingException {
if (parent != null) {
return parent.getStreamProvider();
} else {
return Part.super.getStreamProvider();
}
}

}
22 changes: 21 additions & 1 deletion api/src/main/java/jakarta/mail/Message.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,11 +17,13 @@
package jakarta.mail;

import jakarta.mail.search.SearchTerm;
import jakarta.mail.util.StreamProvider;

import java.io.InvalidObjectException;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.Date;
import java.util.ServiceConfigurationError;

/**
* This class models an email message. This is an abstract class.
Expand Down Expand Up @@ -705,4 +707,22 @@ protected void setExpunged(boolean expunged) {
public boolean match(SearchTerm term) throws MessagingException {
return term.match(this);
}

@Override
public StreamProvider getStreamProvider() throws MessagingException {
try {
try {
final Session s = this.session;
if (s != null) {
return s.getStreamProvider();
} else {
return Session.getDefaultInstance(System.getProperties(), null).getStreamProvider();
}
} catch (ServiceConfigurationError sce) {
throw new IllegalStateException(sce);
}
} catch (RuntimeException re) {
throw new MessagingException("Unable to get " + StreamProvider.class.getName(), re);
}
}
}
29 changes: 21 additions & 8 deletions api/src/main/java/jakarta/mail/Multipart.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -61,13 +61,6 @@ public abstract class Multipart {
*/
protected Part parent;

/**
* Instance of stream provider.
*
* @since JavaMail 2.1
*/
protected final StreamProvider streamProvider = StreamProvider.provider();

/**
* Default constructor. An empty Multipart object is created.
*/
Expand Down Expand Up @@ -266,4 +259,24 @@ public synchronized Part getParent() {
public synchronized void setParent(Part parent) {
this.parent = parent;
}

/**
* Obtains the {@link StreamProvider} from the parent, if possible.
* Otherwise it obtains it from
* {@link Session#getDefaultInstance(java.util.Properties, Authenticator)}.
*
* @return the StreamProvider implementation from the session.
* @throws MessagingException if errors.
*
* @since JavaMail 2.2
*/
protected StreamProvider getStreamProvider() throws MessagingException {
Part parent = this.parent;
if (parent != null) {
return parent.getStreamProvider();
} else {
return Session.getDefaultInstance(System.getProperties(), null).getStreamProvider();
jmehrens marked this conversation as resolved.
Show resolved Hide resolved
}
}

}
16 changes: 15 additions & 1 deletion api/src/main/java/jakarta/mail/Part.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,6 +17,7 @@
package jakarta.mail;

import jakarta.activation.DataHandler;
import jakarta.mail.util.StreamProvider;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -453,4 +454,17 @@ Enumeration<Header> getMatchingHeaders(String[] header_names)
*/
Enumeration<Header> getNonMatchingHeaders(String[] header_names)
throws MessagingException;

/**
* Obtains the {@link StreamProvider}.
* It defaults to {@link Session#getDefaultInstance(java.util.Properties, Authenticator)}.
*
* @return the StreamProvider.
* @throws MessagingException if errors.
*
* @since JavaMail 2.2
*/
default StreamProvider getStreamProvider() throws MessagingException {
return Session.getDefaultInstance(System.getProperties(), null).getStreamProvider();
jmehrens marked this conversation as resolved.
Show resolved Hide resolved
}
}
5 changes: 2 additions & 3 deletions api/src/main/java/jakarta/mail/internet/MimeBodyPart.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -30,7 +30,6 @@
import jakarta.mail.Multipart;
import jakarta.mail.Part;
import jakarta.mail.util.LineOutputStream;
import jakarta.mail.util.StreamProvider;
import jakarta.mail.util.StreamProvider.EncoderTypes;

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

// First, write out the header
Expand Down
28 changes: 3 additions & 25 deletions api/src/main/java/jakarta/mail/internet/MimeMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import jakarta.mail.Multipart;
import jakarta.mail.Session;
import jakarta.mail.util.LineOutputStream;
import jakarta.mail.util.StreamProvider;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
Expand All @@ -45,8 +44,6 @@
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.ServiceConfigurationError;


/**
* This class represents a MIME style email message. It implements
Expand Down Expand Up @@ -245,7 +242,7 @@ public MimeMessage(MimeMessage source) throws MessagingException {
strict = source.strict;
source.writeTo(bos);
bos.close();
try (InputStream bis = provider().inputSharedByteArray(bos.toByteArray())) {
try (InputStream bis = getStreamProvider().inputSharedByteArray(bos.toByteArray())) {
parse(bis);
}
saved = true;
Expand Down Expand Up @@ -1410,7 +1407,7 @@ protected InputStream getContentStream() throws MessagingException {
if (contentStream != null)
return ((SharedInputStream) contentStream).newStream(0, -1);
if (content != null) {
return provider().inputSharedByteArray(content);
return getStreamProvider().inputSharedByteArray(content);
}
throw new MessagingException("No MimeMessage content");
}
Expand Down Expand Up @@ -1917,7 +1914,7 @@ public void writeTo(OutputStream os, String[] ignoreList)
// Else, the content is untouched, so we can just output it
// First, write out the header
Enumeration<String> hdrLines = getNonMatchingHeaderLines(ignoreList);
LineOutputStream los = provider().outputLineStream(os, allowutf8);
LineOutputStream los = getStreamProvider().outputLineStream(os, allowutf8);
while (hdrLines.hasMoreElements())
los.writeln(hdrLines.nextElement());

Expand Down Expand Up @@ -2322,23 +2319,4 @@ protected MimeMessage createMimeMessage(Session session)
throws MessagingException {
return new MimeMessage(session);
}

private StreamProvider provider() throws MessagingException {
try {
try {
final Session s = this.session;
if (s != null) {
return s.getStreamProvider();
} else {
return Session.getDefaultInstance(System.getProperties(),
null).getStreamProvider();
}
} catch (ServiceConfigurationError sce) {
throw new IllegalStateException(sce);
}
} catch (RuntimeException re) {
throw new MessagingException("Unable to get "
+ StreamProvider.class.getName(), re);
}
}
}
6 changes: 3 additions & 3 deletions api/src/main/java/jakarta/mail/internet/MimeMultipart.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -520,7 +520,7 @@ public synchronized void writeTo(OutputStream os)

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

try {
// Skip and save the preamble
LineInputStream lin = streamProvider.inputLineStream(in, false);
LineInputStream lin = getStreamProvider().inputLineStream(in, false);
StringBuilder preamblesb = null;
String line;
while ((line = lin.readLine()) != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -78,7 +78,7 @@ public void writeTo(OutputStream os)
if (os instanceof LineOutputStream) {
los = (LineOutputStream) os;
} else {
los = streamProvider.outputLineStream(os, false);
los = getStreamProvider().outputLineStream(os, false);
}

// First, write out the header
Expand Down
Loading