haul 2004/05/09 13:05:59
Modified:
src/blocks/mail/java/org/apache/cocoon/components/language/markup/xsp/java
sendmail.xsl
src/blocks/mail/java/org/apache/cocoon/acting Sendmail.java
src/blocks/mail/java/org/apache/cocoon/mail
MailMessageSender.java
Added: src/blocks/mail/conf mailsender.xconf
src/blocks/mail/java/org/apache/cocoon/mail MailSender.java
Log:
while I'm at it:
refactor MailMessageSender to be a Component so it can be used from flow more
easily
Revision Changes Path
1.4 +12 -3
cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/components/language/markup/xsp/java/sendmail.xsl
Index: sendmail.xsl
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/components/language/markup/xsp/java/sendmail.xsl,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- sendmail.xsl 17 Mar 2004 11:28:20 -0000 1.3
+++ sendmail.xsl 9 May 2004 20:05:59 -0000 1.4
@@ -84,8 +84,16 @@
</xsl:variable>
<xsp:logic>
{ // sendmail
- org.apache.cocoon.mail.MailMessageSender _sendmail_mms =
- new
org.apache.cocoon.mail.MailMessageSender(String.valueOf(<xsl:copy-of
select="$smtphost"/>));
+ org.apache.cocoon.mail.MailSender _sendmail_mms = null;
+ try {
+ _sendmail_mms = (org.apache.cocoon.mail.MailSender)
this.manager.lookup(org.apache.cocoon.mail.MailSender.ROLE);
+ } catch (org.apache.avalon.framework.component.ComponentException
e) {
+ throw new ProcessingException("Could not setup mail
components.", e);
+ }
+
+ <xsl:if test="$smtphost != ''">
+ _sendmail_mms.setSmtpHost(String.valueOf(<xsl:copy-of
select="$smtphost"/>));
+ </xsl:if>
_sendmail_mms.setTo(String.valueOf(<xsl:copy-of select="$to"/>));
_sendmail_mms.setSubject(String.valueOf(<xsl:copy-of
select="$subject"/>));
@@ -119,6 +127,7 @@
</xsl:otherwise>
</xsl:choose>
}
+
this.manager.release((org.apache.avalon.framework.component.Component)
_sendmail_mms);
}// sendmail
</xsp:logic>
</xsl:template>
1.8 +22 -5
cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/acting/Sendmail.java
Index: Sendmail.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/acting/Sendmail.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Sendmail.java 28 Mar 2004 14:28:04 -0000 1.7
+++ Sendmail.java 9 May 2004 20:05:59 -0000 1.8
@@ -19,6 +19,7 @@
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.service.ServiceException;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.environment.ObjectModelHelper;
@@ -26,6 +27,8 @@
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.mail.MailMessageSender;
+import org.apache.cocoon.mail.MailSender;
+
import org.apache.commons.lang.StringUtils;
import java.util.HashMap;
@@ -107,7 +110,7 @@
* @since 2.1
* @version CVS $Id$
*/
-public class Sendmail extends AbstractAction implements ThreadSafe,
Configurable {
+public class Sendmail extends ServiceableAction implements ThreadSafe,
Configurable {
private final static String STATUS = "status";
private final static String MESSAGE = "message";
/** Request-Attribute that holds status data*/
@@ -120,9 +123,9 @@
getLogger().debug("SendmailAction: init");
}
- smtpHost = conf.getAttribute("smtphost", "127.0.0.1");
+ smtpHost = conf.getAttribute("smtphost", null);
- if (this.getLogger().isDebugEnabled()) {
+ if (smtpHost != null && this.getLogger().isDebugEnabled()) {
getLogger().debug(
"SendmailAction: using " + smtpHost + " as the smtp server");
}
@@ -138,6 +141,7 @@
boolean success = false;
Map status = null;
+ MailSender mms = null;
try {
if (this.getLogger().isDebugEnabled()) {
getLogger().debug("SendmailAction: act start");
@@ -155,7 +159,10 @@
}
}
- MailMessageSender mms = new MailMessageSender(smtpHost);
+ mms = (MailSender) this.manager.lookup(MailSender.ROLE);
+ if (smtpHost != null) {
+ mms.setSmtpHost(smtpHost);
+ }
if (parameters.isParameter("from")) {
mms.setFrom(parameters.getParameter("from", null));
@@ -245,6 +252,15 @@
status.put(
Sendmail.MESSAGE,
"An error occured while sending email: " + me.getMessage());
+
+ } catch (ServiceException e) {
+ this.getLogger().error(
+ "SendmailAction: An exception was thrown while
initializing mail component.",
+ e);
+
+ status = new HashMap(2);
+ status.put(Sendmail.STATUS, "server-error");
+ status.put(Sendmail.MESSAGE, "An exception was thrown while
sending email: "+e.getMessage());
} catch (Exception e) {
this.getLogger().error(
@@ -259,6 +275,7 @@
ObjectModelHelper.getRequest(objectModel).setAttribute(
Sendmail.REQUEST_ATTRIBUTE,
status);
+ this.manager.release(mms);
}
return (success ? status : null);
}
1.1 cocoon-2.1/src/blocks/mail/conf/mailsender.xconf
Index: mailsender.xconf
===================================================================
<?xml version="1.0"?>
<!--
Copyright 1999-2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<xconf xpath="/cocoon" unless="[EMAIL
PROTECTED]'org.apache.cocoon.mail.MailSender']">
<!--+
| Component for simplified mail sending.
+-->
<component role="org.apache.cocoon.mail.MailSender"
class="org.apache.cocoon.mail.MailMessageSender"
logger="core.mail.MailSender">
<!--
<parameter name="smtp-host" value="127.0.0.1"/>
-->
</component>
</xconf>
1.12 +53 -2
cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/mail/MailMessageSender.java
Index: MailMessageSender.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/mail/MailMessageSender.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- MailMessageSender.java 9 May 2004 18:10:33 -0000 1.11
+++ MailMessageSender.java 9 May 2004 20:05:59 -0000 1.12
@@ -20,6 +20,13 @@
import org.apache.cocoon.mail.datasource.SourceDataSource;
import org.apache.cocoon.servlet.multipart.Part;
+import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.component.Component;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.parameters.ParameterException;
+import org.apache.avalon.framework.parameters.Parameterizable;
+import org.apache.avalon.framework.parameters.Parameters;
+
import org.apache.excalibur.source.Source;
import java.io.IOException;
@@ -47,13 +54,19 @@
/** A helper class used by the [EMAIL PROTECTED]
org.apache.cocoon.acting.Sendmail}
* and the <CODE>sendmail.xsl</CODE> logicsheet for sending an email message.
+ *
+ * <h3>Parameters</h3>
+ * <table><tbody>
+ * <tr><th>smtp-host</th><td>SMTP server to use sending
mail.</td><td>opt</td><td>String</td><td><code>localhost</code></td></tr>
+ * </tbody></table>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Frank Ridderbusch</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Christian Haul</a>
* @since 2.1
* @version CVS $Id$
*/
-public class MailMessageSender {
+public class MailMessageSender extends AbstractLogEnabled
+ implements MailSender, Parameterizable, Initializable, Component {
private MimeMessage message;
private String from;
@@ -67,6 +80,7 @@
private String body;
private List attachmentList;
private Exception exception = null;
+ private String smtpHost;
/**
* Helper class for attachment data.
@@ -158,9 +172,15 @@
}
}
+ public MailMessageSender() {
+
+ }
+
/** Creates a new instance of MailMessageSender
+ * Keep constructor for backwards compatibility.
* @param smtpHost The host name or ip-address of a host to accept
* the email for delivery.
+ * @deprecated Please use [EMAIL PROTECTED] MailSender} component
instead.
*/
public MailMessageSender(String smtpHost) {
smtpHost = smtpHost.trim();
@@ -534,5 +554,36 @@
attachmentList.add(new Attachment(url, type, name, true));
}
}
+
+ /* (non-Javadoc)
+ * @see
org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
+ */
+ public void parameterize(Parameters parameters) throws
ParameterException {
+ this.smtpHost = parameters.getParameter("smtp-host", null);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.avalon.framework.activity.Initializable#initialize()
+ */
+ public void initialize() throws Exception {
+ Properties properties = new Properties();
+ Session session = Session.getDefaultInstance(properties);
+
+ if (smtpHost == null || "null".equals(smtpHost)) {
+ properties.put("mail.smtp.host", "127.0.0.1");
+ } else {
+ properties.put("mail.smtp.host", smtpHost);
+ }
+
+ this.message = new MimeMessage(session);
+ this.attachmentList = new ArrayList();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.cocoon.mail.MailSender#setSmtpHost(java.lang.String)
+ */
+ public void setSmtpHost(String hostname) {
+ this.smtpHost = hostname;
+ }
}
1.1
cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/mail/MailSender.java
Index: MailSender.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cocoon.mail;
import org.apache.cocoon.environment.SourceResolver;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
/** A helper component used by the [EMAIL PROTECTED]
org.apache.cocoon.acting.Sendmail}
* and the <CODE>sendmail.xsl</CODE> logicsheet for sending an email message.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Christian Haul</a>
* @version CVS $Id: MailSender.java,v 1.1 2004/05/09 20:05:59 haul Exp $
*/
public interface MailSender {
String ROLE = MailSender.class.getName();
/** Assemble the message from the defined fields and send it.
* @throws AddressException when problems with email addresses are found
* @throws MessagingException when message could not be send.
*/
void send(SourceResolver resolver) throws AddressException,
MessagingException;
/**
* Invokes the [EMAIL PROTECTED] #send(SourceResolver)} method but
catches any exception thrown. This
* method is intended to be used from the sendmail logicsheet.
* @return true when successful
*/
boolean sendIt(SourceResolver resolver);
/**
* Accesses any Exception caught by [EMAIL PROTECTED]
#sendIt(SourceResolver)}.
* @return AddressException or MessagingException
*/
Exception getException();
/**
* Set SMTP hostname to use for mail sending.
*
* @param hostname
*/
void setSmtpHost(String hostname);
/** Set the <CODE>from</CODE> address of the message.
* @param from The address the message appears to be from.
*/
void setFrom(String from);
/** Sets the destination address(es) for the message. The address
* is in the format, that
* [EMAIL PROTECTED] javax.mail.internet.InternetAddress#parse(String)}
can handle
* (one or more email addresses separated by a commas).
* @param to the destination address(es)
* @see javax.mail.internet.InternetAddress#parse(String)
*/
void setTo(String to);
/** Sets the address(es), which should receive a carbon copy of the
* message. The address is in the format, that
* [EMAIL PROTECTED] javax.mail.internet.InternetAddress#parse(String)}
can handle
* (one or more email addresses separated by a commas).
* @param cc the address(es), which should receive a carbon copy.
* @see javax.mail.internet.InternetAddress#parse(String)
*/
void setCc(String cc);
/** Sets the address(es), which should receive a black carbon copy of
* the message. The address is in the format, that
* [EMAIL PROTECTED] javax.mail.internet.InternetAddress#parse(String)}
can handle
* (one or more email addresses separated by a commas).
* @param bcc the address(es), which should receive a black carbon copy.
* @see javax.mail.internet.InternetAddress#parse(String)
*/
void setBcc(String bcc);
/** Sets the character set for encoding the message. This has no effect,
* if any attachments are send in the message.
* @param charset the character set to be used for enbcoding the message
*/
void setCharset(String charset);
/** Sets the subject line of the message.
* @param subject the subject line of the message
*/
void setSubject(String subject);
/** Sets the body text of the email message.
* If both a text body and a body read from a source are set,
* only the latter will be used.
*
* @param body The body text of the email message
*/
void setBody(String body);
/** Sets the body source URL of the email message.
* If both a text body and a body read from a source are set,
* only the latter will be used.
*
* @param src The body source URL of the email message
*/
void setBodyFromSrc(String src);
/** Sets the optional body source Mime Type of the email message.
* @param srcMimeType The optional body source Mime Type of the email
message
*/
void setBodyFromSrcMimeType(String srcMimeType);
/** Add an attachement to the message to be send. The attachment can
* be of type <CODE>org.apache.excalibur.source.Source</CODE> or
* [EMAIL PROTECTED] org.apache.cocoon.servlet.multipart.Part} or its
* subclasses.
* @param attachment to be send with the message
* @see org.apache.excalibur.source.Source
*/
void addAttachment(Object attachment);
/** Add an attachement to the message to be send. The attachment can
* be of type <CODE>org.apache.excalibur.source.Source</CODE> or
* [EMAIL PROTECTED] org.apache.cocoon.servlet.multipart.Part} or its
* subclasses.
* @param attachment to be send with the message
* @param type mime type (optional)
* @param name attachment name (optional)
* @see org.apache.excalibur.source.Source
*/
void addAttachment(Object attachment, String type, String name);
/** Add an attachement to the message to be send. The attachment can
* be of type <CODE>org.apache.excalibur.source.Source</CODE> or
* [EMAIL PROTECTED] org.apache.cocoon.servlet.multipart.Part} or its
* subclasses.
* @param url URL to attach to the message
* @see org.apache.excalibur.source.Source
*/
void addAttachmentURL(String url);
/** Add an attachement to the message to be send. The attachment can
* be of type <CODE>org.apache.excalibur.source.Source</CODE> or
* [EMAIL PROTECTED] org.apache.cocoon.servlet.multipart.Part} or its
* subclasses.
* @param url URL to attach to the message
* @param type mime type (optional)
* @param name attachment name (optional)
* @see org.apache.excalibur.source.Source
*/
void addAttachmentURL(String url, String type, String name);
}