001: /*
002: * Copyright (C) 2007 Erik Swenson - erik@oreports.com
003: *
004: * This program is free software; you can redistribute it and/or modify it
005: * under the terms of the GNU General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option)
007: * any later version.
008: *
009: * This program is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
012: * more details.
013: *
014: * You should have received a copy of the GNU General Public License along with
015: * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
016: * Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package org.efs.openreports.delivery;
020:
021: import java.util.ArrayList;
022: import java.util.Date;
023: import java.util.Iterator;
024: import java.util.Map;
025: import org.apache.commons.lang.StringUtils;
026: import org.apache.log4j.Logger;
027: import org.efs.openreports.ReportConstants.ExportType;
028: import org.efs.openreports.engine.output.JasperReportEngineOutput;
029: import org.efs.openreports.engine.output.ReportEngineOutput;
030: import org.efs.openreports.objects.DeliveredReport;
031: import org.efs.openreports.objects.MailMessage;
032: import org.efs.openreports.objects.ReportSchedule;
033: import org.efs.openreports.objects.ReportUser;
034: import org.efs.openreports.providers.MailProvider;
035: import org.efs.openreports.providers.ProviderException;
036: import org.efs.openreports.util.ByteArrayDataSource;
037:
038: public class EMailDeliveryMethod implements DeliveryMethod {
039: protected static Logger log = Logger
040: .getLogger(EMailDeliveryMethod.class.getName());
041:
042: private MailProvider mailProvider;
043:
044: public void deliverReport(ReportSchedule reportSchedule,
045: ReportEngineOutput reportOutput) throws DeliveryException {
046: ArrayList<ByteArrayDataSource> htmlImageDataSources = new ArrayList<ByteArrayDataSource>();
047:
048: ByteArrayDataSource byteArrayDataSource = exportReport(
049: reportOutput, reportSchedule, htmlImageDataSources);
050:
051: MailMessage mail = new MailMessage();
052: mail.setByteArrayDataSource(byteArrayDataSource);
053: mail.addHtmlImageDataSources(htmlImageDataSources);
054: mail.setSender(reportSchedule.getUser().getEmail());
055: mail.parseRecipients(reportSchedule.getRecipients());
056: mail
057: .setBounceAddress(reportSchedule
058: .getDeliveryReturnAddress());
059:
060: if (reportSchedule.getScheduleDescription() != null
061: && reportSchedule.getScheduleDescription().trim()
062: .length() > 0) {
063: mail.setSubject(reportSchedule.getScheduleDescription());
064: } else {
065: mail.setSubject(reportSchedule.getReport().getName());
066: }
067:
068: if (reportSchedule.getExportType() != ExportType.HTML.getCode()) {
069: mail.setText(reportSchedule.getReport().getName()
070: + ": Generated on " + new Date());
071: }
072:
073: try {
074: mailProvider.sendMail(mail);
075: } catch (ProviderException pe) {
076: throw new DeliveryException(pe);
077: }
078:
079: log.debug(byteArrayDataSource.getName() + " sent to: "
080: + mail.formatRecipients(";"));
081: }
082:
083: public byte[] getDeliveredReport(DeliveredReport deliveredReport)
084: throws DeliveryException {
085: throw new DeliveryException(
086: "Method getDeliveredReport not implemented by EMailDeliveryMethod");
087: }
088:
089: public DeliveredReport[] getDeliveredReports(ReportUser user)
090: throws DeliveryException {
091: throw new DeliveryException(
092: "Method getDeliveredReports not implemented by EMailDeliveryMethod");
093: }
094:
095: protected ByteArrayDataSource exportReport(
096: ReportEngineOutput reportOutput,
097: ReportSchedule reportSchedule,
098: ArrayList<ByteArrayDataSource> htmlImageDataSources) {
099: String reportName = StringUtils.deleteWhitespace(reportSchedule
100: .getReport().getName());
101:
102: ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(
103: reportOutput.getContent(), reportOutput
104: .getContentType());
105: byteArrayDataSource.setName(reportName
106: + reportOutput.getContentExtension());
107:
108: if (reportSchedule.getExportType() == ExportType.HTML.getCode()
109: && reportSchedule.getReport().isJasperReport()) {
110: Map imagesMap = ((JasperReportEngineOutput) reportOutput)
111: .getImagesMap();
112:
113: for (Iterator entryIter = imagesMap.entrySet().iterator(); entryIter
114: .hasNext();) {
115: Map.Entry entry = (Map.Entry) entryIter.next();
116:
117: ByteArrayDataSource imageDataSource = new ByteArrayDataSource(
118: (byte[]) entry.getValue(),
119: getImageContentType((byte[]) entry.getValue()));
120:
121: imageDataSource.setName((String) entry.getKey());
122:
123: htmlImageDataSources.add(imageDataSource);
124: }
125: }
126:
127: return byteArrayDataSource;
128: }
129:
130: /**
131: * Try to figure out the image type from its bytes.
132: */
133: private String getImageContentType(byte[] bytes) {
134: String header = new String(bytes, 0, (bytes.length > 100) ? 100
135: : bytes.length);
136: if (header.startsWith("GIF")) {
137: return "image/gif";
138: }
139:
140: if (header.startsWith("BM")) {
141: return "image/bmp";
142: }
143:
144: if (header.indexOf("JFIF") >= 0) {
145: return "image/jpeg";
146: }
147:
148: if (header.indexOf("PNG") >= 0) {
149: return "image/png";
150: }
151:
152: // We are out of guesses, so just guess tiff
153: return "image/tiff";
154: }
155:
156: public void setMailProvider(MailProvider mailProvider) {
157: this.mailProvider = mailProvider;
158: }
159: }
|