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.cms.explorer; 017 018import java.io.IOException; 019import java.util.ArrayList; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.ProcessingException; 027import org.apache.cocoon.environment.ObjectModelHelper; 028import org.apache.cocoon.generation.ServiceableGenerator; 029import org.apache.cocoon.xml.AttributesImpl; 030import org.apache.cocoon.xml.XMLUtils; 031import org.xml.sax.SAXException; 032 033import org.ametys.core.right.RightManager; 034import org.ametys.core.user.CurrentUserProvider; 035import org.ametys.core.user.UserIdentity; 036import org.ametys.plugins.explorer.resources.ModifiableResourceCollection; 037import org.ametys.plugins.explorer.resources.Resource; 038import org.ametys.plugins.repository.AmetysObjectResolver; 039import org.ametys.plugins.repository.ModifiableAmetysObject; 040import org.ametys.plugins.repository.UnknownAmetysObjectException; 041 042 043/** 044 * SAX the explorer nodes information 045 */ 046public class ResourceInformationGenerator extends ServiceableGenerator 047{ 048 private AmetysObjectResolver _resolver; 049 050 private RightManager _rightManager; 051 052 private CurrentUserProvider _userProvider; 053 054 @Override 055 public void service(ServiceManager serviceManager) throws ServiceException 056 { 057 super.service(serviceManager); 058 059 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 060 _rightManager = (RightManager) serviceManager.lookup(RightManager.ROLE); 061 _userProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE); 062 } 063 064 @SuppressWarnings("unchecked") 065 @Override 066 public void generate() throws IOException, SAXException, ProcessingException 067 { 068 Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 069 070 List<String> ids = null; 071 Object idParam = jsParameters.get("id"); 072 if (idParam instanceof String) 073 { 074 ids = new ArrayList<>(); 075 ids.add((String) idParam); 076 } 077 else 078 { 079 ids = (List<String>) idParam; 080 } 081 082 contentHandler.startDocument(); 083 XMLUtils.startElement(contentHandler, "resources"); 084 085 for (String id : ids) 086 { 087 try 088 { 089 Resource resource = (Resource) _resolver.resolveById(id); 090 091 AttributesImpl attrs = new AttributesImpl(); 092 attrs.addCDATAAttribute("id", resource.getId()); 093 attrs.addCDATAAttribute("name", resource.getName()); 094 attrs.addCDATAAttribute("path", resource.getResourcePath()); 095 attrs.addCDATAAttribute("size", String.valueOf(resource.getLength())); 096 attrs.addCDATAAttribute("isModifiable", String.valueOf(resource instanceof ModifiableAmetysObject)); 097 attrs.addCDATAAttribute("canCreateChild", String.valueOf(resource instanceof ModifiableResourceCollection)); 098 099 XMLUtils.startElement(contentHandler, "resource", attrs); 100 _saxUserRights(resource); 101 XMLUtils.endElement(contentHandler, "resource"); 102 } 103 catch (UnknownAmetysObjectException e) 104 { 105 getLogger().warn("The resource of id '" + id + "' does not exist anymore", e); 106 } 107 108 } 109 110 XMLUtils.endElement(contentHandler, "resources"); 111 contentHandler.endDocument(); 112 } 113 114 private void _saxUserRights(Resource resource) throws SAXException 115 { 116 UserIdentity user = _userProvider.getUser(); 117 Set<String> rights = _rightManager.getUserRights(user, resource); 118 119 XMLUtils.startElement(contentHandler, "user-rights"); 120 121 for (String rightID : rights) 122 { 123 AttributesImpl rightAttr = new AttributesImpl(); 124 rightAttr.addCDATAAttribute("id", rightID); 125 XMLUtils.createElement(contentHandler, "right", rightAttr); 126 } 127 XMLUtils.endElement(contentHandler, "user-rights"); 128 } 129}