Author: jvdrean
Date: 2007-10-26 18:50:54 +0200 (Fri, 26 Oct 2007)
New Revision: 5526

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:
Context related fixes

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
   2007-10-26 16:41:01 UTC (rev 5525)
+++ 
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java
   2007-10-26 16:50:54 UTC (rev 5526)
@@ -84,10 +84,8 @@
 
     protected static final String URL_SEPARATOR = "/";
 
-    private static Log log = LogFactory.getLog(MailSenderPlugin.class);
+    private static Log log = LogFactory.getLog(MailSenderPlugin.class);    
 
-    protected XWikiContext context;         
-
     /**
      * [EMAIL PROTECTED]
      *
@@ -97,7 +95,6 @@
     {
         super(name, className, context);
         init(context);
-        this.context = context;
     }
 
     /**
@@ -233,7 +230,7 @@
      * @param mpart Multipart message
      * @param attachments List of attachments
      */
-    public void addAttachments(Multipart mpart, List attachments)
+    public void addAttachments(Multipart mpart, List attachments, XWikiContext 
context)
         throws XWikiException, IOException, MessagingException
     {
         if (attachments != null) {
@@ -270,7 +267,8 @@
      * @param session Mail session
      * @return The MIME message
      */
-    private MimeMessage createMimeMessage(Mail mail, Session session) throws 
MessagingException,
+    private MimeMessage createMimeMessage(Mail mail, Session session, 
XWikiContext context)
+        throws MessagingException,
         XWikiException, IOException
     {
         // this will also check for email error
@@ -303,7 +301,7 @@
         message.setSubject(mail.getSubject(), "UTF-8");
 
         if (mail.getHtmlPart() != null || mail.getAttachments() != null) {
-            Multipart multipart = createMimeMultipart(mail);
+            Multipart multipart = createMimeMultipart(mail, context);
             message.setContent(multipart);
         } else {
             message.setText(mail.getTextPart());
@@ -321,7 +319,8 @@
      * @param mail The original Mail
      * @return The Multipart MIME message
      */
-    public Multipart createMimeMultipart(Mail mail) throws MessagingException, 
XWikiException,
+    public Multipart createMimeMultipart(Mail mail, XWikiContext context)
+        throws MessagingException, XWikiException,
         IOException
     {
 
@@ -331,7 +330,7 @@
             BodyPart part = new MimeBodyPart();
             part.setContent(mail.getTextPart(), "text/plain");
             multipart.addBodyPart(part);
-            addAttachments(multipart, mail.getAttachments());
+            addAttachments(multipart, mail.getAttachments(), context);
             return multipart;
         } else {
 
@@ -360,7 +359,7 @@
                 part.setContent(alternativeMultipart);
                 mixedMultipart.addBodyPart(part);
 
-                addAttachments(mixedMultipart, mail.getAttachments());
+                addAttachments(mixedMultipart, mail.getAttachments(), context);
                 return mixedMultipart;
             }
             return alternativeMultipart;
@@ -404,7 +403,7 @@
      *
      * @return The properties
      */
-    private Properties initProperties()
+    private Properties initProperties(XWikiContext context)
     {
         Properties properties = new Properties();
 
@@ -435,7 +434,7 @@
      * @return The prepared context
      */
     public VelocityContext prepareVelocityContext(String fromAddr, String 
toAddr, String bccAddr,
-        VelocityContext vcontext)
+        VelocityContext vcontext, XWikiContext context)
     {
         if (vcontext == null) {
             vcontext = new VelocityContext();
@@ -448,8 +447,10 @@
         vcontext.put("to.bcc", bccAddr);
         vcontext.put("bounce", fromAddr);
 
-        // com.xpn.xwiki.api.XWiki xwikiApi = new 
com.xpn.xwiki.api.XWiki(context.getWiki(), context);
-        // vcontext.put("xwiki", xwikiApi);
+        com.xpn.xwiki.api.XWiki xwikiApi = new 
com.xpn.xwiki.api.XWiki(context.getWiki(), context);
+        vcontext.put("xwiki", xwikiApi);
+        com.xpn.xwiki.api.Context contextApi = new 
com.xpn.xwiki.api.Context(context);
+        vcontext.put("context", contextApi);
 
         return vcontext;
     }
@@ -487,12 +488,12 @@
      * @param mailItem The Mail to send
      * @return True if the the email has been sent
      */
-    public boolean sendMail(Mail mailItem) throws MessagingException,
+    public boolean sendMail(Mail mailItem, XWikiContext context) throws 
MessagingException,
         UnsupportedEncodingException
     {
         ArrayList mailList = new ArrayList();
         mailList.add(mailItem);
-        return sendMails(mailList);
+        return sendMails(mailList, context);
     }
 
     /**
@@ -501,7 +502,7 @@
      * @param emails Mail Collection
      * @return True in any case (TODO ?)
      */
-    public boolean sendMails(Collection emails) throws MessagingException,
+    public boolean sendMails(Collection emails, XWikiContext context) throws 
MessagingException,
         UnsupportedEncodingException
     {
         Session session = null;
@@ -513,7 +514,7 @@
             for (Iterator emailIt = emails.iterator(); emailIt.hasNext();) {
                 count++;
                 if ((transport == null) || (session == null)) {
-                    Properties props = initProperties();
+                    Properties props = initProperties(context);
                     session = Session.getDefaultInstance(props, null);
                     transport = session.getTransport("smtp");
                     transport.connect();
@@ -524,7 +525,7 @@
 
                 try {
 
-                    MimeMessage message = createMimeMessage(mail, session);
+                    MimeMessage message = createMimeMessage(mail, session, 
context);
                     if (message == null) {
                         continue;
                     }
@@ -594,10 +595,12 @@
      * @return True if the email has been sent
      */
     public int sendMailFromTemplate(String templateDocFullName, String from, 
String to,
-        String cc, String bcc, String language, VelocityContext vcontext) 
throws XWikiException
+        String cc, String bcc, String language, VelocityContext vcontext, 
XWikiContext context)
+        throws XWikiException
     {
 
-        VelocityContext updatedVelocityContext = prepareVelocityContext(from, 
to, bcc, vcontext);
+        VelocityContext updatedVelocityContext =
+            prepareVelocityContext(from, to, bcc, vcontext, context);
         XWiki xwiki = context.getWiki();
         XWikiDocument doc = xwiki.getDocument(templateDocFullName, context);
         BaseObject obj = doc.getObject(EMAIL_XWIKI_CLASS_NAME, "language", 
language);
@@ -633,7 +636,7 @@
             }
             mail.setTextPart(msg);
             mail.setHtmlPart(html);
-            sendMail(mail);
+            sendMail(mail, context);
             return 0;
         } catch (Exception e) {
             log.error("sendEmailFromTemplate: " + templateDocFullName + " 
vcontext: "

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
        2007-10-26 16:41:01 UTC (rev 5525)
+++ 
xwiki-platform/xwiki-plugins/trunk/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPluginApi.java
        2007-10-26 16:50:54 UTC (rev 5526)
@@ -17,24 +17,19 @@
  * 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 java.util.List;
-
-import org.apache.velocity.VelocityContext;
-
 import com.xpn.xwiki.XWikiContext;
-import com.xpn.xwiki.api.Attachment;
+import com.xpn.xwiki.api.Context;
 import com.xpn.xwiki.plugin.PluginApi;
+import org.apache.velocity.VelocityContext;
 
+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 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
  *
  * This is the wrapper accessible from in-document scripts.
  *
@@ -47,12 +42,11 @@
      *
      * @param plugin The wrapped plugin object.
      * @param context Context of the request.
-     * @see PluginApi#PluginApi(com.xpn.xwiki.plugin.XWikiPluginInterface, 
XWikiContext)
+     * @see 
PluginApi#PluginApi(com.xpn.xwiki.plugin.XWikiPluginInterface,XWikiContext)
      */
     public MailSenderPluginApi(MailSenderPlugin plugin, XWikiContext context)
     {
         super(plugin, context);
-
     }
 
     /**
@@ -62,7 +56,7 @@
      */
     public MailSenderPlugin getMailSenderPlugin()
     {
-        return (MailSenderPlugin)getPlugin();
+        return (MailSenderPlugin) getPlugin();
     }
 
     /**
@@ -91,7 +85,7 @@
             email.setTextPart(alternative);
             email.setHtmlPart(body);
             email.setAttachments(attachments);
-            getMailSenderPlugin().sendMail(email);
+            getMailSenderPlugin().sendMail(email, context);
             return 0;
         } catch (Exception e) {
             context.put("error", e.getMessage());
@@ -100,11 +94,9 @@
         }
     }
 
-
-
     /**
      * Sends a simple text plain mail
-     * 
+     *
      * @param to the recipient of the message
      * @param from the sender
      * @param subject the subject of the message
@@ -114,13 +106,13 @@
     public int sendTextMessage(String from, String to, String subject, String 
message)
     {
         try {
-             Mail email = new Mail();
-             email.setSubject(subject);
-             email.setTextPart(message);
-             email.setFrom(from);
-             email.setTo(to);
-             
-             getMailSenderPlugin().sendMail(email);
+            Mail email = new Mail();
+            email.setSubject(subject);
+            email.setTextPart(message);
+            email.setFrom(from);
+            email.setTo(to);
+
+            getMailSenderPlugin().sendMail(email, context);
             return 0;
         } catch (Exception e) {
             context.put("error", e.getMessage());
@@ -131,7 +123,7 @@
 
     /**
      * 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
@@ -153,7 +145,7 @@
             email.setCc(cc);
             email.setBcc(bcc);
             email.setAttachments(attachments);
-            getMailSenderPlugin().sendMail(email);
+            getMailSenderPlugin().sendMail(email, context);
             return 0;
         } catch (Exception e) {
             context.put("error", e.getMessage());
@@ -172,7 +164,7 @@
      * @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
+     * 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
      */
@@ -180,11 +172,11 @@
         String language, String documentFullName, VelocityContext vcontext)
     {
         try {
-            return 
getMailSenderPlugin().sendMailFromTemplate(documentFullName, from, to, cc, bcc, 
language, vcontext);
+            return 
getMailSenderPlugin().sendMailFromTemplate(documentFullName, from, to, cc, bcc,
+                language, vcontext, context);
         } catch (Exception e) {
             getMailSenderPlugin().getLogger().error("sendMessageFromTemplate", 
e);
             return -1;
         }
     }
-
 }

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to