001/* 002 * (C) Copyright 2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 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.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 * Florent Guillaume 016 * 017 * $Id: ExportRepresentation.java 30251 2008-02-18 19:17:33Z fguillaume $ 018 */ 019 020package org.nuxeo.ecm.platform.ui.web.restAPI; 021 022import java.io.IOException; 023import java.io.OutputStream; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.core.api.CoreInstance; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.IdRef; 031import org.nuxeo.ecm.core.io.DocumentPipe; 032import org.nuxeo.ecm.core.io.DocumentReader; 033import org.nuxeo.ecm.core.io.DocumentWriter; 034import org.restlet.data.MediaType; 035import org.restlet.resource.OutputRepresentation; 036 037/** 038 * Facelet resource representation that calls a {@link DocumentPipe} using the facelet's output stream for the document 039 * writer's output. 040 * <p> 041 * This abstract method must be subclassed to implement {@link #makePipe}, {@link #makeDocumentReader} and 042 * {@link #makeDocumentWriter}. 043 * 044 * @author Florent Guillaume 045 */ 046public abstract class ExportRepresentation extends OutputRepresentation { 047 048 private static final Log log = LogFactory.getLog(ExportRepresentation.class); 049 050 protected final String repositoryName; 051 052 protected final String rootId; 053 054 protected final boolean isUnrestricted; 055 056 protected ExportRepresentation(MediaType mediaType, DocumentModel root) { 057 this(mediaType, root, false); 058 } 059 060 protected ExportRepresentation(MediaType mediaType, DocumentModel root, boolean unrestricted) { 061 super(mediaType); 062 repositoryName = root.getRepositoryName(); 063 rootId = root.getId(); 064 isUnrestricted = unrestricted; 065 } 066 067 /** 068 * Create a {@link DocumentPipe} adequate for the number of documents needed by the export. 069 * 070 * @return the document pipe. 071 */ 072 protected abstract DocumentPipe makePipe(); 073 074 /** 075 * Create a {@link DocumentReader} for the export. 076 * 077 * @param documentManager a session 078 * @param root the root of the export 079 * @return the document reader 080 */ 081 protected abstract DocumentReader makeDocumentReader(CoreSession documentManager, DocumentModel root); 082 083 /** 084 * Create a {@link DocumentWriter} for the export. 085 * 086 * @param outputStream the stream to use 087 * @return the document writer 088 */ 089 protected abstract DocumentWriter makeDocumentWriter(OutputStream outputStream) throws IOException; 090 091 @Override 092 public void write(OutputStream outputStream) throws IOException { 093 CoreSession session; 094 if (isUnrestricted) { 095 session = CoreInstance.openCoreSessionSystem(repositoryName); 096 } else { 097 session = CoreInstance.openCoreSession(repositoryName); 098 } 099 try { 100 DocumentReader documentReader = null; 101 DocumentWriter documentWriter = null; 102 try { 103 DocumentModel root = session.getDocument(new IdRef(rootId)); 104 documentReader = makeDocumentReader(session, root); 105 documentWriter = makeDocumentWriter(outputStream); 106 DocumentPipe pipe = makePipe(); 107 pipe.setReader(documentReader); 108 pipe.setWriter(documentWriter); 109 pipe.run(); 110 } finally { 111 if (documentReader != null) { 112 documentReader.close(); 113 } 114 if (documentWriter != null) { 115 documentWriter.close(); 116 } 117 } 118 } finally { 119 session.close(); 120 } 121 } 122 123}