KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > Sendmail


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.acting;
17
18 import org.apache.avalon.framework.configuration.Configurable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.avalon.framework.service.ServiceException;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Redirector;
27 import org.apache.cocoon.environment.Request;
28 import org.apache.cocoon.environment.SourceResolver;
29 import org.apache.cocoon.mail.MailSender;
30
31 import org.apache.commons.lang.StringUtils;
32
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import javax.mail.MessagingException JavaDoc;
37 import javax.mail.internet.AddressException JavaDoc;
38
39 /**
40  * The Sendmail action class sends email. Action supports following
41  * parameters:
42  *
43  * <dl>
44  * <dt>smtp-host</dt>
45  * <dd>The smtp server to send the mail through. If not specified,
46  * default from cocoon.xconf will be used.</dd>
47  * <dt>smtp-user</dt>
48  * <dd>The smtp user. If smtp-user and smtp-host not specified,
49  * default from cocoon.xconf will be used.</dd>
50  * <dt>smtp-password</dt>
51  * <dd>The smtp user's password. If smtp-user and smtp-host not
52  * specified, default from cocoon.xconf will be used.</dd>
53  * <dt>from</dt>
54  * <dd>the email address the mail appears to be from</dd>
55  * <dt>to</dt>
56  * <dd>the email address(es) the mail it sent to. This can
57  * be multiple addresses separated with commas.</dd>
58  * <dt>replyTo</dt>
59  * <dd>the email address(es) replies should be sent to. This can
60  * be multiple addresses separated with commas.</dd>
61  * <dt>cc</dt>
62  * <dd>an email address(es) of someone, who should receive a
63  * carbon copy. This can also be a list of multiple addresses
64  * separated by commas.</dd>
65  * <dt>bcc</dt>
66  * <dd>an email address(es) of someone, who should receive a black
67  * carbon copy. This can also be a list of multiple addresses
68  * separated by commas.</dd>
69  * <dt>subject</dt>
70  * <dd>the subject line of the email</dd>
71  * <dt>src</dt>
72  * <dd>A url specifying the source of the text body of the email</dd>
73  * <dt>srcMimeType</dt>
74  * <dd>The optional Mime Type of the source of the text body of the email
75  * if you specified src</dd>
76  * <dt>body</dt>
77  * <dd>the text body of the email, if src is specified, body will be ignored</dd>
78  * <dt>charset</dt>
79  * <dd>the character set, which should be used the encode the body text.
80  * This parameter is only used, if no attachements are send.</dd>
81  * <dt>attachments</dt>
82  * <dd>One or more attachments, separated by whitespace, which should be
83  * attached to the email message. If the argument contains a ':', it is
84  * assumed, that the argument describes a
85  * <code>org.apache.excalibur.source.Source</code> object. Otherwise, it
86  * is assumed, that the argument describes a request parameter of an
87  * uploaded file, which Cocoon has internally turned into a
88  * {@link org.apache.cocoon.servlet.multipart.Part}
89  * object.</dd>
90  * </dl>
91  *
92  * <p>
93  * Minimally, <code>from</code>, <code>to</code>, <code>body</code> parameters
94  * should be specified. Rest of parameters are optional.</p>
95  *
96  * <p>
97  * The class loads all of these parameters from the sitemap, except the
98  * attachements, which may come from file upload request parameters.
99  * Note it's strongly recommended that the to, cc and bcc addresses be
100  * specified by the sitemap, not the request, to prevent possible abuse of the
101  * SendmailAction as a spam source.</p>
102  *
103  * <p>
104  * One or two parameters are returned to the sitemap depending on the outcome
105  * of sending the message: <code>status</code> and <code>message</code>.</p>
106  *
107  * <p>
108  * If the email message could be successfully delivered only the parameter
109  * <code>status</code> with the value <code>success</code> is returned.</p>
110  *
111  * <p>
112  * If there was a problem sending the message, <code>status</code> can have
113  * the value <code>user-error</code> and the <code>message</code>
114  * parameter is set to an explainatory text. This usually indicates problems with
115  * one or more email addresses. Other problems lead to a value of
116  * <code>server-error</code> for <code>status</code> and
117  * <code>message</code> contains a corresponding message.</p>
118  *
119  * @author <a HREF="mailto:frank.ridderbusch@gmx.de">Frank Ridderbusch</a>
120  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
121  * @author <a HREF="mailto:balld@apache.org">Donald Ball</a>
122  * @author <a HREF="mailto:andrzej@chaeron.com">Andrzej Taramina</a>
123  * @since 2.1
124  * @version CVS $Id: Sendmail.java 155031 2005-02-23 17:32:37Z vgritsenko $
125  */

