KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > servlet > PathDispatcher


1 /*--
2  Copyright (C) 2001-2003 Aetrion LLC.
3  All rights reserved.
4
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions
7  are met:
8
9  1. Redistributions of source code must retain the above copyright
10     notice, this list of conditions, and the following disclaimer.
11
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions, and the disclaimer that follows
14     these conditions in the documentation and/or other materials
15     provided with the distribution.
16  3. The name "JPublish" must not be used to endorse or promote products
17     derived from this software without prior written permission. For
18     written permission, please contact info@aetrion.com.
19
20  4. Products derived from this software may not be called "JPublish", nor
21     may "JPublish" appear in their name, without prior written permission
22     from Aetrion LLC (info@aetrion.com).
23
24  In addition, the authors of this software request (but do not require)
25  that you include in the end-user documentation provided with the
26  redistribution and/or in the software itself an acknowledgement equivalent
27  to the following:
28      "This product includes software developed by
29       Aetrion LLC (http://www.aetrion.com/)."
30  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
31  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
34  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  POSSIBILITY OF SUCH DAMAGE.
41  For more information on JPublish, please see <http://www.jpublish.org/>.
42
43  */

44
45 /*--
46  Copyright (C) 2001-2003 Aetrion LLC.
47  All rights reserved.
48
49  Redistribution and use in source and binary forms, with or without
50  modification, are permitted provided that the following conditions
51  are met:
52
53  1. Redistributions of source code must retain the above copyright
54     notice, this list of conditions, and the following disclaimer.
55
56  2. Redistributions in binary form must reproduce the above copyright
57     notice, this list of conditions, and the disclaimer that follows
58     these conditions in the documentation and/or other materials
59     provided with the distribution.
60  3. The name "JPublish" must not be used to endorse or promote products
61     derived from this software without prior written permission. For
62     written permission, please contact info@aetrion.com.
63
64  4. Products derived from this software may not be called "JPublish", nor
65     may "JPublish" appear in their name, without prior written permission
66     from Aetrion LLC (info@aetrion.com).
67
68  In addition, the authors of this software request (but do not require)
69  that you include in the end-user documentation provided with the
70  redistribution and/or in the software itself an acknowledgement equivalent
71  to the following:
72      "This product includes software developed by
73       Aetrion LLC (http://www.aetrion.com/)."
74  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
78  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
79  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
80  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
82  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
83  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
84  POSSIBILITY OF SUCH DAMAGE.
85  For more information on JPublish, please see <http://www.jpublish.org/>.
86
87  */

88 package org.jpublish.servlet;
89
90 import java.io.IOException JavaDoc;
91 import java.util.*;
92
93 import javax.servlet.RequestDispatcher JavaDoc;
94 import javax.servlet.ServletException JavaDoc;
95 import javax.servlet.http.HttpServletRequest JavaDoc;
96 import javax.servlet.http.HttpServletResponse JavaDoc;
97
98 import com.anthonyeden.lib.config.Configuration;
99 import com.anthonyeden.lib.config.ConfigurationException;
100 import org.apache.commons.logging.Log;
101 import org.apache.commons.logging.LogFactory;
102 import org.jpublish.SiteContext;
103 import org.jpublish.util.PathUtilities;
104
105 /**
106  * @author
107  */

