1 25 package org.ofbiz.webapp.view; 26 27 import java.io.File ; 28 import java.io.FileNotFoundException ; 29 import java.io.IOException ; 30 import java.io.OutputStream ; 31 import java.io.StringWriter ; 32 import java.io.Writer ; 33 import java.security.AccessController ; 34 import java.security.PrivilegedAction ; 35 import java.util.Iterator ; 36 import javax.servlet.ServletContext ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import javax.servlet.http.HttpSession ; 40 41 import org.ofbiz.base.util.Debug; 42 import org.ofbiz.base.util.GeneralException; 43 44 import org.jpublish.JPublishContext; 45 import org.jpublish.Page; 46 import org.jpublish.Repository; 47 import org.jpublish.RepositoryWrapper; 48 import org.jpublish.SiteContext; 49 import org.jpublish.StaticResourceManager; 50 import org.jpublish.Template; 51 import org.jpublish.action.ActionManager; 52 import org.jpublish.component.ComponentMap; 53 import org.jpublish.page.PageInstance; 54 import org.jpublish.util.CharacterEncodingMap; 55 import org.jpublish.util.DateUtilities; 56 import org.jpublish.util.NumberUtilities; 57 import org.jpublish.util.URLUtilities; 58 59 66 public class JPublishWrapper { 67 68 public static final String module = JPublishWrapper.class.getName(); 69 70 protected ServletContext servletContext = null; 71 protected SiteContext siteContext = null; 72 73 public JPublishWrapper(ServletContext context) { 74 this.servletContext = context; 75 String rootDir = servletContext.getRealPath("/"); 77 File contextRoot = new File (rootDir); 78 File webInfPath = new File (contextRoot, "WEB-INF"); 79 80 configureClasspath(webInfPath); 82 83 try { 85 siteContext = new SiteContext(contextRoot, "WEB-INF/jpublish.xml"); 87 siteContext.setWebInfPath(webInfPath); 88 } catch (Exception e) { 89 Debug.logError(e, "Cannot load SiteContext", module); 90 } 91 92 try { 94 ActionManager actionManager = siteContext.getActionManager(); 95 actionManager.executeStartupActions(); 96 } catch (Exception e) { 97 Debug.logError(e, "Problems executing JPublish startup actions", module); 98 } 99 100 servletContext.setAttribute("jpublishWrapper", this); 102 } 103 104 protected void configureClasspath(File webInfPath) { 105 File webLibPath = new File (webInfPath, "lib"); 106 File webClassPath = new File (webInfPath, "classes"); 107 108 StringBuffer classPath = new StringBuffer (); 110 classPath.append(System.getProperty("java.class.path")); 111 112 if (webClassPath.exists()) { 113 classPath.append(System.getProperty("path.separator")); 114 classPath.append(webClassPath); 115 } 116 117 if (webLibPath.exists()) { 119 File [] files = webLibPath.listFiles(); 120 for (int i = 0; i < files.length; i++) { 121 if (files[i].getName().toLowerCase().endsWith(".jar") 122 || files[i].getName().toLowerCase().endsWith(".zip")) { 123 classPath.append(System.getProperty("path.separator")); 124 classPath.append(files[i]); 125 } 126 } 127 } 128 129 AccessController.doPrivileged(new SetClassPathAction(classPath.toString())); 130 } 131 132 protected boolean executeGlobalActions(HttpServletRequest request, HttpServletResponse response, JPublishContext context, String path, boolean allowRedirect) throws Exception { 133 ActionManager actionManager = siteContext.getActionManager(); 134 return optionalRedirect(actionManager.executeGlobalActions(context), path, response, allowRedirect); 135 } 136 137 protected boolean executePathActions(HttpServletRequest request, HttpServletResponse response, JPublishContext context, String path, boolean allowRedirect) throws Exception { 138 ActionManager actionManager = siteContext.getActionManager(); 139 return optionalRedirect(actionManager.executePathActions(path, context), path, response, allowRedirect); 140 } 141 142 protected boolean executeParameterActions(HttpServletRequest request, HttpServletResponse response, JPublishContext context, String path, boolean allowRedirect) throws Exception { 143 if (!siteContext.isParameterActionsEnabled()) { 144 return false; 145 } 146 147 ActionManager actionManager = siteContext.getActionManager(); 148 String [] actionNames = request.getParameterValues(siteContext.getActionIdentifier()); 149 if (actionNames != null) { 150 for (int i = 0; i < actionNames.length; i++) { 151 return optionalRedirect(actionManager.execute(actionNames[i], context), path, response, allowRedirect); 152 } 153 } 154 return false; 155 } 156 157 protected boolean executePreEvaluationActions(HttpServletRequest request, HttpServletResponse response, JPublishContext context, String path) throws Exception { 158 ActionManager actionManager = siteContext.getActionManager(); 159 return actionManager.executePreEvaluationActions(path, context); 160 } 161 162 protected boolean executePostEvaluationActions(HttpServletRequest request, HttpServletResponse response, JPublishContext context, String path) throws Exception { 163 ActionManager actionManager = siteContext.getActionManager(); 164 actionManager.executePostEvaluationActions(path, context); 165 return false; 166 } 167 168 private boolean optionalRedirect(String redirect, String path, HttpServletResponse response, boolean allowRedirect) throws IOException { 169 if (redirect != null && allowRedirect) { 170 response.sendRedirect(redirect); 171 return true; 172 } 173 return false; 174 } 175 176 184 public String render(String path, HttpServletRequest request, HttpServletResponse response) throws GeneralException { 185 Writer writer = new StringWriter (); 186 String content = null; 187 render(path, request, response, writer); 188 try { 189 content = writer.toString(); 190 writer.close(); 191 } catch (IOException e) { 192 throw new GeneralException("Problems closing the Writer", e); 193 } 194 return content; 195 } 196 197 public void render(String path, HttpServletRequest request, HttpServletResponse response, Writer writer) throws GeneralException { 198 render(path, request, response, writer, null, false); 199 } 200 201 public void render(String path, HttpServletRequest request, HttpServletResponse response, Writer writer, OutputStream outputStream) throws GeneralException { 202 render(path, request, response, writer, outputStream, false); 203 } 204 205 public void render(String path, HttpServletRequest request, HttpServletResponse response, Writer writer, OutputStream outputStream, boolean allowRedirect) throws GeneralException { 206 HttpSession session = request.getSession(); 207 ActionManager actionManager = siteContext.getActionManager(); 208 211 CharacterEncodingMap characterEncodingMap = siteContext.getCharacterEncodingManager().getMap(path); 213 214 JPublishContext context = new JPublishContext(this); 216 context.put("request", request); 217 context.put("response", response); 218 context.put("session", session); 219 context.put("application", servletContext); 220 221 context.put("characterEncodingMap", characterEncodingMap); 223 224 URLUtilities urlUtilities = new URLUtilities(request, response); 226 context.put("urlUtilities", urlUtilities); 227 228 context.put("dateUtilities", DateUtilities.getInstance()); 230 231 context.put("numberUtilities", NumberUtilities.getInstance()); 233 234 context.put("syslog", SiteContext.syslog); 236 237 context.put("site", siteContext); 239 240 if (siteContext.isProtectReservedNames()) { 241 context.enableCheckReservedNames(this); 242 } 243 244 Iterator repositories = siteContext.getRepositories().iterator(); 246 while (repositories.hasNext()) { 247 Repository repository = (Repository) repositories.next(); 248 context.put(repository.getName(), new RepositoryWrapper(repository, context)); 249 if (repository.getName().equals("fs_repository")) { 253 context.put("pages", new RepositoryWrapper(repository, context)); 254 } 255 } 256 257 try { 258 if (executePreEvaluationActions(request, response, context, path)) 259 return; 260 261 StaticResourceManager staticResourceManager = siteContext.getStaticResourceManager(); 263 if (staticResourceManager.resourceExists(path)) { 264 if (outputStream != null) { 265 if (executeGlobalActions(request, response, context, path, allowRedirect)) 267 return; 268 269 if (executePathActions(request, response, context, path, allowRedirect)) 271 return; 272 273 if (executeParameterActions(request, response, context, path, allowRedirect)) 275 return; 276 277 staticResourceManager.load(path, outputStream); 279 outputStream.flush(); 280 return; 281 } else { 282 throw new GeneralException("Cannot load static resource with a null OutputStream"); 283 } 284 } 285 286 if (writer == null) 288 throw new GeneralException("Cannot load dynamic content with a null Writer"); 289 290 PageInstance pageInstance = siteContext.getPageManager().getPage(path); 292 Page page = new Page(pageInstance); 293 294 context.disableCheckReservedNames(this); 295 296 context.put("page", page); 298 299 context.put("components", new ComponentMap(context)); 301 302 if (siteContext.isProtectReservedNames()) { 303 context.enableCheckReservedNames(this); 304 } 305 306 if (executeGlobalActions(request, response, context, path, allowRedirect)) 308 return; 309 310 if (executePathActions(request, response, context, path, allowRedirect)) 312 return; 313 314 if (executeParameterActions(request, response, context, path, allowRedirect)) 316 return; 317 318 if (optionalRedirect(page.executeActions(context), path, response, allowRedirect)) 320 return; 321 322 Template template = siteContext.getTemplateManager().getTemplate(page.getFullTemplateName()); 324 325 template.merge(context, page, writer); 327 writer.flush(); 328 } catch (FileNotFoundException e) { 329 throw new GeneralException("File not found", e); 330 } catch (Exception e) { 331 throw new GeneralException("JPublish execution error", e); 332 } finally { 333 try { 334 executePostEvaluationActions(request, response, context, path); 335 } catch (Exception e) { 336 throw new GeneralException("Error executing JPublish post evaluation actions", e); 337 } 338 } 339 } 340 341 350 class SetClassPathAction implements PrivilegedAction { 351 private String classPath; 352 353 357 public SetClassPathAction(String classPath) { 358 this.classPath = classPath; 359 } 360 361 365 public Object run() { 366 System.setProperty("java.class.path", classPath); 367 return null; } 369 } 370 } 371 | Popular Tags |