001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 *
019 * $Id: NuxeoArchiveWriter.java 29029 2008-01-14 18:38:14Z ldoguin $
020 */
021
022package org.nuxeo.ecm.core.io.impl.plugins;
023
024import java.io.BufferedOutputStream;
025import java.io.File;
026import java.io.FileOutputStream;
027import java.io.IOException;
028import java.io.InputStream;
029import java.io.OutputStream;
030import java.util.Map;
031import java.util.zip.Deflater;
032import java.util.zip.ZipEntry;
033import java.util.zip.ZipOutputStream;
034
035import org.apache.commons.io.IOUtils;
036import org.dom4j.Document;
037import org.dom4j.io.OutputFormat;
038import org.dom4j.io.XMLWriter;
039import org.nuxeo.ecm.core.api.Blob;
040import org.nuxeo.ecm.core.api.DocumentLocation;
041import org.nuxeo.ecm.core.api.DocumentRef;
042import org.nuxeo.ecm.core.io.DocumentTranslationMap;
043import org.nuxeo.ecm.core.io.ExportConstants;
044import org.nuxeo.ecm.core.io.ExportedDocument;
045import org.nuxeo.ecm.core.io.impl.AbstractDocumentWriter;
046import org.nuxeo.ecm.core.io.impl.DWord;
047import org.nuxeo.ecm.core.io.impl.DocumentTranslationMapImpl;
048
049/**
050 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
051 */
052public class NuxeoArchiveWriter extends AbstractDocumentWriter {
053
054    protected ZipOutputStream out;
055
056    public NuxeoArchiveWriter(File destination) throws IOException {
057        this(new BufferedOutputStream(new FileOutputStream(destination)), Deflater.DEFAULT_COMPRESSION);
058    }
059
060    public NuxeoArchiveWriter(File destination, int compressionLevel) throws IOException {
061        this(new BufferedOutputStream(new FileOutputStream(destination)), compressionLevel);
062    }
063
064    public NuxeoArchiveWriter(OutputStream out) throws IOException {
065        this(new ZipOutputStream(out), Deflater.DEFAULT_COMPRESSION);
066    }
067
068    public NuxeoArchiveWriter(OutputStream out, int compressionLevel) throws IOException {
069        this(new ZipOutputStream(out), compressionLevel);
070    }
071
072    public NuxeoArchiveWriter(ZipOutputStream out) throws IOException {
073        this(out, Deflater.DEFAULT_COMPRESSION);
074    }
075
076    public NuxeoArchiveWriter(ZipOutputStream out, int compressionLevel) throws IOException {
077        this.out = out;
078        this.out.setLevel(compressionLevel);
079        setComment("");
080        // write the marker entry
081        writeMarker();
082    }
083
084    public void setComment(String comment) {
085        if (out != null) {
086            out.setComment(ExportConstants.ZIP_HEADER + "\r\n" + comment);
087        }
088    }
089
090    @Override
091    public DocumentTranslationMap write(ExportedDocument doc) throws IOException {
092        String path = doc.getPath().toString();
093        writeDocument(path, doc);
094        // keep location unchanged
095        DocumentLocation oldLoc = doc.getSourceLocation();
096        String oldServerName = oldLoc.getServerName();
097        DocumentRef oldDocRef = oldLoc.getDocRef();
098        DocumentTranslationMap map = new DocumentTranslationMapImpl(oldServerName, oldServerName);
099        map.put(oldDocRef, oldDocRef);
100        return map;
101    }
102
103    @Override
104    public void close() {
105        if (out != null) {
106            try {
107                out.close();
108            } catch (IOException e) {
109                // do nothing
110            } finally {
111                out = null;
112            }
113        }
114    }
115
116    protected void writeDocument(String path, ExportedDocument doc) throws IOException {
117
118        if (path.equals("/") || path.length() == 0) {
119            path = "";
120        } else { // avoid adding a root entry
121            path += '/';
122            ZipEntry entry = new ZipEntry(path);
123            // store the number of child as an extra info on the entry
124            entry.setExtra(new DWord(doc.getFilesCount()).getBytes());
125            out.putNextEntry(entry);
126            out.closeEntry();
127            // System.out.println(">> add entry: "+entry.getName());
128        }
129
130        // write metadata
131        ZipEntry entry = new ZipEntry(path + ExportConstants.DOCUMENT_FILE);
132        out.putNextEntry(entry);
133        try {
134            writeXML(doc.getDocument(), out);
135        } finally {
136            out.closeEntry();
137            // System.out.println(">> add entry: "+entry.getName());
138        }
139
140        // write external documents
141        for (Map.Entry<String, Document> ext : doc.getDocuments().entrySet()) {
142            String fileName = ext.getKey() + ".xml";
143            entry = new ZipEntry(path + fileName);
144            out.putNextEntry(entry);
145            try {
146                writeXML(ext.getValue(), out);
147            } finally {
148                out.closeEntry();
149            }
150        }
151
152        // write blobs
153        Map<String, Blob> blobs = doc.getBlobs();
154        for (Map.Entry<String, Blob> blobEntry : blobs.entrySet()) {
155            String fileName = blobEntry.getKey();
156            entry = new ZipEntry(path + fileName);
157            out.putNextEntry(entry);
158            try (InputStream in = blobEntry.getValue().getStream()) {
159                IOUtils.copy(in, out);
160            }
161            // DO NOT CALL out.close(), we want to keep writing to it
162            out.closeEntry();
163        }
164    }
165
166    protected static void writeXML(Document doc, OutputStream out) throws IOException {
167        OutputFormat format = AbstractDocumentWriter.createPrettyPrint();
168        XMLWriter writer = new XMLWriter(out, format);
169        writer.write(doc);
170    }
171
172    protected void writeMarker() throws IOException {
173        ZipEntry entry = new ZipEntry(ExportConstants.MARKER_FILE);
174        out.putNextEntry(entry);
175        out.closeEntry();
176    }
177}