108 public class PathDispatcher {
109
110     private static Log log = LogFactory.getLog(PathDispatcher.class);
111     private static List includeDispatchers;
112     private static List forwardDispatchers;
113
114     private SiteContext siteContext;
115
116     /**
117      * Construct a new PathDispatcher.
118      *
119      * @param siteContext The SiteContext
120      */

121     public PathDispatcher(SiteContext siteContext) {
122         includeDispatchers = new ArrayList();
123         forwardDispatchers = new ArrayList();
124         //servletContext = siteContext.getServletContext(); //is not defined yet by JPublish :(
125
this.siteContext = siteContext;
126     }
127
128     /**
129      * Load the PathDispatcher configuration from the given configuration object.
130      *
131      * @param configuration The configuration object
132      * @throws ConfigurationException If a configuration error occurs
133      */

134     public void loadConfiguration(Configuration configuration)
135             throws ConfigurationException {
136
137         // load path-dispatcher definitions
138
Iterator pathDispatcherElements = configuration.getChildren("path-dispatcher").iterator();
139
140         while (pathDispatcherElements.hasNext()) {
141             Configuration pathDispatcherElement =
142                     (Configuration) pathDispatcherElements.next();
143             String JavaDoc name = pathDispatcherElement.getAttribute("name");
144             String JavaDoc path = pathDispatcherElement.getAttribute("path");
145
146             String JavaDoc action = pathDispatcherElement.getAttribute("action");
147             if ("forward".equalsIgnoreCase(action)) {
148                 forwardDispatchers.add(pathDispatcherElement);
149             } else {
150                 includeDispatchers.add(pathDispatcherElement);
151             }
152
153             if (log.isDebugEnabled()) {
154                 log.debug("Defined `" + action + "` action for path: " +
155                         path + ", to be dispatched by: [" + name + "]");
156             }
157         }
158     }
159
160     /**
161      * Return true if the path can be forwarded or included.
162      *
163      * @param path The path
164      * @return True if it can be dispatched
165      */

166     public boolean canDispatch(String JavaDoc path) {
167         return canForwardDispatch(path) != null || canIncludeDispatch(path) != null;
168     }
169
170     /**
171      * Return the dispatcher name if the path can be forwarded.
172      *
173      * @param path The path
174      * @return True if it can be forwarded
175      */

176     public String JavaDoc canForwardDispatch(String JavaDoc path) {
177         Iterator configurations = forwardDispatchers.iterator();
178         while (configurations.hasNext()) {
179             Configuration configuration = (Configuration) configurations.next();
180             if ("true".equals(configuration.getAttribute("regex"))) {
181                 // handle regexs here
182
} else if (PathUtilities.match(path, configuration.getAttribute("path"))) {
183                 return configuration.getAttribute("name");
184             }
185         }
186         return null;
187     }
188
189     /**
190      * Return true if the path can be included.
191      *
192      * @param path The path
193      * @return True if the path can be included
194      */

195     public String JavaDoc canIncludeDispatch(String JavaDoc path) {
196         Iterator configurations = includeDispatchers.iterator();
197         while (configurations.hasNext()) {
198             Configuration configuration = (Configuration) configurations.next();
199             if ("true".equals(configuration.getAttribute("regex"))) {
200                 // handle regexs here
201
} else if (PathUtilities.match(path, configuration.getAttribute("path"))) {
202                 return configuration.getAttribute("name");
203             }
204         }
205         return null;
206     }
207
208     /**
209      * Forward the request.
210      *
211      * @param request The servlet request
212      * @param response The servlet response
213      * @param dispatcherName The dispatcher name
214      * @throws IOException
215      * @throws ServletException
216      */

217     public void forward(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc dispatcherName)
218             throws IOException JavaDoc, ServletException JavaDoc {
219         if (dispatcherName != null) {
220             RequestDispatcher JavaDoc rd = siteContext.getServletContext().getNamedDispatcher(dispatcherName);
221             rd.forward(request, response);
222         }
223     }
224
225     /**
226      * Dispatch to includeDispatchers.
227      *
228      * This method is damn tricky. Ask Anthony how we can re-process the input supplied by our new "slave": the Servlet
229      * which just proccessed the request ;) <p/> We can therefore to "influence" the output by using Velocity or
230      * Actions... todo... check this f@#$ gizmo <p/> BufferedReader br = new BufferedReader( new
231      * InputStreamReader(request.getInputStream() ) ); String data = br.readLine(); ...todo: eat this data if(br !=null)
232      * br.close(); apparently is working but is weird :)) I hope is not a mistake
233      *
234      * @param request The request object
235      * @param response The response object
236      * @param dispatcherName The dispatcher name
237      * @throws IOException
238      * @throws ServletException
239      */

240     public void include(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc dispatcherName)
241             throws IOException JavaDoc, ServletException JavaDoc {
242         if (dispatcherName != null) {
243             RequestDispatcher JavaDoc rd = siteContext.getServletContext().getNamedDispatcher(dispatcherName);
244             rd.include(request, response);
245             // unfinished....
246
}
247     }
248
249     public void dispatch(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc path)
250             throws IOException JavaDoc, ServletException JavaDoc {
251         //todo: add a decisional logic to decide if is forward or includeDispatchers...
252
}
253
254     //HELPER methods from Apache's StringUtils library
255

256     // Count matches
257
//-----------------------------------------------------------------------
258
/**
259      * <p>Counts how many times the substring appears in the larger String.</p> <p/> <p>A <code>null</code> or empty
260      * ("") String input returns <code>0</code>.</p> <p/>
261      * <pre>
262      * StringUtils.countMatches(null, *) = 0
263      * StringUtils.countMatches("", *) = 0
264      * StringUtils.countMatches("abba", null) = 0
265      * StringUtils.countMatches("abba", "") = 0
266      * StringUtils.countMatches("abba", "a") = 2
267      * StringUtils.countMatches("abba", "ab") = 1
268      * StringUtils.countMatches("abba", "xxx") = 0
269      * </pre>
270      *
271      * @param str the String to check, may be null
272      * @param sub the substring to count, may be null
273      * @return the number of occurances, 0 if either String is <code>null</code>
274      */

275     public static int countMatches(String JavaDoc str, String JavaDoc sub) {
276         if (str == null || str.length() == 0 || sub == null || sub.length() == 0) {
277             return 0;
278         }
279         int count = 0;
280         int idx = 0;
281         while ((idx = str.indexOf(sub, idx)) != -1) {
282             count++;
283             idx += sub.length();
284         }
285         return count;
286     }
287
288 }
Popular Tags