1 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 ; 43 import javax.servlet.ServletContext ; 44 import javax.servlet.ServletException ; 45 import javax.servlet.http.HttpServletRequest ; 46 import javax.servlet.http.HttpServletResponse ; 47 import java.io.IOException ; 48 import java.util.Iterator ; 49 import java.util.Map ; 50 import java.util.Properties ; 51 52 59 public class JSPDispatcher implements Dispatcher { 60 61 private Log _logger = LogFactory.getLog(JSPDispatcher.class); 62 63 private ServletContext _context; 64 private ServletConfig _servletConfig; 65 private Properties _blojsomProperties; 66 private String _templatesDirectory; 67 private String _blogsDirectory; 68 69 72 public JSPDispatcher() { 73 } 74 75 80 public void setBlojsomProperties(Properties blojsomProperties) { 81 _blojsomProperties = blojsomProperties; 82 } 83 84 89 public void setServletConfig(ServletConfig servletConfig) { 90 _servletConfig = servletConfig; 91 } 92 93 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 121 public void dispatch(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, String flavorTemplate, String flavorContentType) throws IOException , ServletException { 122 httpServletResponse.setContentType(flavorContentType); 123 124 if (!flavorTemplate.startsWith("/")) { 125 flavorTemplate = '/' + flavorTemplate; 126 } 127 128 String 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 Iterator contextIterator = context.keySet().iterator(); 139 while (contextIterator.hasNext()) { 140 String contextKey = (String ) 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 if (flavorTemplateForPage != null) { 151 String 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 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 String 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 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 |