126 public class Sendmail extends ServiceableAction
127                       implements ThreadSafe, Configurable {
128
129     private static final String JavaDoc STATUS = "status";
130     private static final String JavaDoc MESSAGE = "message";
131
132     /** Request-Attribute that holds status data*/
133     public static final String JavaDoc REQUEST_ATTRIBUTE = "org.apache.cocoon.acting.Sendmail";
134
135     private String JavaDoc smtpHost;
136     private String JavaDoc smtpUser;
137     private String JavaDoc smtpPassword;
138
139     public void configure(Configuration conf) throws ConfigurationException {
140         if (getLogger().isDebugEnabled()) {
141             getLogger().debug("configure");
142         }
143
144         // FIXME Remove support of old "smtphost" attribute.
145
smtpHost = conf.getChild("smtp-host").getValue(conf.getAttribute("smtphost", null));
146         smtpUser = conf.getChild("smtp-user").getValue(null);
147         smtpPassword = conf.getChild("smtp-password").getValue(null);
148
149         if (getLogger().isDebugEnabled()) {
150             if (smtpHost != null)
151                 getLogger().debug("Using " + smtpHost + " as the smtp server");
152             if (smtpUser != null)
153                 getLogger().debug("Using " + smtpUser + " as the smtp user");
154         }
155     }
156
157     public Map JavaDoc act(Redirector redirector,
158                    SourceResolver resolver,
159                    Map JavaDoc objectModel,
160                    String JavaDoc source,
161                    Parameters parameters)
162     throws Exception JavaDoc {
163         boolean success = false;
164         Map JavaDoc status = null;
165
166         MailSender mms = null;
167         try {
168             Request request = ObjectModelHelper.getRequest(objectModel);
169
170             // FIXME Remove support of old smtphost parameter
171
String JavaDoc smtpHost = parameters.getParameter("smtp-host", parameters.getParameter("smtphost", this.smtpHost));
172             String JavaDoc smtpUser = parameters.getParameter("smtp-user", this.smtpUser);
173             String JavaDoc smtpPassword = parameters.getParameter("smtp-password", this.smtpPassword);
174
175             // Empty parameter means absent parameter
176
if ("".equals(smtpHost)) {
177                 smtpHost = this.smtpHost;
178             }
179             if ("".equals(smtpUser)) {
180                 smtpUser = this.smtpUser;
181             }
182             if ("".equals(smtpPassword)) {
183                 smtpPassword = this.smtpPassword;
184             }
185
186             mms = (MailSender) this.manager.lookup(MailSender.ROLE);
187
188             // Initialize non-default session if host or user specified.
189
if (smtpHost != null || smtpUser != null) {
190                 mms.setSmtpHost(smtpHost, smtpUser, smtpPassword);
191             }
192
193             if (parameters.isParameter("from")) {
194                 mms.setFrom(parameters.getParameter("from", null));
195             }
196             if (parameters.isParameter("to")) {
197                 mms.setTo(parameters.getParameter("to", null));
198             }
199             if (parameters.isParameter("replyTo")) {
200                 mms.setReplyTo(parameters.getParameter("replyTo", null));
201             }
202             if (parameters.isParameter("cc")) {
203                 mms.setCc(parameters.getParameter("cc", null));
204             }
205             if (parameters.isParameter("bcc")) {
206                 mms.setBcc(parameters.getParameter("bcc", null));
207             }
208             if (parameters.isParameter("subject")) {
209                 mms.setSubject(parameters.getParameter("subject", null));
210             }
211             if (parameters.isParameter("charset")) {
212                 mms.setCharset(parameters.getParameter("charset", null));
213             }
214
215             if (parameters.isParameter("src")) {
216                 mms.setBodyFromSrc(parameters.getParameter("src", null));
217                 if (parameters.isParameter("srcMimeType")) {
218                     mms.setBodyFromSrcMimeType(
219                         parameters.getParameter("srcMimeType", null));
220                 }
221             } else if (parameters.isParameter("body")) {
222                 mms.setBody(parameters.getParameter("body", null));
223             }
224
225             if (parameters.isParameter("attachments")) {
226                 String JavaDoc fileName[] = StringUtils.split(parameters.getParameter("attachments"));
227                 for (int i = 0; i < fileName.length; i++) {
228                     String JavaDoc srcName = fileName[i];
229
230                     if (srcName.indexOf(":") == -1) {
231                         Object JavaDoc obj = request.get(srcName);
232                         mms.addAttachment(obj);
233                         if (getLogger().isDebugEnabled()) {
234                             getLogger().debug("request-attachment: " + obj);
235                         }
236                     } else {
237                         mms.addAttachmentURL(srcName,
238                                              null,
239                                              srcName.substring(srcName.lastIndexOf('/') + 1));
240                         if (getLogger().isDebugEnabled()) {
241                             getLogger().debug("sitemap-attachment: " + srcName);
242                         }
243                     }
244                 }
245             }
246
247             mms.send();
248
249             success = true;
250             status = new HashMap JavaDoc(3);
251             status.put(Sendmail.STATUS, "success");
252
253         } catch (AddressException JavaDoc e) {
254             getLogger().warn("AddressException: ", e);
255
256             status = new HashMap JavaDoc(3);
257             status.put(Sendmail.STATUS, "user-error");
258             status.put(Sendmail.MESSAGE, e.getMessage());
259
260         } catch (MessagingException JavaDoc e) {
261             getLogger().warn("MessagingException: " +
262                              "An error occured while sending email.", e);
263
264             status = new HashMap JavaDoc(3);
265             status.put(Sendmail.STATUS, "server-error");
266             status.put(Sendmail.MESSAGE,
267                        "An error occured while sending email: " + e.getMessage());
268
269         } catch (ServiceException e) {
270             getLogger().error("ServiceException: " +
271                               "An error occured while initializing.", e);
272
273             status = new HashMap JavaDoc(3);
274             status.put(Sendmail.STATUS, "server-error");
275             status.put(Sendmail.MESSAGE,
276                        "An exception was thrown while sending email: " + e.getMessage());
277
278         } catch (Exception JavaDoc e) {
279             getLogger().error("An exception was thrown while sending email.", e);
280
281             status = new HashMap JavaDoc(3);
282             status.put(Sendmail.STATUS, "server-error");
283             status.put(Sendmail.MESSAGE, "An exception was thrown while sending email.");
284
285         } finally {
286             ObjectModelHelper.getRequest(objectModel).setAttribute(Sendmail.REQUEST_ATTRIBUTE,
287                                                                    status);
288             this.manager.release(mms);
289         }
290
291         return success ? status : null;
292     }
293 }
294
Popular Tags