KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > dispatcher > jsp > JSPDispatcher


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
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 are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.dispatcher.jsp;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.BlojsomException;
36 import org.blojsom.dispatcher.Dispatcher;
37 import org.blojsom.blog.Blog;
38 import org.blojsom.filter.PermalinkFilter;
39 import org.blojsom.util.BlojsomConstants;
40 import org.blojsom.util.BlojsomUtils;
41
42 import javax.servlet.ServletConfig JavaDoc;
43 import javax.servlet.ServletContext JavaDoc;
44 import javax.servlet.ServletException JavaDoc;
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.Map JavaDoc;
50 import java.util.Properties JavaDoc;
51
52 /**
53  * JSPDispatcher
54  *
55  * @author David Czarnecki
56  * @since blojsom 3.0
57  * @version $Id: JSPDispatcher.java,v 1.2 2006/03/21 16:32:21 czarneckid Exp $
58  */

59 public class JSPDispatcher implements Dispatcher {
60
61     private Log _logger = LogFactory.getLog(JSPDispatcher.class);
62
63     private ServletContext JavaDoc _context;
64     private ServletConfig JavaDoc _servletConfig;
65     private Properties JavaDoc _blojsomProperties;
66     private String JavaDoc _templatesDirectory;
67     private String JavaDoc _blogsDirectory;
68
69     /**
70      * Create a new JSPDispatcher
71      */

72     public JSPDispatcher() {
73     }
74
75     /**
76      * Set the properties in use by blojsom
77      *
78      * @param blojsomProperties Properties in use by blojsom
79      */

80     public void setBlojsomProperties(Properties JavaDoc blojsomProperties) {
81         _blojsomProperties = blojsomProperties;
82     }
83
84     /**
85      * Set the {@link ServletConfig}
86      *
87      * @param servletConfig {@link ServletConfig}
88      */

89     public void setServletConfig(ServletConfig JavaDoc servletConfig) {
90         _servletConfig = servletConfig;
91     }
92
93     /**
94      * Initialization method for blojsom dispatchers
95      *
96      * @param servletConfig ServletConfig for obtaining any initialization parameters
97      * @param blojsomConfiguration BlojsomConfiguration for blojsom-specific configuration information
98      * @throws BlojsomException If there is an error initializing the dispatcher
99      */

100     public void init() throws BlojsomException {
101         _templatesDirectory = _blojsomProperties.getProperty(BlojsomConstants.TEMPLATES_DIRECTORY_IP, BlojsomConstants.DEFAULT_TEMPLATES_DIRECTORY);
102         _blogsDirectory = _blojsomProperties.getProperty(BlojsomConstants.BLOGS_DIRECTORY_IP, BlojsomConstants.DEFAULT_BLOGS_DIRECTORY);
103         _context = _servletConfig.getServletContext();
104     }
105
106     /**
107      * Dispatch a request and response. A context map is provided for the BlojsomServlet to pass
108      * any required information for use by the dispatcher. The dispatcher is also
109      * provided with the template for the requested flavor along with the content type for the
110      * specific flavor.
111      *
112      * @param httpServletRequest Request
113      * @param httpServletResponse Response
114      * @param blog {@link Blog} instance
115      * @param context Context map
116      * @param flavorTemplate Template to dispatch to for the requested flavor
117      * @param flavorContentType Content type for the requested flavor
118      * @throws IOException If there is an exception during IO
119      * @throws ServletException If there is an exception in dispatching the request
120      */

121     public void dispatch(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, String JavaDoc flavorTemplate, String JavaDoc flavorContentType) throws IOException JavaDoc, ServletException JavaDoc {
122         httpServletResponse.setContentType(flavorContentType);
123
124         if (!flavorTemplate.startsWith("/")) {
125             flavorTemplate = '/' + flavorTemplate;
126         }
127
128         String JavaDoc flavorTemplateForPage = null;
129         if (BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest) != null) {
130             flavorTemplateForPage = BlojsomUtils.getTemplateForPage(flavorTemplate, BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest));
131
132             if (_logger.isDebugEnabled()) {
133                 _logger.debug("Retrieved template for page: " + flavorTemplateForPage);
134             }
135         }
136
137         // Populate the request with context attributes from the blog
138
Iterator JavaDoc contextIterator = context.keySet().iterator();
139         while (contextIterator.hasNext()) {
140             String JavaDoc contextKey = (String JavaDoc) contextIterator.next();
141             httpServletRequest.setAttribute(contextKey, context.get(contextKey));
142         }
143
144         if (httpServletRequest instanceof PermalinkFilter.PermalinkRequest) {
145             PermalinkFilter.PermalinkRequest permalinkRequest = (PermalinkFilter.PermalinkRequest) httpServletRequest;
146             permalinkRequest.setPathInfo(null);
147         }
148
149         // Try and look for the original flavor template with page for the individual user
150
if (flavorTemplateForPage != null) {
151             String JavaDoc templateToLoad = BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + _blogsDirectory + blog.getBlogId() + _templatesDirectory + BlojsomUtils.removeInitialSlash(flavorTemplateForPage);
152             if (_context.getResource(templateToLoad) != null) {
153                 httpServletRequest.getRequestDispatcher(templateToLoad).forward(httpServletRequest, httpServletResponse);
154                 httpServletResponse.getWriter().flush();
155
156                 if (_logger.isDebugEnabled()) {
157                     _logger.debug("Dispatched to flavor page template for user: " + templateToLoad);
158                 }
159
160             } else {
161                 templateToLoad = BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + BlojsomUtils.removeInitialSlash(_templatesDirectory) + BlojsomUtils.removeInitialSlash(flavorTemplateForPage);
162                 if (_context.getResource(templateToLoad) != null) {
163                     // Otherwise, fallback and look for the flavor template with page without including any user information
164
httpServletRequest.getRequestDispatcher(templateToLoad).forward(httpServletRequest, httpServletResponse);
165                     httpServletResponse.getWriter().flush();
166
167                     if (_logger.isDebugEnabled()) {
168                         _logger.debug("Dispatched to flavor page template: " + templateToLoad);
169                     }
170
171                 } else {
172                     if (_logger.isErrorEnabled()) {
173                         _logger.error("Unable to dispatch to flavor page template: " + templateToLoad);
174                     }
175                 }
176             }
177         } else {
178             // Otherwise, fallback and look for the flavor template for the individual user
179
String JavaDoc templateToLoad = BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + _blogsDirectory + blog.getBlogId() + _templatesDirectory + BlojsomUtils.removeInitialSlash(flavorTemplate);
180             if (_context.getResource(templateToLoad) != null) {
181                 httpServletRequest.getRequestDispatcher(templateToLoad).forward(httpServletRequest, httpServletResponse);
182                 httpServletResponse.getWriter().flush();
183
184                 if (_logger.isDebugEnabled()) {
185                     _logger.debug("Dispatched to flavor template for user: " + templateToLoad);
186                 }
187
188             } else {
189                 templateToLoad = BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + BlojsomUtils.removeInitialSlash(_templatesDirectory) + BlojsomUtils.removeInitialSlash(flavorTemplate);
190                 if (_context.getResource(templateToLoad) != null) {
191                     // Otherwise, fallback and look for the flavor template without including any user information
192
httpServletRequest.getRequestDispatcher(templateToLoad).forward(httpServletRequest, httpServletResponse);
193                     httpServletResponse.getWriter().flush();
194                     
195                     if (_logger.isDebugEnabled()) {
196                         _logger.debug("Dispatched to flavor template: " + templateToLoad);
197                     }
198                 } else {
199                     if (_logger.isErrorEnabled()) {
200                         _logger.error("Unable to dispatch to flavor template: " + templateToLoad);
201                     }
202                 }
203             }
204         }
205     }
206 }
Popular Tags