001/*
002 *  Copyright 2021 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.extraction.execution.pipeline;
017
018import java.io.IOException;
019import java.util.Collection;
020import java.util.HashSet;
021import java.util.Set;
022
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.AbstractGenerator;
027import org.apache.cocoon.serialization.ZipArchiveSerializer;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.apache.commons.lang3.StringUtils;
031import org.apache.excalibur.source.SourceException;
032import org.apache.excalibur.source.impl.FileSource;
033import org.xml.sax.SAXException;
034
035/**
036 * Generate the list of file to include in a zip for downloading results files 
037 */
038public class ExtractionResultsDownloadGenerator extends AbstractGenerator
039{
040    
041    public void generate() throws IOException, SAXException, ProcessingException
042    {
043        Request request = ObjectModelHelper.getRequest(objectModel);
044        String[] filePaths = request.getParameterValues("file");
045        
046        contentHandler.startDocument();
047        contentHandler.startPrefixMapping("zip", ZipArchiveSerializer.ZIP_NAMESPACE);
048        XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive");
049        
050        Set<String> saxedPath = new HashSet<>();
051        for (String filePath : filePaths)
052        {
053            FileSource fileSource = (FileSource) resolver.resolveURI("ametys-home://data/extraction/" + filePath);
054            String parentPath = filePath.contains("/") ? filePath.substring(0, filePath.lastIndexOf("/")) : StringUtils.EMPTY;
055            saxedPath.addAll(_resultFileSourceToSAX(fileSource, parentPath, saxedPath));
056        }
057        
058        XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive");
059        contentHandler.endPrefixMapping("zip");
060        contentHandler.endDocument();
061    }
062    
063    /**
064     * Generate SAX events for the given result file source
065     * @param resultSource the result file source
066     * @param parentPath the path of the parent folder
067     * @param alreadySaxedPaths paths of the files that have already been saxed
068     * @return The paths of the files that have been saxed
069     * @throws SourceException if an error occurs while traversing a collection source
070     * @throws SAXException if an error occurs while generating SAX events
071     */
072    protected Set<String> _resultFileSourceToSAX(FileSource resultSource, String parentPath, Set<String> alreadySaxedPaths) throws SourceException, SAXException
073    {
074        Set<String> allSaxedPaths = new HashSet<>(alreadySaxedPaths);
075        String currentSourcePath = StringUtils.isEmpty(parentPath) ? resultSource.getName() : parentPath + "/" + resultSource.getName();
076        
077        if (resultSource.isCollection())
078        {
079            Collection<FileSource> children = resultSource.getChildren();
080            for (FileSource child : children)
081            {
082                allSaxedPaths.addAll(_resultFileSourceToSAX(child, currentSourcePath, allSaxedPaths));
083            }
084        }
085        else if (allSaxedPaths.add(currentSourcePath))
086        {
087            AttributesImpl zipAttrs = new AttributesImpl();
088            zipAttrs.addAttribute("", "name", "name", "CDATA", currentSourcePath);
089            zipAttrs.addAttribute("", "src", "src", "CDATA", resultSource.getURI());
090            XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry", zipAttrs);
091            XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry");
092        }
093        
094        return allSaxedPaths;
095    }
096}