Skip to content

Commit

Permalink
SAK-42763 Bubble attachment exception in mailsender (sakaiproject#7706)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianfish authored Jun 30, 2020
1 parent 9079644 commit 301c2de
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 364 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional;
import javax.activation.DataSource;
import javax.activation.FileDataSource;

Expand Down Expand Up @@ -104,7 +105,7 @@ public Attachment(DataSource dataSource, String filename)
*/
public File getFile()
{
return (dataSource instanceof FileDataSource)?((FileDataSource)dataSource).getFile():null;
return (dataSource instanceof FileDataSource) ? ((FileDataSource)dataSource).getFile() : null;
}

/**
Expand Down Expand Up @@ -149,6 +150,18 @@ public String getContentDispositionHeader()
return contentDisposition;
}

public Optional<Long> getSizeIfFile() {

if (dataSource instanceof RenamedDataSource) {
RenamedDataSource rds = (RenamedDataSource) dataSource;
if (rds.dataSource instanceof FileDataSource) {
return Optional.of(((FileDataSource) rds.dataSource).getFile().length());
}
}

return Optional.empty();
}

/**
* Class which can be used when you wish to rename a file when adding it as an attachment.
* All calls are passed through to the original data source apart from the name.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**********************************************************************************
* Copyright 2008 Sakai Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**********************************************************************************/
package org.sakaiproject.email.api;

import lombok.Getter;

public class AttachmentSizeException extends Exception {

/**
* Default serial version ID.
*/
private static final long serialVersionUID = 1L;

@Getter
private long attemptedSize = 0L;

public AttachmentSizeException(String message, long attemptedSize) {

super(message);
this.attemptedSize = attemptedSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;

import org.sakaiproject.email.api.AttachmentSizeException;
import org.sakaiproject.email.api.EmailAddress.RecipientType;
import org.sakaiproject.user.api.User;

Expand All @@ -38,6 +39,9 @@
*/
public interface EmailService
{
public static final String MAIL_SENDFROMSAKAI_MAXSIZE = "mail.sendfromsakai.maxsize";
public static final int DEFAULT_MAXSIZE = 25000000;

/**
* Creates and sends a generic text MIME message to the address contained in to.
*
Expand Down Expand Up @@ -163,5 +167,5 @@ List<EmailAddress> send(EmailMessage message) throws AddressValidationException,
*/

List<EmailAddress> send(EmailMessage message, boolean messagingException) throws AddressValidationException,
NoRecipientsException, MessagingException;
}
AttachmentSizeException, NoRecipientsException, MessagingException;
}
Loading

0 comments on commit 301c2de

Please sign in to comment.