001/*
002 *  Copyright 2020 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.export;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.acting.ServiceableAction;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Redirector;
026import org.apache.cocoon.environment.Response;
027import org.apache.cocoon.environment.SourceResolver;
028import org.apache.commons.lang.StringUtils;
029
030import org.ametys.core.util.I18nUtils;
031import org.ametys.core.util.URIUtils;
032import org.ametys.odf.program.SubProgram;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.repository.UnknownAmetysObjectException;
035import org.ametys.runtime.i18n.I18nizableText;
036
037/**
038 * Set the title of the sub program in response header
039 */
040public class SetSubProgramHeader extends ServiceableAction
041{
042    /** The ametys object resolver */
043    protected AmetysObjectResolver _resolver;
044    
045    /** The i18n utils */
046    protected I18nUtils _i18nUtils;
047    
048    @Override
049    public void service(ServiceManager smanager) throws ServiceException
050    {
051        super.service(smanager);
052        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
053        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
054    }
055    
056    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
057    {
058        String subProgramId = parameters.getParameter("subProgramId", null);
059        if (StringUtils.isBlank(subProgramId))
060        {
061            throw new IllegalArgumentException("The subProgram ID must be defined");
062        }
063        
064        try
065        {
066            SubProgram subProgram = _resolver.resolveById(subProgramId);
067            
068            Response response = ObjectModelHelper.getResponse(objectModel);
069            
070            String prefix = _i18nUtils.translate(new I18nizableText("plugin.odf", "PLUGINS_ODF_EDUCATIONAL_BOOKLET_SUBPROGRAM_PDF_NAME_PREFIX"), subProgram.getLanguage());
071            
072            String fileName = prefix + " " + subProgram.getTitle() + ".pdf";
073            String encodedName = URIUtils.encodeHeader(fileName);
074            
075            response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedName + "\";filename*=UTF-8''" + encodedName);
076        }
077        catch (UnknownAmetysObjectException e) 
078        {
079            throw new IllegalArgumentException("No subProgram is defined for ID " + subProgramId, e);
080        }
081        
082        return EMPTY_MAP;
083    }
084}