1 31 32 package com.opencms.legacy; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsRequestContext; 38 import org.opencms.file.CmsResource; 39 import org.opencms.flex.CmsFlexController; 40 import org.opencms.importexport.CmsCompatibleCheck; 41 import org.opencms.jsp.CmsJspTagInclude; 42 import org.opencms.loader.I_CmsLoaderIncludeExtension; 43 import org.opencms.loader.I_CmsResourceLoader; 44 import org.opencms.main.CmsEvent; 45 import org.opencms.main.CmsException; 46 import org.opencms.main.CmsLog; 47 import org.opencms.main.I_CmsEventListener; 48 import org.opencms.main.OpenCms; 49 import org.opencms.staticexport.CmsLinkManager; 50 import org.opencms.workplace.CmsWorkplace; 51 import org.opencms.workplace.editors.CmsDefaultPageEditor; 52 53 import com.opencms.core.*; 54 import com.opencms.template.*; 55 import com.opencms.template.cache.*; 56 57 import java.io.IOException ; 58 import java.io.OutputStream ; 59 import java.util.Collections ; 60 import java.util.Enumeration ; 61 import java.util.Hashtable ; 62 import java.util.Locale ; 63 import java.util.Map ; 64 import java.util.TreeMap ; 65 66 import javax.servlet.ServletRequest ; 67 import javax.servlet.ServletResponse ; 68 import javax.servlet.http.HttpServletRequest ; 69 import javax.servlet.http.HttpServletResponse ; 70 import javax.servlet.http.HttpSession ; 71 72 import org.apache.commons.collections.ExtendedProperties; 73 74 99 public class CmsXmlTemplateLoader implements I_CmsResourceLoader, I_CmsLoaderIncludeExtension, I_CmsEventListener { 100 101 102 public static final String C_BODYLOADER_URI = CmsWorkplace.VFS_PATH_SYSTEM + "shared/bodyloader.html"; 103 104 105 public static final String C_ELEMENT_REPLACE = "_CMS_ELEMENTREPLACE"; 106 107 108 public static final int C_RESOURCE_LOADER_ID = 3; 109 110 111 private static final int DEBUG = 0; 112 113 114 private static CmsElementCache m_elementCache; 115 116 117 private static I_CmsTemplateCache m_templateCache; 118 119 120 private static Hashtable m_variantDeps; 121 122 123 private Map m_configuration; 124 125 128 public CmsXmlTemplateLoader() { 129 130 OpenCms.addCmsEventListener(this); 131 m_templateCache = new CmsTemplateCache(); 132 m_configuration = new TreeMap (); 133 } 134 135 141 public static final CmsElementCache getElementCache() { 142 143 return m_elementCache; 144 } 145 146 151 public static final CmsElementCache getOnlineElementCache() { 152 153 return m_elementCache; 154 } 155 156 163 public static I_CmsRequest getRequest(CmsRequestContext context) { 164 165 return (I_CmsRequest)context.getAttribute(I_CmsRequest.C_CMS_REQUEST); 166 } 167 168 175 public static I_CmsResponse getResponse(CmsRequestContext context) { 176 177 return (I_CmsResponse)context.getAttribute(I_CmsResponse.C_CMS_RESPONSE); 178 } 179 180 188 public static I_CmsSession getSession(CmsRequestContext context, boolean value) { 189 190 I_CmsRequest req = (I_CmsRequest)context.getAttribute(I_CmsRequest.C_CMS_REQUEST); 191 HttpSession session = req.getOriginalRequest().getSession(value); 192 if (session != null) { 193 return new CmsSession(session); 194 } else { 195 return null; 196 } 197 } 198 199 204 public static final Hashtable getVariantDependencies() { 205 206 return m_variantDeps; 207 } 208 209 217 public static void initLegacyRequest(CmsObject cms, HttpServletRequest req, HttpServletResponse res) 218 throws CmsException { 219 220 if (cms.getRequestContext().getAttribute(I_CmsRequest.C_CMS_REQUEST) != null) { 221 return; 222 } 223 try { 224 CmsRequestHttpServlet cmsReq = new CmsRequestHttpServlet(req, cms.getRequestContext().getFileTranslator()); 225 CmsResponseHttpServlet cmsRes = new CmsResponseHttpServlet(req, res); 226 cms.getRequestContext().setAttribute(I_CmsRequest.C_CMS_REQUEST, cmsReq); 227 cms.getRequestContext().setAttribute(I_CmsResponse.C_CMS_RESPONSE, cmsRes); 228 } catch (IOException e) { 229 throw new CmsLegacyException("Trouble setting up legacy request / response", e); 230 } 231 } 232 233 238 public static final boolean isElementCacheEnabled() { 239 240 return m_elementCache != null; 241 } 242 243 250 private static void clearLoaderCache(boolean clearFiles, boolean clearTemplates) { 251 252 if (clearFiles) { 253 A_CmsXmlContent.clearFileCache(); 254 } 255 if (clearTemplates) { 256 m_templateCache.clearCache(); 257 } 258 } 259 260 263 public void addConfigurationParameter(String paramName, String paramValue) { 264 265 m_configuration.put(paramName, paramValue); 266 } 267 268 275 public synchronized void cmsEvent(CmsEvent event) { 276 277 switch (event.getType()) { 278 279 case I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR: 280 clearLoaderCache(true, true); 281 break; 282 case I_CmsEventListener.EVENT_CLEAR_ONLINE_CACHES: 283 clearLoaderCache(true, true); 284 break; 285 case I_CmsEventListener.EVENT_CLEAR_OFFLINE_CACHES: 286 clearLoaderCache(true, true); 287 break; 288 case I_CmsEventListener.EVENT_CLEAR_CACHES: 289 clearLoaderCache(true, true); 290 break; 291 default: 292 } 294 } 295 296 299 public void destroy() { 300 301 } 303 304 307 public byte[] dump( 308 CmsObject cms, 309 CmsResource file, 310 String element, 311 Locale locale, 312 HttpServletRequest req, 313 HttpServletResponse res) throws CmsException { 314 315 initLegacyRequest(cms, req, res); 316 String absolutePath = cms.getSitePath(file); 317 CmsXmlTemplate template = new CmsXmlTemplate(); 319 return template.getContent(cms, absolutePath, element, null); 321 } 322 323 326 public byte[] export(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 327 throws IOException , CmsException { 328 329 CmsFile file = CmsFile.upgrade(resource, cms); 330 initLegacyRequest(cms, req, res); 331 CmsRequestHttpServlet cmsReq = new CmsRequestHttpServlet(req, cms.getRequestContext().getFileTranslator()); 332 return generateOutput(cms, file, cmsReq); 333 } 334 335 338 public Map getConfiguration() { 339 340 return Collections.unmodifiableMap(m_configuration); 342 } 343 344 347 public int getLoaderId() { 348 349 return C_RESOURCE_LOADER_ID; 350 } 351 352 358 public String getResourceLoaderInfo() { 359 360 return "The OpenCms default resource loader for XMLTemplates"; 361 } 362 363 366 public String includeExtension( 367 String target, 368 String element, 369 boolean editable, 370 Map parameterMap, 371 ServletRequest req, 372 ServletResponse res) throws CmsException { 373 374 CmsFlexController controller = CmsFlexController.getController(req); 376 if (controller == null) { 378 return target; 379 } 380 if (element != null) { 382 if (!("body".equals(element) || "(default)".equals(element))) { 383 CmsJspTagInclude.addParameter(parameterMap, CmsXmlTemplate.C_FRAME_SELECTOR, element, true); 385 } 386 } 387 boolean isPageTarget; 388 try { 389 CmsResource targetResource = controller.getCmsObject().readResource(target); 391 isPageTarget = ((CmsResourceTypePage.getStaticTypeId() == targetResource.getTypeId())); 392 } catch (CmsException e) { 393 controller.setThrowable(e, target); 394 throw new CmsLegacyException("File not found: " + target, e); 395 } 396 String bodyAttribute = (String )controller.getCmsObject().getRequestContext().getAttribute( 397 CmsDefaultPageEditor.XML_BODY_ELEMENT); 398 if (bodyAttribute == null) { 399 if (isPageTarget) { 401 if (!target.startsWith(CmsCompatibleCheck.VFS_PATH_BODIES)) { 403 target = CmsCompatibleCheck.VFS_PATH_BODIES + target.substring(1); 404 } 405 CmsJspTagInclude.addParameter( 407 parameterMap, 408 CmsXmlTemplateLoader.C_ELEMENT_REPLACE, 409 "body:" + target, 410 true); 411 target = C_BODYLOADER_URI; 412 } 413 } else { 414 if (target.equals(controller.getCmsObject().getRequestContext().getUri())) { 416 CmsJspTagInclude.addParameter(parameterMap, CmsXmlTemplateLoader.C_ELEMENT_REPLACE, "body:" 418 + bodyAttribute, true); 419 target = C_BODYLOADER_URI; 421 } else { 422 if (isPageTarget) { 423 if (isPageTarget && !target.startsWith(CmsCompatibleCheck.VFS_PATH_BODIES)) { 425 target = CmsCompatibleCheck.VFS_PATH_BODIES + target.substring(1); 426 } 427 CmsJspTagInclude.addParameter(parameterMap, CmsXmlTemplateLoader.C_ELEMENT_REPLACE, "body:" 429 + target, true); 430 target = C_BODYLOADER_URI; 431 } 432 } 433 } 434 435 return target; 436 } 437 438 441 public void initConfiguration() { 442 443 ExtendedProperties config = new ExtendedProperties(); 444 config.putAll(m_configuration); 445 446 boolean elementCacheEnabled = config.getBoolean("elementcache.enabled", false); 448 if (CmsLog.INIT.isInfoEnabled()) { 449 CmsLog.INIT.info(". Loader init : XMLTemplate element cache " 450 + (elementCacheEnabled ? "enabled" : "disabled")); 451 } 452 if (elementCacheEnabled) { 453 try { 454 m_elementCache = new CmsElementCache(config.getInteger("elementcache.uri", 10000), config.getInteger( 455 "elementcache.elements", 456 50000), config.getInteger("elementcache.variants", 100)); 457 } catch (Exception e) { 458 if (CmsLog.INIT.isWarnEnabled()) { 459 CmsLog.INIT.warn(". Loader init : XMLTemplate element cache non-critical error " 460 + e.toString()); 461 } 462 } 463 m_variantDeps = new Hashtable (); 464 m_elementCache.getElementLocator().setExternDependencies(m_variantDeps); 465 } else { 466 m_elementCache = null; 467 } 468 469 if (CmsLog.INIT.isInfoEnabled()) { 470 CmsLog.INIT.info(". Loader init : " + this.getClass().getName() + " initialized"); 471 } 472 } 473 474 477 public boolean isStaticExportEnabled() { 478 479 return true; 480 } 481 482 485 public boolean isStaticExportProcessable() { 486 487 return true; 488 } 489 490 493 public boolean isUsableForTemplates() { 494 495 return true; 496 } 497 498 501 public boolean isUsingUriWhenLoadingTemplate() { 502 503 return true; 504 } 505 506 509 public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 510 throws CmsException { 511 512 initLegacyRequest(cms, req, res); 513 processXmlTemplate(cms, CmsFile.upgrade(resource, cms)); 514 } 515 516 519 public void service(CmsObject cms, CmsResource file, ServletRequest req, ServletResponse res) 520 throws CmsException, IOException { 521 522 long timer1 = 0; 523 if (DEBUG > 0) { 524 timer1 = System.currentTimeMillis(); 525 System.err.println("============ CmsXmlTemplateLoader loading: " + cms.getSitePath(file)); 526 System.err.println("CmsXmlTemplateLoader.service() cms uri is: " + cms.getRequestContext().getUri()); 527 } 528 String rnc = cms.getRequestContext().getEncoding().trim(); 530 532 initLegacyRequest(cms, (HttpServletRequest )req, (HttpServletResponse )res); 533 I_CmsRequest cms_req = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()); 534 HttpServletRequest originalreq = cms_req.getOriginalRequest(); 535 536 try { 537 byte[] result = null; 539 org.opencms.file.CmsFile fx = cms.readFile(cms.getSitePath(file)); 540 String dnc = OpenCms.getSystemInfo().getDefaultEncoding().trim(); 542 String enc = cms.readPropertyObject( 543 cms.getSitePath(fx), 544 CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, 545 true).getValue(dnc).trim(); 546 cms_req.setOriginalRequest((HttpServletRequest )req); 548 cms.getRequestContext().setEncoding(enc); 549 if (DEBUG > 1) { 550 System.err.println("CmsXmlTemplateLoader.service(): Encodig set to " 551 + cms.getRequestContext().getEncoding()); 552 System.err.println("CmsXmlTemplateLoader.service(): Uri set to " + cms.getRequestContext().getUri()); 553 } 554 result = generateOutput(cms, fx, cms_req); 556 if (result != null) { 558 if (DEBUG > 1) { 559 System.err.println("CmsXmlTemplateLoader.service(): encoding=" 560 + enc 561 + " requestEncoding=" 562 + rnc 563 + " defaultEncoding=" 564 + dnc); 565 } 566 res.getOutputStream().write(result); 567 } 568 } finally { 569 cms_req.setOriginalRequest(originalreq); 571 cms.getRequestContext().setEncoding(rnc); 572 if (DEBUG > 1) { 574 System.err.println("CmsXmlTemplateLoader.service(): Encodig reset to " 575 + cms.getRequestContext().getEncoding()); 576 System.err.println("CmsXmlTemplateLoader.service(): Uri reset to " + cms.getRequestContext().getUri()); 577 } 578 } 579 580 if (DEBUG > 0) { 581 long timer2 = System.currentTimeMillis() - timer1; 582 System.err.println("============ CmsXmlTemplateLoader time delivering XmlTemplate for " 583 + cms.getSitePath(file) 584 + ": " 585 + timer2 586 + "ms"); 587 } 588 } 589 590 600 protected byte[] generateOutput(CmsObject cms, CmsFile file, I_CmsRequest req) throws CmsException { 601 602 byte[] output = null; 603 604 Hashtable newParameters = new Hashtable (); 606 String uri = cms.getRequestContext().getUri(); 607 608 String absolutePath = cms.getSitePath(file); 610 if (CmsLog.getLog(this).isDebugEnabled()) { 611 CmsLog.getLog(this).debug("absolutePath=" + absolutePath); 612 } 613 String templateProp = cms.readPropertyObject(absolutePath, CmsPropertyDefinition.PROPERTY_TEMPLATE, false).getValue(); 614 if (CmsLog.getLog(this).isDebugEnabled()) { 615 CmsLog.getLog(this).debug("templateProp=" + templateProp); 616 } 617 String templateClassProp = cms.readPropertyObject( 618 absolutePath, 619 org.opencms.file.CmsPropertyDefinition.PROPERTY_BODY_CLASS, 620 false).getValue(org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS); 621 if (CmsLog.getLog(this).isDebugEnabled()) { 622 CmsLog.getLog(this).debug("templateClassProp=" + templateClassProp); 623 } 624 625 String xmlTemplateContent = null; 627 if (templateProp != null) { 628 StringBuffer buf = new StringBuffer (256); 630 buf.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); 631 buf.append("<PAGE>\n<class>"); 632 buf.append(org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS); 633 buf.append("</class>\n<masterTemplate>"); 634 buf.append(templateProp); 636 buf.append("</masterTemplate>\n<ELEMENTDEF name=\"body\">\n<CLASS>"); 637 buf.append(templateClassProp); 638 buf.append("</CLASS>\n<TEMPLATE>"); 639 buf.append(uri); 641 buf.append("</TEMPLATE>\n</ELEMENTDEF>\n</PAGE>\n"); 642 xmlTemplateContent = buf.toString(); 644 uri += com.opencms.core.I_CmsConstants.C_XML_CONTROL_FILE_SUFFIX; 645 } 646 647 boolean elementCacheEnabled = CmsXmlTemplateLoader.isElementCacheEnabled(); 649 CmsElementCache elementCache = null; 650 CmsUriDescriptor uriDesc = null; 651 CmsUri cmsUri = null; 652 653 String templateClass = null; 654 String templateName = null; 655 CmsXmlControlFile doc = null; 656 657 if (elementCacheEnabled) { 658 elementCache = CmsXmlTemplateLoader.getElementCache(); 660 661 uriDesc = new CmsUriDescriptor(uri); 663 cmsUri = elementCache.getUriLocator().get(uriDesc); 664 if (CmsLog.getLog(this).isDebugEnabled()) { 666 CmsLog.getLog(this).debug("found cmsUri=" + cmsUri); 667 } 668 if ((cmsUri != null) && !cmsUri.getElementDescriptor().getTemplateName().equalsIgnoreCase(templateProp)) { 669 if (CmsLog.getLog(this).isDebugEnabled()) { 670 CmsLog.getLog(this).debug( 671 "cmsUri has different template: " 672 + cmsUri.getElementDescriptor().getTemplateName() 673 + " than current template: " 674 + templateProp 675 + ", not using cmsUri from cache"); 676 } 677 cmsUri = null; 678 } 679 } 680 681 String replace = req.getParameter(C_ELEMENT_REPLACE); 683 boolean elementreplace = false; 684 CmsElementDefinition replaceDef = null; 685 if (replace != null) { 686 int index = replace.indexOf(":"); 687 if (index != -1) { 688 elementreplace = true; 689 cmsUri = null; 690 String elementName = replace.substring(0, index); 691 String replaceUri = replace.substring(index + 1); 692 replaceDef = new CmsElementDefinition( 693 elementName, 694 org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS, 695 replaceUri, 696 null, 697 new Hashtable ()); 698 newParameters.put(C_ELEMENT_REPLACE + "_VFS_" + elementName, cms.getRequestContext().addSiteRoot( 699 replaceUri)); 700 } 701 } 702 703 if ((cmsUri == null) || !elementCacheEnabled) { 704 708 try { 710 if (xmlTemplateContent == null) { 711 doc = new CmsXmlControlFile(cms, file); 712 } else { 713 doc = new CmsXmlControlFile(cms, uri, xmlTemplateContent); 714 } 715 } catch (Exception e) { 716 handleException(cms, e, "There was an error while parsing XML page file " + cms.getSitePath(file)); 719 return "".getBytes(); 720 } 721 722 if (!elementCacheEnabled && (replaceDef != null)) { 723 doc.setElementClass(replaceDef.getName(), replaceDef.getClassName()); 725 doc.setElementTemplate(replaceDef.getName(), replaceDef.getTemplateName()); 726 } 727 728 templateClass = doc.getTemplateClass(); 732 if (templateClass == null || "".equals(templateClass)) { 733 templateClass = this.getClass().getName(); 734 } 735 if (templateClass == null || "".equals(templateClass)) { 736 templateClass = org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS; 737 } 738 templateName = doc.getMasterTemplate(); 739 if (templateName != null && !"".equals(templateName)) { 740 templateName = CmsLinkManager.getAbsoluteUri(templateName, cms.getSitePath(file)); 741 } 742 743 747 Enumeration masterTemplateParams = doc.getParameterNames(); 750 while (masterTemplateParams.hasMoreElements()) { 751 String paramName = (String )masterTemplateParams.nextElement(); 752 String paramValue = doc.getParameter(paramName); 753 newParameters.put(com.opencms.core.I_CmsConstants.C_ROOT_TEMPLATE_NAME + "." + paramName, paramValue); 754 } 755 756 Enumeration elementDefinitions = doc.getElementDefinitions(); 758 while (elementDefinitions.hasMoreElements()) { 759 String elementName = (String )elementDefinitions.nextElement(); 760 if (doc.isElementClassDefined(elementName)) { 761 newParameters.put(elementName + "._CLASS_", doc.getElementClass(elementName)); 762 } 763 if (doc.isElementTemplateDefined(elementName)) { 764 String template = doc.getElementTemplate(elementName); 767 if (xmlTemplateContent == null) { 768 template = doc.validateBodyPath(cms, template, file); 769 } 770 if (CmsDefaultPageEditor.XML_BODY_ELEMENT.equalsIgnoreCase(elementName)) { 771 if (template != null) { 773 cms.getRequestContext().setAttribute(CmsDefaultPageEditor.XML_BODY_ELEMENT, template); 774 } 775 } 776 newParameters.put(elementName + "._TEMPLATE_", template); 777 } 778 if (doc.isElementTemplSelectorDefined(elementName)) { 779 newParameters.put(elementName + "._TEMPLATESELECTOR_", doc.getElementTemplSelector(elementName)); 780 } 781 Enumeration parameters = doc.getElementParameterNames(elementName); 782 while (parameters.hasMoreElements()) { 783 String paramName = (String )parameters.nextElement(); 784 String paramValue = doc.getElementParameter(elementName, paramName); 785 if (paramValue != null) { 786 newParameters.put(elementName + "." + paramName, paramValue); 787 } else { 788 if (CmsLog.getLog(this).isInfoEnabled()) { 789 CmsLog.getLog(this).info("Empty parameter \"" + paramName + "\" found."); 790 } 791 } 792 } 793 } 794 } 795 796 800 String datafor = req.getParameter("datafor"); 801 if (datafor == null) { 802 datafor = ""; 803 } else { 804 if (!"".equals(datafor)) { 805 datafor = datafor + "."; 806 } 807 } 808 Enumeration urlParameterNames = req.getParameterNames(); 809 while (urlParameterNames.hasMoreElements()) { 810 String pname = (String )urlParameterNames.nextElement(); 811 String paramValue = req.getParameter(pname); 812 if (paramValue != null) { 813 if ((!"datafor".equals(pname)) && (!"_clearcache".equals(pname))) { 814 newParameters.put(datafor + pname, paramValue); 815 } 816 } else { 817 if (CmsLog.getLog(this).isInfoEnabled()) { 818 CmsLog.getLog(this).info("Empty URL parameter \"" + pname + "\" found."); 819 } 820 } 821 } 822 823 if (elementCacheEnabled && (cmsUri == null)) { 824 CmsElementDescriptor elemDesc = new CmsElementDescriptor(templateClass, templateName); 828 CmsElementDefinitionCollection eldefs = doc.getElementDefinitionCollection(); 829 if (elementreplace) { 830 eldefs.add(replaceDef); 832 cmsUri = new CmsUri(elemDesc, eldefs); 833 } else { 834 cmsUri = new CmsUri(elemDesc, eldefs); 835 elementCache.getUriLocator().put(uriDesc, cmsUri); 836 } 837 } 838 839 if (elementCacheEnabled) { 840 clearLoaderCache(true, true); 842 843 if (elementreplace) { 845 output = cmsUri.callCanonicalRoot(elementCache, cms, newParameters); 846 } else { 847 output = elementCache.callCanonicalRoot(cms, newParameters, uri); 848 } 849 } else { 850 try { 853 CmsFile masterTemplate = loadMasterTemplateFile(cms, templateName, doc); 854 I_CmsTemplate tmpl = getTemplateClass(templateClass); 855 if (!(tmpl instanceof I_CmsXmlTemplate)) { 856 String errorMessage = "Error in " 857 + cms.getSitePath(file) 858 + ": " 859 + templateClass 860 + " is not a XML template class."; 861 if (CmsLog.getLog(this).isErrorEnabled()) { 862 CmsLog.getLog(this).error(errorMessage); 863 } 864 throw new CmsLegacyException(errorMessage, CmsLegacyException.C_XML_WRONG_TEMPLATE_CLASS); 865 } 866 clearLoaderCache(true, true); 868 output = callCanonicalRoot(cms, tmpl, masterTemplate, newParameters); 869 } catch (CmsException e) { 870 if (CmsLog.getLog(this).isWarnEnabled()) { 871 CmsLog.getLog(this); 872 } 873 doc.removeFromFileCache(); 874 throw e; 875 } 876 } 877 return output; 878 } 879 880 894 private byte[] callCanonicalRoot( 895 CmsObject cms, 896 I_CmsTemplate templateClass, 897 CmsFile masterTemplate, 898 Hashtable parameters) throws CmsException { 899 900 try { 901 CmsRootTemplate root = new CmsRootTemplate(); 902 return root.getMasterTemplate(cms, templateClass, masterTemplate, m_templateCache, parameters); 903 } catch (Exception e) { 904 handleException(cms, e, "Received error while calling canonical root for requested file " 906 + masterTemplate.getName() 907 + ". "); 908 } 909 return null; 910 } 911 912 924 private I_CmsTemplate getTemplateClass(String classname) throws CmsException { 925 926 if (CmsLog.getLog(this).isDebugEnabled()) { 927 CmsLog.getLog(this).debug("Getting start template class: " + classname); 928 } 929 Object o = CmsTemplateClassManager.getClassInstance(classname); 930 931 933 if (!(o instanceof I_CmsTemplate)) { 935 String errorMessage = "Class " + classname + " is not an OpenCms template class."; 936 if (CmsLog.getLog(this).isErrorEnabled()) { 937 CmsLog.getLog(this).error(errorMessage); 938 } 939 throw new CmsLegacyException(errorMessage, CmsLegacyException.C_XML_NO_TEMPLATE_CLASS); 940 } 941 I_CmsTemplate cmsTemplate = (I_CmsTemplate)o; 942 if (!cmsTemplate.isTemplateCacheSet()) { 943 cmsTemplate.setTemplateCache(m_templateCache); 944 } 945 return cmsTemplate; 946 } 947 948 965 private void handleException(CmsObject cms, Exception e, String errorText) throws CmsException { 966 967 if (!(e instanceof CmsException)) { 969 if (CmsLog.getLog(this).isErrorEnabled()) { 970 CmsLog.getLog(this).error(errorText, e); 971 } 972 } 973 974 CmsRequestContext reqContext = cms.getRequestContext(); 977 if ((DEBUG == 0) 978 && reqContext.currentUser().isGuestUser() 979 && (!(e instanceof CmsLegacyException && ((e instanceof CmsLegacyException) && (((CmsLegacyException)e).getType() == CmsLegacyException.C_NO_USER))))) { 980 throw new CmsLegacyException(errorText, CmsLegacyException.C_SERVICE_UNAVAILABLE, e); 981 } else { 982 if (e instanceof CmsException) { 983 throw (CmsException)e; 984 } else { 985 throw new CmsLegacyException(errorText, CmsLegacyException.C_LOADER_GENERIC_ERROR, e); 986 } 987 } 988 } 989 990 998 private CmsFile loadMasterTemplateFile( 999 CmsObject cms, 1000 String templateName, 1001 com.opencms.template.CmsXmlControlFile doc) throws CmsException { 1002 1003 CmsFile masterTemplate = null; 1004 try { 1005 masterTemplate = cms.readFile(templateName); 1006 } catch (Exception e) { 1007 handleException(cms, e, "Cannot load master template " + templateName + ". "); 1008 doc.removeFromFileCache(); 1009 } 1010 return masterTemplate; 1011 } 1012 1013 1022 private void processXmlTemplate(CmsObject cms, CmsFile file) throws CmsException { 1023 1024 if ((DEBUG > 0) && CmsLog.getLog(this).isDebugEnabled()) { 1026 CmsLog.getLog(this).debug("Loader started for " + file.getName()); 1027 } 1028 1029 String errorMessage = null; 1031 if (file == null) { 1032 errorMessage = "CmsFile missing"; 1033 } 1034 if (cms == null) { 1035 errorMessage = "CmsObject missing"; 1036 } 1037 if (errorMessage != null) { 1038 if (CmsLog.getLog(this).isErrorEnabled()) { 1039 CmsLog.getLog(this).error(errorMessage); 1040 } 1041 throw new CmsLegacyException(errorMessage, CmsLegacyException.C_LOADER_GENERIC_ERROR); 1042 } 1043 1044 String clearcache = getRequest(cms.getRequestContext()).getParameter("_clearcache"); 1046 1047 clearLoaderCache( 1049 ((clearcache != null) && ("all".equals(clearcache) || "file".equals(clearcache))), 1050 ((clearcache != null) && ("all".equals(clearcache) || "template".equals(clearcache)))); 1051 1052 I_CmsRequest req = getRequest(cms.getRequestContext()); 1054 byte[] result = generateOutput(cms, file, req); 1055 if (result != null) { 1056 writeBytesToResponse(cms, result); 1057 } 1058 } 1059 1060 1067 private void writeBytesToResponse(CmsObject cms, byte[] result) throws CmsException { 1068 1069 try { 1070 I_CmsResponse resp = getResponse(cms.getRequestContext()); 1071 if ((result != null) && !resp.isRedirected()) { 1072 OutputStream out = resp.getOutputStream(); 1075 1076 resp.setContentLength(result.length); 1077 resp.setHeader("Connection", "keep-alive"); 1078 out.write(result); 1079 out.close(); 1080 } 1081 } catch (IOException ioe) { 1082 if (CmsLog.getLog(this).isDebugEnabled()) { 1083 CmsLog.getLog(this).debug( 1084 "IO error while writing to response stream for " + cms.getRequestContext().getUri(), 1085 ioe); 1086 } 1087 } catch (Exception e) { 1088 String errorMessage = "Cannot write output to HTTP response stream"; 1089 handleException(cms, e, errorMessage); 1090 } 1091 } 1092} 1093
| Popular Tags
|