1 17 18 19 20 package org.apache.lenya.cms.cocoon.task; 21 22 import java.util.Iterator ; 23 import java.util.Map ; 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 49 public class CocoonTaskWrapper extends DefaultTaskWrapper { 50 51 private static Category log = Category.getInstance(CocoonTaskWrapper.class); 52 53 59 public CocoonTaskWrapper(Map 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 77 protected CocoonTaskWrapper() { 78 } 79 80 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 taskId = request.getParameter(TaskWrapperParameters.TASK_ID); 95 taskId = parameters.getParameter(TaskWrapperParameters.TASK_ID, taskId); 96 97 String webappUrl = ServletHelper.getWebappURI(request); 98 initialize(taskId, publication, webappUrl, taskParameters); 99 100 String 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 131 protected void setNotifying(Request request) { 132 133 log.debug("Trying to initialize notification ..."); 134 135 Map requestParameters = ServletHelper.getParameterMap(request); 136 137 log.debug(" Request parameters:"); 138 for (Iterator i = requestParameters.keySet().iterator(); i.hasNext();) { 139 Object 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 i = notificationMap.getMap().keySet().iterator(); i.hasNext();) { 147 Object 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 eMail = user.getEmail(); 159 notificationMap.put(Notifier.PARAMETER_FROM, eMail); 160 log.debug(" Setting from address [" + Notifier.PARAMETER_FROM + "] = [" + eMail + "]"); 161 162 String toKey = NamespaceMap.getFullName(Notifier.PREFIX, Notifier.PARAMETER_TO); 163 String toString = ""; 164 String [] toValues = request.getParameterValues(toKey); 165 166 if (toValues == null) { 167 throw new IllegalStateException ("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 |