Skip to content

Commit

Permalink
#2718 Mapping path static resources - databaseSource use from Spring …
Browse files Browse the repository at this point in the history
…container; refactor PermissionsUtils.java
  • Loading branch information
Limraj committed Nov 22, 2023
1 parent 1e03f74 commit 995aa3a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
6 changes: 5 additions & 1 deletion WebContent/WEB-INF/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,13 @@
</bean>
<!-- -->

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:env.properties"/>
</bean>

<bean id="databaseSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="expectedType" value="javax.sql.DataSource"/>
<property name="jndiName" value="java:comp/env/jdbc/scadalts"/>
<property name="jndiName" value="${db.datasourceName}"/>
</bean>

<bean id="mangoContextListener" class="com.serotonin.mango.MangoContextListener" />
Expand Down
34 changes: 17 additions & 17 deletions src/com/serotonin/mango/db/BasePooledAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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());
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/org/scada_lts/permissions/service/util/PermissionsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,27 @@ public static <T> Set<T> reduce(Set<T> accesses, ToIntFunction<T> getAccess, ToI
}

private static <T, U> void updatePermissions(U user, List<T> accessesFromUser,
PermissionsService<T, U> service,
PermissionsService<T, U> service,
Comparator<T> comparator) {
List<T> accessesFromDatabase = service.getPermissions(user);
if(!sortEquals(accessesFromUser, accessesFromDatabase, comparator)) {
if(!accessesFromDatabase.isEmpty() && !accessesFromUser.containsAll(accessesFromDatabase)) {
if(!accessesFromDatabase.isEmpty() && !containsAll(accessesFromUser, accessesFromDatabase)) {
List<T> 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<T> noExistInDatabase = diff(accessesFromDatabase, accessesFromUser);
service.addOrUpdatePermissions(user, noExistInDatabase);
}
}
}

private static <T> boolean containsAll(List<T> accesses1, List<T> accesses2) {
return new HashSet<>(accesses1).containsAll(accesses2);
}

private static <T, U> void update(U user,
List<T> permissionsFromUser,
PermissionsService<T, U> service,
Expand Down
6 changes: 3 additions & 3 deletions src/org/scada_lts/utils/PathSecureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public static Optional<File> toSecurePath(Path path, BinaryOperator<Path> 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) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/scada_lts/utils/StaticImagesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/org/scada_lts/utils/UploadFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private static List<Path> getImageSystemFilePaths(Supplier<String> getLocalPath,
createIfNotExists(path);
paths.add(path);
}
Path path = getAppContextSystemFilePath(Paths.get(normalizeFolder));
Path path = getAppContextSystemFilePath(normalizeFolder);
createIfNotExists(path);
paths.add(path);
return paths;
Expand All @@ -333,7 +333,7 @@ private static Path getImageSystemFileToWritePath(Supplier<String> getLocalPath,
|| normalizedPath.endsWith(normalizedFolder + File.separator))) {
path = getAbsoluteResourcePath(normalizedPath);
} else {
path = getAppContextSystemFilePath(Paths.get(normalizedFolder));
path = getAppContextSystemFilePath(normalizedFolder);
}
createIfNotExists(path);
return path;
Expand Down

0 comments on commit 995aa3a

Please sign in to comment.