001/*
002 *  Copyright 2011 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.plugins.forms.data;
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.ProcessingException;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Response;
028import org.apache.cocoon.environment.SourceResolver;
029import org.apache.commons.lang.StringUtils;
030
031import org.ametys.core.util.URIUtils;
032import org.ametys.plugins.forms.Form;
033import org.ametys.plugins.forms.FormsException;
034import org.ametys.plugins.forms.jcr.FormPropertiesManager;
035
036/**
037 * This action adds file attachment HTTP header to the response for XSL export
038 */
039public class SetHttpHeaderForExport extends ServiceableAction
040{
041    private FormPropertiesManager _formPropertiesManager;
042
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        _formPropertiesManager = (FormPropertiesManager) smanager.lookup(FormPropertiesManager.ROLE);
047    }
048    
049    @Override
050    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
051    {
052        String siteName = parameters.getParameter("site", "");
053        String formId = parameters.getParameter("id", "");
054        
055        if (StringUtils.isEmpty(siteName) || StringUtils.isEmpty(formId))
056        {
057            throw new ProcessingException("The site name and form ID must be provided.");
058        }
059        
060        try
061        {
062            Form form = _formPropertiesManager.getForm(siteName, formId);
063            
064            if (form == null)
065            {
066                throw new ProcessingException("The form of ID '" + formId + " can't be found in the site '" + siteName + "'.");
067            }
068            
069            String fileName = URIUtils.decode(form.getLabel());
070            String encodedName = URIUtils.encodeHeader(fileName);
071            
072            Response response = ObjectModelHelper.getResponse(objectModel);
073            
074            response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedName + ".xls\";filename*=UTF-8''" + encodedName + ".xls");
075        }
076        catch (FormsException e)
077        {
078            getLogger().error("Unable to get form with id " + formId, e);
079        }
080        
081        return EMPTY_MAP;
082    }
083}