From c5213baece0a556375c505184b8675f3ededea43 Mon Sep 17 00:00:00 2001 From: Dario Ghunney Ware Date: Mon, 6 Jan 2025 11:30:18 +0000 Subject: [PATCH] cleanup --- ...r-compose-latest-fat-security-postgres.yml | 2 +- .../security/database/DatabaseConfig.java | 6 ++- .../security/database/DatabaseService.java | 48 +++++++++++-------- .../software/SPDF/model/SessionEntity.java | 1 - src/main/resources/application.properties | 2 +- src/main/resources/settings.yml.template | 6 +-- .../security/database/DatabaseConfigTest.java | 10 ++-- .../database/DatabaseServiceTest.java | 32 ++++--------- .../SPDFApplicationIntegrationTest.java | 2 - 9 files changed, 50 insertions(+), 59 deletions(-) diff --git a/exampleYmlFiles/docker-compose-latest-fat-security-postgres.yml b/exampleYmlFiles/docker-compose-latest-fat-security-postgres.yml index b30d6f63612..1b4989deb6b 100644 --- a/exampleYmlFiles/docker-compose-latest-fat-security-postgres.yml +++ b/exampleYmlFiles/docker-compose-latest-fat-security-postgres.yml @@ -1,7 +1,7 @@ services: stirling-pdf: container_name: Stirling-PDF-Security-Fat-Postgres - image: stirlingtools/stirling-pdf:latest-fat + image: stirlingtools/stirling-pdf:latest-fat-postgres deploy: resources: limits: diff --git a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java index 9f1850f613e..338d34aa03e 100644 --- a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java +++ b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java @@ -29,7 +29,7 @@ public class DatabaseConfig { public DatabaseConfig(ApplicationProperties applicationProperties, boolean runningEE) { this.applicationProperties = applicationProperties; - this.runningEE = true; // fixMe: change back + this.runningEE = runningEE; } /** @@ -59,6 +59,10 @@ public DataSource dataSource() throws UnsupportedProviderException { log.info("Using custom database configuration"); if (!datasource.getCustomDatabaseUrl().isBlank()) { + if (datasource.getCustomDatabaseUrl().contains("postgresql")) { + dataSourceBuilder.driverClassName(POSTGRES_DRIVER); + } + dataSourceBuilder.url(datasource.getCustomDatabaseUrl()); } else { dataSourceBuilder.driverClassName(getDriverClassName(datasource.getType())); diff --git a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java index 0219e80a4fd..b0a04702164 100644 --- a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java +++ b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java @@ -7,6 +7,7 @@ import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -20,11 +21,8 @@ import javax.sql.DataSource; -import org.springframework.core.io.PathResource; -import org.springframework.core.io.support.EncodedResource; import org.springframework.jdbc.datasource.init.CannotReadScriptException; import org.springframework.jdbc.datasource.init.ScriptException; -import org.springframework.jdbc.datasource.init.ScriptUtils; import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; @@ -50,13 +48,14 @@ public DatabaseService(ApplicationProperties applicationProperties, DataSource d } /** - * Checks if there is at least one backup + * Checks if there is at least one backup. First checks if the directory exists, then checks if + * there are backup scripts within the directory * * @return true if there are backup scripts, false if there are not */ @Override public boolean hasBackup() { - Path filePath = Paths.get(BACKUP_DIR + "*"); + Path filePath = Paths.get(BACKUP_DIR); if (Files.exists(filePath)) { return !getBackupList().isEmpty(); @@ -116,9 +115,11 @@ public void importDatabase() { if (!hasBackup()) throw new BackupNotFoundException("No backup scripts were found."); List backupList = this.getBackupList(); - backupList.sort(Comparator.comparing(FileInfo::getModificationDate).reversed()); - executeDatabaseScript(Paths.get(backupList.get(0).getFilePath())); + + Path latestExport = Paths.get(backupList.get(0).getFilePath()); + + executeDatabaseScript(latestExport); } /** Imports a database backup from the specified file. */ @@ -149,7 +150,6 @@ public boolean importDatabaseFromUI(Path tempTemplatePath) throws IOException { return true; } - /** Filter and delete old backups if there are more than 5 */ @Override public void exportDatabase() { List filteredBackupList = @@ -166,16 +166,21 @@ public void exportDatabase() { Path insertOutputFilePath = this.getBackupFilePath(BACKUP_PREFIX + dateNow.format(myFormatObj) + SQL_SUFFIX); - try (Connection conn = dataSource.getConnection()) { - ScriptUtils.executeSqlScript( - conn, new EncodedResource(new PathResource(insertOutputFilePath))); + if (isH2Database()) { + String query = "SCRIPT SIMPLE COLUMNS DROP to ?;"; - log.info("Database export completed: {}", insertOutputFilePath); - } catch (SQLException e) { - log.error("Error during database export: {}", e.getMessage(), e); - } catch (CannotReadScriptException e) { - log.error("Error during database export: File {} not found", insertOutputFilePath); + try (Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement(query)) { + stmt.setString(1, insertOutputFilePath.toString()); + stmt.execute(); + } catch (SQLException e) { + log.error("Error during database export: {}", e.getMessage(), e); + } catch (CannotReadScriptException e) { + log.error("Error during database export: File {} not found", insertOutputFilePath); + } } + + log.info("Database export completed: {}", insertOutputFilePath); } private static void deleteOldestBackup(List filteredBackupList) { @@ -259,17 +264,20 @@ public Path getBackupFilePath(String fileName) { private void executeDatabaseScript(Path scriptPath) { if (isH2Database()) { - try (Connection conn = dataSource.getConnection()) { - ScriptUtils.executeSqlScript( - conn, new EncodedResource(new PathResource(scriptPath))); + String query = "RUNSCRIPT from ?;"; - log.info("Database import completed: {}", scriptPath); + try (Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement(query)) { + stmt.setString(1, scriptPath.toString()); + stmt.execute(); } catch (SQLException e) { log.error("Error during database import: {}", e.getMessage(), e); } catch (ScriptException e) { log.error("Error: File {} not found", scriptPath.toString(), e); } } + + log.info("Database import completed: {}", scriptPath); } /** diff --git a/src/main/java/stirling/software/SPDF/model/SessionEntity.java b/src/main/java/stirling/software/SPDF/model/SessionEntity.java index 2742ed96305..fcdb8777b64 100644 --- a/src/main/java/stirling/software/SPDF/model/SessionEntity.java +++ b/src/main/java/stirling/software/SPDF/model/SessionEntity.java @@ -5,7 +5,6 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; -import jakarta.persistence.Lob; import jakarta.persistence.Table; import lombok.Data; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f0d2e6a739c..cccfd3b063a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -28,7 +28,7 @@ spring.thymeleaf.encoding=UTF-8 spring.web.resources.mime-mappings.webmanifest=application/manifest+json spring.mvc.async.request-timeout=${SYSTEM_CONNECTIONTIMEOUTMILLISECONDS:1200000} -spring.datasource.url=jdbc:h2:file:./configs/stirling-pdf-DB-2.3.232;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE +spring.datasource.url=jdbc:h2:file:./configs/stirling-pdf-DB-2.3.232;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=PostgreSQL spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= diff --git a/src/main/resources/settings.yml.template b/src/main/resources/settings.yml.template index 519b6f6e94e..248aa93cc03 100644 --- a/src/main/resources/settings.yml.template +++ b/src/main/resources/settings.yml.template @@ -86,14 +86,14 @@ system: tessdataDir: /usr/share/tessdata # path to the directory containing the Tessdata files. This setting is relevant for Windows systems. For Windows users, this path should be adjusted to point to the appropriate directory where the Tessdata files are stored. enableAnalytics: undefined # set to 'true' to enable analytics, set to 'false' to disable analytics; for enterprise users, this is set to true datasource: - enableCustomDatabase: true # set this property to 'true' if you would like to use your own custom database configuration + enableCustomDatabase: false # set this property to 'true' if you would like to use your own custom database configuration customDatabaseUrl: jdbc:postgresql://localhost:5432/postgres # set the url for your own custom database connection. If provided, the type, hostName, port and name are not necessary and will not be used + username: postgres # set the database username + password: postgres # set the database password type: postgresql # the type of the database to set (e.g. 'h2', 'postgresql') hostName: localhost # the host name to use for the database url. Set to 'localhost' when running the app locally. Set to match the name of the container name of your database container when running the app on a server (Docker configuration) port: 5432 # set the port number of the database. Ensure this matches the port the database is listening to name: postgres # set the name of your database. Should match the name of the database you create - username: postgres # set the database username - password: postgres # set the database password ui: appName: '' # application's visible name diff --git a/src/test/java/stirling/software/SPDF/config/security/database/DatabaseConfigTest.java b/src/test/java/stirling/software/SPDF/config/security/database/DatabaseConfigTest.java index 114845b8488..2c61ce1a7e0 100644 --- a/src/test/java/stirling/software/SPDF/config/security/database/DatabaseConfigTest.java +++ b/src/test/java/stirling/software/SPDF/config/security/database/DatabaseConfigTest.java @@ -1,20 +1,17 @@ package stirling.software.SPDF.config.security.database; +import javax.sql.DataSource; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import stirling.software.SPDF.model.ApplicationProperties; import stirling.software.SPDF.model.provider.UnsupportedProviderException; - -import javax.sql.DataSource; - -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -32,7 +29,6 @@ void setUp() { } @Test - @Disabled void testDataSource_whenRunningEEIsFalse() throws UnsupportedProviderException { databaseConfig = new DatabaseConfig(applicationProperties, false); diff --git a/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java b/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java index 45ef8b23715..153ce8dc978 100644 --- a/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java +++ b/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java @@ -5,15 +5,12 @@ import java.nio.file.Path; import java.nio.file.Paths; import javax.sql.DataSource; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import stirling.software.SPDF.model.ApplicationProperties; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -21,7 +18,7 @@ @ExtendWith(MockitoExtension.class) class DatabaseServiceTest { - public static final String TEST_FILE = "test"; + public static final String TEST_FILE = "test.txt"; private final String BACKUP_PATH = "configs/db/backup/"; @Mock @@ -33,33 +30,22 @@ class DatabaseServiceTest { @InjectMocks private DatabaseService databaseService; - @BeforeEach - void setUp() throws IOException { - Files.deleteIfExists(Paths.get(BACKUP_PATH + TEST_FILE)); - } - @Test - void testHasNoBackups() { - ApplicationProperties.System system = mock(ApplicationProperties.System.class); - ApplicationProperties.Datasource datasource = mock(ApplicationProperties.Datasource.class); - - when(applicationProperties.getSystem()).thenReturn(system); - when(system.getDatasource()).thenReturn(datasource); - when(datasource.isEnableCustomDatabase()).thenReturn(false); - - assertFalse(databaseService.hasBackup()); - } - - @Test - @Disabled void testHasBackups() throws IOException { Path backupDir = Paths.get(BACKUP_PATH); Files.createDirectories(backupDir); Path testFile = Paths.get(BACKUP_PATH + TEST_FILE); + ApplicationProperties.System system = mock(ApplicationProperties.System.class); + ApplicationProperties.Datasource datasource = mock(ApplicationProperties.Datasource.class); Files.createFile(testFile); - Files.createTempFile(backupDir, TEST_FILE, null); + + when(applicationProperties.getSystem()).thenReturn(system); + when(system.getDatasource()).thenReturn(datasource); + when(datasource.isEnableCustomDatabase()).thenReturn(false); assertTrue(databaseService.hasBackup()); + + Files.deleteIfExists(testFile); } } \ No newline at end of file diff --git a/src/test/java/stirling/software/SPDF/integrationtests/SPDFApplicationIntegrationTest.java b/src/test/java/stirling/software/SPDF/integrationtests/SPDFApplicationIntegrationTest.java index 62f0a0765a9..662ea3041fe 100644 --- a/src/test/java/stirling/software/SPDF/integrationtests/SPDFApplicationIntegrationTest.java +++ b/src/test/java/stirling/software/SPDF/integrationtests/SPDFApplicationIntegrationTest.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import stirling.software.SPDF.SPDFApplication; @@ -13,7 +12,6 @@ import static java.nio.file.Files.exists; import static org.junit.jupiter.api.Assertions.assertTrue; -@Disabled @SpringBootTest public class SPDFApplicationIntegrationTest {