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.odf.cdmfr; 017 018import java.io.IOException; 019import java.nio.file.AccessDeniedException; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.cocoon.ProcessingException; 024import org.apache.cocoon.environment.ObjectModelHelper; 025import org.apache.cocoon.environment.Request; 026import org.apache.cocoon.generation.ServiceableGenerator; 027import org.xml.sax.SAXException; 028 029import org.ametys.core.right.RightManager; 030import org.ametys.core.right.RightManager.RightResult; 031import org.ametys.odf.program.Program; 032import org.ametys.plugins.repository.AmetysObjectResolver; 033 034/** 035 * Generates CDM-fr for a {@link CDMEntity}. 036 */ 037public class ExportToCDMfrGenerator extends ServiceableGenerator 038{ 039 /** The right to export a program to CDM-Fr */ 040 public static final String RIGHT_EXPORT_CDM = "ODF_Rights_ExportCDM"; 041 /** The request attribute name to check rights or not. Default to true */ 042 public static final String CHECK_RIGHTS_ATTRIBUTE_NAME = ExportToCDMfrGenerator.class.getName() + ".checkRights"; 043 044 private ExportCDMfrHelper _exportCMDfrHelper; 045 private RightManager _rightManager; 046 private AmetysObjectResolver _resolver; 047 048 @Override 049 public void service(ServiceManager smanager) throws ServiceException 050 { 051 super.service(smanager); 052 _exportCMDfrHelper = (ExportCDMfrHelper) smanager.lookup(ExportCDMfrHelper.ROLE); 053 _rightManager = (RightManager) smanager.lookup(RightManager.ROLE); 054 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 055 } 056 057 @Override 058 public void generate() throws IOException, SAXException, ProcessingException 059 { 060 Request request = ObjectModelHelper.getRequest(objectModel); 061 062 // Get the program from request 063 String programId = request.getParameter("id"); 064 Program program = _resolver.resolveById(programId); 065 066 boolean checkRights = request.getAttribute(CHECK_RIGHTS_ATTRIBUTE_NAME) != null ? (Boolean) request.getAttribute(CHECK_RIGHTS_ATTRIBUTE_NAME) : true; 067 if (checkRights && _rightManager.currentUserHasRight(RIGHT_EXPORT_CDM, program) != RightResult.RIGHT_ALLOW) 068 { 069 throw new AccessDeniedException("User tried to export program '" + program + "' to CDM-fr without sufficient rights"); 070 } 071 072 _exportCMDfrHelper.export(contentHandler, program); 073 } 074}