Return-Path: Delivered-To: apmail-xml-cocoon-cvs-archive@xml.apache.org Received: (qmail 15442 invoked by uid 500); 17 Apr 2003 20:30:06 -0000 Mailing-List: contact cocoon-cvs-help@xml.apache.org; run by ezmlm Precedence: bulk Reply-To: cocoon-dev@xml.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cocoon-cvs@xml.apache.org Received: (qmail 15390 invoked by uid 500); 17 Apr 2003 20:30:05 -0000 Delivered-To: apmail-cocoon-2.1-cvs@apache.org Received: (qmail 15272 invoked from network); 17 Apr 2003 20:30:04 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 17 Apr 2003 20:30:04 -0000 Received: (qmail 42701 invoked by uid 1348); 17 Apr 2003 20:30:03 -0000 Date: 17 Apr 2003 20:30:03 -0000 Message-ID: <20030417203003.42700.qmail@icarus.apache.org> From: haul@apache.org To: cocoon-2.1-cvs@apache.org Subject: cvs commit: cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/acting Sendmail.java SendmailAction.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N haul 2003/04/17 13:30:03 Modified: src/blocks/mail/java/org/apache/cocoon/acting SendmailAction.java Added: src/blocks/mail/java/org/apache/cocoon/acting Sendmail.java Log: improved sendmail action and logicsheet includes attachments and more thanks to Frank Ridderbusch Revision Changes Path 1.2 +23 -109 cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/acting/SendmailAction.java Index: SendmailAction.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/acting/SendmailAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SendmailAction.java 9 Mar 2003 00:04:33 -0000 1.1 +++ SendmailAction.java 17 Apr 2003 20:30:03 -0000 1.2 @@ -50,30 +50,18 @@ */ package org.apache.cocoon.acting; -import java.util.Date; -import java.util.Map; -import java.util.Properties; - -import javax.mail.Message; -import javax.mail.Session; -import javax.mail.Transport; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; - -import org.apache.avalon.framework.configuration.Configurable; -import org.apache.avalon.framework.configuration.Configuration; -import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.parameters.Parameters; -import org.apache.avalon.framework.thread.ThreadSafe; import org.apache.cocoon.environment.ObjectModelHelper; import org.apache.cocoon.environment.Redirector; import org.apache.cocoon.environment.Request; import org.apache.cocoon.environment.SourceResolver; +import java.util.Map; + /** - * The SendmailAction class sends email. The action needs four parameters: + * The SendmailAction class sends email. Please use the {@link Sendmail Sendmail} action instead. + * The action needs four parameters: * *
*
smtphost
@@ -95,114 +83,40 @@ * specified by the sitemap, not the request, to prevent possible abuse of the * SendmailAction as a spam source. * + * @deprecated * @author Donald Ball + * @author Christian Haul * @version CVS $Id$ */ -public class SendmailAction - extends AbstractAction - implements ThreadSafe, Configurable { - - Properties default_properties = null; - - public void configure(Configuration conf) throws ConfigurationException { - if (this.getLogger().isDebugEnabled()) { - getLogger().debug("SendmailAction: init"); - } - default_properties = new Properties(); - default_properties.put( - "mail.smtp.host", - conf.getAttribute("smtphost", "127.0.0.1")); - if (this.getLogger().isDebugEnabled()) { - getLogger().debug( - "SendmailAction: using " - + default_properties.get("mail.smtp.host") - + " as the smtp server"); - } - } +public class SendmailAction extends Sendmail { public Map act( Redirector redirector, SourceResolver resolver, Map objectModel, String source, - Parameters parameters) + Parameters parameters) throws Exception { - try { if (this.getLogger().isDebugEnabled()) { getLogger().debug("SendmailAction: act start"); } Request request = ObjectModelHelper.getRequest(objectModel); - Properties properties = new Properties(default_properties); - if (parameters.isParameter("smtphost")) { - properties.put( - "mail.smtp.host", - parameters.getParameter("smtphost", null)); - if (this.getLogger().isDebugEnabled()) { - getLogger().debug( - "SendmailAction: overriding default smtp server, using " - + properties.get("mail.smtp.host")); - } - } - Session session = Session.getDefaultInstance(properties); - Message message = new MimeMessage(session); - String from = null; - String to = null; - String subject = null; - String body = null; - try { - if (parameters.isParameter("from")) { - from = parameters.getParameter("from", null); - } else if ((from = request.getParameter("from")) == null) { - throw new SendmailActionException("no from address"); - } - message.setFrom(new InternetAddress(from)); - } catch (AddressException e) { - throw new SendmailActionException( - "invalid from address: " + from + ": " + e.getMessage()); - } - try { - if (parameters.isParameter("to")) { - to = parameters.getParameter("to", null); - } else if ((to = request.getParameter("to")) == null) { - throw new SendmailActionException("no to address"); - } - message.setRecipient( - Message.RecipientType.TO, - new InternetAddress(to)); - } catch (AddressException e) { - throw new SendmailActionException( - "invalid to address: " + to + ": " + e.getMessage()); - } - if (parameters.isParameter("subject")) { - subject = parameters.getParameter("subject", null); - } else if ((subject = request.getParameter("subject")) == null) { - throw new SendmailActionException("no subject"); - } - message.setSubject(subject); - if (parameters.isParameter("body")) { - body = parameters.getParameter("body", null); - } else if ((body = request.getParameter("body")) == null) { - throw new SendmailActionException("no body"); - } - message.setText(body); - message.setSentDate(new Date()); - Transport.send(message); + if (!parameters.isParameter("from")) { + parameters.setParameter("from", request.getParameter("from")); + } + if (!parameters.isParameter("to")) { + parameters.setParameter("to", request.getParameter("to")); + } + if (!parameters.isParameter("subject")) { + parameters.setParameter("subject", request.getParameter("subject")); + } + if (!parameters.isParameter("body")) { + parameters.setParameter("body", request.getParameter("body")); + } if (this.getLogger().isDebugEnabled()) { getLogger().debug("SendmailAction: act stop"); } - return EMPTY_MAP; - } catch (SendmailActionException e) { - getLogger().error("SendmailAction: " + e.getMessage()); - return EMPTY_MAP; + return super.act(redirector, resolver, objectModel, source, parameters); } - } - - class SendmailActionException extends Exception { - - public SendmailActionException(String message) { - super(message); - } - - } -} +} \ No newline at end of file 1.1 cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/acting/Sendmail.java Index: Sendmail.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact apache@apache.org. 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi . For more information on the Apache Software Foundation, please see . */ package org.apache.cocoon.acting; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.thread.ThreadSafe; //import org.apache.cocoon.servlet.multipart.MultipartHttpServletRequest; import org.apache.cocoon.environment.ObjectModelHelper; import org.apache.cocoon.environment.Redirector; import org.apache.cocoon.environment.Request; import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.mail.MailMessageSender; import org.apache.cocoon.util.Tokenizer; import java.util.HashMap; import java.util.Map; import javax.mail.MessagingException; import javax.mail.internet.AddressException; /** The Sendmail action class sends email. The action minimally needs four parameters: * *
*
smtphost
*
the smtp server to send the mail through. localhost * by default if this parameter is not sprecified.
*
from
*
the email address the mail appears to be from
*
to
*
the email address the mail it sent to. This can * be multiple addresses separated with commas.
*
subject
*
the subject line of the email
*
body
*
the text body of the email
*
* * The following optionals parameters can be used: * *
*
cc
*
an email address of someone, who should receive a * carbon copy. This can also be a list of multiple addresses * separated by commas.
*
bcc
*
an email address of someone, who should receive a black * carbon copy. This can also be a list of multiple addresses * separated by commas.
*
charset
*
the character set, which should be used the encode the body text. * This parameter is only used, if no attachements are send.
*
attachments
*
One or more attachments, separated by whitespace, which should be * attached to the email message. If the argument contains a ':', it is * assumed, that the argument describes a * org.apache.excalibur.source.Source object. Otherwise, it * is assumed, that the argument describes a request parameter of an * uploaded file, which * Cocoon has internally turned into a * {@link org.apache.cocoon.components.request.multipart.FilePart} * object.
*
*

