diff --git a/cfg/beetroot.cfg b/cfg/beetroot.cfg index 080202ec..3ecbe273 100644 --- a/cfg/beetroot.cfg +++ b/cfg/beetroot.cfg @@ -350,7 +350,7 @@ ws_cache_size=2 # File cache size in kB. # Upper limit to cache a file. # -ws_file_cache_size=100 +ws_file_cache_size=200 # # Response buffer size in kBytes. diff --git a/cfg/beetroot_dist.cfg b/cfg/beetroot_dist.cfg index 9a891def..e823cb0f 100644 --- a/cfg/beetroot_dist.cfg +++ b/cfg/beetroot_dist.cfg @@ -350,7 +350,7 @@ ws_cache_size=2 # File cache size in kB. # Upper limit to cache a file. # -ws_file_cache_size=100 +ws_file_cache_size=200 # # Response buffer size in kBytes. diff --git a/cfg/beetroot_test.cfg b/cfg/beetroot_test.cfg index 268dc180..2541d320 100644 --- a/cfg/beetroot_test.cfg +++ b/cfg/beetroot_test.cfg @@ -344,13 +344,13 @@ ws_use_csrf_tokens=yes # # Overall max. cache size in MB. # -ws_cache_size=2 +ws_cache_size=1 # # File cache size in kB. # Upper limit to cache a file. # -ws_file_cache_size=100 +ws_file_cache_size=200 # # Response buffer size in kBytes. @@ -748,6 +748,7 @@ ws_mime_allowed_octet = image/png \ # ws_mime_allowed_archive = application/zip \ application/gzip \ - application/x-tar + application/x-tar \ + application/java-archive diff --git a/cfg/logging-dist.xml b/cfg/logging-dist.xml index fbfe92f0..e786dec2 100644 --- a/cfg/logging-dist.xml +++ b/cfg/logging-dist.xml @@ -38,7 +38,7 @@ - + @@ -47,11 +47,13 @@ + + diff --git a/cfg/logging.xml b/cfg/logging.xml index e89fa908..6377c665 100644 --- a/cfg/logging.xml +++ b/cfg/logging.xml @@ -5,7 +5,7 @@ Version: 3.0 - 2023 autumo GmbH --> - + ${sys:ROOTPATH}/log @@ -39,7 +39,7 @@ - + @@ -48,11 +48,13 @@ + + diff --git a/cfg/routing.xml b/cfg/routing.xml index 26a74870..4179ce42 100644 --- a/cfg/routing.xml +++ b/cfg/routing.xml @@ -64,6 +64,7 @@ + diff --git a/src/main/java/ch/autumo/beetroot/cache/FileCache.java b/src/main/java/ch/autumo/beetroot/cache/FileCache.java index f2f9ecf2..150bf78b 100644 --- a/src/main/java/ch/autumo/beetroot/cache/FileCache.java +++ b/src/main/java/ch/autumo/beetroot/cache/FileCache.java @@ -139,7 +139,7 @@ public FileCache(Path filePath, ContentType contentType) throws IOException { * * @param filePath file path * @param mimeType mime type, e.g. "text/html" - * @param forcedCaching force caching? + * @param forcedCaching force caching? Force caching breaks the file size limit, but not the cache size limit! * @throws IOException IO exception */ public FileCache(Path filePath, String mimeType, boolean forcedCaching) throws IOException { @@ -154,7 +154,7 @@ public FileCache(Path filePath, String mimeType, boolean forcedCaching) throws I * @param filePath file path * @param contentType content header type, e.g. * "text/html; charset=UTF-8" - * @param forcedCaching force caching? + * @param forcedCaching force caching? Force caching breaks the file size limit, but not the cache size limit! * @throws IOException IO exception */ public FileCache(Path filePath, ContentType contentType, boolean forcedCaching) throws IOException { @@ -196,29 +196,30 @@ public FileCache(Path filePath, ContentType contentType, boolean forcedCaching) /** * File cache constructor. + * Resources are immutable, hence no re-caching will be done. * * @param resourcePath resource path * @throws IOException IO exception */ public FileCache(String resourcePath) throws IOException { - this(resourcePath, (String) null); } /** * File cache constructor. + * Resources are immutable, hence no re-caching will be done. * * @param resourcePath resource path * @param mimeType mime type, e.g. "text/html" * @throws IOException IO exception */ public FileCache(String resourcePath, String mimeType) throws IOException { - this(resourcePath, getContentType(mimeType)); } /** * File cache constructor. + * Resources are immutable, hence no re-caching will be done. * * @param resourcePath resource path * @param contentType content header type, e.g. @@ -448,6 +449,14 @@ public long getLastModified() { return lastModified; } + /** + * Get file size. + * @return file size + */ + public long getFileSize() { + return fileSize; + } + /** * Is it an archive? * @return is it an archive @@ -458,6 +467,7 @@ public boolean isArchive() { /** * Is it a binary? + * Resources are immutable, hence no re-caching will be done. * @return is it a binary */ public boolean isBinary() { diff --git a/src/main/java/ch/autumo/beetroot/cache/FileCacheManager.java b/src/main/java/ch/autumo/beetroot/cache/FileCacheManager.java index a6f26813..93516488 100644 --- a/src/main/java/ch/autumo/beetroot/cache/FileCacheManager.java +++ b/src/main/java/ch/autumo/beetroot/cache/FileCacheManager.java @@ -59,7 +59,6 @@ public class FileCacheManager { private Map cacheMap = new ConcurrentHashMap(); private long size = 0; - private boolean maxSizeReached = false; /** @@ -82,19 +81,11 @@ public long getSize() { return size; } - /** - * Is max. cache size reached? - * @return true, if so - */ - public boolean isMaxSizeReached() { - return maxSizeReached; - } - /** * Is there space left in the cache? * * @param oldValueInBytes old amount of bytes that have been used - * for the sam resource + * for the same resource * @param newValueInBytes new amount of bytes that is used by the * changed resource * @return true, is there is space, otherwise false @@ -115,22 +106,15 @@ public synchronized boolean hasSpace(long oldValueInBytes, long newValueInBytes) * Update cache size. * * @param oldValueInBytes old amount of bytes that have been used - * for the sam resource + * for the same resource * @param newValueInBytes new amount of bytes that is used by the * changed resource - * @return true if max. cache size has been reached, otherwise false + * @return new cache size; */ - public synchronized boolean updateCacheSize(long oldValueInBytes, long newValueInBytes) { - + public synchronized long updateCacheSize(long oldValueInBytes, long newValueInBytes) { size -= oldValueInBytes; size += newValueInBytes; - - if (size > MAX_CACHE_SIZE) - maxSizeReached = true; - else - maxSizeReached = false; - - return maxSizeReached; + return size; } /** @@ -182,7 +166,8 @@ public FileCache findOrCreate(String path) throws IOException { * Find or create file cache. * * @param path file path - * @param forceCaching caching is forced when true if max. cache size isn't reached + * @param forcedCaching caching is forced when true if max. cache size isn't reached; + * force caching breaks the file size limit, but not the cache size limit! * @return file cache * @throws IOException IO exception */ @@ -219,7 +204,8 @@ public FileCache findOrCreate(Path path) throws IOException { * Find or create file cache. * * @param path resource path - * @param forcedCaching caching is forced when true if max. cache size isn't reached. + * @param forcedCaching caching is forced when true if max. cache size isn't reached; + * force caching breaks the file size limit, but not the cache size limit! * @return file cache * @throws IOException IO exception */ @@ -294,9 +280,17 @@ public void clear() { final FileCache fCache = cacheMap.get(key); fCache.clear(); } + this.size = 0; cacheMap.clear(); } + /** + * Get maximum size of this cache. + * @return max. size + */ + public long getMaxSize() { + return MAX_CACHE_SIZE; + } /** * Get normalized path. diff --git a/src/main/resources/empty b/src/main/resources/empty new file mode 100644 index 00000000..e69de29b diff --git a/src/test/java/ch/autumo/beetroot/DBManagerTest.java b/src/test/java/ch/autumo/beetroot/DBManagerTest.java index 02cac071..693e08d1 100644 --- a/src/test/java/ch/autumo/beetroot/DBManagerTest.java +++ b/src/test/java/ch/autumo/beetroot/DBManagerTest.java @@ -1,3 +1,20 @@ +/** + * + * Copyright (c) 2024 autumo Ltd. Switzerland, Michael Gasche + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ package ch.autumo.beetroot; import java.util.Iterator; @@ -8,6 +25,10 @@ import ch.autumo.beetroot.utils.DBField; + +/** + * DB manager test. See also {@link ModelTest}. + */ public class DBManagerTest { @BeforeClass diff --git a/src/test/java/ch/autumo/beetroot/ModelTest.java b/src/test/java/ch/autumo/beetroot/ModelTest.java index f0e768de..97a5f6f0 100644 --- a/src/test/java/ch/autumo/beetroot/ModelTest.java +++ b/src/test/java/ch/autumo/beetroot/ModelTest.java @@ -1,3 +1,20 @@ +/** + * + * Copyright (c) 2024 autumo Ltd. Switzerland, Michael Gasche + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ package ch.autumo.beetroot; import static org.junit.Assert.assertEquals; @@ -13,6 +30,7 @@ import ch.autumo.beetroot.models.Product; import ch.autumo.beetroot.models.Variant; + /** * Model test. * diff --git a/src/test/java/ch/autumo/beetroot/cache/FileCacheTest.java b/src/test/java/ch/autumo/beetroot/cache/FileCacheTest.java new file mode 100644 index 00000000..3efe00cf --- /dev/null +++ b/src/test/java/ch/autumo/beetroot/cache/FileCacheTest.java @@ -0,0 +1,150 @@ +/** + * + * Copyright (c) 2024 autumo Ltd. Switzerland, Michael Gasche + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package ch.autumo.beetroot.cache; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import ch.autumo.beetroot.BeetRootConfigurationManager; +import ch.autumo.beetroot.BeetRootDatabaseManager; + +/** + * File cache test. + */ +public class FileCacheTest { + + private static List allFiles = null; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + + BeetRootConfigurationManager.getInstance().initialize("cfg/beetroot_test.cfg"); + BeetRootDatabaseManager.getInstance().initialize(); + + allFiles = new ArrayList<>(); + + Path p1 = Paths.get("src/main/java"); + listAllFiles(p1, allFiles); + Path p2 = Paths.get("web/"); + listAllFiles(p2, allFiles); + Path p3 = Paths.get("doc/"); + listAllFiles(p3, allFiles); + } + + @Before + public void setUp() throws Exception { + } + + + @Test + public void testSize() throws IOException { + boolean hasSpace = false; + + //Path lpath = null; + LOOP: for (Iterator iterator = allFiles.iterator(); iterator.hasNext();) { + Path path = iterator.next(); + long fs = path.toFile().length(); + hasSpace =FileCacheManager.getInstance().hasSpace(0, fs); + if (hasSpace) { + FileCache fc = FileCacheManager.getInstance().findOrCreate(path); + System.out.println("FC: Adding "+fc.getFullPath()); + } else { + assertFalse("Still has space!", FileCacheManager.getInstance().hasSpace(0, fs)); + System.out.println("Size: "+ FileCacheManager.getInstance().getSize()); + System.out.println("====================="); + //lpath = path; + break LOOP; + } + } + /** + long size = FileCacheManager.getInstance().getSize(); + System.out.println("FC: Size = "+size); + System.out.println("Force cache last file... : " + lpath.getFileName()); + FileCacheManager.getInstance().findOrCreate(lpath, true); + long size2 = FileCacheManager.getInstance().getSize(); + System.out.println("FC: Size = "+FileCacheManager.getInstance().getSize()); + */ + + FileCacheManager.getInstance().clear(); + System.out.println("FC: Size = "+FileCacheManager.getInstance().getSize()); + assertTrue("File cahce should be zero!", FileCacheManager.getInstance().getSize() == 0); + } + + @Test + public void testBinary() throws IOException { + + Path p1 = Paths.get("lib/repo"); + listAllFiles(p1, allFiles, "jar"); + allFiles.forEach(System.out::println); + File f = allFiles.get(0).toFile(); + FileCache fc = FileCacheManager.getInstance().findOrCreate(f.toPath()); + System.out.println("FC: Adding binary file "+fc.getFullPath()); + assertTrue("File should not be cached!", fc.isCached() == false); + } + + + @After + public void tearDown() throws Exception { + FileCacheManager.getInstance().clear(); + allFiles.clear(); + } + + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + + private static void listAllFiles(Path currentPath, List allFiles, String... extensions) throws IOException { + try (DirectoryStream stream = Files.newDirectoryStream(currentPath)) { + for (Path entry : stream) { + if (Files.isDirectory(entry)) { + listAllFiles(entry, allFiles, extensions); + } else { + if (extensions == null || extensions.length == 0) { + allFiles.add(entry); + } else { + LOOP: for (int i = 0; i < extensions.length; i++) { + if (entry.toFile().getName().endsWith(extensions[i])) { + allFiles.add(entry); + break LOOP; + } + } + } + } + } + } + } + +} diff --git a/src/test/java/ch/autumo/beetroot/security/password/PasswordHashTest.java b/src/test/java/ch/autumo/beetroot/security/password/PasswordHashTest.java index de4a44b4..60a55fce 100644 --- a/src/test/java/ch/autumo/beetroot/security/password/PasswordHashTest.java +++ b/src/test/java/ch/autumo/beetroot/security/password/PasswordHashTest.java @@ -1,3 +1,20 @@ +/** + * + * Copyright (c) 2023 autumo Ltd. Switzerland, Michael Gasche + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ package ch.autumo.beetroot.security.password; import static org.junit.Assert.assertTrue; @@ -6,6 +23,10 @@ import ch.autumo.beetroot.BeetRootConfigurationManager; + +/** + * Test password implementations. + */ public class PasswordHashTest { @Test