001/* 002 * Copyright 2018 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.io.OutputStream; 020import java.util.Collections; 021import java.util.Enumeration; 022import java.util.Map; 023import java.util.function.Function; 024import java.util.stream.Collectors; 025 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.cocoon.ProcessingException; 029import org.apache.cocoon.environment.ObjectModelHelper; 030import org.apache.cocoon.environment.Request; 031import org.apache.cocoon.reading.ServiceableReader; 032import org.xml.sax.SAXException; 033 034import org.ametys.plugins.extraction.execution.pipeline.PipelineDescriptor; 035import org.ametys.plugins.extraction.execution.pipeline.PipelineManager; 036 037/** 038 * Reader for copying the extraction result into the pipeline {@link OutputStream} 039 */ 040public class ExtractionReader extends ServiceableReader 041{ 042 private static final String __PIPELINE_PARAMETER = "pipeline"; 043 private static final String __LANG_PARAMETER = "lang"; 044 private static final String __DEFINITION_FILE_PARAMETER = "file"; 045 046 private PipelineManager _pipelineManager; 047 private ExtractionExecutor _extractionExecutor; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 super.service(smanager); 053 _pipelineManager = (PipelineManager) manager.lookup(PipelineManager.ROLE); 054 _extractionExecutor = (ExtractionExecutor) smanager.lookup(ExtractionExecutor.ROLE); 055 } 056 057 @Override 058 public void generate() throws IOException, SAXException, ProcessingException 059 { 060 Request request = ObjectModelHelper.getRequest(objectModel); 061 062 String definitionFileName = request.getParameter(__DEFINITION_FILE_PARAMETER); 063 if (definitionFileName == null) 064 { 065 throw new IllegalArgumentException("The '" + __DEFINITION_FILE_PARAMETER + "' parameter is mandatory, it must be present to select an extraction to execute."); 066 } 067 String lang = request.getParameter(__LANG_PARAMETER); 068 Map<String, Object> params = _getParams(request); 069 PipelineDescriptor pipeline = _getPipeline(request); 070 071 try 072 { 073 _extractionExecutor.execute(definitionFileName, out, lang, params, pipeline); 074 } 075 catch (Exception e) 076 { 077 throw new ProcessingException(e); 078 } 079 } 080 081 private PipelineDescriptor _getPipeline(Request request) throws IOException 082 { 083 String pipelineId = request.getParameter(__PIPELINE_PARAMETER); 084 if (pipelineId == null) 085 { 086 // Use default pipeline 087 pipelineId = _pipelineManager.getDefaultPipeline(); 088 } 089 else if (!_pipelineManager.has(pipelineId)) 090 { 091 // Pipeline does not exists 092 throw new IllegalArgumentException("The pipeline '" + pipelineId + "' does not exists."); 093 } 094 PipelineDescriptor pipeline = _pipelineManager.get(pipelineId); 095 return pipeline; 096 } 097 098 private static Map<String, Object> _getParams(Request request) 099 { 100 return Collections.list((Enumeration<String>) request.getParameterNames()) 101 .stream() 102 .collect(Collectors.toMap( 103 Function.identity(), 104 paramName -> request.getParameter(paramName))); 105 } 106} 107