KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openinventions > webappfilter > DefaultFilter


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 JavaDoc configFileName;
14     private Element config;
15
16     public void init(State pluginState, Element context) throws Exception JavaDoc {
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         // Each unique filter has its own application state per config file
35
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 JavaDoc {
66 // Element config = (Element) state.get("com.openinventions.metaframework.Config");
67

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 JavaDoc {
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 JavaDoc {
105         String JavaDoc nextFilter = (String JavaDoc) previousState.get("nextCommand");
106         String JavaDoc direction = (String JavaDoc) previousState.get("direction");
107         if (log.isDebugEnabled()) {
108             log.debug("direction = " + direction);
109         }
110         // Set request state
111
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 JavaDoc) state.get("rootDir"));
126             log.debug("rootDir = " + (String JavaDoc) previousState.get("rootDir"));
127         }
128
129         if (config.isExists("session-identifier")) {
130             Element element = (Element) state.get(config.getValue("session-identifier/element"));
131             String JavaDoc 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 JavaDoc nextCommand = (String JavaDoc) 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 JavaDoc) 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                     // Nothing to do here. try next filter
168
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                     // Nothing to do here. try next filter
178
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 JavaDoc filterDirection = (String JavaDoc) 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 /* ====================================================================
219  * The webappfilter License, Version 1.1
220  *
221  * Copyright (c) 2002 Ivar Chan. All rights
222  * reserved.
223  *
224  * Redistribution and use in source and binary forms, with or without
225  * modification, are permitted provided that the following conditions
226  * are met:
227  *
228  * 1. Redistributions of source code must retain the above copyright
229  * notice, this list of conditions and the following disclaimer.
230  *
231  * 2. Redistributions in binary form must reproduce the above copyright
232  * notice, this list of conditions and the following disclaimer in
233  * the documentation and/or other materials provided with the
234  * distribution.
235  *
236  * 3. The end-user documentation included with the redistribution,
237  * if any, must include the following acknowledgment:
238  * "This product includes software developed by
239  * Ivar Chan (http://www.openinventions.com/webappfilter/)."
240  * Alternately, this acknowledgment may appear in the software itself,
241  * if and wherever such third-party acknowledgments normally appear.
242  *
243  * 4. The name "webappfilter" must not be used to endorse or promote products
244  * derived from this software without prior written permission. For
245  * written permission, please contact ivarchan@acm.org.
246  *
247  * 5. Products derived from this software may not be called "webappfilter",
248  * nor may "webappfilter" appear in their name, without
249  * prior written permission of Ivar Chan.
250  *
251  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
252  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
253  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
254  * DISCLAIMED. IN NO EVENT SHALL THE IVAR CHAN BE LIABLE FOR ANY
255  * DIRECT, INDIRECT, INCIDENTAL,
256  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
257  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
258  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
259  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
260  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
261  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262  * SUCH DAMAGE.
263  * ====================================================================
264  *
265  * This software consists of voluntary contributions made by many
266  * individuals. For more information on webappfilter, please see
267  * <http://www.openinventions/webappfilter/>.
268  */

269
270
Popular Tags