KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > dispatcher > webmacro > WebMacroDispatcher


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.webmacro;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.velocity.util.EnumerationIterator;
36 import org.blojsom.BlojsomException;
37 import org.blojsom.blog.Blog;
38 import org.blojsom.dispatcher.Dispatcher;
39 import org.blojsom.util.BlojsomConstants;
40 import org.blojsom.util.BlojsomUtils;
41 import org.webmacro.*;
42
43 import javax.servlet.ServletConfig JavaDoc;
44 import javax.servlet.ServletException JavaDoc;
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47 import javax.servlet.http.HttpSession JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.util.Map JavaDoc;
50 import java.util.Properties JavaDoc;
51
52 /**
53  * WebMacro dispatcher
54  *
55  * @author David Czarnecki
56  * @since blojsom 3.0
57  * @version $Id: WebMacroDispatcher.java,v 1.1 2006/03/23 14:27:58 czarneckid Exp $
58  */

59 public class WebMacroDispatcher implements Dispatcher {
60
61     private Log _logger = LogFactory.getLog(WebMacroDispatcher.class);
62
63     private Properties JavaDoc _webMacroProperties;
64     private Properties JavaDoc _blojsomProperties;
65     private ServletConfig JavaDoc _servletConfig;
66
67     private String JavaDoc _templatesDirectory;
68     private String JavaDoc _blogsDirectory;
69
70
71     /**
72      * Construct a new instance of the WebMacro dispatcher
73      */

74     public WebMacroDispatcher() {
75     }
76
77     /**
78      * Set the WebMacro properties for use by the dispatcher
79      *
80      * @param webMacroProperties Properties for WebMacro configuration
81      */

82     public void setWebMacroProperties(Properties JavaDoc webMacroProperties) {
83         _webMacroProperties = webMacroProperties;
84     }
85
86     /**
87      * Set the properties in use by blojsom
88      *
89      * @param blojsomProperties Properties in use by blojsom
90      */

91     public void setBlojsomProperties(Properties JavaDoc blojsomProperties) {
92         _blojsomProperties = blojsomProperties;
93     }
94
95     /**
96      * Set the {@link javax.servlet.ServletConfig}
97      *
98      * @param servletConfig {@link javax.servlet.ServletConfig}
99      */

100     public void setServletConfig(ServletConfig JavaDoc servletConfig) {
101         _servletConfig = servletConfig;
102     }
103
104     /**
105      * Initialization method for blojsom dispatchers
106      *
107      * @throws org.blojsom.BlojsomException If there is an error initializing the dispatcher
108      */

109     public void init() throws BlojsomException {
110         _templatesDirectory = _blojsomProperties.getProperty(BlojsomConstants.TEMPLATES_DIRECTORY_IP, BlojsomConstants.DEFAULT_TEMPLATES_DIRECTORY);
111         _blogsDirectory = _blojsomProperties.getProperty(BlojsomConstants.BLOGS_DIRECTORY_IP, BlojsomConstants.DEFAULT_BLOGS_DIRECTORY);
112     }
113
114     /**
115      * Return a path appropriate for the WebMacro file resource loader for a given blog
116      *
117      * @param blogID Blog ID
118      * @return blojsom installation directory + base configuration directory + Blog ID + templates directory
119      */

120     protected String JavaDoc getWebMacroTemplatePathForBlog(String JavaDoc blogID) {
121         StringBuffer JavaDoc templatePathForBlog = new StringBuffer JavaDoc();
122         templatePathForBlog.append(_servletConfig.getServletContext().getRealPath(BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY));
123         templatePathForBlog.append(_blogsDirectory);
124         templatePathForBlog.append(blogID);
125         templatePathForBlog.append(_templatesDirectory);
126
127         return templatePathForBlog.toString();
128     }
129
130     /**
131      * Return a path appropriate for the WebMacro file resource loader
132      *
133      * @return blojsom installation directory + base configuration directory + templates directory
134      */

135     protected String JavaDoc getWebMacroGlobalTemplatePath() {
136         StringBuffer JavaDoc templatePath = new StringBuffer JavaDoc();
137
138         templatePath.append(_servletConfig.getServletContext().getRealPath(BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY));
139         templatePath.append(_templatesDirectory);
140
141         return templatePath.toString();
142     }
143
144     /**
145      * Populate the WebMacro context with the request and session attributes
146      *
147      * @param httpServletRequest Request
148      * @param context Context
149      */

150     protected void populateWebMacroContext(HttpServletRequest JavaDoc httpServletRequest, Map JavaDoc context) {
151         EnumerationIterator iterator = new EnumerationIterator(httpServletRequest.getAttributeNames());
152         while (iterator.hasNext()) {
153             Object JavaDoc key = iterator.next();
154             Object JavaDoc value = httpServletRequest.getAttribute(key.toString());
155             context.put(key, value);
156         }
157
158         HttpSession JavaDoc httpSession = httpServletRequest.getSession();
159         if (httpSession != null) {
160             iterator = new EnumerationIterator(httpSession.getAttributeNames());
161             while (iterator.hasNext()) {
162                 Object JavaDoc key = iterator.next();
163                 Object JavaDoc value = httpSession.getAttribute(key.toString());
164                 context.put(key, value);
165             }
166         }
167     }
168
169     /**
170      * Dispatch a request and response. A context map is provided for the BlojsomServlet to pass
171      * any required information for use by the dispatcher. The dispatcher is also
172      * provided with the template for the requested flavor along with the content type for the
173      * specific flavor.
174      *
175      * @param httpServletRequest Request
176      * @param httpServletResponse Response
177      * @param blog {@link Blog}
178      * @param context Context map
179      * @param flavorTemplate Template to dispatch to for the requested flavor
180      * @param flavorContentType Content type for the requested flavor
181      * @throws java.io.IOException If there is an exception during IO
182      * @throws javax.servlet.ServletException If there is an exception in dispatching the request
183      */

184     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 {
185         httpServletResponse.setContentType(flavorContentType);
186
187         try {
188             Properties JavaDoc updatedWebMacroProperties = (Properties JavaDoc) _webMacroProperties.clone();
189             updatedWebMacroProperties.setProperty("TemplateLoaderPath.1", "file:" + getWebMacroTemplatePathForBlog(blog.getBlogId()));
190             updatedWebMacroProperties.setProperty("TemplateLoaderPath.2", "file:" + getWebMacroGlobalTemplatePath());
191
192             WM wm = new WM(updatedWebMacroProperties);
193             Context wmContext = wm.getContext();
194             populateWebMacroContext(httpServletRequest, context);
195             wmContext.setMap(context);
196
197             String JavaDoc flavorTemplateForPage = null;
198             String JavaDoc pageParameter = BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest, true);
199
200             if (pageParameter != null) {
201                 flavorTemplateForPage = BlojsomUtils.getTemplateForPage(flavorTemplate, pageParameter);
202                 if (_logger.isDebugEnabled()) {
203                     _logger.debug("Retrieved template for page: " + flavorTemplateForPage);
204                 }
205             }
206
207             if (BlojsomUtils.checkNullOrBlank(flavorTemplateForPage)) {
208                 try {
209                     wm.writeTemplate(flavorTemplate, httpServletResponse.getOutputStream(), wmContext);
210                 } catch (ResourceException e) {
211                     if (_logger.isErrorEnabled()) {
212                         _logger.error(e);
213                     }
214                 } catch (PropertyException e) {
215                     if (_logger.isErrorEnabled()) {
216                         _logger.error(e);
217                     }
218                 }
219
220                 if (_logger.isDebugEnabled()) {
221                     _logger.debug("Dispatched to flavor template: " + flavorTemplate);
222                 }
223             } else {
224                 try {
225                     wm.writeTemplate(flavorTemplateForPage, httpServletResponse.getOutputStream(), wmContext);
226                 } catch (ResourceException e) {
227                     if (_logger.isErrorEnabled()) {
228                         _logger.error(e);
229                     }
230                 } catch (PropertyException e) {
231                     if (_logger.isErrorEnabled()) {
232                         _logger.error(e);
233                     }
234                 }
235
236                 if (_logger.isDebugEnabled()) {
237                     _logger.debug("Dispatched to flavor page template: " + flavorTemplateForPage);
238                 }
239
240             }
241
242             httpServletResponse.getOutputStream().flush();
243         } catch (InitException e) {
244             if (_logger.isErrorEnabled()) {
245                 _logger.error(e);
246             }
247         }
248     }
249 }
250
Popular Tags