001/* 002 * (C) Copyright 2006-2009 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 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.filemanager.core.listener; 023 024import java.io.Serializable; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028 029import org.apache.logging.log4j.LogManager; 030import org.apache.logging.log4j.Logger; 031import org.nuxeo.ecm.core.api.Blob; 032import org.nuxeo.ecm.core.api.CoreSession; 033import org.nuxeo.ecm.core.api.DocumentLocation; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.core.api.NuxeoPrincipal; 036import org.nuxeo.ecm.core.api.event.DocumentEventCategories; 037import org.nuxeo.ecm.core.api.model.PropertyNotFoundException; 038import org.nuxeo.ecm.core.event.Event; 039import org.nuxeo.ecm.core.event.EventProducer; 040import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 041import org.nuxeo.ecm.platform.filemanager.api.FileManager; 042import org.nuxeo.runtime.api.Framework; 043 044public abstract class AbstractUnicityChecker { 045 046 private static final Logger log = LogManager.getLogger(AbstractUnicityChecker.class); 047 048 protected static final String DUPLICATED_FILE = "duplicatedFile"; 049 050 protected void doUnicityCheck(DocumentModel doc2Check, CoreSession session, Event event) { 051 052 List<String> xpathFields = Framework.getService(FileManager.class).getFields(); 053 054 if (xpathFields == null || xpathFields.isEmpty()) { 055 log.info("Unicity check has been automatically disabled"); 056 return; 057 } 058 059 for (String field : xpathFields) { 060 Blob blob; 061 try { 062 blob = (Blob) doc2Check.getPropertyValue(field); 063 } catch (PropertyNotFoundException pnfe) { 064 continue; 065 } 066 if (blob == null) { 067 log.debug("No blob retrieved"); 068 continue; 069 } 070 071 String digest = blob.getDigest(); 072 if (digest == null) { 073 log.debug("Blob has no digest, can not check for unicity"); 074 continue; 075 } 076 077 List<DocumentLocation> existingDocuments = // 078 Framework.getService(FileManager.class) 079 .findExistingDocumentWithFile(session, doc2Check.getPathAsString(), digest, 080 session.getPrincipal()); 081 082 if (!existingDocuments.isEmpty()) { 083 existingDocuments.removeIf(documentLocation -> documentLocation.getDocRef() == doc2Check.getRef()); 084 log.debug("Existing Documents[{}]", existingDocuments::size); 085 086 onDuplicatedDoc(session, session.getPrincipal(), doc2Check, existingDocuments, event); 087 } 088 } 089 } 090 091 protected abstract void onDuplicatedDoc(CoreSession session, NuxeoPrincipal principal, DocumentModel newDoc, 092 List<DocumentLocation> existingDocs, Event event); 093 094 protected void raiseDuplicatedFileEvent(CoreSession session, NuxeoPrincipal principal, DocumentModel newDoc, 095 List<DocumentLocation> existingDocs) { 096 097 DocumentEventContext ctx = new DocumentEventContext(session, principal, newDoc); 098 099 Map<String, Serializable> props = new HashMap<>(); 100 props.put("category", DocumentEventCategories.EVENT_CLIENT_NOTIF_CATEGORY); 101 props.put("duplicatedDocLocation", (Serializable) existingDocs); 102 ctx.setProperties(props); 103 104 Event event = ctx.newEvent(DUPLICATED_FILE); 105 EventProducer producer = Framework.getService(EventProducer.class); 106 producer.fireEvent(event); 107 } 108 109 protected boolean isUnicityCheckEnabled() { 110 return Framework.getService(FileManager.class).isUnicityEnabled(); 111 } 112 113}