001/* 002 * Copyright 2019 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.lheo; 017 018import java.io.IOException; 019import java.nio.file.AccessDeniedException; 020import java.util.Collections; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.cocoon.ProcessingException; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Request; 027import org.apache.cocoon.environment.Response; 028import org.apache.cocoon.generation.ServiceableGenerator; 029import org.xml.sax.SAXException; 030 031import org.ametys.core.right.RightManager; 032import org.ametys.core.right.RightManager.RightResult; 033import org.ametys.odf.program.AbstractProgram; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035 036/** 037 * Generates entity to LHEO 038 */ 039public class ExportToLHEOGenerator extends ServiceableGenerator 040{ 041 /** The ametys object resolver */ 042 protected AmetysObjectResolver _resolver; 043 044 /** The right manager */ 045 protected RightManager _rightManager; 046 047 /** The export LHEO manager */ 048 protected ExportToLHEOManager _exportLHEOManager; 049 050 @Override 051 public void service(ServiceManager smanager) throws ServiceException 052 { 053 super.service(smanager); 054 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 055 _exportLHEOManager = (ExportToLHEOManager) smanager.lookup(ExportToLHEOManager.ROLE); 056 _rightManager = (RightManager) smanager.lookup(RightManager.ROLE); 057 } 058 059 @Override 060 public void generate() throws IOException, SAXException, ProcessingException 061 { 062 Request request = ObjectModelHelper.getRequest(objectModel); 063 064 // Get the program from request 065 String programId = request.getParameter("id"); 066 AbstractProgram program = _resolver.resolveById(programId); 067 068 if (_rightManager.currentUserHasRight(ExportToLHEOManager.RIGHT_EXPORT_LHEO, program) != RightResult.RIGHT_ALLOW) 069 { 070 throw new AccessDeniedException("User tried to export program '" + program + "' to LHEO without sufficient rights"); 071 } 072 073 // Configure the response header 074 Response response = ObjectModelHelper.getResponse(objectModel); 075 response.setHeader("Content-Disposition", "attachment; filename=\"LHEO_" + program.getCode() + ".xml" + "\""); 076 077 _exportLHEOManager.saxLHEO(contentHandler, Collections.singletonList(program)); 078 } 079}