001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: CocoonTaskWrapper.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.cocoon.task;
022:
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import org.apache.avalon.framework.parameters.Parameters;
027: import org.apache.avalon.framework.service.ServiceManager;
028: import org.apache.cocoon.environment.ObjectModelHelper;
029: import org.apache.cocoon.environment.Request;
030: import org.apache.cocoon.environment.Session;
031: import org.apache.lenya.ac.AccessControlException;
032: import org.apache.lenya.ac.Identity;
033: import org.apache.lenya.ac.Role;
034: import org.apache.lenya.ac.User;
035: import org.apache.lenya.cms.ac.PolicyUtil;
036: import org.apache.lenya.cms.publication.Publication;
037: import org.apache.lenya.cms.publication.PublicationUtil;
038: import org.apache.lenya.cms.task.DefaultTaskWrapper;
039: import org.apache.lenya.cms.task.ExecutionException;
040: import org.apache.lenya.cms.task.Notifier;
041: import org.apache.lenya.cms.task.TaskWrapperParameters;
042: import org.apache.lenya.cms.task.WorkflowInvoker;
043: import org.apache.lenya.util.NamespaceMap;
044: import org.apache.lenya.util.ServletHelper;
045: import org.apache.log4j.Logger;
046:
047: /**
048: * Task wrapper to be used from Cocoon components.
049: *
050: * @deprecated Use the usecase framework instead.
051: */
052: public class CocoonTaskWrapper extends DefaultTaskWrapper {
053:
054: private static Logger log = Logger
055: .getLogger(CocoonTaskWrapper.class);
056:
057: /**
058: * Ctor to be called from a Cocoon component.
059: * @param objectModel A Cocoon object model.
060: * @param parameters A parameters object.
061: * @param manager The service manager to use.
062: * @throws ExecutionException when something went wrong.
063: */
064: public CocoonTaskWrapper(Map objectModel, Parameters parameters,
065: ServiceManager manager) throws ExecutionException {
066: super (manager);
067:
068: log.debug("Creating CocoonTaskWrapper");
069:
070: Publication publication;
071: try {
072: publication = PublicationUtil.getPublication(manager,
073: objectModel);
074: } catch (Exception e) {
075: throw new ExecutionException(e);
076: }
077: Request request = ObjectModelHelper.getRequest(objectModel);
078:
079: initialize(parameters, publication, request);
080: }
081:
082: /**
083: * Ctor.
084: * @param manager The service manager.
085: */
086: protected CocoonTaskWrapper(ServiceManager manager) {
087: super (manager);
088: }
089:
090: /**
091: * Initializes this wrapper.
092: * @param parameters The task parameters.
093: * @param publication The publication.
094: * @param request The request.
095: * @throws ExecutionException when something went wrong.
096: */
097: protected void initialize(Parameters parameters,
098: Publication publication, Request request)
099: throws ExecutionException {
100: setNotifying(request);
101:
102: Parameters taskParameters = extractTaskParameters(parameters,
103: publication, request);
104: getTaskParameters().parameterize(taskParameters);
105:
106: String taskId = request
107: .getParameter(TaskWrapperParameters.TASK_ID);
108: taskId = parameters.getParameter(TaskWrapperParameters.TASK_ID,
109: taskId);
110:
111: String webappUrl = ServletHelper.getWebappURI(request);
112: initialize(taskId, publication, webappUrl, taskParameters);
113:
114: String eventName = request
115: .getParameter(WorkflowInvoker.EVENT_REQUEST_PARAMETER);
116: if (eventName == null) {
117: eventName = request
118: .getParameter(WorkflowInvoker.LENYA_EVENT_REQUEST_PARAMETER);
119: }
120: if (eventName != null) {
121: Session session = request.getSession(false);
122: if (session == null) {
123: log
124: .debug("No session found - not enabling workflow handling.");
125: } else {
126: Identity identity = Identity.getIdentity(session);
127: if (identity == null) {
128: log
129: .debug("No identity found - not enabling workflow handling.");
130: } else {
131: log
132: .debug("Identity found - enabling workflow handling.");
133: Role[] roles;
134: try {
135: roles = PolicyUtil.getRoles(request);
136: } catch (AccessControlException e) {
137: throw new ExecutionException(e);
138: }
139: setWorkflowAware(eventName, identity, roles);
140: }
141: }
142: }
143:
144: }
145:
146: /**
147: * Enables notification if the corresponding request parameters exist.
148: * @param request The request.
149: */
150: protected void setNotifying(Request request) {
151:
152: log.debug("Trying to initialize notification ...");
153:
154: Map requestParameters = ServletHelper.getParameterMap(request);
155: String key;
156: String value;
157: Map.Entry entry;
158:
159: log.debug(" Request parameters:");
160: for (Iterator iter = requestParameters.entrySet().iterator(); iter
161: .hasNext();) {
162: entry = (Map.Entry) iter.next();
163: key = (String) entry.getKey();
164: value = (String) entry.getValue();
165: log.debug(" [" + key + "] = [" + value + "]");
166: }
167:
168: NamespaceMap notificationMap = new NamespaceMap(
169: requestParameters, Notifier.PREFIX);
170:
171: log.debug(" Notification parameters:");
172: for (Iterator iter = notificationMap.getMap().entrySet()
173: .iterator(); iter.hasNext();) {
174: entry = (Map.Entry) iter.next();
175: key = (String) entry.getKey();
176: value = (String) entry.getValue();
177: log.debug(" [" + key + "] = [" + value + "]");
178: }
179:
180: if (notificationMap.getMap().isEmpty()) {
181: log.debug(" No notification parameters found.");
182: } else {
183: log.debug(" Initializing notification");
184:
185: Identity identity = Identity.getIdentity(request
186: .getSession());
187: User user = identity.getUser();
188: String eMail = user.getEmail();
189: notificationMap.put(Notifier.PARAMETER_FROM, eMail);
190: log.debug(" Setting from address ["
191: + Notifier.PARAMETER_FROM + "] = [" + eMail + "]");
192:
193: String toKey = NamespaceMap.getFullName(Notifier.PREFIX,
194: Notifier.PARAMETER_TO);
195: StringBuffer buf = new StringBuffer();
196: String[] toValues = request.getParameterValues(toKey);
197:
198: if (toValues == null) {
199: throw new IllegalStateException(
200: "You must specify at least one [notification.tolist] request parameter!");
201: }
202:
203: for (int i = 0; i < toValues.length; i++) {
204: if (i > 0 && !"".equals(buf.toString())) {
205: buf.append(",");
206: }
207: log.debug(" Adding notification address ["
208: + toValues[i].trim() + "]");
209: buf.append(toValues[i].trim());
210: }
211:
212: notificationMap.put(Notifier.PARAMETER_TO, buf.toString());
213: setNotifying(notificationMap);
214: }
215: }
216:
217: }
|