From 995aa3a61589432a1fd0d073714aadba11133621 Mon Sep 17 00:00:00 2001 From: kamiljarmusik Date: Wed, 22 Nov 2023 22:53:48 +0100 Subject: [PATCH] #2718 Mapping path static resources - databaseSource use from Spring container; refactor PermissionsUtils.java --- WebContent/WEB-INF/applicationContext.xml | 6 +++- .../serotonin/mango/db/BasePooledAccess.java | 34 +++++++++---------- .../service/util/PermissionsUtils.java | 10 ++++-- src/org/scada_lts/utils/PathSecureUtils.java | 6 ++-- .../scada_lts/utils/StaticImagesUtils.java | 2 +- src/org/scada_lts/utils/UploadFileUtils.java | 4 +-- 6 files changed, 35 insertions(+), 27 deletions(-) diff --git a/WebContent/WEB-INF/applicationContext.xml b/WebContent/WEB-INF/applicationContext.xml index 0e94f408f9..c51933e551 100644 --- a/WebContent/WEB-INF/applicationContext.xml +++ b/WebContent/WEB-INF/applicationContext.xml @@ -224,9 +224,13 @@ + + + + - + diff --git a/src/com/serotonin/mango/db/BasePooledAccess.java b/src/com/serotonin/mango/db/BasePooledAccess.java index 97cf86c680..13a56d73fb 100644 --- a/src/com/serotonin/mango/db/BasePooledAccess.java +++ b/src/com/serotonin/mango/db/BasePooledAccess.java @@ -23,8 +23,6 @@ import java.util.ArrayList; import java.util.List; -import javax.naming.InitialContext; -import javax.naming.NamingException; import javax.servlet.ServletContext; import javax.sql.DataSource; @@ -35,6 +33,8 @@ import com.serotonin.ShouldNeverHappenException; import com.serotonin.db.spring.ExtendedJdbcTemplate; import com.serotonin.mango.Common; +import org.scada_lts.web.beans.ApplicationBeans; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; /** * @author Matthew Lohbihler @@ -56,23 +56,23 @@ protected void initializeImpl(String propertyPrefix, String dataSourceName) { log.info("Initializing pooled connection manager"); - if(Common.getEnvironmentProfile().getString(propertyPrefix + "db.datasource", "false").equals("true")) - { - try - { - log.info("Looking for Datasource: " + Common.getEnvironmentProfile().getString(propertyPrefix + "db.datasourceName")); - dataSource = (DataSource) new InitialContext().lookup(Common.getEnvironmentProfile().getString(propertyPrefix + "db.datasourceName")); - try (Connection conn = dataSource.getConnection()) { - log.info("DataSource meta: " + conn.getMetaData().getDatabaseProductName() + " " + conn.getMetaData().getDatabaseProductVersion()); - dataSourceFound = true; + boolean datasource = Common.getEnvironmentProfile().getBoolean(propertyPrefix + "db.datasource", false); + + if(datasource) { + try { + log.info("Looking for Datasource: " + dataSourceName); + dataSource = ApplicationBeans.getBean("databaseSource", DataSource.class); + if(dataSource == null) { + log.info("Datasource not found! dataSourceName: " + dataSourceName); + } else { + try (Connection conn = dataSource.getConnection()) { + log.info("DataSource meta: " + conn.getMetaData().getDatabaseProductName() + " " + conn.getMetaData().getDatabaseProductVersion()); + dataSourceFound = true; + } } - } - catch(NamingException e) - { + } catch(IllegalArgumentException | NoSuchBeanDefinitionException e) { log.info("Datasource not found!" + e.getLocalizedMessage()); - } - catch(SQLException e) - { + } catch(SQLException e) { log.error("SQL Exception: " + e.getLocalizedMessage()); } } diff --git a/src/org/scada_lts/permissions/service/util/PermissionsUtils.java b/src/org/scada_lts/permissions/service/util/PermissionsUtils.java index 74f8674790..3adece697f 100644 --- a/src/org/scada_lts/permissions/service/util/PermissionsUtils.java +++ b/src/org/scada_lts/permissions/service/util/PermissionsUtils.java @@ -84,16 +84,16 @@ public static Set reduce(Set accesses, ToIntFunction getAccess, ToI } private static void updatePermissions(U user, List accessesFromUser, - PermissionsService service, + PermissionsService service, Comparator comparator) { List accessesFromDatabase = service.getPermissions(user); if(!sortEquals(accessesFromUser, accessesFromDatabase, comparator)) { - if(!accessesFromDatabase.isEmpty() && !accessesFromUser.containsAll(accessesFromDatabase)) { + if(!accessesFromDatabase.isEmpty() && !containsAll(accessesFromUser, accessesFromDatabase)) { List notExistInUser = diff(accessesFromUser, accessesFromDatabase); service.removePermissions(user, notExistInUser); accessesFromDatabase.removeAll(notExistInUser); } - if((accessesFromDatabase.isEmpty() || !accessesFromDatabase.containsAll(accessesFromUser)) + if((accessesFromDatabase.isEmpty() || !containsAll(accessesFromDatabase, accessesFromUser)) && !accessesFromUser.isEmpty()) { List noExistInDatabase = diff(accessesFromDatabase, accessesFromUser); service.addOrUpdatePermissions(user, noExistInDatabase); @@ -101,6 +101,10 @@ private static void updatePermissions(U user, List accessesFromUser, } } + private static boolean containsAll(List accesses1, List accesses2) { + return new HashSet<>(accesses1).containsAll(accesses2); + } + private static void update(U user, List permissionsFromUser, PermissionsService service, diff --git a/src/org/scada_lts/utils/PathSecureUtils.java b/src/org/scada_lts/utils/PathSecureUtils.java index 8856c4a0d8..10e653a200 100644 --- a/src/org/scada_lts/utils/PathSecureUtils.java +++ b/src/org/scada_lts/utils/PathSecureUtils.java @@ -32,17 +32,17 @@ public static Optional toSecurePath(Path path, BinaryOperator reduce return normalizePath(path, reduce).map(Path::toFile); } - public static Path getAppContextSystemFilePath(Path folder) { + public static Path getAppContextSystemFilePath(String folder) { if(folder == null) throw new NullPointerException(); - String realPath = Common.ctx.getServletContext().getRealPath(normalizeSeparator(folder.toString())); + String realPath = Common.ctx.getServletContext().getRealPath(normalizeSeparator(decodePath(folder))); if(realPath == null) return Paths.get(""); return Paths.get(realPath); } public static Path getAppContextSystemFilePath() { - return getAppContextSystemFilePath(Paths.get(File.separator)); + return getAppContextSystemFilePath(File.separator); } public static String getPartialPath(File file) { diff --git a/src/org/scada_lts/utils/StaticImagesUtils.java b/src/org/scada_lts/utils/StaticImagesUtils.java index a366408ef0..5930057830 100644 --- a/src/org/scada_lts/utils/StaticImagesUtils.java +++ b/src/org/scada_lts/utils/StaticImagesUtils.java @@ -75,7 +75,7 @@ private static File getSystemFileByRequest(HttpServletRequest request) { path = getUploadsSystemFilePath(Paths.get(url)); } if(StringUtils.isEmpty(path.toString())) - path = PathSecureUtils.getAppContextSystemFilePath(Paths.get(url)); + path = PathSecureUtils.getAppContextSystemFilePath(url); return path.toFile(); } diff --git a/src/org/scada_lts/utils/UploadFileUtils.java b/src/org/scada_lts/utils/UploadFileUtils.java index 53d1b6dbb8..3793dee850 100644 --- a/src/org/scada_lts/utils/UploadFileUtils.java +++ b/src/org/scada_lts/utils/UploadFileUtils.java @@ -319,7 +319,7 @@ private static List getImageSystemFilePaths(Supplier getLocalPath, createIfNotExists(path); paths.add(path); } - Path path = getAppContextSystemFilePath(Paths.get(normalizeFolder)); + Path path = getAppContextSystemFilePath(normalizeFolder); createIfNotExists(path); paths.add(path); return paths; @@ -333,7 +333,7 @@ private static Path getImageSystemFileToWritePath(Supplier getLocalPath, || normalizedPath.endsWith(normalizedFolder + File.separator))) { path = getAbsoluteResourcePath(normalizedPath); } else { - path = getAppContextSystemFilePath(Paths.get(normalizedFolder)); + path = getAppContextSystemFilePath(normalizedFolder); } createIfNotExists(path); return path;