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.pipeline.impl; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.io.OutputStream; 021import java.nio.file.Files; 022import java.nio.file.Path; 023import java.nio.file.Paths; 024import java.util.Map; 025import java.util.UUID; 026 027import javax.xml.transform.sax.TransformerHandler; 028 029import org.apache.avalon.framework.service.ServiceException; 030import org.apache.avalon.framework.service.ServiceManager; 031import org.apache.avalon.framework.service.Serviceable; 032import org.apache.cocoon.components.source.impl.SitemapSource; 033import org.apache.excalibur.source.SourceResolver; 034import org.apache.excalibur.source.SourceUtil; 035 036import org.ametys.core.util.URIUtils; 037import org.ametys.plugins.extraction.execution.pipeline.PipelineSerializerModel; 038import org.ametys.plugins.extraction.execution.pipeline.Pipelines; 039import org.ametys.runtime.util.AmetysHomeHelper; 040 041/** 042 * Model for PDF pipeline serializers 043 */ 044public class PdfPipelineSerializerModel implements PipelineSerializerModel, Serviceable 045{ 046 private SourceResolver _sourceResolver; 047 048 @Override 049 public void service(ServiceManager manager) throws ServiceException 050 { 051 _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 052 } 053 054 @Override 055 public String getDefaultFileExtension() 056 { 057 return "pdf"; 058 } 059 060 @Override 061 public PipelineSerializer newSerializer(TransformerHandler handler, OutputStream out, Map<String, String> outputParameters) 062 { 063 return new PdfPipelineSerializer(handler, out, _sourceResolver); 064 } 065 066 private static final class PdfPipelineSerializer extends AbstractSerializerImpl 067 { 068 private SourceResolver _sourceResolver; 069 private OutputStream _tmpFileOs; 070 private Path _tmpFile; 071 072 PdfPipelineSerializer(TransformerHandler handler, OutputStream out, SourceResolver sourceResolver) 073 { 074 super(handler, out); 075 _sourceResolver = sourceResolver; 076 } 077 078 @Override 079 public void prepare() throws Exception 080 { 081 String tmpFilename = UUID.randomUUID().toString(); 082 _tmpFile = AmetysHomeHelper.getAmetysHomeTmp().toPath().resolve(Paths.get("extraction", tmpFilename)); 083 _tmpFileOs = Pipelines.getOutputStream(_tmpFile); 084 _streamResult = Pipelines.setResult(_handler, _tmpFileOs); 085 } 086 087 @Override 088 public void serialize() throws Exception 089 { 090 StringBuilder uri = new StringBuilder("cocoon://_admin/plugins/extraction/pdf-serialize"); 091 uri.append("?file=").append(URIUtils.encodeParameter(_tmpFile.toString())); 092 SitemapSource src = (SitemapSource) _sourceResolver.resolveURI(uri.toString()); 093 try (InputStream sourceIs = src.getInputStream()) 094 { 095 SourceUtil.copy(sourceIs, _out); 096 } 097 finally 098 { 099 _sourceResolver.release(src); 100 } 101 } 102 103 @Override 104 public void close() throws IOException 105 { 106 super.close(); 107 _tmpFileOs.close(); 108 Files.delete(_tmpFile); 109 } 110 } 111}