Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from slide/JENKINS-9018
Implementing attachments (JENKINS-9018)
- Loading branch information
Showing
10 changed files
with
273 additions
and
9 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
src/main/java/hudson/plugins/emailext/AttachmentUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* | ||
*/ | ||
package hudson.plugins.emailext; | ||
|
||
import hudson.FilePath; | ||
import hudson.Util; | ||
import hudson.FilePath.FileCallable; | ||
import hudson.model.BuildListener; | ||
import hudson.model.AbstractBuild; | ||
import hudson.remoting.VirtualChannel; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.activation.DataHandler; | ||
import javax.activation.DataSource; | ||
import javax.activation.MimetypesFileTypeMap; | ||
import javax.mail.MessagingException; | ||
import javax.mail.Multipart; | ||
import javax.mail.internet.MimeBodyPart; | ||
|
||
import org.apache.tools.ant.DirectoryScanner; | ||
import org.apache.tools.ant.types.FileSet; | ||
|
||
/** | ||
* @author acearl | ||
* | ||
*/ | ||
public class AttachmentUtils implements Serializable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private String attachmentsPattern; | ||
|
||
public AttachmentUtils(String attachmentsPattern) { | ||
this.attachmentsPattern = attachmentsPattern; | ||
} | ||
|
||
/** | ||
* Provides a datasource wrapped around the FilePath class to | ||
* allow access to remote files (on slaves). | ||
* @author acearl | ||
* | ||
*/ | ||
private static class FilePathDataSource implements DataSource { | ||
private FilePath file; | ||
|
||
public FilePathDataSource(FilePath file) { | ||
this.file = file; | ||
} | ||
|
||
public InputStream getInputStream() throws IOException { | ||
return file.read(); | ||
} | ||
|
||
public OutputStream getOutputStream() throws IOException { | ||
throw new IOException("Unsupported"); | ||
} | ||
|
||
public String getContentType() { | ||
return MimetypesFileTypeMap.getDefaultFileTypeMap() | ||
.getContentType(file.getName()); | ||
} | ||
|
||
public String getName() { | ||
return file.getName(); | ||
} | ||
} | ||
|
||
private List<MimeBodyPart> getAttachments(final AbstractBuild<?, ?> build, final BuildListener listener) | ||
throws MessagingException, InterruptedException, IOException { | ||
List<MimeBodyPart> attachments = null; | ||
FilePath ws = build.getWorkspace(); | ||
long totalAttachmentSize = 0; | ||
long maxAttachmentSize = | ||
ExtendedEmailPublisher.DESCRIPTOR.getMaxAttachmentSize(); | ||
if(ws == null) { | ||
listener.error("Error: No workspace found!"); | ||
} else if(attachmentsPattern != null && attachmentsPattern.trim().length() > 0) { | ||
attachments = new ArrayList<MimeBodyPart>(); | ||
FilePath[] files = ws.list(attachmentsPattern); | ||
|
||
for(FilePath file : files) { | ||
if(maxAttachmentSize > 0 && | ||
(totalAttachmentSize + file.length()) >= maxAttachmentSize) { | ||
listener.getLogger().println("Skipping `" + file.getName() | ||
+ "' ("+ file.length() + | ||
" bytes) - too large for maximum attachments size"); | ||
continue; | ||
} | ||
|
||
MimeBodyPart attachmentPart = new MimeBodyPart(); | ||
FilePathDataSource fileDataSource = new FilePathDataSource(file); | ||
|
||
try { | ||
attachmentPart.setDataHandler(new DataHandler(fileDataSource)); | ||
attachmentPart.setFileName(file.getName()); | ||
attachments.add(attachmentPart); | ||
totalAttachmentSize += file.length(); | ||
} catch(MessagingException e) { | ||
listener.getLogger().println("Error adding `" + | ||
file.getName() + "' as attachment - " + | ||
e.getMessage()); | ||
} | ||
} | ||
} | ||
return attachments; | ||
} | ||
|
||
public void attach(Multipart multipart, AbstractBuild<?,?> build, BuildListener listener) { | ||
try { | ||
List<MimeBodyPart> attachments = getAttachments(build, listener); | ||
if(attachments != null) { | ||
for(MimeBodyPart attachment : attachments) { | ||
multipart.addBodyPart(attachment); | ||
} | ||
} | ||
} catch (IOException e) { | ||
listener.error("Error accessing files to attach: " + e.getMessage()); | ||
} catch (MessagingException e) { | ||
listener.error("Error attaching items to message: " + e.getMessage()); | ||
} catch (InterruptedException e) { | ||
listener.error("Interrupted in processing attachments: " + e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/config.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
projectContentType.plainText=Plain Text (text/plain) | ||
projectContentType.html=HTML (text/html) | ||
description=Can use wildcards like ''module/dist/**/*.zip''. \ | ||
See the <a href="{0}">@includes of Ant fileset</a> for the exact format. \ | ||
The base directory is <a href="ws/">the workspace</a>. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<div> | ||
The maximum size (in MB) for all attachments. If left blank, | ||
there is no limit to the size of all attachments. | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<div> | ||
This is the set of attachments that will be used for the email. The format is a | ||
comma separated list of Ant include file syntax. | ||
</div> |
Oops, something went wrong.