1 package com.openinventions.webappfilter; 2 3 import com.openinventions.metaframework.*; 4 import java.util.*; 5 import org.apache.commons.logging.*; 6 7 public class DefaultFilter implements Processor, Plugin { 8 private static final Log log = LogFactory.getLog(DefaultFilter.class); 9 private static Map applications = new HashMap(); 10 private ElementFactory factory; 11 private Map sessions; 12 private Map defaultSession; 13 private String configFileName; 14 private Element config; 15 16 public void init(State pluginState, Element context) throws Exception { 17 factory = new JXPathElementFileFactory(); 18 State state = new DefaultState(); 19 this.sessions = new HashMap(); 20 this.defaultSession = new HashMap(); 21 this.configFileName = context.getValue("file"); 22 if ((pluginState != null) && (pluginState.isExists("rootDir"))) { 23 this.configFileName = pluginState.get("rootDir") + "/" + this.configFileName; 24 } 25 if (!context.getValue("rootDir").equals("")) { 26 this.configFileName = context.getValue("rootDir") + "/" + this.configFileName; 27 } 28 if (log.isDebugEnabled()) { 29 log.debug("configFileName = " + configFileName); 30 } 31 this.config = factory.read(configFileName); 32 this.config.setCached(true); 33 defaultSession.put("com.openinventions.metaframework.Config", config); 34 if (!applications.containsKey(configFileName)) { 36 applications.put(configFileName, new HashMap()); 37 } 38 39 if (config.isExists("/plugin/init")) { 40 initState(state); 41 if (log.isDebugEnabled()) { 42 log.debug("configFileName = " + configFileName); 43 log.debug("plugin = " + config.getValue("name(/plugin/init/child::*[1])")); 44 Element config2 = (Element) state.get("com.openinventions.metaframework.Config"); 45 log.debug(config + " " + config2); 46 log.debug("exists = " + config2.isExists("/plugin/init/child::*[1]")); 47 } 48 state.set("filterDirection", ""); 49 state.set("nextCommand", "/plugin/init/child::*[1]"); 50 state.set("relativeCommand", "/following-sibling::*[1]"); 51 state.set("lastCommand", "/plugin/stop/expr"); 52 if ((pluginState != null) && (pluginState.isExists("rootDir"))) { 53 state.set("rootDir", pluginState.get("rootDir")); 54 } 55 if (!context.getValue("rootDir").equals("")) { 56 state.set("rootDir", context.getValue("rootDir")); 57 } 58 Processor traverser = (Processor) state.get("traverser"); 59 traverser.process(state, null); 60 state.set("nextCommand", ""); 61 state.set("filterDirection", ""); 62 } 63 } 64 65 public void destroy(State pluginState, Element context) throws Exception { 66 68 if (log.isDebugEnabled()) { 69 log.debug("config = " + config); 70 log.debug("this.config = " + this.config); 71 log.debug("init = " + config.isExists("/plugin/init")); 72 log.debug("destory = " + config.isExists("/plugin/destroy")); 73 } 74 75 if (config.isExists("/plugin/destroy")) { 76 State state = new DefaultState(); 77 initState(state); 78 state.set("filterDirection", ""); 79 state.set("nextCommand", "/plugin/destroy/child::*[1]"); 80 state.set("relativeCommand", "/following-sibling::*[1]"); 81 state.set("lastCommand", "/plugin/stop/expr"); 82 if ((pluginState != null) && (pluginState.isExists("rootDir"))) { 83 state.set("rootDir", pluginState.get("rootDir")); 84 } 85 if ((context != null) && (!context.getValue("rootDir").equals(""))) { 86 state.set("rootDir", context.getValue("rootDir")); 87 } 88 Processor traverser = (Processor) state.get("traverser"); 89 traverser.process(state, null); 90 state.set("nextCommand", ""); 91 state.set("filterDirection", ""); 92 } 93 } 94 95 protected void initState(State state) throws Exception { 96 state.set("com.openinventions.metaframework.Config", config); 97 state.set("config", config); 98 state.set("com.openinventions.metaframework.SessionState", this.defaultSession); 99 Map application = (Map) this.applications.get(configFileName); 100 application.put("com.openinventions.metaframework.Config", config); 101 state.set("com.openinventions.metaframework.ApplicationState", application); 102 } 103 104 public void process(State previousState, Element context) throws Exception { 105 String nextFilter = (String ) previousState.get("nextCommand"); 106 String direction = (String ) previousState.get("direction"); 107 if (log.isDebugEnabled()) { 108 log.debug("direction = " + direction); 109 } 110 State state = new DefaultState(); 112 initState(state); 113 114 if (previousState != null) { 115 if (previousState.isExists("req")) { 116 state.set("req", previousState.get("req")); 117 } 118 if (previousState.isExists("res")) { 119 state.set("res", previousState.get("res")); 120 } 121 if (previousState.isExists("rootDir")) { 122 state.set("rootDir", previousState.get("rootDir")); 123 } 124 125 log.debug("rootDir = " + (String ) state.get("rootDir")); 126 log.debug("rootDir = " + (String ) previousState.get("rootDir")); 127 } 128 129 if (config.isExists("session-identifier")) { 130 Element element = (Element) state.get(config.getValue("session-identifier/element")); 131 String id = element.getValue(config.getValue("session-identifier/xpath")); 132 Map newSession = null; 133 134 if (!sessions.containsKey(id)) { 135 newSession = new HashMap(); 136 newSession.put("com.openinventions.metaframework.Config", config); 137 sessions.put(id, newSession); 138 } else { 139 newSession = (Map) sessions.get(id); 140 } 141 state.set("com.openinventions.metaframework.SessionState", newSession); 142 } 143 144 if (!state.isExists("req")) { 145 state.set("req", factory.createElement("request")); 146 } 147 if (!state.isExists("res")) { 148 state.set("res", factory.createElement("response")); 149 } 150 151 String nextCommand = (String ) state.get("nextCommand"); 152 if (nextCommand.equals("")) { 153 Element config = (Element) state.get("com.openinventions.metaframework.Config"); 154 155 if (log.isDebugEnabled()) { 156 log.debug("direction = " + direction); 157 log.debug("nextFilter = " + (String ) state.get("filterDirection")); 158 } 159 160 if (direction.equals("forward")) { 161 if (config.isExists("start/forward")) { 162 if (!config.getValue("start/forward/xpath").equals("")) { 163 state.set("nextCommand", config.getValue("start/forward/xpath")); 164 } 165 } else { 166 previousState.set("nextCommand", nextFilter + "/following-sibling::*[1]"); 167 return; 169 } 170 } else if (direction.equals("backward")) { 171 if (config.isExists("start/backward")) { 172 if (!config.getValue("start/backward/xpath").equals("")) { 173 state.set("nextCommand", config.getValue("start/backward/xpath")); 174 } 175 } else { 176 previousState.set("nextCommand", nextFilter + "/preceding-sibling::*[1]"); 177 return; 179 } 180 } else { 181 log.error("nextFilter not set properly = " + nextFilter); 182 } 183 } 184 185 if (log.isDebugEnabled()) { 186 log.debug("nextCommand = " + state.get("nextCommand")); 187 log.debug("req = " + factory.toString((Element) state.get("req"))); 188 log.debug("res = " + factory.toString((Element) state.get("res"))); 189 log.debug("res/body = " + ((Element) state.get("res")).getValue("body")); 190 } 191 192 state.set("filterDirection", ""); 193 Processor traverser = (Processor) state.get("traverser"); 194 traverser.process(state, null); 195 196 if (state.isExists("direction") && (!state.get("direction").equals(""))) { 197 previousState.set("direction", state.get("direction")); 198 } 199 200 String filterDirection = (String ) state.get("filterDirection"); 201 if (filterDirection.equals("forward")) { 202 previousState.set("nextCommand", nextFilter + "/following-sibling::*[1]"); 203 previousState.set("direction", "forward"); 204 } else if (filterDirection.equals("backward")) { 205 previousState.set("nextCommand", nextFilter + "/preceding-sibling::*[1]"); 206 previousState.set("direction", "backward"); 207 } else { 208 previousState.set("nextCommand", ""); 209 previousState.set("filterDirection", "stop"); 210 } 211 212 if (previousState != null) { 213 previousState.set("req", state.get("req")); 214 previousState.set("res", state.get("res")); 215 } 216 } 217 } 218 269 270 | Popular Tags |