Permalink
Browse files

Added SMTP routing for group-directed messages

  • Loading branch information...
1 parent afe414a commit e812da73c68216a5df2de562894fbe5e2e3ce254 @codders committed Sep 17, 2009
View
6 slingtests/osgikernel/bundles/mailman/pom.xml
@@ -74,6 +74,12 @@
<scope>provided</scope>
</dependency>
<dependency>
+ <groupId>org.sakaiproject.kernel</groupId>
+ <artifactId>org.sakaiproject.kernel.personal</artifactId>
+ <version>0.1-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.api</artifactId>
<version>2.0.4-incubator</version>
View
2 ...ikernel/bundles/mailman/src/main/java/org/sakaiproject/kernel/mailman/MailmanManager.java
@@ -16,6 +16,7 @@
*/
package org.sakaiproject.kernel.mailman;
+import org.sakaiproject.kernel.api.message.MessageRoute;
import org.sakaiproject.kernel.mailman.impl.MailmanException;
import java.util.List;
@@ -28,4 +29,5 @@
public boolean listHasMember(String listName, String listPassword, String memberEmail) throws MailmanException;
public boolean addMember(String listName, String listPassword, String userEmail) throws MailmanException;
public boolean removeMember(String listName, String listPassword, String userEmail) throws MailmanException;
+ public MessageRoute generateMessageRouteForGroup(String groupName);
}
View
33 ...ndles/mailman/src/main/java/org/sakaiproject/kernel/mailman/impl/MailmanGroupManager.java
@@ -18,11 +18,13 @@
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
+import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
+import org.sakaiproject.kernel.api.personal.PersonalUtils;
import org.sakaiproject.kernel.api.user.AuthorizableEvent;
import org.sakaiproject.kernel.api.user.AuthorizableEvent.Operation;
import org.sakaiproject.kernel.mailman.MailmanManager;
@@ -31,7 +33,9 @@
import java.util.Dictionary;
+import javax.jcr.Node;
import javax.jcr.RepositoryException;
+import javax.jcr.Session;
/**
* @scr.component immediate="true" label="MailManagerImpl"
@@ -54,6 +58,9 @@
/** @scr.reference */
private MailmanManager mailmanManager;
+
+ /** @scr.reference */
+ private SlingRepository slingRepository;
private String listManagementPassword;
@@ -90,8 +97,13 @@ public void handleEvent(Event event) {
Group group = (Group)event.getProperty(AuthorizableEvent.GROUP);
User user = (User)event.getProperty(AuthorizableEvent.USER);
try {
- LOGGER.info("Adding " + user.getID() + " to mailman group " + group.getID());
- mailmanManager.addMember(group.getID(), listManagementPassword, user.getID() + "@example.com");
+ String emailAddress = getEmailForUser(user);
+ if (emailAddress != null) {
+ LOGGER.info("Adding " + user.getID() + " to mailman group " + group.getID());
+ mailmanManager.addMember(group.getID(), listManagementPassword, emailAddress);
+ } else {
+ LOGGER.warn("No email address recorded for user: " + user.getID() + ". Not adding to mailman list");
+ }
} catch (RepositoryException e) {
LOGGER.error("Repository exception adding user to mailman group", e);
} catch (MailmanException e) {
@@ -105,8 +117,13 @@ public void handleEvent(Event event) {
Group group = (Group)event.getProperty(AuthorizableEvent.GROUP);
User user = (User)event.getProperty(AuthorizableEvent.USER);
try {
- LOGGER.info("Adding " + user.getID() + " to mailman group " + group.getID());
- mailmanManager.removeMember(group.getID(), listManagementPassword, user.getID() + "@example.com");
+ String emailAddress = getEmailForUser(user);
+ if (emailAddress != null) {
+ LOGGER.info("Adding " + user.getID() + " to mailman group " + group.getID());
+ mailmanManager.removeMember(group.getID(), listManagementPassword, emailAddress);
+ } else {
+ LOGGER.warn("No email address recorded for user: " + user.getID() + ". Not removing from mailman list");
+ }
} catch (RepositoryException e) {
LOGGER.error("Repository exception removing user from mailman group", e);
} catch (MailmanException e) {
@@ -117,6 +134,14 @@ public void handleEvent(Event event) {
}
}
+ private String getEmailForUser(User user) throws RepositoryException {
+ Session session = slingRepository.loginAdministrative(null);
+ Node profileNode = (Node)session.getItem(PersonalUtils.getProfilePath(user.getID()));
+ String emailAddress = PersonalUtils.getEmailAddress(profileNode);
+ session.logout();
+ return emailAddress;
+ }
+
@SuppressWarnings("unchecked")
public void updated(Dictionary config) throws ConfigurationException {
LOGGER.info("Got config update");
View
5 ...undles/mailman/src/main/java/org/sakaiproject/kernel/mailman/impl/MailmanManagerImpl.java
@@ -29,6 +29,7 @@
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.osgi.service.component.ComponentContext;
+import org.sakaiproject.kernel.api.message.MessageRoute;
import org.sakaiproject.kernel.api.proxy.ProxyClientService;
import org.sakaiproject.kernel.mailman.MailmanManager;
import org.slf4j.Logger;
@@ -353,4 +354,8 @@ public void updated(Dictionary config) throws ConfigurationException {
configMap = builder.build();
}
+ public MessageRoute generateMessageRouteForGroup(String groupName) {
+ return new MailmanMessageRoute(groupName + "@" + configMap.get(MAILMAN_HOST), "smtp");
+ }
+
}
View
23 ...ndles/mailman/src/main/java/org/sakaiproject/kernel/mailman/impl/MailmanMessageRoute.java
@@ -0,0 +1,23 @@
+package org.sakaiproject.kernel.mailman.impl;
+
+import org.sakaiproject.kernel.api.message.MessageRoute;
+
+public class MailmanMessageRoute implements MessageRoute {
+
+ private String transport;
+ private String rcpt;
+
+ public MailmanMessageRoute(String rcpt, String transport) {
+ this.rcpt = rcpt;
+ this.transport = transport;
+ }
+
+ public String getRcpt() {
+ return rcpt;
+ }
+
+ public String getTransport() {
+ return transport;
+ }
+
+}
View
49 ...dles/mailman/src/main/java/org/sakaiproject/kernel/mailman/impl/MailmanMessageRouter.java
@@ -0,0 +1,49 @@
+package org.sakaiproject.kernel.mailman.impl;
+
+import org.sakaiproject.kernel.api.message.MessageRoute;
+import org.sakaiproject.kernel.api.message.MessageRouter;
+import org.sakaiproject.kernel.api.message.MessageRoutes;
+import org.sakaiproject.kernel.mailman.MailmanManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.jcr.Node;
+
+/**
+ *
+ * @scr.component inherit="true" label="MailmanMessageRouter" immediate="true"
+ * @scr.service interface="org.sakaiproject.kernel.api.message.MessageRouter"
+ * @scr.property name="service.description"
+ * value="Manages Routing for group mailing lists."
+ * @scr.property name="service.vendor" value="The Sakai Foundation"
+ */
+public class MailmanMessageRouter implements MessageRouter {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(MailmanMessageRouter.class);
+
+ /** @scr.reference */
+ private MailmanManager mailmanManager;
+
+ public int getPriority() {
+ return 1;
+ }
+
+ public void route(Node n, MessageRoutes routing) {
+ LOGGER.info("Mailman routing message: " + n);
+ List<MessageRoute> toRemove = new ArrayList<MessageRoute>();
+ List<MessageRoute> toAdd = new ArrayList<MessageRoute>();
+ for (MessageRoute route : routing) {
+ if ("internal".equals(route.getTransport()) && route.getRcpt().startsWith("g-")) {
+ LOGGER.info("Found an internal group message. Routing to SMTP");
+ toRemove.add(route);
+ toAdd.add(mailmanManager.generateMessageRouteForGroup(route.getRcpt()));
+ }
+ }
+ routing.removeAll(toRemove);
+ routing.addAll(toAdd);
+ }
+
+}
View
5 ...undles/personal/src/main/java/org/sakaiproject/kernel/api/personal/PersonalConstants.java
@@ -66,5 +66,10 @@
* The node name of the authentication profile in public space.
*/
public static final String AUTH_PROFILE = "authprofile";
+
+ /**
+ * Property name for the e-mail property of a user's profile
+ */
+ public static final String EMAIL_ADDRESS = "email";
}
View
24 ...el/bundles/personal/src/main/java/org/sakaiproject/kernel/api/personal/PersonalUtils.java
@@ -48,6 +48,23 @@
private static final Logger LOGGER = LoggerFactory.getLogger(PersonalUtils.class);
+ /**
+ * Writes userinfo out for a property in a node. Make sure that the resultNode has a
+ * property with propertyName that contains a userid.
+ *
+ * @param session
+ * The JCR Session
+ * @param user
+ * The user name
+ * @param write
+ * The writer to write to.
+ * @param jsonName
+ * The json name that should be used.
+ * @throws ValueFormatException
+ * @throws PathNotFoundException
+ * @throws RepositoryException
+ * @throws JSONException
+ */
public static void writeUserInfo(Session session, String user, JSONWriter write,
String jsonName) {
try {
@@ -139,4 +156,11 @@ public static String getPrivatePath(String user, String path) {
return PathUtils.toInternalHashedPath(_USER_PRIVATE, userS, path);
}
}
+
+ public static String getEmailAddress(Node profileNode) throws RepositoryException {
+ if (profileNode.hasProperty(PersonalConstants.EMAIL_ADDRESS)) {
+ return profileNode.getProperty(PersonalConstants.EMAIL_ADDRESS).getString();
+ }
+ return null;
+ }
}
View
4 slingtests/osgikernel/testscripts/SlingRuby/sling/message.rb
@@ -8,8 +8,8 @@ def initialize(sling)
@sling = sling
end
- def create(name, type, box = "drafts")
- return @sling.execute_post(@sling.url_for("_user/message.create.html"), "sakai:type" => type, "sakai:to" => name, "sakai:sendstate" => "pending", "sakai:messagebox" => box )
+ def create(name, type, box = "drafts", props = {})
+ return @sling.execute_post(@sling.url_for("_user/message.create.html"), props.update("sakai:type" => type, "sakai:to" => name, "sakai:sendstate" => "pending", "sakai:messagebox" => box))
end
def send(messageId)

0 comments on commit e812da7

Please sign in to comment.