You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2006/12/07 19:15:48 UTC

svn commit: r483590 - in /lenya/trunk/src: java/org/apache/lenya/cms/ac/ modules/notification/config/lenya-roles/ modules/notification/java/src/org/apache/lenya/notification/ pubs/default/modules/defaultusecases/java/src/org/apache/lenya/defaultpub/cms...

Author: andreas
Date: Thu Dec  7 10:15:47 2006
New Revision: 483590

URL: http://svn.apache.org/viewvc?view=rev&rev=483590
Log:
Refactoring: extract abstract superclass from DefaultNotifier, send Publish notification to submitter

Added:
    lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/AbstractNotifier.java
    lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/EmailNotifier.java
Removed:
    lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/DefaultNotifier.java
Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/ac/PolicyUtil.java
    lenya/trunk/src/modules/notification/config/lenya-roles/notifier.xroles
    lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/NotificationUtil.java
    lenya/trunk/src/pubs/default/modules/defaultusecases/java/src/org/apache/lenya/defaultpub/cms/usecases/Publish.java

Modified: lenya/trunk/src/java/org/apache/lenya/cms/ac/PolicyUtil.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/cms/ac/PolicyUtil.java?view=diff&rev=483590&r1=483589&r2=483590
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/ac/PolicyUtil.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/ac/PolicyUtil.java Thu Dec  7 10:15:47 2006
@@ -68,6 +68,45 @@
         Role[] roles = (Role[]) roleList.toArray(new Role[roleList.size()]);
         return roles;
     }
+    
+    /**
+     * @param manager The service manager.
+     * @param webappUrl The web application URL.
+     * @param userId The user ID.
+     * @param logger The logger.
+     * @return A user.
+     * @throws AccessControlException if an error occurs.
+     */
+    public static final User getUser(ServiceManager manager, String webappUrl,
+            String userId, Logger logger) throws AccessControlException {
+        ServiceSelector selector = null;
+        AccessControllerResolver resolver = null;
+        AccessController controller = null;
+        try {
+            selector = (ServiceSelector) manager.lookup(AccessControllerResolver.ROLE + "Selector");
+            resolver = (AccessControllerResolver) selector
+                    .select(AccessControllerResolver.DEFAULT_RESOLVER);
+            controller = resolver.resolveAccessController(webappUrl);
+
+            AccreditableManager accreditableManager = controller.getAccreditableManager();
+            UserManager userManager = accreditableManager.getUserManager();
+            
+            return userManager.getUser(userId);
+        } catch (ServiceException e) {
+            throw new AccessControlException(e);
+        } finally {
+            if (selector != null) {
+                if (resolver != null) {
+                    if (controller != null) {
+                        resolver.release(controller);
+                    }
+                    selector.release(resolver);
+                }
+                manager.release(selector);
+            }
+        }
+
+    }
 
     /**
      * @param manager The service manager.

Modified: lenya/trunk/src/modules/notification/config/lenya-roles/notifier.xroles
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/notification/config/lenya-roles/notifier.xroles?view=diff&rev=483590&r1=483589&r2=483590
==============================================================================
--- lenya/trunk/src/modules/notification/config/lenya-roles/notifier.xroles (original)
+++ lenya/trunk/src/modules/notification/config/lenya-roles/notifier.xroles Thu Dec  7 10:15:47 2006
@@ -3,6 +3,6 @@
 
   <role name="org.apache.lenya.notification.Notifier"
     shorthand="notifier"
-    default-class="org.apache.lenya.notification.DefaultNotifier"/>
+    default-class="org.apache.lenya.notification.EmailNotifier"/>
   
 </xroles>

Added: lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/AbstractNotifier.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/AbstractNotifier.java?view=auto&rev=483590
==============================================================================
--- lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/AbstractNotifier.java (added)
+++ lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/AbstractNotifier.java Thu Dec  7 10:15:47 2006
@@ -0,0 +1,134 @@
+/*
+ * 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.lenya.notification;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.cocoon.components.ContextHelper;
+import org.apache.cocoon.environment.Request;
+import org.apache.cocoon.environment.Session;
+import org.apache.cocoon.transformation.I18nTransformer;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.lenya.xml.DocumentHelper;
+import org.apache.lenya.xml.NamespaceHelper;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Base class for notifier implementation.
+ */
+public abstract class AbstractNotifier extends AbstractLogEnabled implements Notifier, Serviceable,
+        Contextualizable {
+
+    protected static final String NAMESPACE = "http://apache.org/lenya/notification/2.0";
+
+    protected Message translateMessage(String locale, Message message) throws NotificationException {
+
+        SourceResolver resolver = null;
+        Source source = null;
+        try {
+
+            NamespaceHelper helper = new NamespaceHelper(NAMESPACE, "not", "message");
+            Document doc = helper.getDocument();
+            NamespaceHelper i18nHelper = new NamespaceHelper(I18nTransformer.I18N_NAMESPACE_URI,
+                    "i18n", doc);
+
+            Element docElement = doc.getDocumentElement();
+
+            Element subjectElement = helper.createElement("subject");
+            docElement.appendChild(subjectElement);
+            Element i18nTranslateSubjectElement = i18nHelper.createElement("translate");
+            subjectElement.appendChild(i18nTranslateSubjectElement);
+            Element subjectI18nElement = i18nHelper.createElement("text", message.getSubject());
+            i18nTranslateSubjectElement.appendChild(subjectI18nElement);
+
+            String[] subjectParams = message.getSubjectParameters();
+            for (int i = 0; i < subjectParams.length; i++) {
+                Element paramElement = i18nHelper.createElement("param", subjectParams[i]);
+                i18nTranslateSubjectElement.appendChild(paramElement);
+            }
+
+            Element bodyElement = helper.createElement("body");
+            docElement.appendChild(bodyElement);
+            Element i18nTranslateElement = i18nHelper.createElement("translate");
+            bodyElement.appendChild(i18nTranslateElement);
+            Element bodyI18nElement = i18nHelper.createElement("text", message.getBody());
+            i18nTranslateElement.appendChild(bodyI18nElement);
+
+            String[] msgParams = message.getBodyParameters();
+            for (int i = 0; i < msgParams.length; i++) {
+                Element paramElement = i18nHelper.createElement("param", msgParams[i]);
+                i18nTranslateElement.appendChild(paramElement);
+            }
+
+            Session session = this.request.getSession();
+            session.setAttribute("notification.dom", doc);
+
+            Map parameters = new HashMap();
+
+            parameters.put("locale", locale);
+
+            resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
+            source = resolver.resolveURI("cocoon://modules/notification/message.xml", null,
+                    parameters);
+
+            doc = DocumentHelper.readDocument(source.getInputStream());
+            helper = new NamespaceHelper(NAMESPACE, "not", doc);
+
+            subjectElement = helper.getFirstChild(doc.getDocumentElement(), "subject");
+            String subject = DocumentHelper.getSimpleElementText(subjectElement);
+
+            bodyElement = helper.getFirstChild(doc.getDocumentElement(), "body");
+            String body = DocumentHelper.getSimpleElementText(bodyElement);
+
+            return new Message(subject, new String[0], body, new String[0]);
+        } catch (Exception e) {
+            throw new NotificationException(e);
+        } finally {
+            if (resolver != null) {
+                if (source != null) {
+                    resolver.release(source);
+                }
+                this.manager.release(resolver);
+            }
+        }
+
+    }
+
+    protected ServiceManager manager;
+
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+    private Request request;
+
+    public void contextualize(Context context) throws ContextException {
+        this.request = ContextHelper.getRequest(context);
+    }
+
+}

Added: lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/EmailNotifier.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/EmailNotifier.java?view=auto&rev=483590
==============================================================================
--- lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/EmailNotifier.java (added)
+++ lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/EmailNotifier.java Thu Dec  7 10:15:47 2006
@@ -0,0 +1,99 @@
+/*
+ * 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.lenya.notification;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.cocoon.mail.MailSender;
+import org.apache.lenya.ac.Group;
+import org.apache.lenya.ac.Identifiable;
+import org.apache.lenya.ac.User;
+
+/**
+ * Default notifier implementation.
+ */
+public class EmailNotifier extends AbstractNotifier implements Configurable {
+
+    public void notify(Identifiable[] recipients, Identifiable sender, Message message)
+            throws NotificationException {
+
+        Set noDuplicates = new HashSet();
+
+        for (int i = 0; i < recipients.length; i++) {
+            if (recipients[i] instanceof Group) {
+                Group group = (Group) recipients[i];
+                noDuplicates.addAll(Arrays.asList(group.getMembers()));
+            } else {
+                noDuplicates.add(recipients[i]);
+            }
+        }
+
+        for (Iterator i = noDuplicates.iterator(); i.hasNext();) {
+            Identifiable identifiable = (Identifiable) i.next();
+            if (identifiable instanceof User) {
+                notify((User) identifiable, sender, message);
+            }
+        }
+
+    }
+
+    protected void notify(User recipient, Identifiable sender, Message message)
+            throws NotificationException {
+
+        MailSender mailer = null;
+        try {
+            mailer = (MailSender) this.manager.lookup(MailSender.ROLE);
+            mailer.setSmtpHost(this.smtpHost);
+
+            mailer.setTo(recipient.getEmail());
+            if (sender instanceof User) {
+                mailer.setFrom(((User) sender).getEmail());
+            }
+
+            Message translatedMessage = translateMessage(recipient.getDefaultMenuLocale(), message);
+
+            mailer.setSubject(translatedMessage.getSubject());
+            mailer.setBody(translatedMessage.getBody(), "text/plain");
+            mailer.setCharset("UTF-8");
+            mailer.send();
+
+        } catch (Exception e) {
+            throw new NotificationException(e);
+        } finally {
+            if (mailer != null) {
+                this.manager.release(mailer);
+            }
+        }
+
+    }
+
+    private String smtpHost;
+
+    protected static final String ELEMENT_SMTP_HOST = "smtp-host";
+
+    public void configure(Configuration config) throws ConfigurationException {
+        this.smtpHost = config.getChild(ELEMENT_SMTP_HOST).getValue();
+    }
+
+}

Modified: lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/NotificationUtil.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/NotificationUtil.java?view=diff&rev=483590&r1=483589&r2=483590
==============================================================================
--- lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/NotificationUtil.java (original)
+++ lenya/trunk/src/modules/notification/java/src/org/apache/lenya/notification/NotificationUtil.java Thu Dec  7 10:15:47 2006
@@ -24,7 +24,7 @@
 /**
  * Notification utility.
  */
-public class NotificationUtil {
+public final class NotificationUtil {
 
     /**
      * Invokes a notification.
@@ -34,7 +34,7 @@
      * @param message The message.
      * @throws NotificationException if an error occurs.
      */
-    public static void notify(ServiceManager manager, Identifiable[] recipients,
+    public static final void notify(ServiceManager manager, Identifiable[] recipients,
             Identifiable sender, Message message) throws NotificationException {
 
         Notifier notifier = null;

Modified: lenya/trunk/src/pubs/default/modules/defaultusecases/java/src/org/apache/lenya/defaultpub/cms/usecases/Publish.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/pubs/default/modules/defaultusecases/java/src/org/apache/lenya/defaultpub/cms/usecases/Publish.java?view=diff&rev=483590&r1=483589&r2=483590
==============================================================================
--- lenya/trunk/src/pubs/default/modules/defaultusecases/java/src/org/apache/lenya/defaultpub/cms/usecases/Publish.java (original)
+++ lenya/trunk/src/pubs/default/modules/defaultusecases/java/src/org/apache/lenya/defaultpub/cms/usecases/Publish.java Thu Dec  7 10:15:47 2006
@@ -60,7 +60,9 @@
 import org.apache.lenya.notification.Message;
 import org.apache.lenya.notification.NotificationException;
 import org.apache.lenya.notification.NotificationUtil;
+import org.apache.lenya.workflow.Version;
 import org.apache.lenya.workflow.WorkflowException;
+import org.apache.lenya.workflow.Workflowable;
 
 /**
  * Publish usecase handler.
@@ -312,27 +314,39 @@
             DocumentException, AccessControlException {
         User sender = getSession().getIdentity().getUser();
 
-        Identifiable[] recipients = PolicyUtil.getUsersWithRole(this.manager, authoringDocument
-                .getCanonicalWebappURL(), "review", getLogger());
-
-        Document liveVersion = authoringDocument.getAreaVersion(Publication.LIVE_AREA);
-        String url;
-
-        Proxy proxy = liveVersion.getPublication().getProxy(liveVersion, false);
-        if (proxy != null) {
-            url = proxy.getURL(liveVersion);
-        } else {
-            Request request = ContextHelper.getRequest(this.context);
-            final String serverUrl = "http://" + request.getServerName() + ":"
-                    + request.getServerPort();
-            final String webappUrl = liveVersion.getCanonicalWebappURL();
-            url = serverUrl + request.getContextPath() + webappUrl;
+        Workflowable workflowable = WorkflowUtil.getWorkflowable(this.manager, getSession(),
+                getLogger(), authoringDocument);
+        Version version = workflowable.getLatestVersion();
+
+        // we assume that the document has been submitted, otherwise we do nothing
+        if (version.getEvent().equals("submit")) {
+            
+            String userId = version.getUserId();
+
+            User user = PolicyUtil.getUser(this.manager, authoringDocument
+                    .getCanonicalWebappURL(), userId, getLogger());
+            
+            Identifiable[] recipients = { user };
+    
+            Document liveVersion = authoringDocument.getAreaVersion(Publication.LIVE_AREA);
+            String url;
+    
+            Proxy proxy = liveVersion.getPublication().getProxy(liveVersion, false);
+            if (proxy != null) {
+                url = proxy.getURL(liveVersion);
+            } else {
+                Request request = ContextHelper.getRequest(this.context);
+                final String serverUrl = "http://" + request.getServerName() + ":"
+                        + request.getServerPort();
+                final String webappUrl = liveVersion.getCanonicalWebappURL();
+                url = serverUrl + request.getContextPath() + webappUrl;
+            }
+            String[] params = { url };
+            Message message = new Message(MESSAGE_SUBJECT, new String[0], MESSAGE_DOCUMENT_PUBLISHED,
+                    params);
+    
+            NotificationUtil.notify(this.manager, recipients, sender, message);
         }
-        String[] params = { url };
-        Message message = new Message(MESSAGE_SUBJECT, new String[0], MESSAGE_DOCUMENT_PUBLISHED,
-                params);
-
-        NotificationUtil.notify(this.manager, recipients, sender, message);
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org