1 package com.dotmarketing.velocity; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.io.PrintWriter ; 6 import java.net.URL ; 7 import java.util.ArrayList ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Locale ; 11 12 import javax.servlet.ServletConfig ; 13 import javax.servlet.ServletContext ; 14 import javax.servlet.ServletException ; 15 import javax.servlet.http.Cookie ; 16 import javax.servlet.http.HttpServlet ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 import javax.servlet.http.HttpSession ; 20 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.struts.Globals; 23 import org.apache.velocity.Template; 24 import org.apache.velocity.app.Velocity; 25 import org.apache.velocity.context.Context; 26 import org.apache.velocity.exception.MethodInvocationException; 27 import org.apache.velocity.exception.ParseErrorException; 28 import org.apache.velocity.exception.ResourceNotFoundException; 29 import org.apache.velocity.tools.view.ToolboxManager; 30 import org.apache.velocity.tools.view.context.ChainedContext; 31 import org.apache.velocity.tools.view.servlet.ServletToolboxManager; 32 33 import com.dotmarketing.beans.Host; 34 import com.dotmarketing.beans.Identifier; 35 import com.dotmarketing.cache.IdentifierCache; 36 import com.dotmarketing.cache.LanguageCache; 37 import com.dotmarketing.cache.PermissionCache; 38 import com.dotmarketing.cms.factories.PublicCompanyFactory; 39 import com.dotmarketing.db.DotHibernate; 40 import com.dotmarketing.factories.ClickstreamFactory; 41 import com.dotmarketing.factories.HostFactory; 42 import com.dotmarketing.factories.IdentifierFactory; 43 import com.dotmarketing.factories.InodeFactory; 44 import com.dotmarketing.factories.PermissionFactory; 45 import com.dotmarketing.factories.RoleFactory; 46 import com.dotmarketing.filters.ClickstreamFilter; 47 import com.dotmarketing.portlets.containers.model.Container; 48 import com.dotmarketing.portlets.contentlet.model.Contentlet; 49 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 50 import com.dotmarketing.portlets.languagesmanager.factories.LanguageFactory; 51 import com.dotmarketing.portlets.languagesmanager.model.Language; 52 import com.dotmarketing.portlets.structure.model.Structure; 53 import com.dotmarketing.portlets.workflows.factories.WorkflowsFactory; 54 import com.dotmarketing.util.Config; 55 import com.dotmarketing.util.GUIDGenerator; 56 import com.dotmarketing.util.Logger; 57 import com.dotmarketing.util.PortletURLUtil; 58 import com.dotmarketing.util.UtilMethods; 59 import com.liferay.portal.ejb.RoleLocalManagerUtil; 60 import com.liferay.portal.language.LanguageUtil; 61 import com.liferay.portal.model.Company; 62 import com.liferay.portal.model.Role; 63 import com.liferay.portal.model.Skin; 64 import com.liferay.portal.util.WebKeys; 65 import com.liferay.util.ObjectValuePair; 66 import com.liferay.util.servlet.SessionMessages; 67 68 public abstract class VelocityServlet extends HttpServlet { 69 private static final long serialVersionUID = 1L; 70 71 private static PortletURLUtil portletURLUtil = new PortletURLUtil(); 72 73 private static UtilMethods utilMethods = new UtilMethods(); 74 75 private String CHARSET = null; 76 77 private String VELOCITY_HTMLPAGE_EXTENSION = null; 78 79 private List languages = null; 80 81 private ToolboxManager toolboxManager = null; 82 83 private ServletContext myContext; 84 85 private static GUIDGenerator guidGenerator = null; 86 87 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 88 String language = String.valueOf(LanguageFactory.getDefaultLanguage().getId()); 89 HttpSession session = request.getSession(); 90 response.setContentType(CHARSET); 91 92 PrintWriter out = response.getWriter(); 93 94 boolean ADMIN_MODE = (session.getAttribute(com.dotmarketing.util.WebKeys.ADMIN_MODE_SESSION) != null); 95 boolean PREVIEW_MODE = ((session.getAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION) != null) && ADMIN_MODE); 96 boolean EDIT_MODE = ((session.getAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION) != null) && ADMIN_MODE); 97 98 if (session.isNew() || !UtilMethods.isSet((String ) session.getAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE))) { 100 LogFactory.getLog(VelocityServlet.class).debug("session new: " + session.isNew()); 101 Language l = LanguageCache.getLanguageById(language); 102 Locale locale = new Locale (l.getLanguageCode() + "_" + l.getCountryCode().toUpperCase()); 103 session.setAttribute(Globals.LOCALE_KEY, locale); 104 session.setAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE, language); 105 } 106 107 if (UtilMethods.isSet(request.getParameter(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE))) { 109 language = request.getParameter(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE); 110 Language l = LanguageFactory.getLanguage(language); 111 Locale locale = new Locale (l.getLanguageCode() + "_" + l.getCountryCode()); 112 session.setAttribute(Globals.LOCALE_KEY, locale); 113 session.setAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE, language); 114 } 115 116 language = (String ) session.getAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE); 118 119 try { 120 if (PREVIEW_MODE && ADMIN_MODE) { 121 124 LogFactory.getLog(VelocityServlet.class).debug("VELOCITY SERVLET I'M ON PREVIEW MODE!!!"); 125 126 doPreviewMode(request, response); 127 } else if (EDIT_MODE && ADMIN_MODE) { 128 131 LogFactory.getLog(VelocityServlet.class).debug("VELOCITY SERVLET I'M ON EDIT MODE!!!"); 132 133 doEditMode(request, response); 134 } else if (ADMIN_MODE) { 135 LogFactory.getLog(VelocityServlet.class).debug("VELOCITY SERVLET I'M ON ADMIN MODE!!!"); 138 139 doAdminMode(request, response); 140 } else { 141 LogFactory.getLog(VelocityServlet.class).debug("VELOCITY SERVLET I'M ON LIVE MODE!!!"); 143 144 doLiveMode(request, response); 145 } 146 } catch (ResourceNotFoundException rnfe) { 147 148 response.sendError(404); 149 150 } catch (ParseErrorException pee) { 151 152 out.println("Template Parse Exception"); 153 out.println("On template:" + request.getRequestURI() + request.getQueryString()); 154 Logger.error(this, pee.toString(), pee); 155 } catch (MethodInvocationException mie) { 156 out.println("something invoked in the template threw an exception"); 159 out.println("Error on template:" + request.getRequestURI() + request.getQueryString()); 160 Logger.error(this, mie.toString(), mie); 161 } catch (Exception e) { 162 out.println("Error on template:" + request.getRequestURI() + request.getQueryString()); 163 Logger.error(this, e.toString(), e); 164 } 165 DotHibernate.commitTransaction(); 167 } 168 169 public void init(ServletConfig config) throws ServletException { 170 174 try { 175 URL url = Thread.currentThread().getContextClassLoader().getResource("system-ext.properties"); 176 Velocity.init(url.getPath()); 177 178 } catch (Exception s) { 179 Logger.warn(this, s.toString(), s); 180 } 181 182 new File (config.getServletContext().getRealPath("/WEB-INF/velocity/working")).mkdirs(); 184 new File (config.getServletContext().getRealPath("/WEB-INF/velocity/live")).mkdir(); 185 186 languages = LanguageFactory.getLanguages(); 187 188 myContext = config.getServletContext(); 190 toolboxManager = ServletToolboxManager.getInstance(config.getServletContext(), "/WEB-INF/toolbox.xml"); 191 192 Config.initializeConfig(); 193 CHARSET = Config.getStringProperty("CHARSET"); 194 VELOCITY_HTMLPAGE_EXTENSION = Config.getStringProperty("VELOCITY_HTMLPAGE_EXTENSION"); 195 196 } 197 198 protected void doAdminMode(HttpServletRequest request, HttpServletResponse response) throws Exception { 199 PrintWriter out = response.getWriter(); 201 Context context = createContext(request, response); 202 203 String uri = request.getRequestURI(); 204 uri = UtilMethods.cleanURI(uri); 205 206 Host host = null; 207 String pageHostId = request.getParameter("host_id"); 208 if (pageHostId != null) { 209 host = HostFactory.getHost(pageHostId); 210 HostFactory.setHostInRequest(request, host); 211 } else { 212 host = HostFactory.getCurrentHost(request); 213 } 214 215 Identifier id = IdentifierFactory.getIdentifierByURI(uri, host); 216 request.setAttribute("idInode", String.valueOf(id.getInode())); 217 218 HTMLPage htmlPage = (HTMLPage) IdentifierFactory.getWorkingChildOfClass(id, HTMLPage.class); 219 220 getContext(context, htmlPage, 0, id.getURI(), request, true, false, host); 221 222 Template template = null; 223 224 if (request.getParameter("leftMenu") != null) { 225 template = Velocity.getTemplate("/preview_left_menu.vl"); 226 } else if (request.getParameter("mainFrame") != null) { 227 template = Velocity.getTemplate("/live/" + id.getInode() + "." + VELOCITY_HTMLPAGE_EXTENSION); 228 } else { 229 template = Velocity.getTemplate("/preview_mode.vl"); 230 } 231 232 LogFactory.getLog(VelocityServlet.class).debug("Got the template!!!!" + id.getInode()); 233 234 template.merge(context, out); 235 } 236 237 protected void doLiveMode(HttpServletRequest request, HttpServletResponse response) throws Exception { 238 239 String uri = request.getRequestURI(); 240 uri = UtilMethods.cleanURI(uri); 241 242 Host host = HostFactory.getCurrentHost(request); 243 244 long idInode = IdentifierCache.getPathFromIdCache(uri, host).getInode(); 246 247 if (idInode == 0) { 249 response.sendError(404); 250 return; 251 } 252 253 request.setAttribute("idInode", String.valueOf(idInode)); 254 LogFactory.getLog(this.getClass()).debug("VELOCITY HTML INODE=" + idInode); 255 256 257 258 String _dotCMSID = UtilMethods.getCookieValue(request.getCookies(), com.dotmarketing.util.WebKeys.LONG_LIVED_DOTCMS_ID_COOKIE); 259 260 if (!UtilMethods.isSet(_dotCMSID)) { 261 262 if (guidGenerator == null) { 263 try { 264 guidGenerator = new GUIDGenerator(); 265 } catch (Exception e) { 266 267 e.printStackTrace(); 268 } 269 } 270 _dotCMSID = guidGenerator.getUUID(); 272 Cookie idCookie = new Cookie (com.dotmarketing.util.WebKeys.LONG_LIVED_DOTCMS_ID_COOKIE, _dotCMSID); 273 idCookie.setPath("/"); 274 idCookie.setMaxAge(60 * 60 * 24 * 356 * 5); 275 response.addCookie(idCookie); 276 277 } 278 279 com.liferay.portal.model.User user = null; 280 281 try { 282 user = (com.liferay.portal.model.User) request.getSession().getAttribute(com.dotmarketing.util.WebKeys.CMS_USER); 284 } catch (Exception nsue) { 285 Logger.warn(this, "Exception trying to getUser: " + nsue.getMessage(), nsue); 286 } 287 288 boolean signedIn = false; 289 290 if (user != null) { 291 signedIn = true; 292 } 293 294 List permissions = PermissionCache.getRoleNamesWithReadPermissionFromCache(uri, host); 295 296 LogFactory.getLog(VelocityServlet.class).debug("Page Permissions for URI=" + uri); 297 298 if (!permissions.contains(Config.getStringProperty("CMS_ANONYMOUS_ROLE"))) { 300 302 303 304 312 if (!signedIn) { 313 314 request.getSession().setAttribute(WebKeys.LAST_PATH, new ObjectValuePair(uri, request.getParameterMap())); 315 request.getSession().setAttribute(com.dotmarketing.util.WebKeys.REDIRECT_AFTER_LOGIN, uri); 316 317 LogFactory.getLog(VelocityServlet.class).debug("VELOCITY CHECKING PERMISSION: Page doesn't have anonymous access" + uri); 318 319 320 LogFactory.getLog(VelocityServlet.class).debug("401 URI = " + uri); 321 322 LogFactory.getLog(VelocityServlet.class).debug("Unauthorized URI = " + uri); 323 response.sendError(401, "The requested page/file is unauthorized"); 324 return; 325 326 327 } else { 328 LogFactory.getLog(VelocityServlet.class).debug("VELOCITY CHECKING PERMISSION: User signed in"); 330 331 if (!PermissionFactory.userHasReadPermission(user, permissions)) { 333 LogFactory.getLog(VelocityServlet.class).warn("VELOCITY CHECKING PERMISSION: Page doesn't have any access for this user"); 336 response.sendError(403, "The requested page/file is forbidden"); 337 return; 338 } 339 } 340 } 341 342 PrintWriter out = response.getWriter(); 343 344 Context context = createContext(request, response); 345 346 LogFactory.getLog(VelocityServlet.class).debug("Got the template!!!!" + idInode); 347 Velocity.getTemplate("/live/" + idInode + "." + VELOCITY_HTMLPAGE_EXTENSION).merge(context, out); 348 out.close(); 349 350 Logger.debug(VelocityServlet.class, "Into the ClickstreamFilter"); 351 if (request.getAttribute(ClickstreamFilter.FILTER_APPLIED) == null) { 353 request.setAttribute(ClickstreamFilter.FILTER_APPLIED, Boolean.TRUE); 354 ClickstreamFactory.addRequest((HttpServletRequest ) request, ((HttpServletResponse ) response)); 355 } 356 357 } 358 359 protected void doPreviewMode(HttpServletRequest request, HttpServletResponse response) throws Exception { 360 361 PrintWriter out = response.getWriter(); 362 String uri = request.getRequestURI(); 363 uri = UtilMethods.cleanURI(uri); 364 365 Host host = null; 366 String pageHostId = request.getParameter("host_id"); 367 if (pageHostId != null) { 368 host = HostFactory.getHost(pageHostId); 369 HostFactory.setHostInRequest(request, host); 370 } else { 371 host = HostFactory.getCurrentHost(request); 372 } 373 374 com.liferay.portal.model.User user = null; 376 try { 377 user = com.liferay.portal.util.PortalUtil.getUser(request); 378 } catch (Exception nsue) { 379 Logger.warn(this, "Exception trying getUser: " + nsue.getMessage(), nsue); 380 } 381 382 Identifier id = IdentifierFactory.getIdentifierByURI(uri, host); 384 request.setAttribute("idInode", String.valueOf(id.getInode())); 385 LogFactory.getLog(VelocityServlet.class).debug("VELOCITY HTML INODE=" + id.getInode()); 386 387 Template template = null; 388 389 Context context = createContext(request, response); 391 392 HTMLPage htmlPage = (HTMLPage) IdentifierFactory.getWorkingChildOfClass(id, HTMLPage.class); 393 394 boolean hasWritePermOverHTMLPage = PermissionFactory.doesUserHavePermission(htmlPage, com.dotmarketing.util.Config 396 .getIntProperty("PERMISSION_WRITE"), user); 397 context.put("EDIT_HTMLPAGE_PERMISSON", new Boolean (hasWritePermOverHTMLPage)); 398 399 com.dotmarketing.portlets.templates.model.Template cmsTemplate = com.dotmarketing.portlets.htmlpages.factories.HTMLPageFactory 400 .getHTMLPageTemplate(htmlPage, true); 401 Identifier templateIdentifier = IdentifierFactory.getParentIdentifier(cmsTemplate); 402 403 LogFactory.getLog(VelocityServlet.class).debug("VELOCITY TEMPLATE INODE=" + cmsTemplate.getInode()); 404 405 getContext(context, htmlPage, cmsTemplate.getInode(), id.getURI(), request, true, false, host); 406 context.put("previewPage", "2"); 407 context.put("livePage", "0"); 408 List identifiers = InodeFactory.getChildrenClass(cmsTemplate, Identifier.class); 410 Iterator i = identifiers.iterator(); 411 412 while (i.hasNext()) { 413 Identifier ident = (Identifier) i.next(); 414 Container c = (Container) IdentifierFactory.getWorkingChildOfClass(ident, Container.class); 415 context.put(String.valueOf("container" + ident.getInode()), "/working/" + ident.getInode() + "." 416 + Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION")); 417 418 boolean hasWritePermOverContainer = PermissionFactory.doesUserHavePermission(c, com.dotmarketing.util.Config 420 .getIntProperty("PERMISSION_WRITE"), user); 421 context.put("EDIT_CONTAINER_PERMISSION" + ident.getInode(), new Boolean (hasWritePermOverContainer)); 422 423 Structure st = (Structure) InodeFactory.getParentOfClass(c, Structure.class); 425 boolean hasWritePermOverTheStructure = PermissionFactory.doesUserHavePermission(st, com.dotmarketing.util.Config 426 .getIntProperty("PERMISSION_WRITE"), user); 427 context.put("ADD_CONTENT_PERMISSION" + ident.getInode(), new Boolean (hasWritePermOverTheStructure)); 428 429 Logger.debug(VelocityServlet.class, String.valueOf("container" + ident.getInode()) + "=/working/" + ident.getInode() + "." 430 + Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION")); 431 432 String sort = (c.getSortContentletsBy() == null) ? "tree_order" : c.getSortContentletsBy(); 433 434 boolean staticContainer = !UtilMethods.isSet(c.getLuceneQuery()); 435 436 List contentlets = null; 437 438 if (request.getParameter("mainFrame") != null) { 440 if (staticContainer) { 441 LogFactory.getLog(VelocityServlet.class).debug("Static Container!!!!"); 442 443 LogFactory.getLog(VelocityServlet.class).debug("html=" + htmlPage.getInode() + " container=" + c.getInode()); 444 445 Identifier idenHtmlPage = IdentifierCache.getIdentifierByInodeFromCache(htmlPage); 447 Identifier idenContainer = IdentifierCache.getIdentifierByInodeFromCache(c); 448 contentlets = com.dotmarketing.portlets.contentlet.factories.ContentletFactory.getContentletsByOrderAndParents(idenHtmlPage 449 .getInode(), idenContainer.getInode(), sort, true, (String ) request.getSession().getAttribute( 450 com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE)); 451 LogFactory.getLog(VelocityServlet.class).debug( 452 "Getting contentlets for language=" 453 + (String ) request.getSession().getAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE) + " contentlets =" 454 + contentlets.size()); 455 456 } 457 458 ArrayList <String > contentletList = new ArrayList <String >(); 459 460 if (contentlets != null) { 461 Iterator iter = contentlets.iterator(); 462 int count = 0; 463 464 while (iter.hasNext() && (count < c.getMaxContentlets())) { 465 count++; 466 467 Contentlet contentlet = (Contentlet) iter.next(); 468 Identifier contentletIdentifier = IdentifierCache.getIdentifierByInodeFromCache(contentlet); 469 470 boolean hasWritePermOverContentlet = PermissionFactory.doesUserHavePermission(contentlet, com.dotmarketing.util.Config 471 .getIntProperty("PERMISSION_WRITE"), user); 472 473 context.put("EDIT_CONTENT_PERMISSION" + contentletIdentifier.getInode(), new Boolean (hasWritePermOverContentlet)); 474 475 contentletList.add("/working/" + contentletIdentifier.getInode() + "_" + contentlet.getLanguageId() + "." 476 + Config.getStringProperty("VELOCITY_CONTENT_EXTENSION")); 477 478 Logger.debug(this, "Adding contentlet=/working/" + contentletIdentifier.getInode() + "_" + contentlet.getLanguageId() + "." 479 + Config.getStringProperty("VELOCITY_CONTENT_EXTENSION")); 480 } 481 } 482 483 context.put("contentletList" + ident.getInode(), contentletList); 486 context.put("totalSize" + ident.getInode(), new Integer (contentletList.size())); 487 } 488 } 489 490 LogFactory.getLog(VelocityServlet.class).debug( 491 "Before finding template: /working/" + templateIdentifier.getInode() + "." + Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION")); 492 493 LogFactory.getLog(VelocityServlet.class).debug("Velocity directory:" + Velocity.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH)); 494 495 if (request.getParameter("leftMenu") != null) { 496 499 500 List <String > list = new ArrayList <String >(); 501 if (SessionMessages.contains(request, "message")) { 502 list.add((String ) SessionMessages.get(request, "message")); 503 SessionMessages.clear(request); 504 } 505 if (SessionMessages.contains(request, "custommessage")) { 506 list.add((String ) SessionMessages.get(request, "custommessage")); 507 SessionMessages.clear(request); 508 } 509 510 if (list.size() > 0) { 511 ArrayList <String > mymessages = new ArrayList <String >(); 512 Iterator it = list.iterator(); 513 514 while (it.hasNext()) { 515 try { 516 String message = (String ) it.next(); 517 Company comp = PublicCompanyFactory.getDefaultCompany(); 518 mymessages.add(LanguageUtil.get(comp.getCompanyId(), user.getLocale(), message)); 519 } catch (Exception e) { 520 } 521 } 522 context.put("vmessages", mymessages); 523 } 524 525 template = Velocity.getTemplate("/preview_left_menu.vl"); 526 } else if (request.getParameter("mainFrame") != null) { 527 template = Velocity.getTemplate("/working/" + templateIdentifier.getInode() + "." 528 + Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION")); 529 } else { 530 template = Velocity.getTemplate("/preview_mode.vl"); 531 } 532 533 template.merge(context, out); 534 } 535 536 protected void doEditMode(HttpServletRequest request, HttpServletResponse response) throws Exception { 537 538 PrintWriter out = response.getWriter(); 539 String uri = request.getRequestURI(); 540 uri = UtilMethods.cleanURI(uri); 541 542 Host host = null; 543 String pageHostId = request.getParameter("host_id"); 544 if (pageHostId != null) { 545 host = HostFactory.getHost(pageHostId); 546 HostFactory.setHostInRequest(request, host); 547 } else { 548 host = HostFactory.getCurrentHost(request); 549 } 550 551 com.liferay.portal.model.User user = null; 553 try { 554 user = com.liferay.portal.util.PortalUtil.getUser(request); 555 } catch (Exception nsue) { 556 Logger.warn(this, "Exception trying getUser: " + nsue.getMessage(), nsue); 557 } 558 559 Identifier id = IdentifierFactory.getIdentifierByURI(uri, host); 561 request.setAttribute("idInode", String.valueOf(id.getInode())); 562 LogFactory.getLog(VelocityServlet.class).debug("VELOCITY HTML INODE=" + id.getInode()); 563 564 Template template = null; 565 566 Context context = createContext(request, response); 568 569 HTMLPage htmlPage = (HTMLPage) IdentifierFactory.getWorkingChildOfClass(id, HTMLPage.class); 570 571 boolean hasWritePermOverHTMLPage = PermissionFactory.doesUserHavePermission(htmlPage, com.dotmarketing.util.Config 573 .getIntProperty("PERMISSION_WRITE"), user); 574 context.put("EDIT_HTMLPAGE_PERMISSON", new Boolean (hasWritePermOverHTMLPage)); 575 576 com.dotmarketing.portlets.templates.model.Template cmsTemplate = com.dotmarketing.portlets.htmlpages.factories.HTMLPageFactory 577 .getHTMLPageTemplate(htmlPage, true); 578 Identifier templateIdentifier = IdentifierFactory.getParentIdentifier(cmsTemplate); 579 580 LogFactory.getLog(VelocityServlet.class).debug("VELOCITY TEMPLATE INODE=" + cmsTemplate.getInode()); 581 582 getContext(context, htmlPage, cmsTemplate.getInode(), id.getURI(), request, true, true, host); 583 context.put("previewPage", "1"); 585 List identifiers = InodeFactory.getChildrenClass(cmsTemplate, Identifier.class); 587 Iterator i = identifiers.iterator(); 588 589 while (i.hasNext()) { 590 Identifier ident = (Identifier) i.next(); 591 Container c = (Container) IdentifierFactory.getWorkingChildOfClass(ident, Container.class); 592 context.put(String.valueOf("container" + ident.getInode()), "/working/" + ident.getInode() + "." 593 + Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION")); 594 595 boolean hasWritePermOverContainer = PermissionFactory.doesUserHavePermission(c, com.dotmarketing.util.Config 597 .getIntProperty("PERMISSION_WRITE"), user); 598 context.put("EDIT_CONTAINER_PERMISSION" + ident.getInode(), new Boolean (hasWritePermOverContainer)); 599 600 Structure st = (Structure) InodeFactory.getParentOfClass(c, Structure.class); 602 boolean hasWritePermOverTheStructure = PermissionFactory.doesUserHavePermission(st, com.dotmarketing.util.Config 603 .getIntProperty("PERMISSION_WRITE"), user); 604 context.put("ADD_CONTENT_PERMISSION" + ident.getInode(), new Boolean (hasWritePermOverTheStructure)); 605 606 Logger.debug(VelocityServlet.class, String.valueOf("container" + ident.getInode()) + "=/working/" + ident.getInode() + "." 607 + Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION")); 608 609 String sort = (c.getSortContentletsBy() == null) ? "tree_order" : c.getSortContentletsBy(); 610 611 List contentlets = null; 612 613 boolean staticContainer = !UtilMethods.isSet(c.getLuceneQuery()); 614 615 if (request.getParameter("mainFrame") != null) { 617 if (staticContainer) { 618 LogFactory.getLog(VelocityServlet.class).debug("Static Container!!!!"); 619 620 LogFactory.getLog(VelocityServlet.class).debug("html=" + htmlPage.getInode() + " container=" + c.getInode()); 621 622 Identifier idenHtmlPage = IdentifierCache.getIdentifierByInodeFromCache(htmlPage); 624 Identifier idenContainer = IdentifierCache.getIdentifierByInodeFromCache(c); 625 contentlets = com.dotmarketing.portlets.contentlet.factories.ContentletFactory.getContentletsByOrderAndParents(idenHtmlPage 626 .getInode(), idenContainer.getInode(), sort, true, (String ) request.getSession().getAttribute( 627 com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE)); 628 LogFactory.getLog(VelocityServlet.class).debug( 629 "Getting contentlets for language=" 630 + (String ) request.getSession().getAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE) + " contentlets =" 631 + contentlets.size()); 632 633 } 634 635 ArrayList <String > contentletList = new ArrayList <String >(); 636 637 if (contentlets != null) { 638 Iterator iter = contentlets.iterator(); 639 int count = 0; 640 641 while (iter.hasNext() && (count < c.getMaxContentlets())) { 642 count++; 643 644 Contentlet contentlet = (Contentlet) iter.next(); 645 Identifier contentletIdentifier = IdentifierCache.getIdentifierByInodeFromCache(contentlet); 646 647 boolean hasWritePermOverContentlet = PermissionFactory.doesUserHavePermission(contentlet, com.dotmarketing.util.Config 648 .getIntProperty("PERMISSION_WRITE"), user); 649 650 context.put("EDIT_CONTENT_PERMISSION" + contentletIdentifier.getInode(), new Boolean (hasWritePermOverContentlet)); 651 652 contentletList.add("/working/" + contentletIdentifier.getInode() + "_" + contentlet.getLanguageId() + "." 653 + Config.getStringProperty("VELOCITY_CONTENT_EXTENSION")); 654 655 Logger.debug(this, "Adding contentlet=/working/" + contentletIdentifier.getInode() + "_" + contentlet.getLanguageId() + "." 656 + Config.getStringProperty("VELOCITY_CONTENT_EXTENSION")); 657 } 658 } 659 660 context.put("contentletList" + ident.getInode(), contentletList); 663 context.put("totalSize" + ident.getInode(), new Integer (contentletList.size())); 664 } 665 } 666 667 LogFactory.getLog(VelocityServlet.class).debug( 668 "Before finding template: /working/" + templateIdentifier.getInode() + "." + Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION")); 669 670 LogFactory.getLog(VelocityServlet.class).debug("Velocity directory:" + Velocity.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH)); 671 672 if (request.getParameter("leftMenu") != null) { 673 676 677 List <String > list = new ArrayList <String >(); 678 if (SessionMessages.contains(request, "message")) { 679 list.add((String ) SessionMessages.get(request, "message")); 680 SessionMessages.clear(request); 681 } 682 if (SessionMessages.contains(request, "custommessage")) { 683 list.add((String ) SessionMessages.get(request, "custommessage")); 684 SessionMessages.clear(request); 685 } 686 687 if (list.size() > 0) { 688 ArrayList <String > mymessages = new ArrayList <String >(); 689 Iterator it = list.iterator(); 690 691 while (it.hasNext()) { 692 try { 693 String message = (String ) it.next(); 694 Company comp = PublicCompanyFactory.getDefaultCompany(); 695 mymessages.add(LanguageUtil.get(comp.getCompanyId(), user.getLocale(), message)); 696 } catch (Exception e) { 697 } 698 } 699 context.put("vmessages", mymessages); 700 } 701 702 template = Velocity.getTemplate("/preview_left_menu.vl"); 703 } else if (request.getParameter("mainFrame") != null) { 704 template = Velocity.getTemplate("/working/" + templateIdentifier.getInode() + "." 705 + Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION")); 706 } else { 707 template = Velocity.getTemplate("/preview_mode.vl"); 708 } 709 710 template.merge(context, out); 711 } 712 713 public void getContext(Context context, HTMLPage htmlPage, long cmsTemplateInode, String idURI, HttpServletRequest request, boolean ADMIN_MODE, 714 boolean EDIT_MODE, Host host) { 715 716 if (htmlPage != null) { 718 context.put("HTMLPAGE_INODE", String.valueOf(htmlPage.getInode())); 719 context.put("HTMLPAGE_TITLE", htmlPage.getTitle()); 720 context.put("HTMLPAGE_META", htmlPage.getMetadata()); 721 context.put("HTMLPAGE_SECURE", String.valueOf(htmlPage.isHttpsRequired())); 722 context.put("HTMLPAGE_REDIRECT", htmlPage.getRedirect()); 723 context.put("pageTitle", htmlPage.getTitle()); 724 try { 725 context.put("htmlPageInode", new Long (htmlPage.getInode())); 726 727 String view = java.net.URLEncoder.encode("(working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" 729 + com.dotmarketing.db.DbConnectionFactory.getDBFalse() + "and language_id = " 730 + (String ) request.getSession().getAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE) + ")", "UTF-8"); 731 context.put("view", view); 732 } catch (Exception e) { 733 Logger.warn(this, e.toString(), e); 734 } 735 } 736 737 context.put("HTMLPAGE_SERVER_NAME", request.getServerName()); 738 context.put("VTLSERVLET_URI", idURI); 739 if (request.getQueryString() != null && request.getQueryString().length() > 0) { 740 context.put("queryString", request.getQueryString()); 741 } else { 742 context.put("queryString", ""); 743 } 744 context.put("TEMPLATE_INODE", String.valueOf(cmsTemplateInode)); 745 context.put("UtilMethods", utilMethods); 746 context.put("PortletURLUtil", portletURLUtil); 747 context.put("mainFrame", request.getParameter("mainFrame")); 748 context.put("previewFrame", request.getParameter("previewFrame")); 749 750 if (EDIT_MODE) { 751 com.liferay.portal.model.User user = null; 753 754 try { 755 user = com.liferay.portal.util.PortalUtil.getUser(request); 756 Skin skin = user.getSkin(); 757 context.put("USER_SKIN", skin.getSkinId()); 758 } catch (Exception nsue) { 759 Logger.warn(this, "Exception trying yo getUser: " + nsue.getMessage(), nsue); 760 } 761 762 boolean permission = PermissionFactory.doesUserHavePermission(htmlPage, 764 com.dotmarketing.util.Config.getIntProperty("PERMISSION_PUBLISH"), user); 765 context.put("permission", new Boolean (permission)); 766 767 boolean adminUser = false; 769 try { 770 Company company = null; 771 company = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany(); 772 773 String adminRoleId = ""; 774 try { 775 Role adminRole = RoleLocalManagerUtil.getRoleByName(company.getCompanyId(), Config.getStringProperty("CMS_ADMINISTRATOR_ROLE")); 776 adminRoleId = adminRole.getRoleId(); 777 } catch (Exception e) { 778 } 779 780 Role[] userRoles = (Role[]) RoleFactory.getAllRolesForUser(user.getUserId()).toArray(new Role[0]); 781 for (int i = 0; i < userRoles.length; i++) { 782 Role userRole = (Role) userRoles[i]; 783 if (userRole.getRoleId().equals(adminRoleId)) { 784 adminUser = true; 785 } 786 } 787 } catch (Exception e) { 788 } 789 context.put("cmsAdminUser", new Boolean (adminUser)); 790 791 } 792 793 String pageChannel = UtilMethods.getPageChannel(idURI); 795 context.put("pageChannel", pageChannel); 796 context.put("EDIT_MODE", new Boolean (EDIT_MODE)); 797 context.put("ADMIN_MODE", new Boolean (ADMIN_MODE)); 798 799 context.put("TEMPLATE_LIVE_CONTENT", new Boolean (false)); 802 context.put("CONTAINER_LIVE_CONTENT", new Boolean (false)); 803 context.put("CONTENTLET_LIVE_CONTENT", new Boolean (false)); 804 805 String livePage = (EDIT_MODE && ADMIN_MODE) ? "0" : "1"; 807 808 LogFactory.getLog(VelocityServlet.class).debug("livePage=" + livePage); 809 810 context.put("livePage", livePage); 811 812 context.put("language", (String ) request.getSession().getAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE)); 813 814 if (ADMIN_MODE) { 815 816 com.liferay.portal.model.User user = null; 818 819 try { 820 user = com.liferay.portal.util.PortalUtil.getUser(request); 821 Skin skin = user.getSkin(); 822 context.put("USER_SKIN", skin.getSkinId()); 823 } catch (Exception nsue) { 824 Logger.warn(this, "Exception trying yo getUser: " + nsue.getMessage(), nsue); 825 } 826 827 HttpSession session = request.getSession(); 828 context.put("directorURL", session.getAttribute(com.dotmarketing.util.WebKeys.DIRECTOR_URL)); 829 context.put("viewFoldersURL", session.getAttribute(com.dotmarketing.util.WebKeys.VIEW_FOLDERS_URL)); 830 context.put("previewPageURL", session.getAttribute(com.dotmarketing.util.WebKeys.PREVIEW_PAGE_URL)); 831 context.put("viewContentsURL", session.getAttribute(com.dotmarketing.util.WebKeys.VIEW_CONTENTS_URL)); 832 context.put("viewBrowserURL", session.getAttribute(com.dotmarketing.util.WebKeys.VIEW_BROWSER_URL)); 833 834 java.util.List workflowTasksList = WorkflowsFactory.getOpenedWorkflowTasksAssociatedWithAsset(htmlPage, user); 835 context.put("workflowTasksList", workflowTasksList); 836 837 } 838 839 context.put("host", host); 840 841 } 842 843 private Context createContext(HttpServletRequest request, HttpServletResponse response) { 844 ChainedContext context = new ChainedContext((Context ) null, request, response, myContext); 845 846 LogFactory.getLog(VelocityServlet.class).debug("ChainedContext=" + context); 847 851 context.setToolbox(toolboxManager.getToolboxContext(context)); 852 853 context.put("quote", "\""); 855 context.put("pounds", "##"); 856 context.put("return", "\n"); 857 858 context.put("UtilMethods", utilMethods); 859 context.put("PortletURLUtil", portletURLUtil); 860 context.put("languages", languages); 863 context.put("language", (String ) request.getSession().getAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE)); 864 865 Host host = HostFactory.getCurrentHost(request); 866 context.put("host", host); 867 com.liferay.portal.model.User user = null; 868 869 try { 870 user = (com.liferay.portal.model.User) request.getSession().getAttribute(com.dotmarketing.util.WebKeys.CMS_USER); 872 context.put("user", user); 873 } catch (Exception nsue) { 874 nsue.printStackTrace(); 875 } 876 877 _setClientVariablesOnContext(request, context); 878 879 return context; 880 881 } 882 883 protected abstract void _setClientVariablesOnContext(HttpServletRequest request, ChainedContext context); 887 888 } | Popular Tags |