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; 029 030import java.io.OutputStream; 031 032/** 033 * Storage for generated image cache entries.<p> 034 */ 035public interface I_CmsImageCache extends AutoCloseable { 036 037 /** 038 * Visitor for image cache entries.<p> 039 */ 040 interface I_CmsImageCacheEntryVisitor { 041 042 /** 043 * Visits an image cache entry.<p> 044 * 045 * @param key the image cache key 046 * @param length the image cache entry length 047 * @throws Exception if visiting fails 048 */ 049 void visit(String key, long length) throws Exception; 050 } 051 052 /** 053 * Clears all image cache entries.<p> 054 * 055 * @throws Exception if clearing fails 056 */ 057 default void clear() throws Exception { 058 059 throw new UnsupportedOperationException("Clearing this image cache is not supported."); 060 } 061 062 /** 063 * Closes the image cache and releases associated resources.<p> 064 * 065 * @throws Exception if closing fails 066 */ 067 default void close() throws Exception { 068 069 // default no-op 070 } 071 072 /** 073 * Returns if the image cache entry exists.<p> 074 * 075 * @param key the image cache key 076 * @return <code>true</code> if the image cache entry exists 077 * @throws Exception if the store access fails 078 */ 079 boolean exists(String key) throws Exception; 080 081 /** 082 * Performs an authoritative existence check which does not rely on locally cached positive metadata.<p> 083 * 084 * This is used for requests which do not read the cache entry body and can therefore not detect a stale positive 085 * existence result while streaming. Implementations without a positive metadata cache can use the default 086 * implementation.<p> 087 * 088 * @param key the image cache key 089 * @return <code>true</code> if the image cache entry exists in the backing store 090 * @throws Exception if the store access fails 091 */ 092 default boolean existsAuthoritatively(String key) throws Exception { 093 094 return exists(key); 095 } 096 097 /** 098 * Returns the image cache entry length.<p> 099 * 100 * @param key the image cache key 101 * @return the image cache entry length 102 * @throws Exception if the store access fails 103 */ 104 long getLength(String key) throws Exception; 105 106 /** 107 * Returns if range delivery is supported.<p> 108 * 109 * @return <code>true</code> if ranges can be streamed 110 */ 111 boolean supportsRangeDelivery(); 112 113 /** 114 * Visits all image cache entries.<p> 115 * 116 * @param visitor the image cache entry visitor 117 * @throws Exception if listing fails 118 */ 119 default void visitEntries(I_CmsImageCacheEntryVisitor visitor) throws Exception { 120 121 throw new UnsupportedOperationException("Listing this image cache is not supported."); 122 } 123 124 /** 125 * Visits image cache entries whose normalized keys start with the given prefix.<p> 126 * 127 * Implementations with a prefix-aware storage backend should override this method to apply the prefix while 128 * listing. The default implementation filters the result of {@link #visitEntries(I_CmsImageCacheEntryVisitor)}. 129 * 130 * @param prefix the image cache key prefix, or an empty string for all entries 131 * @param visitor the image cache entry visitor 132 * @throws Exception if listing fails 133 */ 134 default void visitEntries(String prefix, I_CmsImageCacheEntryVisitor visitor) throws Exception { 135 136 String normalizedPrefix = prefix == null ? "" : prefix; 137 while (normalizedPrefix.startsWith("/")) { 138 normalizedPrefix = normalizedPrefix.substring(1); 139 } 140 final String keyPrefix = normalizedPrefix; 141 visitEntries((key, length) -> { 142 String normalizedKey = key; 143 while (normalizedKey.startsWith("/")) { 144 normalizedKey = normalizedKey.substring(1); 145 } 146 if (normalizedKey.startsWith(keyPrefix)) { 147 visitor.visit(key, length); 148 } 149 }); 150 } 151 152 /** 153 * Writes an image cache entry.<p> 154 * 155 * @param key the image cache key 156 * @param content the image cache content 157 * @throws Exception if writing fails 158 */ 159 void write(String key, byte[] content) throws Exception; 160 161 /** 162 * Writes an image cache entry byte range to the output stream.<p> 163 * 164 * @param key the image cache key 165 * @param start the first byte to write 166 * @param length the number of bytes to write 167 * @param out the output stream 168 * @throws Exception if reading fails 169 */ 170 void writeRangeTo(String key, long start, long length, OutputStream out) throws Exception; 171 172 /** 173 * Writes an image cache entry to the output stream.<p> 174 * 175 * @param key the image cache key 176 * @param out the output stream 177 * @throws Exception if reading fails 178 */ 179 void writeTo(String key, OutputStream out) throws Exception; 180}