001/* 002 * Copyright 2024 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.cms.properties.tab; 017 018import java.io.IOException; 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.cocoon.ProcessingException; 024import org.apache.cocoon.components.source.SourceUtil; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Request; 027import org.apache.cocoon.generation.ServiceableGenerator; 028import org.apache.cocoon.xml.XMLUtils; 029import org.apache.excalibur.source.Source; 030import org.xml.sax.SAXException; 031 032import org.ametys.cms.properties.section.PropertySection; 033import org.ametys.cms.properties.section.PropertySectionExtensionPoint; 034import org.ametys.core.right.RightManager; 035import org.ametys.core.util.IgnoreRootHandler; 036import org.ametys.plugins.repository.AmetysObject; 037import org.ametys.plugins.repository.AmetysObjectResolver; 038 039/** 040 * The properties generator, it receive an object id, check for each section if it is compatible then resolve the section for the object. 041 */ 042public class PropertiesTabGenerator extends ServiceableGenerator 043{ 044 private PropertySectionExtensionPoint _propertySectionEP; 045 private AmetysObjectResolver _resolver; 046 private RightManager _rightManager; 047 048 @Override 049 public void service(ServiceManager smanager) throws ServiceException 050 { 051 super.service(smanager); 052 _propertySectionEP = (PropertySectionExtensionPoint) smanager.lookup(PropertySectionExtensionPoint.ROLE); 053 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 054 _rightManager = (RightManager) manager.lookup(RightManager.ROLE); 055 } 056 057 public void generate() throws IOException, SAXException, ProcessingException 058 { 059 Request request = ObjectModelHelper.getRequest(objectModel); 060 061 String objectId = parameters.getParameter("objectId", request.getParameter("objectId")); 062 AmetysObject ametysObject = _getObject(objectId); 063 064 contentHandler.startDocument(); 065 066 if (ametysObject == null) 067 { 068 XMLUtils.createElement(contentHandler, "object-not-found"); 069 } 070 else if (!_rightManager.currentUserHasReadAccess(ametysObject)) 071 { 072 XMLUtils.createElement(contentHandler, "no-read-access"); 073 } 074 else 075 { 076 XMLUtils.startElement(contentHandler, "sections"); 077 078 for (PropertySection section : _propertySectionEP.getSupportingExtensions(ametysObject)) 079 { 080 Source src = null; 081 try 082 { 083 // Set request attributes 084 Map<String, Object> requestParams = section.getParameters(ametysObject); 085 for (String attributeName : requestParams.keySet()) 086 { 087 request.setAttribute(attributeName, requestParams.get(attributeName)); 088 } 089 090 // Resolve URI 091 src = resolver.resolveURI(section.getDataURL(ametysObject)); 092 SourceUtil.toSAX(src, new IgnoreRootHandler(contentHandler)); 093 } 094 catch (Exception e) 095 { 096 throw new ProcessingException("Error while using property section " + section.getId() + " on object " + objectId, e); 097 } 098 finally 099 { 100 resolver.release(src); 101 } 102 } 103 104 XMLUtils.endElement(contentHandler, "sections"); 105 } 106 107 contentHandler.endDocument(); 108 } 109 110 /** 111 * Get the resolved object from the object id. 112 * If the object does not exists or is not of the right type, null i returned. 113 * @param objectId the object id 114 * @return The resolved object or null 115 */ 116 private AmetysObject _getObject(String objectId) 117 { 118 try 119 { 120 return _resolver.resolveById(objectId); 121 } 122 catch (Exception e) 123 { 124 // Nothing to do 125 } 126 return null; 127 } 128}