1 31 package org.blojsom.dispatcher.velocity; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.velocity.VelocityContext; 36 import org.apache.velocity.app.Velocity; 37 import org.apache.velocity.app.VelocityEngine; 38 import org.apache.velocity.exception.MethodInvocationException; 39 import org.apache.velocity.exception.ParseErrorException; 40 import org.apache.velocity.exception.ResourceNotFoundException; 41 import org.apache.velocity.util.EnumerationIterator; 42 import org.blojsom.dispatcher.Dispatcher; 43 import org.blojsom.blog.Blog; 44 import org.blojsom.util.BlojsomConstants; 45 import org.blojsom.util.BlojsomUtils; 46 47 import javax.servlet.ServletConfig ; 48 import javax.servlet.ServletContext ; 49 import javax.servlet.ServletException ; 50 import javax.servlet.http.HttpServletRequest ; 51 import javax.servlet.http.HttpServletResponse ; 52 import javax.servlet.http.HttpSession ; 53 import java.io.IOException ; 54 import java.io.StringWriter ; 55 import java.io.Writer ; 56 import java.util.Map ; 57 import java.util.Properties ; 58 59 66 public class VelocityDispatcher implements Dispatcher { 67 68 private Log _logger = LogFactory.getLog(VelocityDispatcher.class); 69 70 private static final String BLOJSOM_RENDER_TOOL = "BLOJSOM_RENDER_TOOL"; 71 72 private Properties _velocityProperties; 73 private ServletConfig _servletConfig; 74 private Properties _blojsomProperties; 75 76 private String _templatesDirectory; 77 private String _blogsDirectory; 78 79 82 public VelocityDispatcher() { 83 } 84 85 90 public void init() throws org.blojsom.BlojsomException { 91 _templatesDirectory = _blojsomProperties.getProperty(BlojsomConstants.TEMPLATES_DIRECTORY_IP, BlojsomConstants.DEFAULT_TEMPLATES_DIRECTORY); 92 _blogsDirectory = _blojsomProperties.getProperty(BlojsomConstants.BLOGS_DIRECTORY_IP, BlojsomConstants.DEFAULT_BLOGS_DIRECTORY); 93 } 94 95 100 public void setVelocityProperties(Properties velocityProperties) { 101 _velocityProperties = velocityProperties; 102 } 103 104 109 public void setBlojsomProperties(Properties blojsomProperties) { 110 _blojsomProperties = blojsomProperties; 111 } 112 113 118 public void setServletConfig(ServletConfig servletConfig) { 119 _servletConfig = servletConfig; 120 } 121 122 128 protected void populateVelocityContext(HttpServletRequest httpServletRequest, Map context) { 129 EnumerationIterator iterator = new EnumerationIterator(httpServletRequest.getAttributeNames()); 130 while (iterator.hasNext()) { 131 Object key = iterator.next(); 132 Object value = httpServletRequest.getAttribute(key.toString()); 133 context.put(key, value); 134 } 135 136 HttpSession httpSession = httpServletRequest.getSession(false); 137 if (httpSession != null) { 138 iterator = new EnumerationIterator(httpSession.getAttributeNames()); 139 while (iterator.hasNext()) { 140 Object key = iterator.next(); 141 Object value = httpSession.getAttribute(key.toString()); 142 context.put(key, value); 143 } 144 } 145 } 146 147 152 protected void destroyVelocityContext(VelocityContext velocityContext) { 153 Object [] contextKeys = velocityContext.getKeys(); 155 for (int i = 0; i < contextKeys.length; i++) { 156 Object contextKey = contextKeys[i]; 157 velocityContext.remove(contextKey); 158 } 159 } 160 161 176 public void dispatch(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, String flavorTemplate, String flavorContentType) throws IOException , ServletException { 177 httpServletResponse.setContentType(flavorContentType); 178 ServletContext servletContext = _servletConfig.getServletContext(); 179 180 VelocityEngine velocityEngine = new VelocityEngine(); 182 try { 183 Properties updatedProperties = (Properties ) _velocityProperties.clone(); 184 updatedProperties.put(VelocityEngine.FILE_RESOURCE_LOADER_PATH, servletContext.getRealPath(BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + _blogsDirectory + blog.getBlogId() + _templatesDirectory) + ", " + servletContext.getRealPath(BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + _templatesDirectory)); 185 velocityEngine.init(updatedProperties); 186 } catch (Exception e) { 187 if (_logger.isErrorEnabled()) { 188 _logger.error(e); 189 } 190 191 return; 192 } 193 194 Writer responseWriter = httpServletResponse.getWriter(); 195 String flavorTemplateForPage = null; 196 String pageParameter = BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest, true); 197 198 if (pageParameter != null) { 199 flavorTemplateForPage = BlojsomUtils.getTemplateForPage(flavorTemplate, pageParameter); 200 201 if (_logger.isDebugEnabled()) { 202 _logger.debug("Retrieved template for page: " + flavorTemplateForPage); 203 } 204 } 205 206 populateVelocityContext(httpServletRequest, context); 208 VelocityContext velocityContext = new VelocityContext(context); 209 velocityContext.put(BLOJSOM_RENDER_TOOL, new BlojsomRenderTool(velocityEngine, velocityContext)); 210 211 if (flavorTemplateForPage != null) { 212 if (!velocityEngine.templateExists(flavorTemplateForPage)) { 214 if (_logger.isErrorEnabled()) { 215 _logger.error("Could not find flavor page template for user: " + flavorTemplateForPage); 216 } 217 218 responseWriter.flush(); 219 destroyVelocityContext(velocityContext); 220 221 return; 222 } else { 223 try { 224 velocityEngine.mergeTemplate(flavorTemplateForPage, BlojsomConstants.UTF8, velocityContext, responseWriter); 225 } catch (Exception e) { 226 if (_logger.isErrorEnabled()) { 227 _logger.error(e); 228 } 229 230 responseWriter.flush(); 231 destroyVelocityContext(velocityContext); 232 233 return; 234 } 235 } 236 237 _logger.debug("Dispatched to flavor page template: " + flavorTemplateForPage); 238 } else { 239 if (!velocityEngine.templateExists(flavorTemplate)) { 241 if (_logger.isErrorEnabled()) { 242 _logger.error("Could not find flavor template for user: " + flavorTemplate); 243 } 244 245 responseWriter.flush(); 246 destroyVelocityContext(velocityContext); 247 248 return; 249 } else { 250 try { 251 velocityEngine.mergeTemplate(flavorTemplate, BlojsomConstants.UTF8, velocityContext, responseWriter); 252 } catch (Exception e) { 253 if (_logger.isErrorEnabled()) { 254 _logger.error(e); 255 } 256 257 responseWriter.flush(); 258 destroyVelocityContext(velocityContext); 259 260 return; 261 } 262 } 263 264 if (_logger.isDebugEnabled()) { 265 _logger.debug("Dispatched to flavor template: " + flavorTemplate); 266 } 267 } 268 269 responseWriter.flush(); 270 destroyVelocityContext(velocityContext); 271 } 272 273 277 public class BlojsomRenderTool { 278 279 private static final String LOG_TAG = "BlojsomRenderTool"; 280 281 private VelocityEngine _velocityEngine; 282 private VelocityContext _velocityContext; 283 284 290 public BlojsomRenderTool(VelocityEngine velocityEngine, VelocityContext velocityContext) { 291 _velocityEngine = velocityEngine; 292 _velocityContext = velocityContext; 293 } 294 295 301 public String evaluate(String template) { 302 if (BlojsomUtils.checkNullOrBlank(template)) { 303 return null; 304 } 305 306 StringWriter sw = new StringWriter (); 307 boolean success = false; 308 309 try { 310 if (_velocityEngine == null) { 311 success = Velocity.evaluate(_velocityContext, sw, LOG_TAG, template); 312 } else { 313 success = _velocityEngine.evaluate(_velocityContext, sw, LOG_TAG, template); 314 } 315 } catch (ParseErrorException e) { 316 if (_logger.isErrorEnabled()) { 317 _logger.error(e); 318 } 319 } catch (MethodInvocationException e) { 320 if (_logger.isErrorEnabled()) { 321 _logger.error(e); 322 } 323 } catch (ResourceNotFoundException e) { 324 if (_logger.isErrorEnabled()) { 325 _logger.error(e); 326 } 327 } catch (IOException e) { 328 if (_logger.isErrorEnabled()) { 329 _logger.error(e); 330 } 331 } 332 333 if (success) { 334 return sw.toString(); 335 } 336 337 return null; 338 } 339 } 340 } 341
| Popular Tags
|