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