001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Thomas Roger 016 */ 017 018package org.nuxeo.ecm.platform.io.operation; 019 020import java.io.File; 021import java.io.FileOutputStream; 022import java.io.IOException; 023import java.io.OutputStream; 024import java.nio.file.Files; 025 026import org.nuxeo.ecm.automation.core.Constants; 027import org.nuxeo.ecm.automation.core.annotations.Context; 028import org.nuxeo.ecm.automation.core.annotations.Operation; 029import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 030import org.nuxeo.ecm.automation.core.annotations.Param; 031import org.nuxeo.ecm.core.api.Blob; 032import org.nuxeo.ecm.core.api.Blobs; 033import org.nuxeo.ecm.core.api.CoreSession; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.core.io.DocumentPipe; 036import org.nuxeo.ecm.core.io.DocumentReader; 037import org.nuxeo.ecm.core.io.DocumentWriter; 038import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl; 039import org.nuxeo.ecm.core.io.impl.plugins.DocumentTreeReader; 040import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveWriter; 041import org.nuxeo.ecm.core.io.impl.plugins.SingleDocumentReader; 042import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentTreeWriter; 043import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter; 044import org.nuxeo.runtime.api.Framework; 045 046/** 047 * Operation that export a document. 048 * 049 * @since 7.2 050 */ 051@Operation(id = ExportDocument.ID, category = Constants.CAT_SERVICES, label = "Document Export", description = "Export the given document.") 052public class ExportDocument { 053 054 public static final String ID = "Document.Export"; 055 056 @Param(name = "exportAsTree", description = "Export the document and all its children", required = false) 057 protected boolean exportAsTree = false; 058 059 @Param(name = "exportAsZip", description = "Create a ZIP of the export. If 'exportAsTree' is true, exportAsZip is force to true", required = false) 060 protected boolean exportAsZip = false; 061 062 @Context 063 protected CoreSession session; 064 065 @OperationMethod 066 public Blob run(DocumentModel doc) throws IOException { 067 if (exportAsTree) { 068 exportAsZip = true; 069 } 070 071 File tempFile = Files.createTempFile(null, null).toFile(); 072 Framework.trackFile(tempFile, tempFile); 073 074 DocumentReader documentReader = null; 075 DocumentWriter documentWriter = null; 076 try { 077 documentReader = makeDocumentReader(session, doc, exportAsTree, exportAsZip); 078 documentWriter = makeDocumentWriter(new FileOutputStream(tempFile), exportAsTree, exportAsZip); 079 DocumentPipe pipe = makePipe(exportAsTree); 080 pipe.setReader(documentReader); 081 pipe.setWriter(documentWriter); 082 pipe.run(); 083 } finally { 084 if (documentReader != null) { 085 documentReader.close(); 086 } 087 if (documentWriter != null) { 088 documentWriter.close(); 089 } 090 } 091 092 String filename = exportAsZip ? "export.zip" : "document.xml"; 093 String mimeType = exportAsZip ? "application/zip" : "text/xml"; 094 return Blobs.createBlob(tempFile, mimeType, null, filename); 095 } 096 097 protected DocumentPipe makePipe(boolean exportAsTree) { 098 if (exportAsTree) { 099 return new DocumentPipeImpl(10); 100 } else { 101 return new DocumentPipeImpl(); 102 } 103 } 104 105 protected DocumentReader makeDocumentReader(CoreSession session, DocumentModel doc, boolean exportAsTree, 106 boolean exportAsZip) { 107 DocumentReader documentReader; 108 if (exportAsTree) { 109 documentReader = new DocumentTreeReader(session, doc, false); 110 if (!exportAsZip) { 111 ((DocumentTreeReader) documentReader).setInlineBlobs(true); 112 } 113 } else { 114 documentReader = new SingleDocumentReader(session, doc); 115 } 116 return documentReader; 117 } 118 119 protected DocumentWriter makeDocumentWriter(OutputStream outputStream, boolean exportAsTree, boolean exportAsZip) 120 throws IOException { 121 DocumentWriter documentWriter; 122 if (exportAsZip) { 123 documentWriter = new NuxeoArchiveWriter(outputStream); 124 } else { 125 if (exportAsTree) { 126 documentWriter = new XMLDocumentTreeWriter(outputStream); 127 } else { 128 documentWriter = new XMLDocumentWriter(outputStream); 129 } 130 } 131 return documentWriter; 132 } 133}