001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.repository; 017 018import java.io.IOException; 019import java.net.MalformedURLException; 020import java.util.HashMap; 021import java.util.Map; 022import java.util.Map.Entry; 023import java.util.Set; 024 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.cocoon.ProcessingException; 028import org.apache.cocoon.components.source.SourceUtil; 029import org.apache.cocoon.environment.ObjectModelHelper; 030import org.apache.cocoon.environment.Request; 031import org.apache.cocoon.generation.ServiceableGenerator; 032import org.apache.cocoon.xml.AttributesImpl; 033import org.apache.cocoon.xml.SaxBuffer; 034import org.apache.cocoon.xml.XMLUtils; 035import org.apache.commons.lang.StringUtils; 036import org.apache.commons.lang.exception.ExceptionUtils; 037import org.apache.excalibur.source.Source; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040import org.xml.sax.ContentHandler; 041import org.xml.sax.SAXException; 042 043import org.ametys.cms.content.ContentHelper; 044import org.ametys.cms.content.GetContentAction; 045import org.ametys.cms.contenttype.ContentTypeDescriptor; 046import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 047import org.ametys.cms.contenttype.ContentTypesHelper; 048import org.ametys.cms.contenttype.DynamicContentTypeDescriptorExtentionPoint; 049import org.ametys.cms.repository.Content; 050import org.ametys.cms.tag.Tag; 051import org.ametys.cms.tag.TagProviderExtensionPoint; 052import org.ametys.core.ui.ClientSideElement.ScriptFile; 053import org.ametys.core.util.IgnoreRootHandler; 054import org.ametys.core.util.URIUtils; 055import org.ametys.plugins.repository.AmetysObject; 056import org.ametys.plugins.repository.AmetysObjectIterable; 057import org.ametys.plugins.repository.AmetysRepositoryException; 058import org.ametys.plugins.repository.provider.WorkspaceSelector; 059import org.ametys.runtime.authentication.AccessDeniedException; 060import org.ametys.runtime.authentication.AuthorizationRequiredException; 061import org.ametys.runtime.exception.ServiceUnavailableException; 062import org.ametys.web.WebConstants; 063import org.ametys.web.cache.monitoring.Constants; 064import org.ametys.web.cache.monitoring.process.access.ResourceAccessComponent; 065import org.ametys.web.cache.monitoring.process.access.impl.PageElementResourceAccess; 066import org.ametys.web.cache.monitoring.process.access.impl.PageElementResourceAccess.PageElementType; 067import org.ametys.web.cache.monitoring.process.access.impl.PageResourceAccess; 068import org.ametys.web.cache.pageelement.PageElementCache; 069import org.ametys.web.renderingcontext.RenderingContext; 070import org.ametys.web.renderingcontext.RenderingContextHandler; 071import org.ametys.web.repository.content.SharedContent; 072import org.ametys.web.repository.page.ContentTypesAssignmentHandler; 073import org.ametys.web.repository.page.ModifiablePage; 074import org.ametys.web.repository.page.MoveablePage; 075import org.ametys.web.repository.page.Page; 076import org.ametys.web.repository.page.Page.PageType; 077import org.ametys.web.repository.page.ServicesAssignmentHandler; 078import org.ametys.web.repository.page.SitemapElement; 079import org.ametys.web.repository.page.Zone; 080import org.ametys.web.repository.page.ZoneItem; 081import org.ametys.web.repository.page.ZoneItem.ZoneType; 082import org.ametys.web.repository.site.Site; 083import org.ametys.web.service.Service; 084import org.ametys.web.service.ServiceExtensionPoint; 085import org.ametys.web.skin.Skin; 086import org.ametys.web.skin.SkinTemplate; 087import org.ametys.web.skin.SkinTemplateZone; 088import org.ametys.web.skin.SkinsManager; 089 090/** 091 * Generator for SAXing <code>Content</code> associated with a Page.<br> 092 * SAX events are like :<br> 093 * <pageContents><br> 094 * <zone id="..."><br> 095 * <i><!-- XHTML content --></i><br> 096 * </zone><br> 097 * ...<br> 098 * </pageContents/><br> 099 */ 100public class PageGenerator extends ServiceableGenerator 101{ 102 private ServiceExtensionPoint _serviceExtPt; 103 private ContentTypeExtensionPoint _contentTypeExtPt; 104 private SkinsManager _skinsManager; 105 private TagProviderExtensionPoint _tagProviderEP; 106 private PageElementCache _zoneItemCache; 107 private WorkspaceSelector _workspaceSelector; 108 private RenderingContextHandler _renderingContextHandler; 109 private ContentTypesHelper _contentTypeHelper; 110 private DynamicContentTypeDescriptorExtentionPoint _dynamicCTDescriptorEP; 111 112 /** The content type assignment handler. */ 113 private ContentTypesAssignmentHandler _cTypeAssignmentHandler; 114 115 /** The service assignment handler. */ 116 private ServicesAssignmentHandler _serviceAssignmentHandler; 117 118 /** The resource access monitoring component */ 119 private ResourceAccessComponent _resourceAccessMonitor; 120 121 /** The monitored resource access */ 122 private PageResourceAccess _pageAccess; 123 124 private ContentHelper _contentHelper; 125 126 private int _zoneItemsInCache; 127 private int _zoneItemsSaxed; 128 private int _zonesSaxed; 129 130 private Logger _timeLogger = LoggerFactory.getLogger("org.ametys.web.rendering.time"); 131 132 @Override 133 public void service(ServiceManager serviceManager) throws ServiceException 134 { 135 super.service(serviceManager); 136 _serviceExtPt = (ServiceExtensionPoint) serviceManager.lookup(ServiceExtensionPoint.ROLE); 137 _contentTypeExtPt = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE); 138 _skinsManager = (SkinsManager) serviceManager.lookup(SkinsManager.ROLE); 139 _tagProviderEP = (TagProviderExtensionPoint) serviceManager.lookup(TagProviderExtensionPoint.ROLE); 140 _zoneItemCache = (PageElementCache) serviceManager.lookup(PageElementCache.ROLE + "/zoneItem"); 141 _workspaceSelector = (WorkspaceSelector) serviceManager.lookup(WorkspaceSelector.ROLE); 142 _renderingContextHandler = (RenderingContextHandler) serviceManager.lookup(RenderingContextHandler.ROLE); 143 _cTypeAssignmentHandler = (ContentTypesAssignmentHandler) serviceManager.lookup(ContentTypesAssignmentHandler.ROLE); 144 _serviceAssignmentHandler = (ServicesAssignmentHandler) serviceManager.lookup(ServicesAssignmentHandler.ROLE); 145 _resourceAccessMonitor = (ResourceAccessComponent) serviceManager.lookup(ResourceAccessComponent.ROLE); 146 _contentTypeHelper = (ContentTypesHelper) serviceManager.lookup(ContentTypesHelper.ROLE); 147 _dynamicCTDescriptorEP = (DynamicContentTypeDescriptorExtentionPoint) serviceManager.lookup(DynamicContentTypeDescriptorExtentionPoint.ROLE); 148 _contentHelper = (ContentHelper) serviceManager.lookup(ContentHelper.ROLE); 149 } 150 151 @Override 152 public void generate() throws IOException, SAXException, ProcessingException 153 { 154 long t0 = System.currentTimeMillis(); 155 156 _zoneItemsInCache = 0; 157 _zoneItemsSaxed = 0; 158 _zonesSaxed = 0; 159 160 Request request = ObjectModelHelper.getRequest(objectModel); 161 162 Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE); 163 String title = page.getTitle(); 164 165 RenderingContext renderingContext = _renderingContextHandler.getRenderingContext(); 166 String workspace = _workspaceSelector.getWorkspace(); 167 String siteName = page.getSiteName(); 168 169 if (page.getType() != PageType.CONTAINER) 170 { 171 throw new IllegalStateException("Cannot invoke the PageGenerator on a Page without a PageContent"); 172 } 173 174 // Monitor the access to this page. 175 _pageAccess = (PageResourceAccess) request.getAttribute(Constants.REQUEST_ATTRIBUTE_PAGEACCESS); 176 177 if (_pageAccess != null) 178 { 179 _pageAccess.setRenderingContext(renderingContext); 180 _pageAccess.setWorkspaceJCR(workspace); 181 _resourceAccessMonitor.addAccessRecord(_pageAccess); 182 } 183 184 contentHandler.startDocument(); 185 AttributesImpl attrs = new AttributesImpl(); 186 attrs.addCDATAAttribute("title", title); 187 attrs.addCDATAAttribute("long-title", page.getLongTitle()); 188 attrs.addCDATAAttribute("id", page.getId()); 189 XMLUtils.startElement(contentHandler, "page", attrs); 190 191 try 192 { 193 XMLUtils.startElement(contentHandler, "metadata"); 194 page.dataToSAX(contentHandler); 195 XMLUtils.endElement(contentHandler, "metadata"); 196 197 // Tags 198 XMLUtils.startElement(contentHandler, "tags"); 199 Set<String> tags = page.getTags(); 200 for (String tagName : tags) 201 { 202 Map<String, Object> contextParameters = new HashMap<>(); 203 contextParameters.put("siteName", siteName); 204 205 Tag tag = _tagProviderEP.getTag(tagName, contextParameters); 206 207 if (tag != null) 208 { 209 // tag may be null if it has been registered on the page and then removed from the application 210 AttributesImpl tagattrs = new AttributesImpl(); 211 if (tag.getParentName() != null) 212 { 213 tagattrs.addCDATAAttribute("parent", tag.getParentName()); 214 } 215 216 XMLUtils.startElement(contentHandler, tagName, tagattrs); 217 tag.getTitle().toSAX(contentHandler); 218 XMLUtils.endElement(contentHandler, tagName); 219 } 220 } 221 XMLUtils.endElement(contentHandler, "tags"); 222 } 223 catch (AmetysRepositoryException e) 224 { 225 _pageAccess = null; 226 throw new ProcessingException("Unable to SAX page metadata", e); 227 } 228 229 long t1 = System.currentTimeMillis(); 230 231 AttributesImpl pcattrs = new AttributesImpl(); 232 pcattrs.addCDATAAttribute("modifiable", Boolean.toString(page instanceof ModifiablePage)); 233 pcattrs.addCDATAAttribute("moveable", Boolean.toString(page instanceof MoveablePage)); 234 XMLUtils.startElement(contentHandler, "pageContents", pcattrs); 235 236 try 237 { 238 // Iterate on existing zones 239 for (Zone zone : page.getZones()) 240 { 241 String zoneName = zone.getName(); 242 AmetysObjectIterable<? extends ZoneItem> zoneItems = zone.getZoneItems(); 243 244 _saxZone(page, zoneName, zoneItems, workspace, siteName, renderingContext); 245 } 246 247 // Iterate on defined zone (that are not existing) 248 SkinTemplate skinTemplate = _getTemplateDefinition(page); 249 if (skinTemplate != null) 250 { 251 for (SkinTemplateZone zoneDef : skinTemplate.getZones().values()) 252 { 253 if (!page.hasZone(zoneDef.getId())) 254 { 255 _saxZone(page, zoneDef.getId(), null, workspace, siteName, renderingContext); 256 } 257 } 258 } 259 } 260 catch (AmetysRepositoryException ex) 261 { 262 _pageAccess = null; 263 throw new ProcessingException("Unable to get Content", ex); 264 } 265 266 long t2 = System.currentTimeMillis(); 267 _timeLogger.debug("Zones processing time: {} ms", t2 - t1); 268 if (getLogger().isInfoEnabled()) 269 { 270 getLogger().info("PageGenerator\t/" + page.getSiteName() + "/" + page.getSitemapName() + "/" + page.getPathInSitemap() + "\t" + page.getId() + "\tprocessing time (in ms):\t" + (t2 - t0) + "\tRendering context:\t" + renderingContext + "\tSaxing (zones, total zoneItems, zoneItems from cache):\t" + _zonesSaxed + "\t" + _zoneItemsSaxed + "\t" + _zoneItemsInCache); 271 } 272 273 XMLUtils.endElement(contentHandler, "pageContents"); 274 XMLUtils.endElement(contentHandler, "page"); 275 contentHandler.endDocument(); 276 277 _timeLogger.debug("Page processing time: {} ms", t2 - t0); 278 279 _pageAccess = null; 280 } 281 282 /** 283 * Sax a zone 284 * @param page The page 285 * @param zoneName The zone in the page to sax 286 * @param zoneItems The items of the zone or null 287 * @param workspace the workspace 288 * @param site the site's name 289 * @param renderingContext the rendering context 290 * @throws SAXException if an error occurs while saxing 291 * @throws IOException if an I/O exception occurs 292 * @throws ProcessingException if an error occurs 293 */ 294 private void _saxZone(Page page, String zoneName, AmetysObjectIterable<? extends ZoneItem> zoneItems, String workspace, String site, RenderingContext renderingContext) throws SAXException, IOException, ProcessingException 295 { 296 _zonesSaxed++; 297 298 AmetysObjectIterable<? extends ZoneItem> localZoneItems = zoneItems; 299 300 AttributesImpl zoneAttrs = new AttributesImpl(); 301 zoneAttrs.addCDATAAttribute("name", zoneName); 302 303 if (localZoneItems == null || !localZoneItems.iterator().hasNext()) 304 { 305 // zone is empty => try to inherit 306 Zone parentPageZone = _inherit(page, page, zoneName); 307 if (parentPageZone != null) 308 { 309 zoneAttrs.addCDATAAttribute("inherited", parentPageZone.getSitemapElement().getId()); 310 311 localZoneItems = parentPageZone.getZoneItems(); 312 } 313 } 314 315 Request request = ObjectModelHelper.getRequest(objectModel); 316 request.setAttribute(Zone.class.getName(), zoneName); 317 318 XMLUtils.startElement(contentHandler, "zone", zoneAttrs); 319 320 _saxAvailableContentTypes(page, zoneName); 321 _saxAvailableServices(page, zoneName); 322 323 _saxZoneItems(page, localZoneItems, workspace, site, renderingContext); 324 325 XMLUtils.endElement(contentHandler, "zone"); 326 request.setAttribute(Zone.class.getName(), null); 327 328 } 329 330 /** 331 * Generate the list of available services for the given zone. 332 * @param page the page. 333 * @param zoneName the zone name in the page. 334 * @throws SAXException if something goes wrong when saxing the available services 335 */ 336 private void _saxAvailableServices(Page page, String zoneName) throws SAXException 337 { 338 Set<String> services = _serviceAssignmentHandler.getAvailableServices(page, zoneName); 339 340 XMLUtils.startElement(contentHandler, "available-services"); 341 342 for (String service : services) 343 { 344 AttributesImpl attrs = new AttributesImpl(); 345 attrs.addCDATAAttribute("id", service); 346 XMLUtils.createElement(contentHandler, "service", attrs); 347 } 348 349 XMLUtils.endElement(contentHandler, "available-services"); 350 } 351 352 /** 353 * Generate the list of available content types for the given zone. 354 * @param page the page. 355 * @param zoneName the zone name in the page. 356 * @throws SAXException if something goes wrong when saxing the available content types 357 */ 358 private void _saxAvailableContentTypes(Page page, String zoneName) throws SAXException 359 { 360 Set<String> cTypes = _cTypeAssignmentHandler.getAvailableContentTypes(page, zoneName, true); 361 362 XMLUtils.startElement(contentHandler, "available-content-types"); 363 364 for (String cType : cTypes) 365 { 366 AttributesImpl attrs = new AttributesImpl(); 367 attrs.addCDATAAttribute("id", cType); 368 XMLUtils.createElement(contentHandler, "content-type", attrs); 369 } 370 371 XMLUtils.endElement(contentHandler, "available-content-types"); 372 } 373 374 /** 375 * Sax zone items 376 * @param page the page 377 * @param zoneItems The zone items to sax 378 * @param workspace the workspace 379 * @param site the site's name 380 * @param renderingContext the rendering context 381 * @throws SAXException if an error occurs while saxing 382 * @throws IOException if an I/O exception occurs 383 * @throws ProcessingException if an error occurs 384 */ 385 private void _saxZoneItems(Page page, AmetysObjectIterable< ? extends ZoneItem> zoneItems, String workspace, String site, RenderingContext renderingContext) throws SAXException, IOException, ProcessingException 386 { 387 if (zoneItems == null) 388 { 389 return; 390 } 391 392 Request request = ObjectModelHelper.getRequest(objectModel); 393 394 for (ZoneItem zoneItem : zoneItems) 395 { 396 _saxZoneItem(page, workspace, site, renderingContext, request, zoneItem); 397 } 398 } 399 400 private void _handleZoneAccess(PageElementResourceAccess zoneAccess, boolean cacheable, boolean hit) 401 { 402 if (zoneAccess != null) 403 { 404 zoneAccess.setCacheable(cacheable); 405 zoneAccess.setCacheHit(hit); 406 } 407 } 408 409 private void _saxZoneItem(Page page, String workspace, String site, RenderingContext renderingContext, Request request, ZoneItem zoneItem) throws SAXException, ProcessingException, IOException 410 { 411 long t0 = System.currentTimeMillis(); 412 413 _zoneItemsSaxed++; 414 415 String id = zoneItem.getId(); 416 ZoneType type = zoneItem.getType(); 417 418 request.setAttribute(WebConstants.REQUEST_ATTR_ZONEITEM, zoneItem); 419 420 AttributesImpl zoneItemAttrs = new AttributesImpl(); 421 zoneItemAttrs.addCDATAAttribute("id", id); 422 423 PageElementResourceAccess zoneAccess = _pageAccess != null ? _pageAccess.createPageElementAccess(id, PageElementType.fromZoneItemType(type)) : null; 424 425 // try to get content from cache only if it is cacheable 426 SaxBuffer cachedContent = null; 427 Exception ex = null; 428 429 boolean isCacheable = false; 430 if (type == ZoneType.CONTENT) 431 { 432 // a Content is always cacheable 433 isCacheable = true; 434 } 435 else if (type == ZoneType.SERVICE) 436 { 437 String serviceId = zoneItem.getServiceId(); 438 Service service = _serviceExtPt.getExtension(serviceId); 439 440 if (service != null) 441 { 442 try 443 { 444 isCacheable = service.isCacheable(page, zoneItem); 445 } 446 catch (Exception e) 447 { 448 ex = new ProcessingException("Error testing the service cachability.", e); 449 } 450 } 451 // if service is null, an exception will be thrown later 452 } 453 454 _handleZoneAccess(zoneAccess, isCacheable, false); 455 456 request.setAttribute("IsZoneItemCacheable", isCacheable); 457 458 if (isCacheable) 459 { 460 cachedContent = _zoneItemCache.getPageElement(workspace, site, _getType(zoneItem), id, page.getId(), renderingContext); 461 } 462 463 if (cachedContent != null) 464 { 465 _handleZoneAccess(zoneAccess, true, true); 466 467 _zoneItemsInCache++; 468 469 request.setAttribute("IsZoneItemCacheable", true); 470 471 cachedContent.toSAX(contentHandler); 472 } 473 else 474 { 475 476 SaxBuffer buffer = null; 477 ContentHandler handler; // the actual ContentHandler, either the real one, or a buffer 478 if (isCacheable) 479 { 480 buffer = new SaxBuffer(); 481 handler = buffer; 482 } 483 else 484 { 485 handler = contentHandler; 486 } 487 488 XMLUtils.startElement(handler, "zoneItem", zoneItemAttrs); 489 490 XMLUtils.startElement(handler, "information"); 491 XMLUtils.createElement(handler, "type", type.toString()); 492 493 Object result = null; 494 if (type == ZoneType.CONTENT) 495 { 496 if (getLogger().isDebugEnabled()) 497 { 498 Content content = zoneItem.getContent(); 499 getLogger().debug("Processing content " + content.getId() + " / " + content.getPath()); 500 } 501 502 result = _saxContentZoneItem(zoneItem, handler, request); 503 } 504 else if (type == ZoneType.SERVICE) 505 { 506 if (getLogger().isDebugEnabled()) 507 { 508 getLogger().debug("Processing service " + zoneItem.getServiceId()); 509 } 510 511 result = _saxServiceZoneItem(zoneItem, handler, ex); 512 } 513 514 Source src = null; 515 if (result instanceof Source) 516 { 517 src = (Source) result; 518 } 519 else 520 { 521 ex = (Exception) result; 522 } 523 524 XMLUtils.endElement(handler, "information"); 525 526 _saxSource(handler, src, ex); 527 528 XMLUtils.endElement(handler, "zoneItem"); 529 530 // finally store the buffered data in the cache and SAX it to the pipeline 531 if (buffer != null) 532 { 533 buffer.toSAX(contentHandler); 534 _zoneItemCache.storePageElement(workspace, site, _getType(zoneItem), id, page.getId(), renderingContext, buffer); 535 } 536 } 537 538 // Monitor the access to this zone item. 539 _resourceAccessMonitor.addAccessRecord(zoneAccess); 540 541 // Empty content request attributes 542 request.setAttribute(Content.class.getName(), null); 543 // Empty zone item request attribute 544 request.setAttribute(WebConstants.REQUEST_ATTR_ZONEITEM, null); 545 // Empty zone item cacheable attribute 546 request.setAttribute("IsZoneItemCacheable", null); 547 548 _timeLogger.debug("Zone item {} processing time: {} ms", id, System.currentTimeMillis() - t0); 549 } 550 551 private Object _saxContentZoneItem(ZoneItem zoneItem, ContentHandler handler, Request request) throws SAXException, MalformedURLException, IOException 552 { 553 try 554 { 555 Content content = zoneItem.getContent(); 556 String viewName = StringUtils.defaultString(zoneItem.getViewName(), "main"); 557 558 XMLUtils.createElement(handler, "contentId", content.getId()); 559 XMLUtils.createElement(handler, "contentName", content.getName()); 560 XMLUtils.createElement(handler, "metadataSetName", viewName); 561 if (content instanceof SharedContent) 562 { 563 XMLUtils.createElement(handler, "sharedContent", "true"); 564 } 565 566 String contentTypeId = _contentTypeHelper.getContentTypeIdForRendering(content); 567 568 ContentTypeDescriptor contentType = _contentTypeExtPt.getExtension(contentTypeId); 569 if (contentType == null) 570 { 571 contentType = _dynamicCTDescriptorEP.getExtension(contentTypeId); 572 } 573 574 if (contentType == null) 575 { 576 return new IllegalStateException("The content type '" + contentTypeId + "' is referenced but does not exist"); 577 } 578 else 579 { 580 AttributesImpl contentTypeAttrs = new AttributesImpl(); 581 contentTypeAttrs.addCDATAAttribute("id", contentType.getId()); 582 XMLUtils.startElement(handler, "type-information", contentTypeAttrs); 583 584 contentType.getLabel().toSAX(handler, "label"); 585 contentType.getDescription().toSAX(handler, "description"); 586 587 if (contentType.getIconGlyph() != null) 588 { 589 XMLUtils.createElement(handler, "iconGlyph", contentType.getIconGlyph()); 590 } 591 if (contentType.getIconDecorator() != null) 592 { 593 XMLUtils.createElement(handler, "iconDecorator", contentType.getIconDecorator()); 594 } 595 596 if (contentType.getSmallIcon() != null) 597 { 598 XMLUtils.createElement(handler, "smallIcon", contentType.getSmallIcon()); 599 XMLUtils.createElement(handler, "mediumIcon", contentType.getMediumIcon()); 600 XMLUtils.createElement(handler, "largeIcon", contentType.getLargeIcon()); 601 } 602 603 XMLUtils.startElement(handler, "css"); 604 for (ScriptFile cssFile : contentType.getCSSFiles()) 605 { 606 _saxCSSFile(handler, cssFile); 607 } 608 XMLUtils.endElement(handler, "css"); 609 610 XMLUtils.endElement(handler, "type-information"); 611 612 String url = "cocoon://_plugins/" + contentType.getPluginName() + "/" + contentType.getId() + ".html"; 613 Map<String, String> urlParams = _contentHelper.getContentViewUrlParameters(content, viewName, "html"); 614 615 // FIXME use a context 616 request.setAttribute(Content.class.getName(), content); 617 request.setAttribute(GetContentAction.RESULT_CONTENTTYPE, contentTypeId); 618 return resolver.resolveURI(URIUtils.buildURI(url, urlParams)); 619 } 620 } 621 catch (AmetysRepositoryException e) 622 { 623 return new ProcessingException("Unable to get content property", e); 624 } 625 } 626 627 private Object _saxServiceZoneItem(ZoneItem zoneItem, ContentHandler handler, Exception ex) throws SAXException, MalformedURLException, IOException 628 { 629 String serviceId = zoneItem.getServiceId(); 630 Service service = _serviceExtPt.getExtension(serviceId); 631 632 if (service == null) 633 { 634 return new ProcessingException("Unable to get service for name '" + serviceId + "'"); 635 } 636 else if (ex == null) // If an exception was caught while testing the service cacheability, do not generate 637 { 638 AttributesImpl serviceAttrs = new AttributesImpl(); 639 serviceAttrs.addCDATAAttribute("id", service.getId()); 640 XMLUtils.startElement(handler, "type-information", serviceAttrs); 641 642 service.getLabel().toSAX(handler, "label"); 643 service.getDescription().toSAX(handler, "description"); 644 645 if (service.getIconGlyph() != null) 646 { 647 XMLUtils.createElement(handler, "iconGlyph", service.getIconGlyph()); 648 } 649 if (service.getIconDecorator() != null) 650 { 651 XMLUtils.createElement(handler, "iconDecorator", service.getIconDecorator()); 652 } 653 if (service.getSmallIcon() != null) 654 { 655 XMLUtils.createElement(handler, "smallIcon", service.getSmallIcon()); 656 XMLUtils.createElement(handler, "mediumIcon", service.getMediumIcon()); 657 } 658 659 XMLUtils.startElement(handler, "css"); 660 for (ScriptFile cssFile : service.getCSSFiles()) 661 { 662 _saxCSSFile(handler, cssFile); 663 } 664 XMLUtils.endElement(handler, "css"); 665 666 XMLUtils.endElement(handler, "type-information"); 667 668 return resolver.resolveURI(service.getURL(), null, PageGeneratorHelper.getParameters(service, zoneItem)); 669 } 670 else 671 { 672 return ex; 673 } 674 } 675 676 private void _saxCSSFile(ContentHandler handler, ScriptFile cssFile) throws SAXException 677 { 678 AttributesImpl fileAttrs = new AttributesImpl(); 679 if (!cssFile.isLangSpecific()) 680 { 681 String rtlMode = cssFile.getRtlMode(); 682 if (rtlMode != null && !"all".equals(rtlMode)) 683 { 684 fileAttrs.addCDATAAttribute("rtl", rtlMode); 685 } 686 687 XMLUtils.createElement(handler, "file", fileAttrs, cssFile.getPath()); 688 } 689 else 690 { 691 fileAttrs.addCDATAAttribute("lang", "true"); 692 XMLUtils.startElement(handler, "file", fileAttrs); 693 694 String defaultLang = cssFile.getDefaultLang(); 695 Map<String, String> langPaths = cssFile.getLangPaths(); 696 697 for (Entry<String, String> langPath : langPaths.entrySet()) 698 { 699 AttributesImpl langAttrs = new AttributesImpl(); 700 701 String codeLang = langPath.getKey(); 702 langAttrs.addCDATAAttribute("code", codeLang); 703 if (codeLang.equals(defaultLang)) 704 { 705 langAttrs.addCDATAAttribute("default", "true"); 706 } 707 708 XMLUtils.createElement(handler, "lang", langAttrs, langPath.getValue()); 709 } 710 711 XMLUtils.endElement(handler, "file"); 712 } 713 } 714 715 private void _saxSource(ContentHandler handler, Source src, Exception ex) throws SAXException, IOException, ProcessingException 716 { 717 if (src == null) 718 { 719 getLogger().error("Unable to display zone item", ex); 720 _saxError(handler, ex); 721 } 722 else 723 { 724 try 725 { 726 SourceUtil.toSAX(src, new IgnoreRootHandler(handler)); 727 } 728 catch (ProcessingException e) 729 { 730 getLogger().error("Unable to display zone item", e); 731 732 if (_throwException(e)) 733 { 734 throw e; 735 } 736 else 737 { 738 _saxError(handler, e.getCause()); 739 } 740 } 741 finally 742 { 743 resolver.release(src); 744 } 745 } 746 } 747 748 private String _getType(ZoneItem zoneItem) 749 { 750 ZoneType type = zoneItem.getType(); 751 752 if (type == ZoneType.CONTENT) 753 { 754 return "CONTENT"; 755 } 756 else 757 { 758 return "SERVICE:" + zoneItem.getServiceId(); 759 } 760 } 761 762 /** 763 * Get the template definition for a page 764 * @param sitemapElement The page. Cannot be null. 765 * @return The template definition. Null if the page is not a container or if the template is not declared. 766 */ 767 private SkinTemplate _getTemplateDefinition(SitemapElement sitemapElement) 768 { 769 String templateName = sitemapElement.getTemplate(); 770 if (templateName == null) 771 { 772 return null; 773 } 774 775 Site site = sitemapElement.getSite(); 776 String skinId = site.getSkinId(); 777 try 778 { 779 Skin skinDef = _skinsManager.getSkin(skinId); 780 return skinDef.getTemplate(templateName); 781 } 782 catch (IllegalStateException e) 783 { 784 getLogger().error("Cannot get template definition for page '" + sitemapElement.getId() + "' using template '" + templateName + "' in skin '" + skinId + "'"); 785 return null; 786 } 787 } 788 789 /** 790 * Try to inherit the zone (as it is empty) 791 * @param childPage The child page that do inherit. Cannot be null 792 * @param page The page to inherit. Cannot be null 793 * @param zoneName The zone name in the page to inherit. Cannot be null or empty 794 * @return The zone inherited or null. 795 */ 796 private Zone _inherit(Page childPage, SitemapElement page, String zoneName) 797 { 798 // The page has an existing zone at this place ? 799 if (page.hasZone(zoneName)) 800 { 801 Zone zone = page.getZone(zoneName); 802 AmetysObjectIterable<? extends ZoneItem> zoneItems = zone.getZoneItems(); 803 804 // With data ? 805 if (zoneItems.iterator().hasNext()) 806 { 807 // This is it (end of the recursion) 808 return zone; 809 } 810 } 811 812 // Get the definition for the zone 813 SkinTemplateZone zoneDef; 814 815 Site site = page.getSite(); 816 String skinId = site.getSkinId(); 817 String templateName = page.getTemplate(); 818 try 819 { 820 SkinTemplate templateDef = _getTemplateDefinition(page); 821 zoneDef = templateDef.getZone(zoneName); 822 } 823 catch (IllegalStateException e) 824 { 825 getLogger().error("The page '" + childPage.getId() + "' cannot inherit a zone '" + zoneName + "' of template '" + templateName + "' in skin '" + skinId + "' as asked for page '" + page.getId() + "' in site '" + site.getName() + "'", e); 826 return null; 827 } 828 829 // This zone is not defined for the template 830 if (zoneDef == null) 831 { 832 getLogger().warn("The page '" + childPage.getId() + "' cannot inherit the undefined zone '" + zoneName + "' of template '" + templateName + "' in skin '" + skinId + "' as asked for page '" + page.getId() + "' in site '" + site.getName() + "'."); 833 return null; 834 } 835 836 // Support inheritance ? 837 if (!zoneDef.hasInheritance()) 838 { 839 return null; 840 } 841 842 // Get the parent page (that is a container page) 843 SitemapElement parentPage = page; 844 do 845 { 846 AmetysObject parentObject = parentPage.getParent(); 847 if (parentObject instanceof SitemapElement pc) 848 { 849 parentPage = pc; 850 } 851 else 852 { 853 // inheritance goes back to the root 854 return null; 855 } 856 } 857 while (parentPage.getTemplate() == null); 858 859 // Get the name of the zone which will be the inheritance source 860 String parentPageTemplate = parentPage.getTemplate(); 861 String inheritanceSrc = zoneDef.getInheritance(parentPageTemplate); 862 if (inheritanceSrc == null) 863 { 864 // No inheritance for this template 865 return null; 866 } 867 868 // Finally we will inherit from the parentPage and the zone inheritanceSrc 869 return _inherit(childPage, parentPage, inheritanceSrc); 870 } 871 872 /** 873 * Test if the error has to be thrown instead of SAXing it as an error zone item. 874 * @param ex the exception. 875 * @return true to throw the exception, false to catch it and SAX it as an error zone item. 876 */ 877 private boolean _throwException(Exception ex) 878 { 879 return _renderingContextHandler.getRenderingContext() == RenderingContext.FRONT 880 && ( 881 _isExceptionType(ex, AuthorizationRequiredException.class) 882 || _isExceptionType(ex, AccessDeniedException.class) 883 // See CMS-12302 if you want to change that one day 884 || _isExceptionType(ex, ServiceUnavailableException.class) 885 ); 886 } 887 888 private boolean _isExceptionType(Throwable throwable, Class clazz) 889 { 890 return ExceptionUtils.indexOfThrowable(throwable, clazz) > -1; 891 } 892 893 private void _saxError (ContentHandler handler, Throwable e) throws SAXException 894 { 895 XMLUtils.startElement(handler, "zone-item-error"); 896 897 XMLUtils.createElement(handler, "exception-message", e != null ? StringUtils.defaultString(e.getMessage()) : ""); 898 XMLUtils.createElement(handler, "exception-stack-trace", StringUtils.defaultString(ExceptionUtils.getFullStackTrace(e))); 899 900 XMLUtils.endElement(handler, "zone-item-error"); 901 } 902}