001/* 002 * (C) Copyright 2012 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 * Mariana Cedica 018 */ 019package org.nuxeo.ecm.platform.routing.core.persistence; 020 021import java.io.IOException; 022import java.util.zip.ZipFile; 023 024import org.nuxeo.common.utils.Path; 025import org.nuxeo.ecm.core.api.Blob; 026import org.nuxeo.ecm.core.api.CloseableFile; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.DocumentRef; 030import org.nuxeo.ecm.core.api.PathRef; 031import org.nuxeo.ecm.core.api.security.ACP; 032import org.nuxeo.ecm.core.io.DocumentPipe; 033import org.nuxeo.ecm.core.io.DocumentReader; 034import org.nuxeo.ecm.core.io.DocumentWriter; 035import org.nuxeo.ecm.core.io.ExportedDocument; 036import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl; 037import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter; 038import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveReader; 039import org.nuxeo.ecm.platform.filemanager.service.extension.ExportedZipImporter; 040import org.nuxeo.ecm.platform.types.TypeManager; 041 042/** 043 * Imports a route document from a zip archive using the IO core service . Existing route model with the same path as 044 * the are one to be imported is deleted before import. 045 * 046 * @since 5.6 047 */ 048public class RouteModelsZipImporter extends ExportedZipImporter { 049 050 private static final long serialVersionUID = 1L; 051 052 @Override 053 public DocumentModel create(CoreSession session, Blob content, String path, boolean overwrite, String filename, 054 TypeManager typeService) throws IOException { 055 try (CloseableFile source = content.getCloseableFile()) { 056 ZipFile zip = getArchiveFileIfValid(source.getFile()); 057 if (zip == null) { 058 return null; 059 } 060 zip.close(); 061 062 boolean overWrite = false; 063 DocumentReader reader = new NuxeoArchiveReader(source.getFile()); 064 ExportedDocument root = reader.read(); 065 PathRef rootRef = new PathRef(path, root.getPath().toString()); 066 ACP currentRouteModelACP = null; 067 if (session.exists(rootRef)) { 068 DocumentModel target = session.getDocument(rootRef); 069 if (target.getPath().removeLastSegments(1).equals(new Path(path))) { 070 overWrite = true; 071 // clean up existing route before import 072 DocumentModel routeModel = session.getDocument(rootRef); 073 currentRouteModelACP = routeModel.getACP(); 074 session.removeDocument(rootRef); 075 } 076 } 077 078 DocumentWriter writer = new DocumentModelWriter(session, path, 10); 079 reader.close(); 080 reader = new NuxeoArchiveReader(source.getFile()); 081 082 DocumentRef resultingRef; 083 if (overwrite && overWrite) { 084 resultingRef = rootRef; 085 } else { 086 String rootName = root.getPath().lastSegment(); 087 resultingRef = new PathRef(path, rootName); 088 } 089 090 try { 091 DocumentPipe pipe = new DocumentPipeImpl(10); 092 pipe.setReader(reader); 093 pipe.setWriter(writer); 094 pipe.run(); 095 } finally { 096 reader.close(); 097 writer.close(); 098 } 099 100 DocumentModel newRouteModel = session.getDocument(resultingRef); 101 if (currentRouteModelACP != null && overwrite && overWrite) { 102 newRouteModel.setACP(currentRouteModelACP, true); 103 } 104 return session.saveDocument(newRouteModel); 105 } 106 } 107}