001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.wfp.vam.intermap.services.wmc;
025:
026: import java.io.File;
027: import java.io.FileWriter;
028: import java.net.URL;
029: import java.net.URLEncoder;
030: import java.util.logging.Logger;
031: import jeeves.exceptions.ResourceNotFoundEx;
032: import jeeves.interfaces.Service;
033: import jeeves.server.ServiceConfig;
034: import jeeves.server.context.ServiceContext;
035: import jeeves.utils.Xml;
036: import org.apache.commons.mail.EmailAttachment;
037: import org.apache.commons.mail.HtmlEmail;
038: import org.jdom.Document;
039: import org.jdom.Element;
040: import org.jdom.output.Format;
041: import org.jdom.output.XMLOutputter;
042: import org.wfp.vam.intermap.Constants;
043: import org.wfp.vam.intermap.kernel.GlobalTempFiles;
044: import org.wfp.vam.intermap.kernel.map.MapMerger;
045: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.GeoRSSCodec;
046: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.WmcCodec;
047: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.schema.type.WMCViewContext;
048: import org.wfp.vam.intermap.kernel.marker.MarkerSet;
049: import org.wfp.vam.intermap.services.map.MapUtil;
050: import org.wfp.vam.intermap.util.XmlTransformer;
051:
052: /**
053: * @author ETj
054: */
055: public class MailWmcContext implements Service {
056: static private File _xslfile;
057: static private String _mailserver;
058: static private int _smtpport;
059: static private String _gnURL;
060:
061: public void init(String appPath, ServiceConfig config)
062: throws Exception {
063: _gnURL = config.getMandatoryValue("gnurl");
064: _xslfile = new File(config
065: .getMandatoryValue("customstylesheet"));
066: _mailserver = config.getValue("mailserver"); // we want to issue a warning ourselves
067: if (_mailserver == null) {
068: Logger
069: .getLogger("MailWmcContext")
070: .warning(
071: "*** The mailserver config property has not been set. WMC mail will not be available.");
072: }
073:
074: try {
075: _smtpport = Integer.parseInt(config.getValue("smtpport"));
076: } catch (NumberFormatException e) {
077: Logger
078: .getLogger("MailWmcContect")
079: .warning(
080: "*** The mail server smtpport property has not been set. Using default port 25");
081: _smtpport = 25;
082: }
083: }
084:
085: //--------------------------------------------------------------------------
086: //---
087: //--- Service
088: //---
089: //--------------------------------------------------------------------------
090:
091: public Element exec(Element params, ServiceContext context)
092: throws Exception {
093: if (_mailserver == null)
094: throw new ResourceNotFoundEx(
095: "'mailserver' property is missing from configuration file.");
096:
097: String title = params.getChildText("wmc_title");
098: String comment = params.getChildText("wmc_comment");
099: String mailfrom = params.getChildText("wmc_mailfrom");
100: String mailto = params.getChildText("wmc_mailto");
101: int width = Integer.parseInt(params.getChildText("width"));
102: int height = Integer.parseInt(params.getChildText("height"));
103:
104: MapMerger mm = MapUtil.getMapMerger(context);
105: MarkerSet ms = (MarkerSet) context.getUserSession()
106: .getProperty(Constants.SESSION_MARKERSET);
107:
108: WMCViewContext viewContext = WmcCodec.createViewContext(mm, ms,
109: title, width, height);
110: Element eViewContext = viewContext.toElement();
111:
112: XMLOutputter xcomp = new XMLOutputter(Format.getCompactFormat());
113: String comp = xcomp.outputString(eViewContext);
114: String enc1 = URLEncoder.encode(comp, "UTF-8");
115: String enc2 = URLEncoder.encode(enc1, "UTF-8");
116:
117: System.out.println("Sending WMC context mail");
118: System.out.println(" from: " + mailfrom);
119: System.out.println(" to: " + mailto);
120: System.out.println(" title: " + title);
121: System.out.println(" comment: " + comment);
122: System.out.println(" by: " + _mailserver);
123: System.out.println(" on port: " + _smtpport);
124:
125: // Create the email message
126: HtmlEmail email = new HtmlEmail();
127: email.setHostName(_mailserver);
128: email.setSmtpPort(_smtpport);
129: email.addTo(mailto);
130: email.setFrom(mailfrom);
131: email.setSubject(title);
132:
133: // create the image file
134: String imagename = mm.merge(300, 200);
135: File imagepath = new File(mm.getImageLocalPath(), imagename);
136: // embed the image and get the content id
137: URL imageurl = imagepath.toURL();
138: String cid = email.embed(imageurl, "Map");
139:
140: // set the html and the text messages
141: String fullurl = _gnURL + "?wmc=" + enc2;
142:
143: Element maildata = new Element("maildata").addContent(
144: new Element("gnurl").setText(_gnURL)).addContent(
145: new Element("url").setText(fullurl)).addContent(
146: new Element("title").setText(title)).addContent(
147: new Element("mailfrom").setText(mailfrom)).addContent(
148: new Element("comment").setText(comment)).addContent(
149: new Element("imgsrc").setText("cid:" + cid));
150:
151: // Add the atom feed to the mail data for further parsing
152: // Element name is "feed"
153: if (ms != null && !ms.isEmpty()) {
154: maildata.addContent(GeoRSSCodec.getGeoRSS(ms, title));
155: }
156:
157: Element stylesheet = (Element) Xml.loadFile(_xslfile).clone();
158: Element tmail = XmlTransformer.transform(maildata, stylesheet);
159:
160: Element ehtml = tmail.getChild("html");
161: Element etext = tmail.getChild("text");
162:
163: XMLOutputter xo = new XMLOutputter(Format.getPrettyFormat());
164: String html = xo.outputString(ehtml);
165: String text = etext.getText();
166:
167: email.setHtmlMsg(html);
168: email.setTextMsg(text); // set the alternative message
169:
170: // Create the context file as attachment
171: File tempContextFile = GlobalTempFiles.getInstance().getFile(
172: "cml");
173: FileWriter fw = new FileWriter(tempContextFile);
174: xo.output(eViewContext, fw);
175: fw.flush();
176: fw.close();
177:
178: // Attach the context
179: EmailAttachment cmlAttachment = new EmailAttachment();
180: cmlAttachment.setPath(tempContextFile.getAbsolutePath());
181: cmlAttachment.setDisposition(EmailAttachment.ATTACHMENT);
182: cmlAttachment.setDescription("Interactive Map");
183: cmlAttachment.setName("InteractiveMap.cml");
184: email.attach(cmlAttachment);
185:
186: if (ms != null && !ms.isEmpty()) {
187: Element erss = GeoRSSCodec.getGeoRSS(ms, title);
188: Document drss = new Document(erss);
189:
190: // Create the rss file as attachment
191: File tempRssFile = GlobalTempFiles.getInstance().getFile(
192: "rss");
193: fw = new FileWriter(tempRssFile);
194: xo.output(drss, fw);
195: fw.flush();
196: fw.close();
197:
198: // Attach the rss
199: EmailAttachment rssAttachment = new EmailAttachment();
200: rssAttachment.setPath(tempRssFile.getAbsolutePath());
201: rssAttachment.setDisposition(EmailAttachment.ATTACHMENT);
202: rssAttachment.setDescription("MarkerSet");
203: rssAttachment.setName("AtomMarkerSet.rss");
204: email.attach(rssAttachment);
205: }
206:
207: // send the email
208: email.send();
209:
210: return new Element("response");
211: }
212:
213: }
214:
215: //=============================================================================
|