james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e...@apache.org
Subject svn commit: r1294562 - in /james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets: LocalDelivery.java SieveMailet.java ToSenderFolder.java
Date Tue, 28 Feb 2012 10:40:49 GMT
Author: eric
Date: Tue Feb 28 10:40:49 2012
New Revision: 1294562

URL: http://svn.apache.org/viewvc?rev=1294562&view=rev
Log:
Implement a ToSenderFolder mailet (JAMES-1365) + make SieveMailet parametrized + use field
level @Resources without setters, easier to read.

Added:
    james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/ToSenderFolder.java
Modified:
    james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/LocalDelivery.java
    james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/SieveMailet.java

Modified: james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/LocalDelivery.java
URL: http://svn.apache.org/viewvc/james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/LocalDelivery.java?rev=1294562&r1=1294561&r2=1294562&view=diff
==============================================================================
--- james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/LocalDelivery.java
(original)
+++ james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/LocalDelivery.java
Tue Feb 28 10:40:49 2012
@@ -45,38 +45,19 @@ import org.apache.mailet.base.GenericMai
  * James 2.3 behavior.
  */
 public class LocalDelivery extends GenericMailet {
-    private RecipientRewriteTable recipientRewriteTable;  // Mailet that applies RecipientRewriteTable
-    private SieveMailet sieveMailet;  // Mailet that actually stores the message
-    private UsersRepository usersRepository;
+    @Resource(name = "recipientrewritetable")
     private org.apache.james.rrt.api.RecipientRewriteTable rrt;
-    private MailboxManager mailboxManager;
-    private DomainList domainList;
-    private FileSystem fileSystem;
-
     @Resource(name = "usersrepository")
-    public void setUsersRepository(UsersRepository usersRepository) {
-        this.usersRepository = usersRepository;
-    }
-
-    @Resource(name = "domainlist")
-    public void setDomainList(DomainList domainList) {
-        this.domainList = domainList;
-    }
-    
+    private UsersRepository usersRepository;
     @Resource(name = "mailboxmanager")
-    public void setMailboxManager(MailboxManager mailboxManager) {
-        this.mailboxManager = mailboxManager;
-    }
-    
+    private MailboxManager mailboxManager;
+    @Resource(name = "domainlist")
+    private DomainList domainList;
     @Resource(name = "filesystem")
-    public void setFileSystem(FileSystem fileSystem) {
-        this.fileSystem = fileSystem;
-    }
+    private FileSystem fileSystem;
 
-    @Resource(name = "recipientrewritetable")
-    public final void setRecipientRewriteTable(org.apache.james.rrt.api.RecipientRewriteTable
rrt) {
-        this.rrt = rrt;
-    }
+    private SieveMailet sieveMailet;  // Mailet that actually stores the message
+    private RecipientRewriteTable recipientRewriteTable;  // Mailet that applies RecipientRewriteTable
 
     /**
      * Delivers a mail to a local mailbox.
@@ -158,6 +139,7 @@ public class LocalDelivery extends Gener
         });
         // Override the default value of "quiet"
         sieveMailet.setQuiet(getInitParameter("quiet", true));
+        sieveMailet.setFolder("INBOX");
         
     }
 

Modified: james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/SieveMailet.java
URL: http://svn.apache.org/viewvc/james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/SieveMailet.java?rev=1294562&r1=1294561&r2=1294562&view=diff
==============================================================================
--- james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/SieveMailet.java
(original)
+++ james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/SieveMailet.java
Tue Feb 28 10:40:49 2012
@@ -16,7 +16,6 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  ****************************************************************/
-
 package org.apache.james.transport.mailets;
 
 import java.util.Date;
@@ -32,6 +31,7 @@ import org.apache.james.mailbox.MailboxS
 import org.apache.james.mailbox.MessageManager;
 import org.apache.james.mailbox.exception.BadCredentialsException;
 import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.transport.util.MailetContextLog;
 import org.apache.james.user.api.UsersRepository;
@@ -46,10 +46,10 @@ import org.apache.mailet.MailetConfig;
  * Contains resource bindings.
  */
 public class SieveMailet extends SieveMailboxMailet implements Poster {
-
     private UsersRepository usersRepos;
     private MailboxManager mailboxManager;
     private FileSystem fileSystem;
+    private String folder;
 
     @Resource(name = "usersrepository")
     public void setUsersRepository(UsersRepository usersRepos) {
@@ -66,6 +66,17 @@ public class SieveMailet extends SieveMa
         this.fileSystem = fileSystem;
     }
     
+    public void setFolder(String folder) {
+        this.folder = folder;
+    }
+    
+    public SieveMailet() {
+        super();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.jsieve.mailet.SieveMailboxMailet#init(org.apache.mailet.MailetConfig)
+     */
     @Override
     public void init(MailetConfig config) throws MessagingException {
         // ATM Fixed implementation
@@ -78,11 +89,6 @@ public class SieveMailet extends SieveMa
         super.init(config);
     }
 
-    public SieveMailet() {
-        super();
-
-    }
-
     /**
      * Return the username to use for sieve processing for the given
      * MailAddress. If virtualhosting is supported use the full emailaddrees as
@@ -105,36 +111,42 @@ public class SieveMailet extends SieveMa
         }
     }
 
+    /* (non-Javadoc)
+     * @see org.apache.jsieve.mailet.SieveMailboxMailet#storeMail(org.apache.mailet.MailAddress,
org.apache.mailet.MailAddress, org.apache.mailet.Mail)
+     */
     @Override
     public void storeMail(MailAddress sender, MailAddress recipient, Mail mail) throws MessagingException
{
         super.storeMail(sender, recipient, mail);
-
         String s;
         if (sender != null) {
             s = sender.toString();
         } else {
             s = "<>";
         }
-        // if no exception was thrown the message was successfully stored in the
-        // mailbox
-        log("Local delivered mail " + mail.getName() + " sucessfully from " + s + " to "
+ recipient.toString());
+        // If no exception was thrown the message was successfully stored in the mailbox
+        log("Local delivered mail " + mail.getName() + " sucessfully from " + s + " to "
+ recipient.toString() + " in folder " + this.folder);
     }
-
-    /**
-     * @see org.apache.jsieve.mailet.Poster#post(java.lang.String,
-     *      javax.mail.internet.MimeMessage)
+    
+    /* (non-Javadoc)
+     * @see org.apache.jsieve.mailet.Poster#post(java.lang.String, javax.mail.internet.MimeMessage)
      */
+    @Override
     public void post(String url, MimeMessage mail) throws MessagingException {
+        
         final int endOfScheme = url.indexOf(':');
+        
         if (endOfScheme < 0) {
             throw new MessagingException("Malformed URI");
-        } else {
+        } 
+        
+        else {
+
             final String scheme = url.substring(0, endOfScheme);
             if ("mailbox".equals(scheme)) {
                 final int startOfUser = endOfScheme + 3;
                 final int endOfUser = url.indexOf('@', startOfUser);
                 if (endOfUser < 0) {
-                    // TODO: when user missing, append to a default location
+                    // TODO: When user missing, append to a default location
                     throw new MessagingException("Shared mailbox is not supported");
                 } else {
                     // lowerCase the user - see https://issues.apache.org/jira/browse/JAMES-1369
@@ -145,13 +157,12 @@ public class SieveMailet extends SieveMa
                     final String urlPath;
                     final int length = url.length();
                     if (endOfHost + 1 == length) {
-                        urlPath = "INBOX";
+                        urlPath = this.folder;
                     } else {
                         urlPath = url.substring(endOfHost, length);
                     }
 
-                    // check if we should use the full emailaddress as
-                    // username
+                    // Check if we should use the full email address as username
                     try {
                         if (usersRepos.supportVirtualHosting()) {
                             user = user + "@" + host;
@@ -169,7 +180,7 @@ public class SieveMailet extends SieveMa
                         throw new MessagingException("Can not access mailbox", e);
                     }
 
-                    // start processing request
+                    // Start processing request
                     mailboxManager.startProcessingRequest(session);
 
                     // This allows Sieve scripts to use a standard delimiter
@@ -177,16 +188,15 @@ public class SieveMailet extends SieveMa
                     String destination = urlPath.replace('/', session.getPathDelimiter());
 
                     if (destination == null || "".equals(destination)) {
-                        destination = "INBOX";
+                        destination = this.folder;
                     }
                     if (destination.startsWith(session.getPathDelimiter() + ""))
                         destination = destination.substring(1);
                     
-                    // Use the MailboxSession to construct the MailboxPath.
-                    // See JAMES-1326
-                    final MailboxPath path = MailboxPath.inbox(session);
+                    // Use the MailboxSession to construct the MailboxPath - See JAMES-1326
+                    final MailboxPath path = new MailboxPath(MailboxConstants.USER_NAMESPACE,
user, this.folder);
                     try {
-                        if ("INBOX".equalsIgnoreCase(destination) && !(mailboxManager.mailboxExists(path,
session))) {
+                        if (this.folder.equalsIgnoreCase(destination) && !(mailboxManager.mailboxExists(path,
session))) {
                             mailboxManager.createMailbox(path, session);
                         }
                         final MessageManager mailbox = mailboxManager.getMailbox(path, session);
@@ -196,6 +206,7 @@ public class SieveMailet extends SieveMa
                         }
 
                         mailbox.appendMessage(new MimeMessageInputStream(mail), new Date(),
session, true, null);
+                    
                     } catch (MailboxException e) {
                         throw new MessagingException("Unable to access mailbox.", e);
                     } finally {
@@ -206,17 +217,21 @@ public class SieveMailet extends SieveMa
                             throw new MessagingException("Can logout from mailbox", e);
                         }
 
-                        // stop processing request
+                        // Stop processing request
                         mailboxManager.endProcessingRequest(session);
 
                     }
                 }
-            } else {
+                
+            } 
+            
+            else {
                 // TODO: add support for more protocols
-                // TODO: for example mailto: for forwarding over SMTP
-                // TODO: for example xmpp: for forwarding over Jabber
+                // TODO: - for example mailto: for forwarding over SMTP
+                // TODO: - for example xmpp: for forwarding over Jabber
                 throw new MessagingException("Unsupported protocol");
             }
         }
     }
+
 }

Added: james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/ToSenderFolder.java
URL: http://svn.apache.org/viewvc/james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/ToSenderFolder.java?rev=1294562&view=auto
==============================================================================
--- james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/ToSenderFolder.java
(added)
+++ james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/ToSenderFolder.java
Tue Feb 28 10:40:49 2012
@@ -0,0 +1,159 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.transport.mailets;
+
+import java.util.Date;
+
+import javax.annotation.Resource;
+import javax.mail.MessagingException;
+
+import org.apache.james.core.MimeMessageInputStream;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.exception.BadCredentialsException;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxConstants;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.transport.util.MailetContextLog;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.user.api.UsersRepositoryException;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.GenericMailet;
+
+/**
+ * Receives a Mail from the Queue and takes care to deliver the message
+ * to a defined folder of the sender.
+ * 
+ * You have to define the folder name of the sender.
+ * The flag 'consume' will tell is the mail will be further
+ * processed by the upcoming processor mailets, or not.
+ * 
+ * <pre>
+ * &lt;mailet match="RecipientIsLocal" class="ToSenderFolder"&gt;
+ *    &lt;folder&gt; <i>Sent Items</i> &lt;/folder&gt;
+ *    &lt;consume&gt; <i>false</i> &lt;/consume&gt;
+ * &lt;/mailet&gt;
+ * </pre>
+ * 
+ */
+public class ToSenderFolder extends GenericMailet {
+    @Resource(name = "usersrepository")
+    private UsersRepository usersRepository;
+    @Resource(name = "mailboxmanager")
+    private MailboxManager mailboxManager;
+
+    private String folder;
+    private boolean consume;
+
+    /**
+     * Delivers a mail to a local mailbox in a given folder.
+     * 
+     * @see org.apache.mailet.base.GenericMailet#service(org.apache.mailet.Mail)
+     */
+    @Override
+    public void service(Mail mail) throws MessagingException {
+        if (mail.getState() != Mail.GHOST) {
+            doService(mail);
+            if (consume) {
+                mail.setState(Mail.GHOST);
+            }
+        }
+    }
+
+    private void doService(Mail mail) throws MessagingException {
+
+        final MailAddress sender = mail.getSender();
+        String username = null;
+        try {
+            if (usersRepository.supportVirtualHosting()) {
+                username = sender.toString();
+            }
+            else {
+                username = sender.getLocalPart();
+            }
+        } catch (UsersRepositoryException e) {
+            throw new MessagingException(e.getMessage());
+        }
+
+        final MailboxSession session;
+        try {
+            session = mailboxManager.createSystemSession(username, new MailetContextLog(getMailetContext()));
+        } catch (BadCredentialsException e) {
+            throw new MessagingException("Unable to authenticate to mailbox", e);
+        } catch (MailboxException e) {
+            throw new MessagingException("Can not access mailbox", e);
+        }
+
+        mailboxManager.startProcessingRequest(session);
+
+        final MailboxPath path = new MailboxPath(MailboxConstants.USER_NAMESPACE, username,
this.folder);
+        
+        try {
+        
+            if (this.folder.equalsIgnoreCase(folder) && !(mailboxManager.mailboxExists(path,
session))) {
+                mailboxManager.createMailbox(path, session);
+            }
+            final MessageManager mailbox = mailboxManager.getMailbox(path, session);
+            if (mailbox == null) {
+                final String error = "Mailbox for username " + username + " was not found
on this server.";
+                throw new MessagingException(error);
+            }
+
+            mailbox.appendMessage(new MimeMessageInputStream(mail.getMessage()), new Date(),
session, true, null);
+
+            log("Local delivery with ToSenderFolder mailet for mail " + mail.getName() +
" with sender " + sender.toString() + " in folder " + this.folder);
+        
+        } catch (MailboxException e) {
+            throw new MessagingException("Unable to access mailbox.", e);
+        } finally {
+            session.close();
+            try {
+                mailboxManager.logout(session, true);
+            } catch (MailboxException e) {
+                throw new MessagingException("Can logout from mailbox", e);
+            }
+    
+            mailboxManager.endProcessingRequest(session);
+
+        }
+
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.mailet.base.GenericMailet#init()
+     */
+    @Override
+    public void init() throws MessagingException {
+        super.init();
+        this.folder = getInitParameter("folder", "Sent");
+        this.consume = getInitParameter("consume", false);
+
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.mailet.base.GenericMailet#getMailetInfo()
+     */
+    @Override
+    public String getMailetInfo() {
+        return ToSenderFolder.class.getName() + " Mailet";
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message