1 package org.jahia.services.events; 2 3 import org.jahia.data.events.JahiaEvent; 4 import org.jahia.data.events.JahiaEventListener; 5 import org.jahia.params.ParamBean; 6 import org.jahia.registries.ServicesRegistry; 7 8 import java.io.File ; 9 import org.jahia.content.events.ContentActivationEvent; 10 11 24 25 public class JSPEventListener extends JahiaEventListener { 26 27 private static org.apache.log4j.Logger logger = 28 org.apache.log4j.Logger.getLogger (JSPEventListener.class); 29 30 private boolean configLoaded = false; 31 private String defaultPathToJSP = null; 32 private String jspFileName = null; 33 34 private static final String DEFAULT_PATH_TO_JSP = 35 "/jsp/jahia/events/eventlistener.jsp"; 36 private static final String JSP_FILE_NAME = "eventlistener.jsp"; 37 38 private boolean checkConfig (ParamBean paramBean) { 39 if (configLoaded) 40 return configLoaded; 41 42 loadConfig (paramBean); 43 44 return configLoaded; 45 } 46 47 private void loadConfig (ParamBean paramBean) { 48 49 53 54 defaultPathToJSP = DEFAULT_PATH_TO_JSP; 55 jspFileName = JSP_FILE_NAME; 56 57 configLoaded = true; 58 } 59 60 public JSPEventListener () { 61 } 62 63 private void dispatchToJSP (String eventName, JahiaEvent je) { 64 if (!checkConfig (je.getParams ())) { 65 return; 66 } 67 68 ParamBean paramBean = je.getParams (); 69 if (paramBean == null) { 70 logger.warn ("Cannot dispatch to JSP because we need request/response pair to do so for event " + 71 eventName); 72 return; 73 } 74 try { 75 String jspFileName = resolveJSPFullFileName (paramBean); 76 77 logger.debug ("Dispatching to JSP " + jspFileName + 78 " for processing of event " + eventName); 79 if (paramBean.getRequest() != null) { 80 paramBean.getRequest ().setAttribute ("eventName", eventName); 81 paramBean.getRequest ().setAttribute ("jahiaEvent", je); 82 ServicesRegistry.getInstance ().getJahiaFetcherService () 83 .fetchServlet (paramBean, jspFileName); 84 } 85 89 } catch (Throwable t) { 90 logger.error ("Error while dispatching to JSP : " + jspFileName, t); 91 } 92 } 93 94 private String resolveJSPFullFileName (ParamBean paramBean) { 95 String jspFullFileName; 96 if ((paramBean.getPage () != null) && 97 (paramBean.getPage ().getPageTemplate () != null) && 98 (paramBean.getPage ().getPageTemplate ().getSourcePath () != null)) { 99 jspFullFileName = paramBean.getPage ().getPageTemplate (). 100 getSourcePath (); 101 logger.debug ("template source path :" + jspFullFileName); 102 103 jspFullFileName = jspFullFileName.substring (0, 104 jspFullFileName.lastIndexOf ("/") + 1) + jspFileName; 105 106 logger.debug ("resolvedJSPFullFileName :" + jspFullFileName); 107 108 File jspFile = new File (paramBean.getContext ().getRealPath (jspFullFileName)); 109 if (!jspFile.exists ()) { 110 jspFullFileName = defaultPathToJSP; 111 } 112 } else { 113 jspFullFileName = defaultPathToJSP; 114 } 115 return jspFullFileName; 116 } 117 118 public void beforeServicesLoad (JahiaEvent je) { 119 dispatchToJSP ("beforeServicesLoad", je); 120 } 121 122 public void afterServicesLoad (JahiaEvent je) { 123 dispatchToJSP ("afterServicesLoad", je); 124 } 125 126 public void beforeFieldActivation (JahiaEvent je) { 127 dispatchToJSP ("beforeFieldActivation", je); 128 } 129 130 public void fieldAdded (JahiaEvent je) { 131 dispatchToJSP ("fieldAdded", je); 132 } 133 134 public void fieldUpdated (JahiaEvent je) { 135 dispatchToJSP ("fieldUpdated", je); 136 } 137 138 public void fieldDeleted (JahiaEvent je) { 139 dispatchToJSP ("fieldDeleted", je); 140 } 141 142 public void addContainerEngineAfterInit (JahiaEvent je) { 143 dispatchToJSP ("addContainerEngineAfterInit", je); 144 } 145 146 public void addContainerEngineBeforeSave (JahiaEvent je) { 147 dispatchToJSP ("addContainerEngineBeforeSave", je); 148 } 149 150 public void containerAdded (JahiaEvent je) { 151 dispatchToJSP ("containerAdded", je); 152 } 153 154 public void containerUpdated (JahiaEvent je) { 155 dispatchToJSP ("containerUpdated", je); 156 } 157 158 public void containerDeleted (JahiaEvent je) { 159 dispatchToJSP ("containerDeleted", je); 160 } 161 162 public void pageAdded (JahiaEvent je) { 163 dispatchToJSP ("pageAdded", je); 164 } 165 166 public void pageLoaded (JahiaEvent je) { 167 dispatchToJSP ("pageLoaded", je); 168 } 169 170 public void pagePropertiesSet (JahiaEvent je) { 171 dispatchToJSP ("pagePropertiesSet", je); 172 } 173 174 public void containerListPropertiesSet (JahiaEvent je) { 175 dispatchToJSP ("containerListPropertiesSet", je); 176 } 177 178 public void rightsSet (JahiaEvent je) { 179 dispatchToJSP ("rightsSet", je); 180 } 181 182 public void userLoggedIn (JahiaEvent je) { 183 dispatchToJSP ("userLoggedIn", je); 184 } 185 186 public void userLoggedOut (JahiaEvent je) { 187 dispatchToJSP ("userLoggedOut", je); 188 } 189 190 public void contentActivation (ContentActivationEvent theEvent) { 191 dispatchToJSP ("contentActivation", theEvent); 192 } 193 } 194 | Popular Tags |