001/* 002 * (C) Copyright 2018 Nuxeo (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 * Antoine Taillefer <ataillefer@nuxeo.com> 018 */ 019package org.nuxeo.wopi.lock; 020 021import static org.nuxeo.wopi.Constants.LOCK_DIRECTORY_DOC_ID; 022import static org.nuxeo.wopi.Constants.LOCK_DIRECTORY_FILE_ID; 023import static org.nuxeo.wopi.Constants.LOCK_DIRECTORY_SCHEMA_NAME; 024 025import java.util.List; 026 027import org.apache.logging.log4j.LogManager; 028import org.apache.logging.log4j.Logger; 029import org.nuxeo.ecm.core.api.CloseableCoreSession; 030import org.nuxeo.ecm.core.api.CoreInstance; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.IdRef; 034import org.nuxeo.ecm.core.event.EventBundle; 035import org.nuxeo.ecm.core.event.PostCommitEventListener; 036import org.nuxeo.ecm.directory.Session; 037 038/** 039 * Handles expired WOPI locks by unlocking the related document and removing the stored lock. 040 * 041 * @since 10.3 042 */ 043public class LockExpirationListener implements PostCommitEventListener { 044 045 private static final Logger log = LogManager.getLogger(LockExpirationListener.class); 046 047 @Override 048 public void handleEvent(EventBundle eventBundle) { 049 LockHelper.doPrivilegedOnLockDirectory(this::handleExpiredLocks); 050 } 051 052 protected void handleExpiredLocks(Session directorySession) { 053 LockHelper.getExpiredLocksByRepository(directorySession).entrySet().forEach(entry -> { 054 String repository = entry.getKey(); 055 List<DocumentModel> lockEntries = entry.getValue(); 056 try (CloseableCoreSession session = CoreInstance.openCoreSession(repository)) { 057 lockEntries.forEach(lockEntry -> handleExpiredLock(session, directorySession, lockEntry)); 058 } 059 }); 060 } 061 062 protected void handleExpiredLock(CoreSession session, Session directorySession, DocumentModel entry) { 063 String docId = (String) entry.getProperty(LOCK_DIRECTORY_SCHEMA_NAME, LOCK_DIRECTORY_DOC_ID); 064 log.debug( 065 "Locking: repository={} docId={} WOPI lock expired, unlocking document and removing lock from directory", 066 session::getRepositoryName, () -> docId); 067 // unlock document 068 session.removeLock(new IdRef(docId)); 069 // remove WOPI lock from directory 070 directorySession.deleteEntry((String) entry.getProperty(LOCK_DIRECTORY_SCHEMA_NAME, LOCK_DIRECTORY_FILE_ID)); 071 } 072 073}