Author: vmassol
Date: 2008-02-07 13:03:31 +0100 (Thu, 07 Feb 2008)
New Revision: 7340
Added:
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSender.java
Modified:
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPluginApi.java
Log:
Refactoring. Introduced a plugin interface. 2 advantages:
* both the plugin and the plugin API can implement it and thus don't have to
copy paste the Javadoc (except here the mailsender plugin was not implemented
following the rule that the API must delegate its calls to the plugin proper)
* allows to clearly see what's the plugin API and even create links to it in
the documentation.
Added:
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSender.java
===================================================================
---
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSender.java
(rev 0)
+++
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSender.java
2008-02-07 12:03:31 UTC (rev 7340)
@@ -0,0 +1,93 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package com.xpn.xwiki.plugin.mailsender;
+
+import org.apache.velocity.VelocityContext;
+
+import java.util.List;
+
+/**
+ * Send mails to recipients (to, cc, bcc). Both text and HTML emails can be
sent along with
+ * attachments. Also support XWiki page templates and allows sending a
collection of emails
+ * in one call.
+ *
+ * @version $Id: $
+ */
+public interface MailSender
+{
+ /**
+ * Sends an HTML mail, with a list of attachments
+ *
+ * @param to the recipient of the message
+ * @param from the sender
+ * @param cc carbon copy
+ * @param bcc hidden carbon copy
+ * @param subject the subject of the message
+ * @param body the body content of the mail
+ * @param alternative the alternative text offered to the mail client
+ * @param attachments List of com.xpn.xwiki.api.Attachment that will be
attached to the mail.
+ * @return 0 on success, -1 on failure. on failure the error message is
stored in XWiki context
+ */
+ int sendHtmlMessage(String from, String to, String cc, String bcc, String
subject,
+ String body, String alternative, List attachments);
+
+ /**
+ * Sends a simple text plain mail
+ *
+ * @param to the recipient of the message
+ * @param from the sender
+ * @param subject the subject of the message
+ * @param message the body of the message
+ * @return 0 on success, -1 on failure. on failure the error message is
stored in XWiki context
+ */
+ int sendTextMessage(String from, String to, String subject, String
message);
+
+ /**
+ * Sends a simple text plain mail with a list of files attachments
+ *
+ * @param to the recipient of the message
+ * @param from the sender
+ * @param cc carbon copy
+ * @param bcc hidden carbon copy
+ * @param subject the subject of the message
+ * @param message the body of the message
+ * @param attachments List of com.xpn.xwiki.api.Attachment that will be
attached to the mail.
+ * @return 0 on success, -1 on failure. on failure the error message is
stored in XWiki context
+ */
+ int sendTextMessage(String from, String to, String cc, String bcc, String
subject,
+ String message, List attachments);
+
+ /**
+ * Uses an XWiki document to build the message subject and context, based
on variables stored in
+ * the VelocityContext. Sends the email.
+ *
+ * @param from Email sender
+ * @param to Email recipient
+ * @param cc Email Carbon Copy
+ * @param bcc Email Hidden Carbon Copy
+ * @param language Language of the email
+ * @param documentFullName Full name of the template to be used (example:
+ * XWiki.MyEmailTemplate). The template needs to have an XWiki.Email
object attached
+ * @param vcontext Velocity context passed to the velocity renderer
+ * @return True if the email has been sent
+ */
+ int sendMessageFromTemplate(String from, String to, String cc, String bcc,
+ String language, String documentFullName, VelocityContext vcontext);
+}
\ No newline at end of file
Modified:
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java
===================================================================
---
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java
2008-02-07 11:00:11 UTC (rev 7339)
+++
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java
2008-02-07 12:03:31 UTC (rev 7340)
@@ -66,14 +66,18 @@
import com.xpn.xwiki.render.XWikiVelocityRenderer;
/**
- * Plugin that brings mailing capbilities to XWiki Recipients : to, cc, bcc
Text messages HTML
- * messages with attachments Text + HTML messages from XWiki pages templates
Send a collection of
- * mails in one call
+ * Plugin that brings powerful mailing capabilities.
*
+ * @see MailSender
* @version $Id: $
*/
-public final class MailSenderPlugin extends XWikiDefaultPlugin implements
XWikiPluginInterface
+public final class MailSenderPlugin extends XWikiDefaultPlugin
{
+ /**
+ * Log object to log messages in this class.
+ */
+ private static final Log LOG = LogFactory.getLog(MailSenderPlugin.class);
+
public static int ERROR_TEMPLATE_EMAIL_OBJECT_NOT_FOUND = -2;
public static int ERROR = -1;
@@ -84,8 +88,6 @@
protected static final String URL_SEPARATOR = "/";
- private static Log log = LogFactory.getLog(MailSenderPlugin.class);
-
/**
* [EMAIL PROTECTED]
*
@@ -137,14 +139,6 @@
}
/**
- * @return log Log interface
- */
- protected Log getLogger()
- {
- return log;
- }
-
- /**
* Split comma separated list of emails
*
* @param email comma separated list of emails
@@ -278,7 +272,7 @@
InternetAddress[] bcc = toInternetAddresses(mail.getBcc());
if ((to == null) && (cc == null) && (bcc == null)) {
- log.info("No recipient -> skipping this email");
+ LOG.info("No recipient -> skipping this email");
return null;
}
@@ -420,7 +414,7 @@
properties.put("mail.host", "localhost");
properties.put("mail.smtp.host", smtp);
properties.put("mail.debug", "false");
- log.debug("smtp: " + smtp);
+ LOG.debug("smtp: " + smtp);
return properties;
}
@@ -516,7 +510,7 @@
}
Mail mail = (Mail) emailIt.next();
- log.info("Sending email: " + mail.toFullString());
+ LOG.info("Sending email: " + mail.toFullString());
try {
@@ -534,15 +528,15 @@
transport.close();
}
} catch (MessagingException ex) {
- log.error("MessagingException has occured.", ex);
+ LOG.error("MessagingException has occured.", ex);
}
transport = null;
session = null;
}
} catch (SendFailedException ex) {
sendFailedCount++;
- log.error("SendFailedException has occured.", ex);
- log.error("Detailed email information" +
mail.toFullString());
+ LOG.error("SendFailedException has occured.", ex);
+ LOG.error("Detailed email information" +
mail.toFullString());
if (emailCount == 1) {
throw ex;
}
@@ -550,15 +544,15 @@
throw ex;
}
} catch (MessagingException mex) {
- log.error("MessagingException has occured.", mex);
- log.error("Detailed email information" +
mail.toFullString());
+ LOG.error("MessagingException has occured.", mex);
+ LOG.error("Detailed email information" +
mail.toFullString());
if (emailCount == 1) {
throw mex;
}
} catch (XWikiException e) {
- log.error("XWikiException has occured.", e);
+ LOG.error("XWikiException has occured.", e);
} catch (IOException e) {
- log.error("IOException has occured.", e);
+ LOG.error("IOException has occured.", e);
}
}
} finally {
@@ -567,10 +561,10 @@
transport.close();
}
} catch (MessagingException ex) {
- log.error("MessagingException has occured.", ex);
+ LOG.error("MessagingException has occured.", ex);
}
- log.info("sendEmails: Email count = " + emailCount + " sent count
= " + count);
+ LOG.info("sendEmails: Email count = " + emailCount + " sent count
= " + count);
}
return true;
}
@@ -603,7 +597,7 @@
obj = doc.getObject(EMAIL_XWIKI_CLASS_NAME, "language", "en");
}
if (obj == null) {
- log.error("No mail object found in the document " +
templateDocFullName);
+ LOG.error("No mail object found in the document " +
templateDocFullName);
return ERROR_TEMPLATE_EMAIL_OBJECT_NOT_FOUND;
}
String subjectContent = obj.getStringValue("subject");
@@ -634,7 +628,7 @@
sendMail(mail, context);
return 0;
} catch (Exception e) {
- log.error("sendEmailFromTemplate: " + templateDocFullName + "
vcontext: "
+ LOG.error("sendEmailFromTemplate: " + templateDocFullName + "
vcontext: "
+ updatedVelocityContext, e);
return ERROR;
}
Modified:
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPluginApi.java
===================================================================
---
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPluginApi.java
2008-02-07 11:00:11 UTC (rev 7339)
+++
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPluginApi.java
2008-02-07 12:03:31 UTC (rev 7340)
@@ -22,21 +22,27 @@
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.plugin.PluginApi;
import org.apache.velocity.VelocityContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import java.util.List;
/**
- * Plugin that brings powerful mailing capbilities to XWiki Recipients : to,
cc, bcc Text messages
- * HTML messages with attachments Text + HTML messages from XWiki pages
templates Send a collection
- * of mails in one call
+ * Plugin that brings powerful mailing capabilities.
*
* This is the wrapper accessible from in-document scripts.
*
+ * @see MailSender
* @version $Id: $
*/
-public class MailSenderPluginApi extends PluginApi
+public class MailSenderPluginApi extends PluginApi implements MailSender
{
/**
+ * Log object to log messages in this class.
+ */
+ private static final Log LOG =
LogFactory.getLog(MailSenderPluginApi.class);
+
+ /**
* API constructor.
*
* @param plugin The wrapped plugin object.
@@ -59,17 +65,8 @@
}
/**
- * Sends an HTML mail, with a list of attachments
- *
- * @param to the recipient of the message
- * @param from the sender
- * @param cc carbon copy
- * @param bcc hidden carbon copy
- * @param subject the subject of the message
- * @param body the body content of the mail
- * @param alternative the alternative text offered to the mail client
- * @param attachments List of com.xpn.xwiki.api.Attachment that will be
attached to the mail.
- * @return 0 on success, -1 on failure. on failure the error message is
stored in XWiki context
+ * [EMAIL PROTECTED]
+ * @see MailSender#sendHtmlMessage(String, String, String, String, String,
String, String, java.util.List)
*/
public int sendHtmlMessage(String from, String to, String cc, String bcc,
String subject,
String body, String alternative, List attachments)
@@ -88,19 +85,14 @@
return 0;
} catch (Exception e) {
context.put("error", e.getMessage());
- getMailSenderPlugin().getLogger().error("sendHtmlMessage", e);
+ LOG.error("sendHtmlMessage", e);
return -1;
}
}
/**
- * Sends a simple text plain mail
- *
- * @param to the recipient of the message
- * @param from the sender
- * @param subject the subject of the message
- * @param message the body of the message
- * @return 0 on success, -1 on failure. on failure the error message is
stored in XWiki context
+ * [EMAIL PROTECTED]
+ * @see MailSender#sendTextMessage(String, String, String, String)
*/
public int sendTextMessage(String from, String to, String subject, String
message)
{
@@ -115,22 +107,14 @@
return 0;
} catch (Exception e) {
context.put("error", e.getMessage());
- getMailSenderPlugin().getLogger().error("sendTextMessage", e);
+ LOG.error("sendTextMessage", e);
return -1;
}
}
/**
- * Sends a simple text plain mail with a list of files attachments
- *
- * @param to the recipient of the message
- * @param from the sender
- * @param cc carbon copy
- * @param bcc hidden carbon copy
- * @param subject the subject of the message
- * @param message the body of the message
- * @param attachments List of com.xpn.xwiki.api.Attachment that will be
attached to the mail.
- * @return 0 on success, -1 on failure. on failure the error message is
stored in XWiki context
+ * [EMAIL PROTECTED]
+ * @see MailSender#sendTextMessage(String, String, String, String, String,
String, java.util.List)
*/
public int sendTextMessage(String from, String to, String cc, String bcc,
String subject,
String message, List attachments)
@@ -148,24 +132,14 @@
return 0;
} catch (Exception e) {
context.put("error", e.getMessage());
- getMailSenderPlugin().getLogger().error("sendTextMessage", e);
+ LOG.error("sendTextMessage", e);
return -1;
}
}
/**
- * Uses an XWiki document to build the message subject and context, based
on variables stored in
- * the VelocityContext. Sends the email.
- *
- * @param from Email sender
- * @param to Email recipient
- * @param cc Email Carbon Copy
- * @param bcc Email Hidden Carbon Copy
- * @param language Language of the email
- * @param documentFullName Full name of the template to be used (example:
- * XWiki.MyEmailTemplate). The template needs to have an XWiki.Email
object attached
- * @param vcontext Velocity context passed to the velocity renderer
- * @return True if the email has been sent
+ * [EMAIL PROTECTED]
+ * @see MailSender#sendMessageFromTemplate(String, String, String, String,
String, String, VelocityContext)
*/
public int sendMessageFromTemplate(String from, String to, String cc,
String bcc,
String language, String documentFullName, VelocityContext vcontext)
@@ -174,7 +148,7 @@
return
getMailSenderPlugin().sendMailFromTemplate(documentFullName, from, to, cc, bcc,
language, vcontext, context);
} catch (Exception e) {
- getMailSenderPlugin().getLogger().error("sendMessageFromTemplate",
e);
+ LOG.error("sendMessageFromTemplate", e);
return -1;
}
}
_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications