KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > task > CocoonTaskWrapper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: CocoonTaskWrapper.java 43089 2004-06-28 08:29:46Z andreas $ */
19
20 package org.apache.lenya.cms.cocoon.task;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.cocoon.environment.ObjectModelHelper;
27 import org.apache.cocoon.environment.Request;
28 import org.apache.cocoon.environment.Session;
29 import org.apache.lenya.ac.AccessControlException;
30 import org.apache.lenya.ac.Identity;
31 import org.apache.lenya.ac.Role;
32 import org.apache.lenya.ac.User;
33 import org.apache.lenya.ac.impl.PolicyAuthorizer;
34 import org.apache.lenya.cms.publication.Publication;
35 import org.apache.lenya.cms.publication.PublicationException;
36 import org.apache.lenya.cms.publication.PublicationFactory;
37 import org.apache.lenya.cms.task.DefaultTaskWrapper;
38 import org.apache.lenya.cms.task.ExecutionException;
39 import org.apache.lenya.cms.task.Notifier;
40 import org.apache.lenya.cms.task.TaskWrapperParameters;
41 import org.apache.lenya.cms.task.WorkflowInvoker;
42 import org.apache.lenya.util.NamespaceMap;
43 import org.apache.lenya.util.ServletHelper;
44 import org.apache.log4j.Category;
45
46 /**
47  * Task wrapper to be used from Cocoon components.
48  */

49 public class CocoonTaskWrapper extends DefaultTaskWrapper {
50
51     private static Category log = Category.getInstance(CocoonTaskWrapper.class);
52
53     /**
54      * Ctor to be called from a Cocoon component.
55      * @param objectModel A Cocoon object model.
56      * @param parameters A parameters object.
57      * @throws ExecutionException when something went wrong.
58      */

59     public CocoonTaskWrapper(Map JavaDoc objectModel, Parameters parameters) throws ExecutionException {
60
61         log.debug("Creating CocoonTaskWrapper");
62
63         Publication publication;
64         try {
65             publication = PublicationFactory.getPublication(objectModel);
66         } catch (PublicationException e) {
67             throw new ExecutionException(e);
68         }
69         Request request = ObjectModelHelper.getRequest(objectModel);
70
71         initialize(parameters, publication, request);
72     }
73     
74     /**
75      * Ctor.
76      */

77     protected CocoonTaskWrapper() {
78     }
79
80     /**
81      * Initializes this wrapper.
82      * @param parameters The task parameters.
83      * @param publication The publication.
84      * @param request The request.
85      * @throws ExecutionException when something went wrong.
86      */

87     protected void initialize(Parameters parameters, Publication publication, Request request)
88         throws ExecutionException {
89         setNotifying(request);
90         
91         Parameters taskParameters = extractTaskParameters(parameters, publication, request);
92         getTaskParameters().parameterize(taskParameters);
93         
94         String JavaDoc taskId = request.getParameter(TaskWrapperParameters.TASK_ID);
95         taskId = parameters.getParameter(TaskWrapperParameters.TASK_ID, taskId);
96         
97         String JavaDoc webappUrl = ServletHelper.getWebappURI(request);
98         initialize(taskId, publication, webappUrl, taskParameters);
99         
100         String JavaDoc eventName = request.getParameter(WorkflowInvoker.EVENT_REQUEST_PARAMETER);
101         if (eventName == null) {
102             eventName = request.getParameter(WorkflowInvoker.LENYA_EVENT_REQUEST_PARAMETER);
103         }
104         if (eventName != null) {
105             Session session = request.getSession(false);
106             if (session == null) {
107                 log.debug("No session found - not enabling workflow handling.");
108             } else {
109                 Identity identity = Identity.getIdentity(session);
110                 if (identity == null) {
111                     log.debug("No identity found - not enabling workflow handling.");
112                 } else {
113                     log.debug("Identity found - enabling workflow handling.");
114                     Role[] roles;
115                     try {
116                         roles = PolicyAuthorizer.getRoles(request);
117                     } catch (AccessControlException e) {
118                         throw new ExecutionException(e);
119                     }
120                     setWorkflowAware(eventName, identity, roles);
121                 }
122             }
123         }
124         
125     }
126
127     /**
128      * Enables notification if the corresponding request parameters exist.
129      * @param request The request.
130      */

131     protected void setNotifying(Request request) {
132
133         log.debug("Trying to initialize notification ...");
134
135         Map JavaDoc requestParameters = ServletHelper.getParameterMap(request);
136
137         log.debug(" Request parameters:");
138         for (Iterator JavaDoc i = requestParameters.keySet().iterator(); i.hasNext();) {
139             Object JavaDoc key = i.next();
140             log.debug(" [" + key + "] = [" + requestParameters.get(key) + "]");
141         }
142
143         NamespaceMap notificationMap = new NamespaceMap(requestParameters, Notifier.PREFIX);
144
145         log.debug(" Notification parameters:");
146         for (Iterator JavaDoc i = notificationMap.getMap().keySet().iterator(); i.hasNext();) {
147             Object JavaDoc key = i.next();
148             log.debug(" [" + key + "] = [" + notificationMap.getMap().get(key) + "]");
149         }
150
151         if (notificationMap.getMap().isEmpty()) {
152             log.debug(" No notification parameters found.");
153         } else {
154             log.debug(" Initializing notification");
155             
156             Identity identity = Identity.getIdentity(request.getSession());
157             User user = identity.getUser();
158             String JavaDoc eMail = user.getEmail();
159             notificationMap.put(Notifier.PARAMETER_FROM, eMail);
160             log.debug(" Setting from address [" + Notifier.PARAMETER_FROM + "] = [" + eMail + "]");
161
162             String JavaDoc toKey = NamespaceMap.getFullName(Notifier.PREFIX, Notifier.PARAMETER_TO);
163             String JavaDoc toString = "";
164             String JavaDoc[] toValues = request.getParameterValues(toKey);
165
166             if (toValues == null) {
167                 throw new IllegalStateException JavaDoc("You must specify at least one [notification.tolist] request parameter!");
168             }
169
170             for (int i = 0; i < toValues.length; i++) {
171                 if (i > 0 && !"".equals(toString)) {
172                     toString += ",";
173                 }
174                 log.debug(" Adding notification address [" + toValues[i].trim() + "]");
175                 toString += toValues[i].trim();
176             }
177
178             notificationMap.put(Notifier.PARAMETER_TO, toString);
179             setNotifying(notificationMap);
180         }
181     }
182
183 }
184
Popular Tags