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.jsp;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.BlojsomException;
035: import org.blojsom.dispatcher.Dispatcher;
036: import org.blojsom.blog.Blog;
037: import org.blojsom.filter.PermalinkFilter;
038: import org.blojsom.util.BlojsomConstants;
039: import org.blojsom.util.BlojsomUtils;
040:
041: import javax.servlet.ServletConfig;
042: import javax.servlet.ServletContext;
043: import javax.servlet.ServletException;
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletResponse;
046: import java.io.IOException;
047: import java.util.Iterator;
048: import java.util.Map;
049: import java.util.Properties;
050:
051: /**
052: * JSPDispatcher
053: *
054: * @author David Czarnecki
055: * @since blojsom 3.0
056: * @version $Id: JSPDispatcher.java,v 1.5 2007/01/17 02:35:16 czarneckid Exp $
057: */
058: public class JSPDispatcher implements Dispatcher {
059:
060: private Log _logger = LogFactory.getLog(JSPDispatcher.class);
061:
062: private ServletContext _context;
063: private ServletConfig _servletConfig;
064: private Properties _blojsomProperties;
065: private String _templatesDirectory;
066: private String _blogsDirectory;
067:
068: /**
069: * Create a new JSPDispatcher
070: */
071: public JSPDispatcher() {
072: }
073:
074: /**
075: * Set the properties in use by blojsom
076: *
077: * @param blojsomProperties Properties in use by blojsom
078: */
079: public void setBlojsomProperties(Properties blojsomProperties) {
080: _blojsomProperties = blojsomProperties;
081: }
082:
083: /**
084: * Set the {@link ServletConfig}
085: *
086: * @param servletConfig {@link ServletConfig}
087: */
088: public void setServletConfig(ServletConfig servletConfig) {
089: _servletConfig = servletConfig;
090: }
091:
092: /**
093: * Initialization method for blojsom dispatchers
094: *
095: * @throws BlojsomException If there is an error initializing the dispatcher
096: */
097: public void init() throws BlojsomException {
098: _templatesDirectory = _blojsomProperties.getProperty(
099: BlojsomConstants.TEMPLATES_DIRECTORY_IP,
100: BlojsomConstants.DEFAULT_TEMPLATES_DIRECTORY);
101: _blogsDirectory = _blojsomProperties.getProperty(
102: BlojsomConstants.BLOGS_DIRECTORY_IP,
103: BlojsomConstants.DEFAULT_BLOGS_DIRECTORY);
104: _context = _servletConfig.getServletContext();
105: }
106:
107: /**
108: * Dispatch a request and response. A context map is provided for the BlojsomServlet to pass
109: * any required information for use by the dispatcher. The dispatcher is also
110: * provided with the template for the requested flavor along with the content type for the
111: * specific flavor.
112: *
113: * @param httpServletRequest Request
114: * @param httpServletResponse Response
115: * @param blog {@link Blog} instance
116: * @param context Context map
117: * @param flavorTemplate Template to dispatch to for the requested flavor
118: * @param flavorContentType Content type for the requested flavor
119: * @throws IOException If there is an exception during IO
120: * @throws ServletException If there is an exception in dispatching the request
121: */
122: public void dispatch(HttpServletRequest httpServletRequest,
123: HttpServletResponse httpServletResponse, Blog blog,
124: Map context, String flavorTemplate, String flavorContentType)
125: throws IOException, ServletException {
126: httpServletResponse.setContentType(flavorContentType);
127:
128: if (!flavorTemplate.startsWith("/")) {
129: flavorTemplate = '/' + flavorTemplate;
130: }
131:
132: String flavorTemplateForPage = null;
133: if (BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM,
134: httpServletRequest) != null) {
135: flavorTemplateForPage = BlojsomUtils.getTemplateForPage(
136: flavorTemplate, BlojsomUtils.getRequestValue(
137: BlojsomConstants.PAGE_PARAM,
138: httpServletRequest));
139:
140: if (_logger.isDebugEnabled()) {
141: _logger.debug("Retrieved template for page: "
142: + flavorTemplateForPage);
143: }
144: }
145:
146: // Populate the request with context attributes from the blog
147: Iterator contextIterator = context.keySet().iterator();
148: while (contextIterator.hasNext()) {
149: String contextKey = (String) contextIterator.next();
150: httpServletRequest.setAttribute(contextKey, context
151: .get(contextKey));
152: }
153:
154: if (httpServletRequest instanceof PermalinkFilter.PermalinkRequest) {
155: PermalinkFilter.PermalinkRequest permalinkRequest = (PermalinkFilter.PermalinkRequest) httpServletRequest;
156: permalinkRequest.setPathInfo(null);
157: }
158:
159: Boolean redirectTo = (Boolean) httpServletRequest
160: .getAttribute(BlojsomConstants.IS_IN_REDIRECT);
161: boolean isRedirect = (redirectTo != null && redirectTo
162: .booleanValue());
163:
164: // Try and look for the original flavor template with page for the individual user
165: if (flavorTemplateForPage != null) {
166: String templateToLoad = BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY
167: + _blogsDirectory
168: + blog.getBlogId()
169: + _templatesDirectory
170: + BlojsomUtils
171: .removeInitialSlash(flavorTemplateForPage);
172: if (_context.getResource(templateToLoad) != null) {
173: if (!isRedirect) {
174: httpServletRequest.getRequestDispatcher(
175: templateToLoad).forward(httpServletRequest,
176: httpServletResponse);
177: httpServletResponse.getWriter().flush();
178:
179: if (_logger.isDebugEnabled()) {
180: _logger
181: .debug("Dispatched to flavor page template for user: "
182: + templateToLoad);
183: }
184: }
185: } else {
186: templateToLoad = BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY
187: + BlojsomUtils
188: .removeInitialSlash(_templatesDirectory)
189: + BlojsomUtils
190: .removeInitialSlash(flavorTemplateForPage);
191: if (_context.getResource(templateToLoad) != null) {
192: // Otherwise, fallback and look for the flavor template with page without including any user information
193: if (!isRedirect) {
194: httpServletRequest.getRequestDispatcher(
195: templateToLoad)
196: .forward(httpServletRequest,
197: httpServletResponse);
198: httpServletResponse.getWriter().flush();
199: if (_logger.isDebugEnabled()) {
200: _logger
201: .debug("Dispatched to flavor page template: "
202: + templateToLoad);
203: }
204: }
205: } else {
206: if (_logger.isErrorEnabled()) {
207: _logger
208: .error("Unable to dispatch to flavor page template: "
209: + templateToLoad);
210: }
211: }
212: }
213: } else {
214: // Otherwise, fallback and look for the flavor template for the individual user
215: String templateToLoad = BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY
216: + _blogsDirectory
217: + blog.getBlogId()
218: + _templatesDirectory
219: + BlojsomUtils.removeInitialSlash(flavorTemplate);
220: if (_context.getResource(templateToLoad) != null) {
221: if (!isRedirect) {
222: httpServletRequest.getRequestDispatcher(
223: templateToLoad).forward(httpServletRequest,
224: httpServletResponse);
225: httpServletResponse.getWriter().flush();
226:
227: if (_logger.isDebugEnabled()) {
228: _logger
229: .debug("Dispatched to flavor template for user: "
230: + templateToLoad);
231: }
232: }
233: } else {
234: templateToLoad = BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY
235: + BlojsomUtils
236: .removeInitialSlash(_templatesDirectory)
237: + BlojsomUtils
238: .removeInitialSlash(flavorTemplate);
239: if (_context.getResource(templateToLoad) != null) {
240: // Otherwise, fallback and look for the flavor template without including any user information
241: if (!isRedirect) {
242: httpServletRequest.getRequestDispatcher(
243: templateToLoad)
244: .forward(httpServletRequest,
245: httpServletResponse);
246: httpServletResponse.getWriter().flush();
247:
248: if (_logger.isDebugEnabled()) {
249: _logger
250: .debug("Dispatched to flavor template: "
251: + templateToLoad);
252: }
253: }
254: } else {
255: if (_logger.isErrorEnabled()) {
256: _logger
257: .error("Unable to dispatch to flavor template: "
258: + templateToLoad);
259: }
260: }
261: }
262: }
263: }
264: }
|