1 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 ; 44 import javax.servlet.ServletException ; 45 import javax.servlet.http.HttpServletRequest ; 46 import javax.servlet.http.HttpServletResponse ; 47 import javax.servlet.http.HttpSession ; 48 import java.io.IOException ; 49 import java.util.Map ; 50 import java.util.Properties ; 51 52 59 public class WebMacroDispatcher implements Dispatcher { 60 61 private Log _logger = LogFactory.getLog(WebMacroDispatcher.class); 62 63 private Properties _webMacroProperties; 64 private Properties _blojsomProperties; 65 private ServletConfig _servletConfig; 66 67 private String _templatesDirectory; 68 private String _blogsDirectory; 69 70 71 74 public WebMacroDispatcher() { 75 } 76 77 82 public void setWebMacroProperties(Properties webMacroProperties) { 83 _webMacroProperties = webMacroProperties; 84 } 85 86 91 public void setBlojsomProperties(Properties blojsomProperties) { 92 _blojsomProperties = blojsomProperties; 93 } 94 95 100 public void setServletConfig(ServletConfig servletConfig) { 101 _servletConfig = servletConfig; 102 } 103 104 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 120 protected String getWebMacroTemplatePathForBlog(String blogID) { 121 StringBuffer templatePathForBlog = new StringBuffer (); 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 135 protected String getWebMacroGlobalTemplatePath() { 136 StringBuffer templatePath = new StringBuffer (); 137 138 templatePath.append(_servletConfig.getServletContext().getRealPath(BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY)); 139 templatePath.append(_templatesDirectory); 140 141 return templatePath.toString(); 142 } 143 144 150 protected void populateWebMacroContext(HttpServletRequest httpServletRequest, Map context) { 151 EnumerationIterator iterator = new EnumerationIterator(httpServletRequest.getAttributeNames()); 152 while (iterator.hasNext()) { 153 Object key = iterator.next(); 154 Object value = httpServletRequest.getAttribute(key.toString()); 155 context.put(key, value); 156 } 157 158 HttpSession httpSession = httpServletRequest.getSession(); 159 if (httpSession != null) { 160 iterator = new EnumerationIterator(httpSession.getAttributeNames()); 161 while (iterator.hasNext()) { 162 Object key = iterator.next(); 163 Object value = httpSession.getAttribute(key.toString()); 164 context.put(key, value); 165 } 166 } 167 } 168 169 184 public void dispatch(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, String flavorTemplate, String flavorContentType) throws IOException , ServletException { 185 httpServletResponse.setContentType(flavorContentType); 186 187 try { 188 Properties updatedWebMacroProperties = (Properties ) _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 flavorTemplateForPage = null; 198 String 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
|