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