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.CoreInstance;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.IdRef;
033import org.nuxeo.ecm.core.event.EventBundle;
034import org.nuxeo.ecm.core.event.PostCommitEventListener;
035import org.nuxeo.ecm.directory.Session;
036
037/**
038 * Handles expired WOPI locks by unlocking the related document and removing the stored lock.
039 *
040 * @since 10.3
041 */
042public class LockExpirationListener implements PostCommitEventListener {
043
044    private static final Logger log = LogManager.getLogger(LockExpirationListener.class);
045
046    @Override
047    public void handleEvent(EventBundle eventBundle) {
048        LockHelper.doPrivilegedOnLockDirectory(this::handleExpiredLocks);
049    }
050
051    protected void handleExpiredLocks(Session directorySession) {
052        LockHelper.getExpiredLocksByRepository(directorySession).entrySet().forEach(entry -> {
053            String repository = entry.getKey();
054            List<DocumentModel> lockEntries = entry.getValue();
055            CoreSession session = CoreInstance.getCoreSession(repository);
056            lockEntries.forEach(lockEntry -> handleExpiredLock(session, directorySession, lockEntry));
057        });
058    }
059
060    protected void handleExpiredLock(CoreSession session, Session directorySession, DocumentModel entry) {
061        String docId = (String) entry.getProperty(LOCK_DIRECTORY_SCHEMA_NAME, LOCK_DIRECTORY_DOC_ID);
062        log.debug(
063                "Locking:  repository={} docId={} WOPI lock expired, unlocking document and removing lock from directory",
064                session::getRepositoryName, () -> docId);
065        // unlock document
066        session.removeLock(new IdRef(docId));
067        // remove WOPI lock from directory
068        directorySession.deleteEntry((String) entry.getProperty(LOCK_DIRECTORY_SCHEMA_NAME, LOCK_DIRECTORY_FILE_ID));
069    }
070
071}