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.filemanager.api.FileImporterContext;
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    @Override
083    public boolean isOneToMany() {
084        return true;
085    }
086
087    @Override
088    public DocumentModel createOrUpdate(FileImporterContext context) throws IOException {
089        CoreSession session = context.getSession();
090        Blob blob = context.getBlob();
091        try (CloseableFile source = blob.getCloseableFile()) {
092            ZipFile zip = getArchiveFileIfValid(source.getFile());
093            if (zip == null) {
094                return null;
095            }
096            zip.close();
097
098            boolean importWithIds = false;
099            DocumentReader reader = new NuxeoArchiveReader(source.getFile());
100            ExportedDocument root = reader.read();
101            IdRef rootRef = new IdRef(root.getId());
102
103            String parentPath = context.getParentPath();
104            if (session.exists(rootRef)) {
105                DocumentModel target = session.getDocument(rootRef);
106                if (target.getPath().removeLastSegments(1).equals(new Path(parentPath))) {
107                    importWithIds = true;
108                }
109            }
110
111            DocumentWriter writer = new DocumentModelWriter(session, parentPath, 10);
112            reader.close();
113            reader = new NuxeoArchiveReader(source.getFile());
114
115            DocumentRef resultingRef;
116            if (context.isOverwrite() && importWithIds) {
117                resultingRef = rootRef;
118            } else {
119                String rootName = root.getPath().lastSegment();
120                resultingRef = new PathRef(parentPath, rootName);
121            }
122
123            try {
124                DocumentPipe pipe = new DocumentPipeImpl(10);
125                pipe.setReader(reader);
126                pipe.setWriter(writer);
127                pipe.run();
128            } catch (IOException e) {
129                log.warn(e, e);
130            } finally {
131                reader.close();
132                writer.close();
133            }
134            return session.getDocument(resultingRef);
135        }
136    }
137
138}