001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.dispatcher.webmacro;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.velocity.util.EnumerationIterator;
035: import org.blojsom.BlojsomException;
036: import org.blojsom.blog.Blog;
037: import org.blojsom.dispatcher.Dispatcher;
038: import org.blojsom.util.BlojsomConstants;
039: import org.blojsom.util.BlojsomUtils;
040: import org.webmacro.*;
041:
042: import javax.servlet.ServletConfig;
043: import javax.servlet.ServletException;
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletResponse;
046: import javax.servlet.http.HttpSession;
047: import java.io.IOException;
048: import java.util.Map;
049: import java.util.Properties;
050:
051: /**
052: * WebMacro dispatcher
053: *
054: * @author David Czarnecki
055: * @since blojsom 3.0
056: * @version $Id: WebMacroDispatcher.java,v 1.2 2007/01/17 01:15:46 czarneckid Exp $
057: */
058: public class WebMacroDispatcher implements Dispatcher {
059:
060: private Log _logger = LogFactory.getLog(WebMacroDispatcher.class);
061:
062: private Properties _webMacroProperties;
063: private Properties _blojsomProperties;
064: private ServletConfig _servletConfig;
065:
066: private String _templatesDirectory;
067: private String _blogsDirectory;
068:
069: /**
070: * Construct a new instance of the WebMacro dispatcher
071: */
072: public WebMacroDispatcher() {
073: }
074:
075: /**
076: * Set the WebMacro properties for use by the dispatcher
077: *
078: * @param webMacroProperties Properties for WebMacro configuration
079: */
080: public void setWebMacroProperties(Properties webMacroProperties) {
081: _webMacroProperties = webMacroProperties;
082: }
083:
084: /**
085: * Set the properties in use by blojsom
086: *
087: * @param blojsomProperties Properties in use by blojsom
088: */
089: public void setBlojsomProperties(Properties blojsomProperties) {
090: _blojsomProperties = blojsomProperties;
091: }
092:
093: /**
094: * Set the {@link javax.servlet.ServletConfig}
095: *
096: * @param servletConfig {@link javax.servlet.ServletConfig}
097: */
098: public void setServletConfig(ServletConfig servletConfig) {
099: _servletConfig = servletConfig;
100: }
101:
102: /**
103: * Initialization method for blojsom dispatchers
104: *
105: * @throws org.blojsom.BlojsomException If there is an error initializing the dispatcher
106: */
107: public void init() throws BlojsomException {
108: _templatesDirectory = _blojsomProperties.getProperty(
109: BlojsomConstants.TEMPLATES_DIRECTORY_IP,
110: BlojsomConstants.DEFAULT_TEMPLATES_DIRECTORY);
111: _blogsDirectory = _blojsomProperties.getProperty(
112: BlojsomConstants.BLOGS_DIRECTORY_IP,
113: BlojsomConstants.DEFAULT_BLOGS_DIRECTORY);
114: }
115:
116: /**
117: * Return a path appropriate for the WebMacro file resource loader for a given blog
118: *
119: * @param blogID Blog ID
120: * @return blojsom installation directory + base configuration directory + Blog ID + templates directory
121: */
122: protected String getWebMacroTemplatePathForBlog(String blogID) {
123: StringBuffer templatePathForBlog = new StringBuffer();
124: templatePathForBlog
125: .append(_servletConfig
126: .getServletContext()
127: .getRealPath(
128: BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY));
129: templatePathForBlog.append(_blogsDirectory);
130: templatePathForBlog.append(blogID);
131: templatePathForBlog.append(_templatesDirectory);
132:
133: return templatePathForBlog.toString();
134: }
135:
136: /**
137: * Return a path appropriate for the WebMacro file resource loader
138: *
139: * @return blojsom installation directory + base configuration directory + templates directory
140: */
141: protected String getWebMacroGlobalTemplatePath() {
142: StringBuffer templatePath = new StringBuffer();
143:
144: templatePath
145: .append(_servletConfig
146: .getServletContext()
147: .getRealPath(
148: BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY));
149: templatePath.append(_templatesDirectory);
150:
151: return templatePath.toString();
152: }
153:
154: /**
155: * Populate the WebMacro context with the request and session attributes
156: *
157: * @param httpServletRequest Request
158: * @param context Context
159: */
160: protected void populateWebMacroContext(
161: HttpServletRequest httpServletRequest, Map context) {
162: EnumerationIterator iterator = new EnumerationIterator(
163: httpServletRequest.getAttributeNames());
164: while (iterator.hasNext()) {
165: Object key = iterator.next();
166: Object value = httpServletRequest.getAttribute(key
167: .toString());
168: context.put(key, value);
169: }
170:
171: HttpSession httpSession = httpServletRequest.getSession();
172: if (httpSession != null) {
173: iterator = new EnumerationIterator(httpSession
174: .getAttributeNames());
175: while (iterator.hasNext()) {
176: Object key = iterator.next();
177: Object value = httpSession.getAttribute(key.toString());
178: context.put(key, value);
179: }
180: }
181: }
182:
183: /**
184: * Dispatch a request and response. A context map is provided for the BlojsomServlet to pass
185: * any required information for use by the dispatcher. The dispatcher is also
186: * provided with the template for the requested flavor along with the content type for the
187: * specific flavor.
188: *
189: * @param httpServletRequest Request
190: * @param httpServletResponse Response
191: * @param blog {@link Blog}
192: * @param context Context map
193: * @param flavorTemplate Template to dispatch to for the requested flavor
194: * @param flavorContentType Content type for the requested flavor
195: * @throws java.io.IOException If there is an exception during IO
196: * @throws javax.servlet.ServletException If there is an exception in dispatching the request
197: */
198: public void dispatch(HttpServletRequest httpServletRequest,
199: HttpServletResponse httpServletResponse, Blog blog,
200: Map context, String flavorTemplate, String flavorContentType)
201: throws IOException, ServletException {
202: httpServletResponse.setContentType(flavorContentType);
203:
204: try {
205: Properties updatedWebMacroProperties = (Properties) _webMacroProperties
206: .clone();
207: updatedWebMacroProperties.setProperty(
208: "TemplateLoaderPath.1", "file:"
209: + getWebMacroTemplatePathForBlog(blog
210: .getBlogId()));
211: updatedWebMacroProperties.setProperty(
212: "TemplateLoaderPath.2", "file:"
213: + getWebMacroGlobalTemplatePath());
214:
215: WM wm = new WM(updatedWebMacroProperties);
216: Context wmContext = wm.getContext();
217: populateWebMacroContext(httpServletRequest, context);
218: wmContext.setMap(context);
219:
220: String flavorTemplateForPage = null;
221: String pageParameter = BlojsomUtils.getRequestValue(
222: BlojsomConstants.PAGE_PARAM, httpServletRequest,
223: true);
224:
225: if (pageParameter != null) {
226: flavorTemplateForPage = BlojsomUtils
227: .getTemplateForPage(flavorTemplate,
228: pageParameter);
229: if (_logger.isDebugEnabled()) {
230: _logger.debug("Retrieved template for page: "
231: + flavorTemplateForPage);
232: }
233: }
234:
235: if (BlojsomUtils.checkNullOrBlank(flavorTemplateForPage)) {
236: try {
237: wm.writeTemplate(flavorTemplate,
238: httpServletResponse.getOutputStream(),
239: wmContext);
240: } catch (ResourceException e) {
241: if (_logger.isErrorEnabled()) {
242: _logger.error(e);
243: }
244: } catch (PropertyException e) {
245: if (_logger.isErrorEnabled()) {
246: _logger.error(e);
247: }
248: }
249:
250: if (_logger.isDebugEnabled()) {
251: _logger.debug("Dispatched to flavor template: "
252: + flavorTemplate);
253: }
254: } else {
255: try {
256: wm.writeTemplate(flavorTemplateForPage,
257: httpServletResponse.getOutputStream(),
258: wmContext);
259: } catch (ResourceException e) {
260: if (_logger.isErrorEnabled()) {
261: _logger.error(e);
262: }
263: } catch (PropertyException e) {
264: if (_logger.isErrorEnabled()) {
265: _logger.error(e);
266: }
267: }
268:
269: if (_logger.isDebugEnabled()) {
270: _logger
271: .debug("Dispatched to flavor page template: "
272: + flavorTemplateForPage);
273: }
274:
275: }
276:
277: httpServletResponse.getOutputStream().flush();
278: } catch (InitException e) {
279: if (_logger.isErrorEnabled()) {
280: _logger.error(e);
281: }
282: }
283: }
284: }
|