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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.ecm.platform.filemanager.service.extension;
022
023import java.io.File;
024import java.io.IOException;
025import java.util.zip.ZipEntry;
026import java.util.zip.ZipException;
027import java.util.zip.ZipFile;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.common.utils.Path;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.CloseableFile;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentRef;
037import org.nuxeo.ecm.core.api.IdRef;
038import org.nuxeo.ecm.core.api.PathRef;
039import org.nuxeo.ecm.core.io.DocumentPipe;
040import org.nuxeo.ecm.core.io.DocumentReader;
041import org.nuxeo.ecm.core.io.DocumentWriter;
042import org.nuxeo.ecm.core.io.ExportedDocument;
043import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl;
044import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter;
045import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveReader;
046import org.nuxeo.ecm.platform.types.TypeManager;
047
048/**
049 * Simple Plugin that imports IO Zip archive into Nuxeo using the IO core service.
050 *
051 * @author tiry
052 */
053public class ExportedZipImporter extends AbstractFileImporter {
054
055    private static final long serialVersionUID = 1876876876L;
056
057    private static final Log log = LogFactory.getLog(ExportedZipImporter.class);
058
059    public static ZipFile getArchiveFileIfValid(File file) throws IOException {
060        ZipFile zip;
061
062        try {
063            zip = new ZipFile(file);
064        } catch (ZipException e) {
065            log.debug("file is not a zipfile ! ", e);
066            return null;
067        } catch (IOException e) {
068            log.debug("can not open zipfile ! ", e);
069            return null;
070        }
071
072        ZipEntry marker = zip.getEntry(".nuxeo-archive");
073
074        if (marker == null) {
075            zip.close();
076            return null;
077        } else {
078            return zip;
079        }
080    }
081
082    public DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite,
083            String filename, TypeManager typeService) throws IOException {
084        try (CloseableFile source = content.getCloseableFile()) {
085            ZipFile zip = getArchiveFileIfValid(source.getFile());
086            if (zip == null) {
087                return null;
088            }
089            zip.close();
090
091            boolean importWithIds = false;
092            DocumentReader reader = new NuxeoArchiveReader(source.getFile());
093            ExportedDocument root = reader.read();
094            IdRef rootRef = new IdRef(root.getId());
095
096            if (documentManager.exists(rootRef)) {
097                DocumentModel target = documentManager.getDocument(rootRef);
098                if (target.getPath().removeLastSegments(1).equals(new Path(path))) {
099                    importWithIds = true;
100                }
101            }
102
103            DocumentWriter writer = new DocumentModelWriter(documentManager, path, 10);
104            reader.close();
105            reader = new NuxeoArchiveReader(source.getFile());
106
107            DocumentRef resultingRef;
108            if (overwrite && importWithIds) {
109                resultingRef = rootRef;
110            } else {
111                String rootName = root.getPath().lastSegment();
112                resultingRef = new PathRef(path, rootName);
113            }
114
115            try {
116                DocumentPipe pipe = new DocumentPipeImpl(10);
117                pipe.setReader(reader);
118                pipe.setWriter(writer);
119                pipe.run();
120            } catch (IOException e) {
121                log.warn(e, e);
122            } finally {
123                reader.close();
124                writer.close();
125            }
126            return documentManager.getDocument(resultingRef);
127        }
128    }
129}