001/*
002 *  Copyright 2017 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;
017
018import java.io.IOException;
019import java.util.Map;
020
021import javax.xml.parsers.DocumentBuilder;
022import javax.xml.parsers.DocumentBuilderFactory;
023import javax.xml.parsers.ParserConfigurationException;
024
025import org.apache.avalon.framework.parameters.Parameters;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.environment.SourceResolver;
031import org.apache.excalibur.source.TraversableSource;
032import org.w3c.dom.Document;
033import org.w3c.dom.Element;
034import org.w3c.dom.NodeList;
035import org.xml.sax.SAXException;
036
037import org.ametys.core.right.RightManager;
038import org.ametys.core.right.RightManager.RightResult;
039import org.ametys.core.user.CurrentUserProvider;
040import org.ametys.plugins.core.ui.parameter.files.GetParameterFileAction;
041import org.ametys.plugins.extraction.ExtractionConstants;
042
043/**
044 * Action for getting extraction definition files
045 */
046public class GetExtractionDefinitionFilesAction extends GetParameterFileAction
047{
048    private RightManager _rightManager;
049    private CurrentUserProvider _currentUserProvider;
050    
051    @Override
052    public void service(ServiceManager serviceManager) throws ServiceException
053    {
054        super.service(serviceManager);
055        _rightManager = (RightManager) serviceManager.lookup(RightManager.ROLE);
056        _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
057    }
058    
059    @Override
060    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
061    {
062        if (_rightManager.hasRight(_currentUserProvider.getUser(), ExtractionConstants.EXECUTE_EXTRACTION_RIGHT_ID, "/${WorkspaceName}") != RightResult.RIGHT_ALLOW)
063        {
064            String errorMessage = "User " + _currentUserProvider.getUser() + " try to list extraction definition files with no sufficient rights"; 
065            getLogger().error(errorMessage);
066            throw new IllegalStateException(errorMessage);
067        }
068        
069        return super.act(redirector, resolver, objectModel, source, parameters);
070    }
071
072    @Override
073    protected String getRootURI(Request request)
074    {
075        return ExtractionConstants.DEFINITIONS_DIR;
076    }
077    
078    @Override
079    protected boolean isIgnoredSource(String fileName)
080    {
081        return !fileName.endsWith(".xml");
082    }
083    
084    @Override
085    protected Map<String, Object> _resource2JsonObject(TraversableSource file, TraversableSource root)
086    {
087        Map<String, Object> jsonObject = super._resource2JsonObject(file, root);
088        
089        try
090        {
091            // Parse existing definition file
092            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
093            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
094            Document document = documentBuilder.parse(file.getInputStream());
095            Element extractionRoot = document.getDocumentElement();
096            
097            // Put the first description content id
098            NodeList descriptions = extractionRoot.getElementsByTagName(ExtractionConstants.DESCRIPTION_TAG);
099            if (descriptions.getLength() > 0)
100            {
101                Element description = (Element) descriptions.item(0);
102                jsonObject.put("descriptionId", description.getAttribute(ExtractionConstants.DESCRIPTION_IDENTIFIER_ATTRIBUTE_NAME));
103            }
104        }
105        catch (ParserConfigurationException | SAXException | IOException e)
106        {
107            if (getLogger().isWarnEnabled())
108            {
109                getLogger().warn("Unable to read the definition file at path '" + _getRelativePath(root, file) + "'. Unable to determine the identifier of its description content.");
110            }
111        }
112        
113        return jsonObject;
114    }
115}