* The class loads all of these parameters from the sitemap, except the * attachements, which may come from file upload request parameters. * Note it's strongly recommended that the to, cc and bcc addresses be * specified by the sitemap, not the request, to prevent possible abuse of the * SendmailAction as a spam source.

*

* One or two parameters are returned to the sitemap depending on the outcome * of sending the message: status and message.

*

* If the email message could be successfully delivered only the parameter * status with the value success is returned.

*

* If there was a problem sending the message, status can have * the value user-error and the message * parameter is set to an explainatory text. This usually indicates problems with * one or more email addresses. Other problems lead to a value of * server-error for status and * message contains a corresponding message.

* * @author Frank Ridderbusch * @author Christian Haul * @author Donald Ball * @since 2.1 * @version CVS $Id: Sendmail.java,v 1.1 2003/04/17 20:30:03 haul Exp $ */ public class Sendmail extends AbstractAction implements ThreadSafe, Configurable { private final static String STATUS = "status"; private final static String MESSAGE = "message"; /** Request-Attribute that holds status data*/ public final static String REQUEST_ATTRIBUTE = "org.apache.cocoon.acting.Sendmail"; String smtpHost = null; public void configure(Configuration conf) throws ConfigurationException { if (this.getLogger().isDebugEnabled()) { getLogger().debug("SendmailAction: init"); } smtpHost = conf.getAttribute("smtphost", "127.0.0.1"); if (this.getLogger().isDebugEnabled()) { getLogger().debug("SendmailAction: using " + smtpHost + " as the smtp server"); } } public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception { boolean success = false; Map status = null; try { if (this.getLogger().isDebugEnabled()) { getLogger().debug("SendmailAction: act start"); } Request request = ObjectModelHelper.getRequest(objectModel); if (parameters.isParameter("smtphost")) { smtpHost = parameters.getParameter("smtphost", null); if (this.getLogger().isDebugEnabled()) { getLogger().debug("SendmailAction: overriding default smtp server, using " + smtpHost); } } MailMessageSender mms = new MailMessageSender(smtpHost); if (parameters.isParameter("from")) { mms.setFrom(parameters.getParameter("from", null)); } if (parameters.isParameter("to")) { mms.setTo(parameters.getParameter("to", null)); } if (parameters.isParameter("cc")) { mms.setCc(parameters.getParameter("cc", null)); } if (parameters.isParameter("bcc")) { mms.setBcc(parameters.getParameter("bcc", null)); } if (parameters.isParameter("subject")) { mms.setSubject(parameters.getParameter("subject", null)); } if (parameters.isParameter("charset")) { mms.setCharset(parameters.getParameter("charset", null)); } if (parameters.isParameter("body")) { mms.setBody(parameters.getParameter("body", null)); } if (parameters.isParameter("attachments")) { Tokenizer tz = new Tokenizer(parameters.getParameter("attachments")); while (tz.hasMoreTokens()) { String srcName = tz.nextToken(); if (srcName.indexOf(":") == -1) { //if (request instanceof MultipartHttpServletRequest) { Object obj = request.get(srcName); mms.addAttachment(obj); if (this.getLogger().isDebugEnabled()) { getLogger().debug("request-attachment: " + obj); } //} } else { mms.addAttachmentURL(srcName, null, srcName.substring(srcName.lastIndexOf('/') + 1)); if (this.getLogger().isDebugEnabled()) { getLogger().debug("sitemap-attachment: " + srcName); } } } } mms.send(resolver); if (this.getLogger().isDebugEnabled()) { getLogger().debug("SendmailAction: act stop"); } success = true; status = new HashMap(1); status.put(Sendmail.STATUS, "success"); } catch (AddressException ae) { this.getLogger().error("SendmailAction: AddressException: " + ae.getMessage()); status = new HashMap(2); status.put(Sendmail.STATUS, "user-error"); status.put(Sendmail.MESSAGE, ae.getMessage()); } catch (MessagingException me) { this.getLogger().error( "SendmailAction: MessagingException: " + "An error occured while sending email.", me); // me contains nested exceptions providing insight on the real // cause. status = new HashMap(2); status.put(Sendmail.STATUS, "server-error"); status.put(Sendmail.MESSAGE, "An error occured while sending email: " + me.getMessage()); } catch (Exception e) { this.getLogger().error("SendmailAction: An exception was thrown while sending email.", e); status = new HashMap(2); status.put(Sendmail.STATUS, "server-error"); status.put(Sendmail.MESSAGE, "An exception was thrown while sending email."); } finally { ObjectModelHelper.getRequest(objectModel).setAttribute(Sendmail.REQUEST_ATTRIBUTE, status); return (success ? status : null); } } }