Skip to content

Commit

Permalink
Redesign of StreamProvider in Part
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Mar 6, 2024
1 parent edae19d commit cfaadd3
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 66 deletions.
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();

/**
* 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();
}
}

}
19 changes: 4 additions & 15 deletions api/src/main/java/jakarta/mail/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -708,32 +708,21 @@ public boolean match(SearchTerm term) throws MessagingException {
return term.match(this);
}

/**
* Obtains the {@link StreamProvider} from the session, if exists.
* 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 provider() throws MessagingException {
@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();
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);
throw new MessagingException("Unable to get " + StreamProvider.class.getName(), re);
}
}
}
45 changes: 16 additions & 29 deletions api/src/main/java/jakarta/mail/Multipart.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.io.IOException;
import java.io.OutputStream;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Multipart is a container that holds multiple body parts. Multipart
Expand All @@ -44,7 +42,6 @@

public abstract class Multipart {

private static final Logger LOGGER = Logger.getLogger(Multipart.class.getName());
/**
* Vector of BodyPart objects.
*/
Expand All @@ -64,13 +61,6 @@ public abstract class Multipart {
*/
protected Part parent;

/**
* Instance of stream provider.
*
* @since JavaMail 2.2
*/
private volatile StreamProvider streamProvider;

/**
* Default constructor. An empty Multipart object is created.
*/
Expand Down Expand Up @@ -270,26 +260,23 @@ public synchronized void setParent(Part parent) {
this.parent = parent;
}

protected StreamProvider provider() {
if (streamProvider == null) {
synchronized (this) {
if (streamProvider == null) {
if (parent != null && parent instanceof Message) {
try {
streamProvider = ((Message)parent).provider();
} catch (MessagingException e) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Cannot reuse streamProvider", e);
}
}
}
}
if (streamProvider == null) {
streamProvider = StreamProvider.provider();
}
}
/**
* 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();
}
return streamProvider;
}

}
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();
}
}
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
9 changes: 3 additions & 6 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
4 changes: 2 additions & 2 deletions api/src/main/java/jakarta/mail/internet/MimeMultipart.java
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ public synchronized void writeTo(OutputStream os)

String boundary = "--" +
(new ContentType(contentType)).getParameter("boundary");
LineOutputStream los = provider().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 = provider().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

0 comments on commit cfaadd3

Please sign in to comment.