Created
October 28, 2011 13:18
-
-
Save wpivotto/1322237 to your computer and use it in GitHub Desktop.
Send a report via Email
This file contains hidden or 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
vraptor.simplemail.main.server = smtp.gmail.com | |
vraptor.simplemail.main.port = 587 | |
vraptor.simplemail.main.tls = true | |
vraptor.simplemail.main.from = [email protected] | |
vraptor.simplemail.main.username = username | |
vraptor.simplemail.main.password = password |
This file contains hidden or 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
import static br.com.caelum.vraptor.jasperreports.formats.ExportFormats.docx; | |
import static br.com.caelum.vraptor.jasperreports.formats.ExportFormats.pdf; | |
import static br.com.caelum.vraptor.jasperreports.formats.ExportFormats.xls; | |
import app.mailer.ReportEmail; | |
import app.mailer.ReportMailer; | |
import app.reports.ClientsReport; | |
import app.repositories.ClientRepository; | |
import br.com.caelum.vraptor.Get; | |
import br.com.caelum.vraptor.Resource; | |
import br.com.caelum.vraptor.Result; | |
import br.com.caelum.vraptor.jasperreports.Report; | |
@Resource | |
public class MailController { | |
private final Result result; | |
private final Clients clients; | |
private final ReportMailer mailer; | |
MailController(Result result, Clients clients, ReportMailer mailer) { | |
this.result = result; | |
this.clients = clients; | |
this.mailer = mailer; | |
} | |
@Get("/send/report/{client.id}") | |
public void send(@Load Client client){ | |
Report<?> report = new ClientsReport(clients.findAll()); | |
ReportEmail email = new ReportEmail(); | |
email.subject("Your subject") | |
.body("This message is automatically generated by ...") | |
.to(client.getEmail()) | |
.attach(report, pdf()) | |
.attach(report, xls()) | |
.attach(report, docx()); | |
mailer.send(email); | |
result.include("msg", "Sending email..."); | |
} | |
} |
This file contains hidden or 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
package app.mailer; | |
import java.io.IOException; | |
import java.util.Collection; | |
import java.util.List; | |
import org.apache.commons.mail.ByteArrayDataSource; | |
import org.apache.commons.mail.EmailAttachment; | |
import org.apache.commons.mail.EmailException; | |
import org.apache.commons.mail.MultiPartEmail; | |
import br.com.caelum.vraptor.jasperreports.Report; | |
import br.com.caelum.vraptor.jasperreports.download.ReportEntry; | |
import br.com.caelum.vraptor.jasperreports.download.ReportZipFile; | |
import br.com.caelum.vraptor.jasperreports.exporter.ReportExporter; | |
import br.com.caelum.vraptor.jasperreports.formats.ExportFormat; | |
import com.google.common.collect.Lists; | |
public class ReportEmail extends MultiPartEmail { | |
private final ReportZipFile zip = new ReportZipFile("attachment.zip"); | |
private final List<ReportEntry> entries = Lists.newArrayList(); | |
private boolean singleFile; | |
public ReportEmail singleFile() { | |
singleFile = true; | |
return this; | |
} | |
public ReportEmail subject(String subject) { | |
setSubject(subject); | |
return this; | |
} | |
public ReportEmail body(String body) { | |
try { | |
setMsg(body); | |
} catch (EmailException e) { | |
throw new IllegalArgumentException(e); | |
} | |
return this; | |
} | |
public ReportEmail to(String... recipients) { | |
try { | |
for (String recipient : recipients) | |
addTo(recipient); | |
} catch (EmailException e) { | |
throw new IllegalArgumentException(e); | |
} | |
return this; | |
} | |
public ReportEmail attach(Report<?> report, ExportFormat format){ | |
add(report, format); | |
return this; | |
} | |
public ReportEmail attach(String filename, Collection<Report<?>> reports, ExportFormat format){ | |
add(filename, reports, format); | |
return this; | |
} | |
private void add(Report<?> report, ExportFormat format){ | |
if(singleFile) | |
zip.add(report, format); | |
else | |
entries.add(new ReportEntry(report, format)); | |
} | |
private void add(String filename, Collection<Report<?>> reports, ExportFormat format){ | |
if(singleFile) | |
zip.add(filename, reports, format); | |
else | |
entries.add(new ReportEntry(filename, reports, format)); | |
} | |
public void attachReports(ReportExporter exporter) throws EmailException, IOException { | |
if(singleFile) | |
write(zip.getName(), zip.getContent(exporter), zip.getContentType()); | |
else { | |
for(ReportEntry entry : entries) | |
write(entry.getFilename(), entry.getContent(exporter), entry.getContentType()); | |
} | |
} | |
private void write(String name, byte[] content, String contentType) throws EmailException, IOException { | |
attach(new ByteArrayDataSource(content, contentType), name, "", EmailAttachment.ATTACHMENT); | |
} | |
} |
This file contains hidden or 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
package app.mailer; | |
import java.io.IOException; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import org.apache.commons.mail.EmailException; | |
import br.com.caelum.vraptor.ioc.Component; | |
import br.com.caelum.vraptor.jasperreports.exporter.ReportExporter; | |
import br.com.caelum.vraptor.simplemail.Mailer; | |
@Component | |
public class ReportMailer { | |
private final Mailer delegate; | |
private final ReportExporter exporter; | |
public ReportMailer(Mailer delegate, ReportExporter exporter) { | |
this.delegate = delegate; | |
this.exporter = exporter; | |
} | |
public void send(final ReportEmail email){ | |
getExecutor().submit(new Runnable() { | |
public void run() { | |
try { | |
email.attachReports(exporter); | |
delegate.send(email); | |
} catch (EmailException e) { | |
throw new RuntimeException(e); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
}); | |
} | |
protected ExecutorService getExecutor(){ | |
return Executors.newSingleThreadExecutor(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment