001/*
002 * This library is part of OpenCms -
003 * The Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.loader.imagecache;
029
030/**
031 * Backend SPI for image cache maintenance.<p>
032 */
033public interface I_CmsImageCacheMaintenance {
034
035    /**
036     * Executes a maintenance request.<p>
037     *
038     * @param request the request
039     * @return the maintenance result
040     * @throws Exception if the request can not be completed
041     */
042    CmsImageCacheMaintenanceResult execute(CmsImageCacheMaintenanceRequest request) throws Exception;
043
044    /**
045     * Returns the backend identifier used for diagnostics and metrics.<p>
046     *
047     * @return the backend identifier
048     */
049    String getBackendId();
050
051    /**
052     * Returns the backend capabilities.<p>
053     *
054     * @return the backend capabilities
055     */
056    CmsImageCacheCapabilities getCapabilities();
057
058    /**
059     * Loads metadata for a single image cache entry.<p>
060     *
061     * @param key the image cache key
062     * @return the entry metadata, or {@code null} if the entry does not exist
063     * @throws Exception if the metadata can not be loaded
064     */
065    default CmsImageCacheEntry getEntry(String key) throws Exception {
066
067        String normalizedKey = key;
068        while (normalizedKey.startsWith("/")) {
069            normalizedKey = normalizedKey.substring(1);
070        }
071        final String expectedKey = normalizedKey;
072        CmsImageCacheEntry[] result = new CmsImageCacheEntry[1];
073        visitEntries(entry -> {
074            String entryKey = entry.getKey();
075            while (entryKey.startsWith("/")) {
076                entryKey = entryKey.substring(1);
077            }
078            if (expectedKey.equals(entryKey)) {
079                result[0] = entry;
080            }
081        });
082        return result[0];
083    }
084
085    /**
086     * Streams all image cache entries to a visitor.<p>
087     *
088     * @param visitor the entry visitor
089     * @throws Exception if listing fails
090     */
091    void visitEntries(I_CmsImageCacheMaintenanceEntryVisitor visitor) throws Exception;
092}