renamed GeneralUtil to GeneralUtils

This commit is contained in:
Dario Ghunney Ware 2025-05-22 12:31:25 +01:00
parent 969cd46772
commit 37c7373dc4
35 changed files with 252 additions and 259 deletions

View File

@ -1,44 +0,0 @@
package stirling.software.common.configuration;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
@Slf4j
@Configuration
public class YamlConfig {
@Bean
public PropertySource<?> dynamicYamlPropertySource(ConfigurableEnvironment environment)
throws IOException {
String configPath = InstallationPathConfig.getSettingsPath();
log.debug("Attempting to load settings from: " + configPath);
File file = new File(configPath);
if (!file.exists()) {
log.error("Warning: Settings file does not exist at: " + configPath);
}
Resource resource = new FileSystemResource(configPath);
if (!resource.exists()) {
throw new FileNotFoundException("Settings file not found at: " + configPath);
}
EncodedResource encodedResource = new EncodedResource(resource);
PropertySource<?> propertySource =
new YamlPropertySourceFactory().createPropertySource(null, encodedResource);
environment.getPropertySources().addFirst(propertySource);
log.debug("Loaded properties: " + propertySource.getSource());
return propertySource;
}
}

View File

@ -1,5 +1,7 @@
package stirling.software.common.model;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
@ -14,21 +16,29 @@ import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.stereotype.Component;
import stirling.software.common.configuration.InstallationPathConfig;
import stirling.software.common.configuration.YamlPropertySourceFactory;
import stirling.software.common.model.exception.UnsupportedProviderException;
import stirling.software.common.model.oauth2.GitHubProvider;
import stirling.software.common.model.oauth2.GoogleProvider;
import stirling.software.common.model.oauth2.KeycloakProvider;
import stirling.software.common.model.oauth2.Provider;
import stirling.software.common.util.ValidationUtil;
import stirling.software.common.util.ValidationUtils;
@Data
@Slf4j
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
@ConfigurationProperties(prefix = "")
@ -49,6 +59,32 @@ public class ApplicationProperties {
private AutoPipeline autoPipeline = new AutoPipeline();
private ProcessExecutor processExecutor = new ProcessExecutor();
@Bean
public PropertySource<?> dynamicYamlPropertySource(ConfigurableEnvironment environment)
throws IOException {
String configPath = InstallationPathConfig.getSettingsPath();
log.debug("Attempting to load settings from: " + configPath);
File file = new File(configPath);
if (!file.exists()) {
log.error("Warning: Settings file does not exist at: " + configPath);
}
Resource resource = new FileSystemResource(configPath);
if (!resource.exists()) {
throw new FileNotFoundException("Settings file not found at: " + configPath);
}
EncodedResource encodedResource = new EncodedResource(resource);
PropertySource<?> propertySource =
new YamlPropertySourceFactory().createPropertySource(null, encodedResource);
environment.getPropertySources().addFirst(propertySource);
log.debug("Loaded properties: " + propertySource.getSource());
return propertySource;
}
@Data
public static class AutoPipeline {
private String outputFolder;
@ -208,11 +244,11 @@ public class ApplicationProperties {
}
public boolean isSettingsValid() {
return !ValidationUtil.isStringEmpty(this.getIssuer())
&& !ValidationUtil.isStringEmpty(this.getClientId())
&& !ValidationUtil.isStringEmpty(this.getClientSecret())
&& !ValidationUtil.isCollectionEmpty(this.getScopes())
&& !ValidationUtil.isStringEmpty(this.getUseAsUsername());
return !ValidationUtils.isStringEmpty(this.getIssuer())
&& !ValidationUtils.isStringEmpty(this.getClientId())
&& !ValidationUtils.isStringEmpty(this.getClientSecret())
&& !ValidationUtils.isCollectionEmpty(this.getScopes())
&& !ValidationUtils.isStringEmpty(this.getUseAsUsername());
}
@Data

View File

@ -30,7 +30,7 @@ import lombok.extern.slf4j.Slf4j;
import stirling.software.common.configuration.InstallationPathConfig;
@Slf4j
public class GeneralUtil {
public class GeneralUtils {
public static File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {
File tempFile = Files.createTempFile("temp", null).toFile();

View File

@ -84,7 +84,7 @@ public class PdfUtils {
public static boolean hasImages(PDDocument document, String pagesToCheck) throws IOException {
String[] pageOrderArr = pagesToCheck.split(",");
List<Integer> pageList =
GeneralUtil.parsePageList(pageOrderArr, document.getNumberOfPages());
GeneralUtils.parsePageList(pageOrderArr, document.getNumberOfPages());
for (int pageNumber : pageList) {
PDPage page = document.getPage(pageNumber);
@ -100,7 +100,7 @@ public class PdfUtils {
throws IOException {
String[] pageOrderArr = pageNumbersToCheck.split(",");
List<Integer> pageList =
GeneralUtil.parsePageList(pageOrderArr, document.getNumberOfPages());
GeneralUtils.parsePageList(pageOrderArr, document.getNumberOfPages());
for (int pageNumber : pageList) {
PDPage page = document.getPage(pageNumber);

View File

@ -1,10 +1,10 @@
package stirling.software.common.util;
import stirling.software.common.model.oauth2.Provider;
import static stirling.software.common.util.ValidationUtil.isCollectionEmpty;
import static stirling.software.common.util.ValidationUtil.isStringEmpty;
import static stirling.software.common.util.ValidationUtils.isCollectionEmpty;
import static stirling.software.common.util.ValidationUtils.isStringEmpty;
public class ProviderUtil {
public class ProviderUtils {
public static boolean validateProvider(Provider provider) {
if (provider == null) {

View File

@ -1,6 +1,6 @@
package stirling.software.common.util;
public class RequestUriUtil {
public class RequestUriUtils {
public static boolean isStaticResource(String requestURI) {
return isStaticResource("", requestURI);

View File

@ -2,7 +2,7 @@ package stirling.software.common.util;
import java.util.Collection;
public class ValidationUtil {
public class ValidationUtils {
public static boolean isStringEmpty(String input) {
return input == null || input.isBlank();

View File

@ -1,41 +0,0 @@
package stirling.software.common.util;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class GeneralUtilAdditionalTest {
@Test
void testConvertSizeToBytes() {
assertEquals(1024L, GeneralUtil.convertSizeToBytes("1KB"));
assertEquals(1024L * 1024, GeneralUtil.convertSizeToBytes("1MB"));
assertEquals(1024L * 1024 * 1024, GeneralUtil.convertSizeToBytes("1GB"));
assertEquals(100L * 1024 * 1024, GeneralUtil.convertSizeToBytes("100"));
assertNull(GeneralUtil.convertSizeToBytes("invalid"));
assertNull(GeneralUtil.convertSizeToBytes(null));
}
@Test
void testFormatBytes() {
assertEquals("512 B", GeneralUtil.formatBytes(512));
assertEquals("1.00 KB", GeneralUtil.formatBytes(1024));
assertEquals("1.00 MB", GeneralUtil.formatBytes(1024L * 1024));
assertEquals("1.00 GB", GeneralUtil.formatBytes(1024L * 1024 * 1024));
}
@Test
void testURLHelpersAndUUID() {
assertTrue(GeneralUtil.isValidURL("https://example.com"));
assertFalse(GeneralUtil.isValidURL("htp:/bad"));
assertFalse(GeneralUtil.isURLReachable("http://localhost"));
assertFalse(GeneralUtil.isURLReachable("ftp://example.com"));
assertTrue(GeneralUtil.isValidUUID("123e4567-e89b-12d3-a456-426614174000"));
assertFalse(GeneralUtil.isValidUUID("not-a-uuid"));
assertFalse(GeneralUtil.isVersionHigher(null, "1.0"));
assertTrue(GeneralUtil.isVersionHigher("2.0", "1.9"));
assertFalse(GeneralUtil.isVersionHigher("1.0", "1.0.1"));
}
}

View File

@ -0,0 +1,41 @@
package stirling.software.common.util;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class GeneralUtilsAdditionalTest {
@Test
void testConvertSizeToBytes() {
assertEquals(1024L, GeneralUtils.convertSizeToBytes("1KB"));
assertEquals(1024L * 1024, GeneralUtils.convertSizeToBytes("1MB"));
assertEquals(1024L * 1024 * 1024, GeneralUtils.convertSizeToBytes("1GB"));
assertEquals(100L * 1024 * 1024, GeneralUtils.convertSizeToBytes("100"));
assertNull(GeneralUtils.convertSizeToBytes("invalid"));
assertNull(GeneralUtils.convertSizeToBytes(null));
}
@Test
void testFormatBytes() {
assertEquals("512 B", GeneralUtils.formatBytes(512));
assertEquals("1.00 KB", GeneralUtils.formatBytes(1024));
assertEquals("1.00 MB", GeneralUtils.formatBytes(1024L * 1024));
assertEquals("1.00 GB", GeneralUtils.formatBytes(1024L * 1024 * 1024));
}
@Test
void testURLHelpersAndUUID() {
assertTrue(GeneralUtils.isValidURL("https://example.com"));
assertFalse(GeneralUtils.isValidURL("htp:/bad"));
assertFalse(GeneralUtils.isURLReachable("http://localhost"));
assertFalse(GeneralUtils.isURLReachable("ftp://example.com"));
assertTrue(GeneralUtils.isValidUUID("123e4567-e89b-12d3-a456-426614174000"));
assertFalse(GeneralUtils.isValidUUID("not-a-uuid"));
assertFalse(GeneralUtils.isVersionHigher(null, "1.0"));
assertTrue(GeneralUtils.isVersionHigher("2.0", "1.9"));
assertFalse(GeneralUtils.isVersionHigher("1.0", "1.0.1"));
}
}

View File

@ -6,152 +6,152 @@ import java.util.List;
import org.junit.jupiter.api.Test;
public class GeneralUtilTest {
public class GeneralUtilsTest {
@Test
void testParsePageListWithAll() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"all"}, 5, false);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"all"}, 5, false);
assertEquals(List.of(0, 1, 2, 3, 4), result, "'All' keyword should return all pages.");
}
@Test
void testParsePageListWithAllOneBased() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"all"}, 5, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"all"}, 5, true);
assertEquals(List.of(1, 2, 3, 4, 5), result, "'All' keyword should return all pages.");
}
@Test
void nFunc() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"n"}, 5, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"n"}, 5, true);
assertEquals(List.of(1, 2, 3, 4, 5), result, "'n' keyword should return all pages.");
}
@Test
void nFuncAdvanced() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"4n"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"4n"}, 9, true);
// skip 0 as not valid
assertEquals(List.of(4, 8), result, "'All' keyword should return all pages.");
}
@Test
void nFuncAdvancedZero() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"4n"}, 9, false);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"4n"}, 9, false);
// skip 0 as not valid
assertEquals(List.of(3, 7), result, "'All' keyword should return all pages.");
}
@Test
void nFuncAdvanced2() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"4n-1"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"4n-1"}, 9, true);
// skip -1 as not valid
assertEquals(List.of(3, 7), result, "4n-1 should do (0-1), (4-1), (8-1)");
}
@Test
void nFuncAdvanced3() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"4n+1"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"4n+1"}, 9, true);
assertEquals(List.of(5, 9), result, "'All' keyword should return all pages.");
}
@Test
void nFunc_spaces() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"n + 1"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"n + 1"}, 9, true);
assertEquals(List.of(2, 3, 4, 5, 6, 7, 8, 9), result);
}
@Test
void nFunc_consecutive_Ns_nnn() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"nnn"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"nnn"}, 9, true);
assertEquals(List.of(1, 8), result);
}
@Test
void nFunc_consecutive_Ns_nn() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"nn"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"nn"}, 9, true);
assertEquals(List.of(1, 4, 9), result);
}
@Test
void nFunc_opening_closing_round_brackets() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"(n-1)(n-2)"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"(n-1)(n-2)"}, 9, true);
assertEquals(List.of(2, 6), result);
}
@Test
void nFunc_opening_round_brackets() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"2(n-1)"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"2(n-1)"}, 9, true);
assertEquals(List.of(2, 4, 6, 8), result);
}
@Test
void nFunc_opening_round_brackets_n() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"n(n-1)"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"n(n-1)"}, 9, true);
assertEquals(List.of(2, 6), result);
}
@Test
void nFunc_closing_round_brackets() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"(n-1)2"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"(n-1)2"}, 9, true);
assertEquals(List.of(2, 4, 6, 8), result);
}
@Test
void nFunc_closing_round_brackets_n() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"(n-1)n"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"(n-1)n"}, 9, true);
assertEquals(List.of(2, 6), result);
}
@Test
void nFunc_function_surrounded_with_brackets() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"(n-1)"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"(n-1)"}, 9, true);
assertEquals(List.of(1, 2, 3, 4, 5, 6, 7, 8), result);
}
@Test
void nFuncAdvanced4() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"3+2n"}, 9, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"3+2n"}, 9, true);
assertEquals(List.of(5, 7, 9), result, "'All' keyword should return all pages.");
}
@Test
void nFuncAdvancedZerobased() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"4n"}, 9, false);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"4n"}, 9, false);
assertEquals(List.of(3, 7), result, "'All' keyword should return all pages.");
}
@Test
void nFuncAdvanced2Zerobased() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"4n-1"}, 9, false);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"4n-1"}, 9, false);
assertEquals(List.of(2, 6), result, "'All' keyword should return all pages.");
}
@Test
void testParsePageListWithRangeOneBasedOutput() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"1-3"}, 5, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"1-3"}, 5, true);
assertEquals(List.of(1, 2, 3), result, "Range should be parsed correctly.");
}
@Test
void testParsePageListWithRangeZeroBaseOutput() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"1-3"}, 5, false);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"1-3"}, 5, false);
assertEquals(List.of(0, 1, 2), result, "Range should be parsed correctly.");
}
@Test
void testParsePageListWithRangeOneBasedOutputFull() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"1,3,7-8"}, 8, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"1,3,7-8"}, 8, true);
assertEquals(List.of(1, 3, 7, 8), result, "Range should be parsed correctly.");
}
@Test
void testParsePageListWithRangeOneBasedOutputFullOutOfRange() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"1,3,7-8"}, 5, true);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"1,3,7-8"}, 5, true);
assertEquals(List.of(1, 3), result, "Range should be parsed correctly.");
}
@Test
void testParsePageListWithRangeZeroBaseOutputFull() {
List<Integer> result = GeneralUtil.parsePageList(new String[] {"1,3,7-8"}, 8, false);
List<Integer> result = GeneralUtils.parsePageList(new String[] {"1,3,7-8"}, 8, false);
assertEquals(List.of(0, 2, 6, 7), result, "Range should be parsed correctly.");
}
}

View File

@ -16,7 +16,7 @@ import stirling.software.common.model.oauth2.Provider;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class ProviderUtilTest {
class ProviderUtilsTest {
@Test
void testSuccessfulValidation() {
@ -26,13 +26,13 @@ class ProviderUtilTest {
when(provider.getClientSecret()).thenReturn("clientSecret");
when(provider.getScopes()).thenReturn(List.of("read:user"));
Assertions.assertTrue(ProviderUtil.validateProvider(provider));
Assertions.assertTrue(ProviderUtils.validateProvider(provider));
}
@ParameterizedTest
@MethodSource("providerParams")
void testUnsuccessfulValidation(Provider provider) {
Assertions.assertFalse(ProviderUtil.validateProvider(provider));
Assertions.assertFalse(ProviderUtils.validateProvider(provider));
}
public static Stream<Arguments> providerParams() {

View File

@ -7,45 +7,45 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
public class RequestUriUtilTest {
public class RequestUriUtilsTest {
@Test
void testIsStaticResource() {
// Test static resources without context path
assertTrue(
RequestUriUtil.isStaticResource("/css/styles.css"), "CSS files should be static");
assertTrue(RequestUriUtil.isStaticResource("/js/script.js"), "JS files should be static");
RequestUriUtils.isStaticResource("/css/styles.css"), "CSS files should be static");
assertTrue(RequestUriUtils.isStaticResource("/js/script.js"), "JS files should be static");
assertTrue(
RequestUriUtil.isStaticResource("/images/logo.png"),
RequestUriUtils.isStaticResource("/images/logo.png"),
"Image files should be static");
assertTrue(
RequestUriUtil.isStaticResource("/public/index.html"),
RequestUriUtils.isStaticResource("/public/index.html"),
"Public files should be static");
assertTrue(
RequestUriUtil.isStaticResource("/pdfjs/pdf.worker.js"),
RequestUriUtils.isStaticResource("/pdfjs/pdf.worker.js"),
"PDF.js files should be static");
assertTrue(
RequestUriUtil.isStaticResource("/api/v1/info/status"),
RequestUriUtils.isStaticResource("/api/v1/info/status"),
"API status should be static");
assertTrue(
RequestUriUtil.isStaticResource("/some-path/icon.svg"),
RequestUriUtils.isStaticResource("/some-path/icon.svg"),
"SVG files should be static");
assertTrue(RequestUriUtil.isStaticResource("/login"), "Login page should be static");
assertTrue(RequestUriUtil.isStaticResource("/error"), "Error page should be static");
assertTrue(RequestUriUtils.isStaticResource("/login"), "Login page should be static");
assertTrue(RequestUriUtils.isStaticResource("/error"), "Error page should be static");
// Test non-static resources
assertFalse(
RequestUriUtil.isStaticResource("/api/v1/users"),
RequestUriUtils.isStaticResource("/api/v1/users"),
"API users should not be static");
assertFalse(
RequestUriUtil.isStaticResource("/api/v1/orders"),
RequestUriUtils.isStaticResource("/api/v1/orders"),
"API orders should not be static");
assertFalse(RequestUriUtil.isStaticResource("/"), "Root path should not be static");
assertFalse(RequestUriUtils.isStaticResource("/"), "Root path should not be static");
assertFalse(
RequestUriUtil.isStaticResource("/register"),
RequestUriUtils.isStaticResource("/register"),
"Register page should not be static");
assertFalse(
RequestUriUtil.isStaticResource("/api/v1/products"),
RequestUriUtils.isStaticResource("/api/v1/products"),
"API products should not be static");
}
@ -55,24 +55,24 @@ public class RequestUriUtilTest {
// Test static resources with context path
assertTrue(
RequestUriUtil.isStaticResource(contextPath, contextPath + "/css/styles.css"),
RequestUriUtils.isStaticResource(contextPath, contextPath + "/css/styles.css"),
"CSS with context path should be static");
assertTrue(
RequestUriUtil.isStaticResource(contextPath, contextPath + "/js/script.js"),
RequestUriUtils.isStaticResource(contextPath, contextPath + "/js/script.js"),
"JS with context path should be static");
assertTrue(
RequestUriUtil.isStaticResource(contextPath, contextPath + "/images/logo.png"),
RequestUriUtils.isStaticResource(contextPath, contextPath + "/images/logo.png"),
"Images with context path should be static");
assertTrue(
RequestUriUtil.isStaticResource(contextPath, contextPath + "/login"),
RequestUriUtils.isStaticResource(contextPath, contextPath + "/login"),
"Login with context path should be static");
// Test non-static resources with context path
assertFalse(
RequestUriUtil.isStaticResource(contextPath, contextPath + "/api/v1/users"),
RequestUriUtils.isStaticResource(contextPath, contextPath + "/api/v1/users"),
"API users with context path should not be static");
assertFalse(
RequestUriUtil.isStaticResource(contextPath, "/"),
RequestUriUtils.isStaticResource(contextPath, "/"),
"Root path with context path should not be static");
}
@ -92,7 +92,7 @@ public class RequestUriUtilTest {
})
void testIsStaticResourceWithFileExtensions(String path) {
assertTrue(
RequestUriUtil.isStaticResource(path),
RequestUriUtils.isStaticResource(path),
"Files with specific extensions should be static regardless of path");
}
@ -100,59 +100,59 @@ public class RequestUriUtilTest {
void testIsTrackableResource() {
// Test non-trackable resources (returns false)
assertFalse(
RequestUriUtil.isTrackableResource("/js/script.js"),
RequestUriUtils.isTrackableResource("/js/script.js"),
"JS files should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/v1/api-docs"),
RequestUriUtils.isTrackableResource("/v1/api-docs"),
"API docs should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("robots.txt"),
RequestUriUtils.isTrackableResource("robots.txt"),
"robots.txt should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/images/logo.png"),
RequestUriUtils.isTrackableResource("/images/logo.png"),
"Images should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/styles.css"),
RequestUriUtils.isTrackableResource("/styles.css"),
"CSS files should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/script.js.map"),
RequestUriUtils.isTrackableResource("/script.js.map"),
"Map files should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/icon.svg"),
RequestUriUtils.isTrackableResource("/icon.svg"),
"SVG files should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/popularity.txt"),
RequestUriUtils.isTrackableResource("/popularity.txt"),
"Popularity file should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/script.js"),
RequestUriUtils.isTrackableResource("/script.js"),
"JS files should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/swagger/index.html"),
RequestUriUtils.isTrackableResource("/swagger/index.html"),
"Swagger files should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/api/v1/info/status"),
RequestUriUtils.isTrackableResource("/api/v1/info/status"),
"API info should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/site.webmanifest"),
RequestUriUtils.isTrackableResource("/site.webmanifest"),
"Webmanifest should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/fonts/font.woff"),
RequestUriUtils.isTrackableResource("/fonts/font.woff"),
"Fonts should not be trackable");
assertFalse(
RequestUriUtil.isTrackableResource("/pdfjs/viewer.js"),
RequestUriUtils.isTrackableResource("/pdfjs/viewer.js"),
"PDF.js files should not be trackable");
// Test trackable resources (returns true)
assertTrue(RequestUriUtil.isTrackableResource("/login"), "Login page should be trackable");
assertTrue(RequestUriUtils.isTrackableResource("/login"), "Login page should be trackable");
assertTrue(
RequestUriUtil.isTrackableResource("/register"),
RequestUriUtils.isTrackableResource("/register"),
"Register page should be trackable");
assertTrue(
RequestUriUtil.isTrackableResource("/api/v1/users"),
RequestUriUtils.isTrackableResource("/api/v1/users"),
"API users should be trackable");
assertTrue(RequestUriUtil.isTrackableResource("/"), "Root path should be trackable");
assertTrue(RequestUriUtils.isTrackableResource("/"), "Root path should be trackable");
assertTrue(
RequestUriUtil.isTrackableResource("/some-other-path"),
RequestUriUtils.isTrackableResource("/some-other-path"),
"Other paths should be trackable");
}
@ -162,27 +162,27 @@ public class RequestUriUtilTest {
// Test with context path
assertFalse(
RequestUriUtil.isTrackableResource(contextPath, "/js/script.js"),
RequestUriUtils.isTrackableResource(contextPath, "/js/script.js"),
"JS files should not be trackable with context path");
assertTrue(
RequestUriUtil.isTrackableResource(contextPath, "/login"),
RequestUriUtils.isTrackableResource(contextPath, "/login"),
"Login page should be trackable with context path");
// Additional tests with context path
assertFalse(
RequestUriUtil.isTrackableResource(contextPath, "/fonts/custom.woff"),
RequestUriUtils.isTrackableResource(contextPath, "/fonts/custom.woff"),
"Font files should not be trackable with context path");
assertFalse(
RequestUriUtil.isTrackableResource(contextPath, "/images/header.png"),
RequestUriUtils.isTrackableResource(contextPath, "/images/header.png"),
"Images should not be trackable with context path");
assertFalse(
RequestUriUtil.isTrackableResource(contextPath, "/swagger/ui.html"),
RequestUriUtils.isTrackableResource(contextPath, "/swagger/ui.html"),
"Swagger UI should not be trackable with context path");
assertTrue(
RequestUriUtil.isTrackableResource(contextPath, "/account/profile"),
RequestUriUtils.isTrackableResource(contextPath, "/account/profile"),
"Account page should be trackable with context path");
assertTrue(
RequestUriUtil.isTrackableResource(contextPath, "/pdf/view"),
RequestUriUtils.isTrackableResource(contextPath, "/pdf/view"),
"PDF view page should be trackable with context path");
}
@ -206,7 +206,7 @@ public class RequestUriUtilTest {
})
void testNonTrackableResources(String path) {
assertFalse(
RequestUriUtil.isTrackableResource(path),
RequestUriUtils.isTrackableResource(path),
"Resources matching patterns should not be trackable: " + path);
}
@ -229,22 +229,22 @@ public class RequestUriUtilTest {
})
void testTrackableResources(String path) {
assertTrue(
RequestUriUtil.isTrackableResource(path),
RequestUriUtils.isTrackableResource(path),
"App routes should be trackable: " + path);
}
@Test
void testEdgeCases() {
// Test with empty strings
assertFalse(RequestUriUtil.isStaticResource("", ""), "Empty path should not be static");
assertTrue(RequestUriUtil.isTrackableResource("", ""), "Empty path should be trackable");
assertFalse(RequestUriUtils.isStaticResource("", ""), "Empty path should not be static");
assertTrue(RequestUriUtils.isTrackableResource("", ""), "Empty path should be trackable");
// Test with null-like behavior (would actually throw NPE in real code)
// These are not actual null tests but shows handling of odd cases
assertFalse(RequestUriUtil.isStaticResource("null"), "String 'null' should not be static");
assertFalse(RequestUriUtils.isStaticResource("null"), "String 'null' should not be static");
// Test String "null" as a path
boolean isTrackable = RequestUriUtil.isTrackableResource("null");
boolean isTrackable = RequestUriUtils.isTrackableResource("null");
assertTrue(isTrackable, "String 'null' should be trackable");
// Mixed case extensions test - note that Java's endsWith() is case-sensitive
@ -252,31 +252,31 @@ public class RequestUriUtilTest {
// Always test the lowercase versions which should definitely work
assertTrue(
RequestUriUtil.isStaticResource("/logo.png"), "PNG (lowercase) should be static");
RequestUriUtils.isStaticResource("/logo.png"), "PNG (lowercase) should be static");
assertTrue(
RequestUriUtil.isStaticResource("/icon.svg"), "SVG (lowercase) should be static");
RequestUriUtils.isStaticResource("/icon.svg"), "SVG (lowercase) should be static");
// Path with query parameters
assertFalse(
RequestUriUtil.isStaticResource("/api/users?page=1"),
RequestUriUtils.isStaticResource("/api/users?page=1"),
"Path with query params should respect base path");
assertTrue(
RequestUriUtil.isStaticResource("/images/logo.png?v=123"),
RequestUriUtils.isStaticResource("/images/logo.png?v=123"),
"Static resource with query params should still be static");
// Paths with fragments
assertTrue(
RequestUriUtil.isStaticResource("/css/styles.css#section1"),
RequestUriUtils.isStaticResource("/css/styles.css#section1"),
"CSS with fragment should be static");
// Multiple dots in filename
assertTrue(
RequestUriUtil.isStaticResource("/js/jquery.min.js"),
RequestUriUtils.isStaticResource("/js/jquery.min.js"),
"JS with multiple dots should be static");
// Special characters in path
assertTrue(
RequestUriUtil.isStaticResource("/images/user's-photo.png"),
RequestUriUtils.isStaticResource("/images/user's-photo.png"),
"Path with special chars should be handled correctly");
}
@ -284,28 +284,28 @@ public class RequestUriUtilTest {
void testComplexPaths() {
// Test complex static resource paths
assertTrue(
RequestUriUtil.isStaticResource("/css/theme/dark/styles.css"),
RequestUriUtils.isStaticResource("/css/theme/dark/styles.css"),
"Nested CSS should be static");
assertTrue(
RequestUriUtil.isStaticResource("/fonts/open-sans/bold/font.woff"),
RequestUriUtils.isStaticResource("/fonts/open-sans/bold/font.woff"),
"Nested font should be static");
assertTrue(
RequestUriUtil.isStaticResource("/js/vendor/jquery/3.5.1/jquery.min.js"),
RequestUriUtils.isStaticResource("/js/vendor/jquery/3.5.1/jquery.min.js"),
"Versioned JS should be static");
// Test complex paths with context
String contextPath = "/app";
assertTrue(
RequestUriUtil.isStaticResource(
RequestUriUtils.isStaticResource(
contextPath, contextPath + "/css/theme/dark/styles.css"),
"Nested CSS with context should be static");
// Test boundary cases for isTrackableResource
assertFalse(
RequestUriUtil.isTrackableResource("/js-framework/components"),
RequestUriUtils.isTrackableResource("/js-framework/components"),
"Path starting with js- should not be treated as JS resource");
assertFalse(
RequestUriUtil.isTrackableResource("/fonts-selection"),
RequestUriUtils.isTrackableResource("/fonts-selection"),
"Path starting with fonts- should not be treated as font resource");
}
}

View File

@ -20,7 +20,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
@Service
@Slf4j
@ -765,7 +765,7 @@ public class KeygenLicenseVerifier {
}
private String generateMachineFingerprint() {
return GeneralUtil.generateMachineFingerprint();
return GeneralUtils.generateMachineFingerprint();
}
/**

View File

@ -12,7 +12,7 @@ import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.EE.KeygenLicenseVerifier.License;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
@Component
@Slf4j
@ -88,7 +88,7 @@ public class LicenseKeyChecker {
public void updateLicenseKey(String newKey) throws IOException {
applicationProperties.getPremium().setKey(newKey);
GeneralUtil.saveKeyToSettings("EnterpriseEdition.key", newKey);
GeneralUtils.saveKeyToSettings("EnterpriseEdition.key", newKey);
checkLicense();
}

View File

@ -18,7 +18,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
@Component
@Slf4j
@ -39,31 +39,31 @@ public class InitialSetup {
public void initUUIDKey() throws IOException {
String uuid = applicationProperties.getAutomaticallyGenerated().getUUID();
if (!GeneralUtil.isValidUUID(uuid)) {
if (!GeneralUtils.isValidUUID(uuid)) {
// Generating a random UUID as the secret key
uuid = UUID.randomUUID().toString();
GeneralUtil.saveKeyToSettings("AutomaticallyGenerated.UUID", uuid);
GeneralUtils.saveKeyToSettings("AutomaticallyGenerated.UUID", uuid);
applicationProperties.getAutomaticallyGenerated().setUUID(uuid);
}
}
public void initSecretKey() throws IOException {
String secretKey = applicationProperties.getAutomaticallyGenerated().getKey();
if (!GeneralUtil.isValidUUID(secretKey)) {
if (!GeneralUtils.isValidUUID(secretKey)) {
// Generating a random UUID as the secret key
secretKey = UUID.randomUUID().toString();
GeneralUtil.saveKeyToSettings("AutomaticallyGenerated.key", secretKey);
GeneralUtils.saveKeyToSettings("AutomaticallyGenerated.key", secretKey);
applicationProperties.getAutomaticallyGenerated().setKey(secretKey);
}
}
public void initEnableCSRFSecurity() throws IOException {
if (GeneralUtil.isVersionHigher(
if (GeneralUtils.isVersionHigher(
"0.36.0", applicationProperties.getAutomaticallyGenerated().getAppVersion())) {
Boolean csrf = applicationProperties.getSecurity().getCsrfDisabled();
if (!csrf) {
GeneralUtil.saveKeyToSettings("security.csrfDisabled", false);
GeneralUtil.saveKeyToSettings("system.enableAnalytics", true);
GeneralUtils.saveKeyToSettings("security.csrfDisabled", false);
GeneralUtils.saveKeyToSettings("system.enableAnalytics", true);
applicationProperties.getSecurity().setCsrfDisabled(false);
}
}
@ -74,14 +74,14 @@ public class InitialSetup {
String termsUrl = applicationProperties.getLegal().getTermsAndConditions();
if (StringUtils.isEmpty(termsUrl)) {
String defaultTermsUrl = "https://www.stirlingpdf.com/terms";
GeneralUtil.saveKeyToSettings("legal.termsAndConditions", defaultTermsUrl);
GeneralUtils.saveKeyToSettings("legal.termsAndConditions", defaultTermsUrl);
applicationProperties.getLegal().setTermsAndConditions(defaultTermsUrl);
}
// Initialize Privacy Policy
String privacyUrl = applicationProperties.getLegal().getPrivacyPolicy();
if (StringUtils.isEmpty(privacyUrl)) {
String defaultPrivacyUrl = "https://www.stirlingpdf.com/privacy-policy";
GeneralUtil.saveKeyToSettings("legal.privacyPolicy", defaultPrivacyUrl);
GeneralUtils.saveKeyToSettings("legal.privacyPolicy", defaultPrivacyUrl);
applicationProperties.getLegal().setPrivacyPolicy(defaultPrivacyUrl);
}
}
@ -95,7 +95,7 @@ public class InitialSetup {
appVersion = props.getProperty("version");
} catch (Exception e) {
}
GeneralUtil.saveKeyToSettings("AutomaticallyGenerated.appVersion", appVersion);
GeneralUtils.saveKeyToSettings("AutomaticallyGenerated.appVersion", appVersion);
applicationProperties.getAutomaticallyGenerated().setAppVersion(appVersion);
}
}

View File

@ -16,7 +16,7 @@ import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import stirling.software.common.util.RequestUriUtil;
import stirling.software.common.util.RequestUriUtils;
@Component
@RequiredArgsConstructor
@ -30,7 +30,7 @@ public class MetricsFilter extends OncePerRequestFilter {
throws ServletException, IOException {
String uri = request.getRequestURI();
if (RequestUriUtil.isTrackableResource(request.getContextPath(), uri)) {
if (RequestUriUtils.isTrackableResource(request.getContextPath(), uri)) {
HttpSession session = request.getSession(false);
String sessionId = (session != null) ? session.getId() : "no-session";
Counter counter =

View File

@ -13,7 +13,7 @@ import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import stirling.software.common.util.RequestUriUtil;
import stirling.software.common.util.RequestUriUtils;
@Slf4j
public class CustomAuthenticationSuccessHandler
@ -48,7 +48,7 @@ public class CustomAuthenticationSuccessHandler
: null;
if (savedRequest != null
&& !RequestUriUtil.isStaticResource(
&& !RequestUriUtils.isStaticResource(
request.getContextPath(), savedRequest.getRedirectUrl())) {
// Redirect to the original destination
super.onAuthenticationSuccess(request, response, authentication);

View File

@ -20,7 +20,7 @@ import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.model.User;
import stirling.software.common.util.RequestUriUtil;
import stirling.software.common.util.RequestUriUtils;
@Slf4j
@Component
@ -40,7 +40,7 @@ public class FirstLoginFilter extends OncePerRequestFilter {
String requestURI = request.getRequestURI();
String contextPath = request.getContextPath();
// Check if the request is for static resources
boolean isStaticResource = RequestUriUtil.isStaticResource(contextPath, requestURI);
boolean isStaticResource = RequestUriUtils.isStaticResource(contextPath, requestURI);
// If it's a static resource, just continue the filter chain and skip the logic below
if (isStaticResource) {
filterChain.doFilter(request, response);

View File

@ -9,7 +9,7 @@ import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import stirling.software.common.util.RequestUriUtil;
import stirling.software.common.util.RequestUriUtils;
@RequiredArgsConstructor
public class IPRateLimitingFilter implements Filter {
@ -29,7 +29,7 @@ public class IPRateLimitingFilter implements Filter {
String requestURI = httpRequest.getRequestURI();
// Check if the request is for static resources
boolean isStaticResource =
RequestUriUtil.isStaticResource(httpRequest.getContextPath(), requestURI);
RequestUriUtils.isStaticResource(httpRequest.getContextPath(), requestURI);
// If it's a static resource, just continue the filter chain and skip the logic below
if (isStaticResource) {

View File

@ -23,7 +23,7 @@ import stirling.software.SPDF.model.AuthenticationType;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.model.ApplicationProperties.Security.OAUTH2;
import stirling.software.common.model.exception.UnsupportedProviderException;
import stirling.software.common.util.RequestUriUtil;
import stirling.software.common.util.RequestUriUtils;
@RequiredArgsConstructor
public class CustomOAuth2AuthenticationSuccessHandler
@ -56,7 +56,7 @@ public class CustomOAuth2AuthenticationSuccessHandler
: null;
if (savedRequest != null
&& !RequestUriUtil.isStaticResource(contextPath, savedRequest.getRedirectUrl())) {
&& !RequestUriUtils.isStaticResource(contextPath, savedRequest.getRedirectUrl())) {
// Redirect to the original destination
super.onAuthenticationSuccess(request, response, authentication);
} else {

View File

@ -1,8 +1,8 @@
package stirling.software.SPDF.config.security.oauth2;
import static org.springframework.security.oauth2.core.AuthorizationGrantType.AUTHORIZATION_CODE;
import static stirling.software.common.util.ProviderUtil.validateProvider;
import static stirling.software.common.util.ValidationUtil.isStringEmpty;
import static stirling.software.common.util.ProviderUtils.validateProvider;
import static stirling.software.common.util.ValidationUtils.isStringEmpty;
import java.util.ArrayList;
import java.util.HashSet;

View File

@ -22,7 +22,7 @@ import stirling.software.SPDF.model.AuthenticationType;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.model.ApplicationProperties.Security.SAML2;
import stirling.software.common.model.exception.UnsupportedProviderException;
import stirling.software.common.util.RequestUriUtil;
import stirling.software.common.util.RequestUriUtils;
@AllArgsConstructor
@Slf4j
@ -58,7 +58,7 @@ public class CustomSaml2AuthenticationSuccessHandler
savedRequest != null);
if (savedRequest != null
&& !RequestUriUtil.isStaticResource(
&& !RequestUriUtils.isStaticResource(
contextPath, savedRequest.getRedirectUrl())) {
log.debug(
"Valid saved request found, redirecting to original destination: {}",

View File

@ -33,7 +33,7 @@ import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.model.api.general.MergePdfsRequest;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
import stirling.software.common.util.WebResponseUtils;
@RestController
@ -137,7 +137,7 @@ public class MergeController {
for (MultipartFile multipartFile : files) {
totalSize += multipartFile.getSize();
File tempFile =
GeneralUtil.convertMultipartFileToFile(
GeneralUtils.convertMultipartFileToFile(
multipartFile); // Convert MultipartFile to File
filesToDelete.add(tempFile); // Add temp file to the list for later deletion
mergerUtility.addSource(tempFile); // Add source file to the merger utility

View File

@ -28,7 +28,7 @@ import lombok.RequiredArgsConstructor;
import stirling.software.SPDF.model.api.general.OverlayPdfsRequest;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
import stirling.software.common.util.WebResponseUtils;
@RestController
@ -56,7 +56,7 @@ public class PdfOverlayController {
try {
for (int i = 0; i < overlayFiles.length; i++) {
overlayPdfFiles[i] = GeneralUtil.multipartToFile(overlayFiles[i]);
overlayPdfFiles[i] = GeneralUtils.multipartToFile(overlayFiles[i]);
}
String mode = request.getOverlayMode(); // "SequentialOverlay", "InterleavedOverlay",

View File

@ -25,7 +25,7 @@ import stirling.software.SPDF.model.SortTypes;
import stirling.software.SPDF.model.api.PDFWithPageNums;
import stirling.software.SPDF.model.api.general.RearrangePagesRequest;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
import stirling.software.common.util.WebResponseUtils;
@RestController
@ -56,7 +56,7 @@ public class RearrangePagesPDFController {
String[] pageOrderArr = pagesToDelete.split(",");
List<Integer> pagesToRemove =
GeneralUtil.parsePageList(pageOrderArr, document.getNumberOfPages(), false);
GeneralUtils.parsePageList(pageOrderArr, document.getNumberOfPages(), false);
Collections.sort(pagesToRemove);
@ -262,7 +262,7 @@ public class RearrangePagesPDFController {
&& !"custom".equals(sortType.toLowerCase())) {
newPageOrder = processSortTypes(sortType, totalPages, pageOrder);
} else {
newPageOrder = GeneralUtil.parsePageList(pageOrderArr, totalPages, false);
newPageOrder = GeneralUtils.parsePageList(pageOrderArr, totalPages, false);
}
log.info("newPageOrder = " + newPageOrder);
log.info("totalPages = " + totalPages);

View File

@ -19,7 +19,7 @@ import lombok.RequiredArgsConstructor;
import stirling.software.SPDF.config.EndpointConfiguration;
import stirling.software.common.configuration.InstallationPathConfig;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
@Controller
@Tag(name = "Settings", description = "Settings APIs")
@ -40,7 +40,7 @@ public class SettingsController {
"Setting has already been set, To adjust please edit "
+ InstallationPathConfig.getSettingsPath());
}
GeneralUtil.saveKeyToSettings("system.enableAnalytics", enabled);
GeneralUtils.saveKeyToSettings("system.enableAnalytics", enabled);
applicationProperties.getSystem().setEnableAnalytics(enabled);
return ResponseEntity.ok("Updated");
}

View File

@ -26,7 +26,7 @@ import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.model.api.general.SplitPdfBySizeOrCountRequest;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
import stirling.software.common.util.WebResponseUtils;
@RestController
@ -81,7 +81,7 @@ public class SplitPdfBySizeController {
if (type == 0) {
log.debug("Processing split by size");
long maxBytes = GeneralUtil.convertSizeToBytes(value);
long maxBytes = GeneralUtils.convertSizeToBytes(value);
log.debug("Max bytes per document: {}", maxBytes);
handleSplitBySize(sourceDocument, maxBytes, zipOut, filename);
} else if (type == 1) {

View File

@ -34,7 +34,7 @@ import stirling.software.SPDF.model.api.converters.ConvertToImageRequest;
import stirling.software.SPDF.model.api.converters.ConvertToPdfRequest;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.CheckProgramInstall;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
import stirling.software.common.util.PdfUtils;
import stirling.software.common.util.ProcessExecutor;
import stirling.software.common.util.ProcessExecutor.ProcessExecutorResult;
@ -251,7 +251,7 @@ public class ConvertImgPDFController {
// Load the input PDF
PDDocument document = pdfDocumentFactory.load(pdfFile);
int totalPages = document.getNumberOfPages();
List<Integer> newPageOrder = GeneralUtil.parsePageList(pageOrderArr, totalPages, false);
List<Integer> newPageOrder = GeneralUtils.parsePageList(pageOrderArr, totalPages, false);
// Create a new list to hold the pages in the new order
List<PDPage> newPages = new ArrayList<>();

View File

@ -23,7 +23,7 @@ import stirling.software.SPDF.model.api.converters.UrlToPdfRequest;
import stirling.software.common.configuration.RuntimePathConfig;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
import stirling.software.common.util.ProcessExecutor;
import stirling.software.common.util.ProcessExecutor.ProcessExecutorResult;
import stirling.software.common.util.WebResponseUtils;
@ -53,12 +53,12 @@ public class ConvertWebsiteToPDF {
throw new IllegalArgumentException("This endpoint has been disabled by the admin.");
}
// Validate the URL format
if (!URL.matches("^https?://.*") || !GeneralUtil.isValidURL(URL)) {
if (!URL.matches("^https?://.*") || !GeneralUtils.isValidURL(URL)) {
throw new IllegalArgumentException("Invalid URL format provided.");
}
// validate the URL is reachable
if (!GeneralUtil.isURLReachable(URL)) {
if (!GeneralUtils.isURLReachable(URL)) {
throw new IllegalArgumentException("URL is not reachable, please provide a valid URL.");
}

View File

@ -52,7 +52,7 @@ import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.config.EndpointConfiguration;
import stirling.software.SPDF.model.api.misc.OptimizePdfRequest;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
import stirling.software.common.util.ProcessExecutor;
import stirling.software.common.util.ProcessExecutor.ProcessExecutorResult;
import stirling.software.common.util.WebResponseUtils;
@ -111,7 +111,7 @@ public class CompressController {
scaleFactor,
jpegQuality,
convertToGrayscale,
GeneralUtil.formatBytes(originalFileSize));
GeneralUtils.formatBytes(originalFileSize));
try (PDDocument doc = pdfDocumentFactory.load(pdfFile)) {
// Find all unique images in the document
@ -145,8 +145,8 @@ public class CompressController {
double overallReduction = 100.0 - ((compressedFileSize * 100.0) / originalFileSize);
log.info(
"Overall PDF compression: {} → {} (reduced by {}%)",
GeneralUtil.formatBytes(originalFileSize),
GeneralUtil.formatBytes(compressedFileSize),
GeneralUtils.formatBytes(originalFileSize),
GeneralUtils.formatBytes(compressedFileSize),
String.format("%.1f", overallReduction));
return newCompressedPDF;
}
@ -316,8 +316,8 @@ public class CompressController {
log.info(
"Image hash {}: Compressed from {} to {} (reduced by {}%)",
imageHash,
GeneralUtil.formatBytes(originalSize),
GeneralUtil.formatBytes(compressedSize),
GeneralUtils.formatBytes(originalSize),
GeneralUtils.formatBytes(compressedSize),
String.format("%.1f", reductionPercentage));
} else {
log.info("Image hash {}: Not suitable for compression, skipping", imageHash);
@ -456,8 +456,8 @@ public class CompressController {
stats.nestedImages);
log.info(
"Total original image size: {}, compressed: {} (reduced by {}%)",
GeneralUtil.formatBytes(stats.totalOriginalBytes),
GeneralUtil.formatBytes(stats.totalCompressedBytes),
GeneralUtils.formatBytes(stats.totalOriginalBytes),
GeneralUtils.formatBytes(stats.totalCompressedBytes),
String.format("%.1f", overallImageReduction));
}
@ -673,7 +673,7 @@ public class CompressController {
Long expectedOutputSize = 0L;
boolean autoMode = false;
if (expectedOutputSizeString != null && expectedOutputSizeString.length() > 1) {
expectedOutputSize = GeneralUtil.convertSizeToBytes(expectedOutputSizeString);
expectedOutputSize = GeneralUtils.convertSizeToBytes(expectedOutputSizeString);
autoMode = true;
}
@ -794,7 +794,7 @@ public class CompressController {
throws IOException {
long preQpdfSize = Files.size(currentFile);
log.info("Pre-QPDF file size: {}", GeneralUtil.formatBytes(preQpdfSize));
log.info("Pre-QPDF file size: {}", GeneralUtils.formatBytes(preQpdfSize));
// Map optimization levels to QPDF compression levels
int qpdfCompressionLevel;
@ -839,7 +839,7 @@ public class CompressController {
double qpdfReduction = 100.0 - ((postQpdfSize * 100.0) / preQpdfSize);
log.info(
"Post-QPDF file size: {} (reduced by {}%)",
GeneralUtil.formatBytes(postQpdfSize), String.format("%.1f", qpdfReduction));
GeneralUtils.formatBytes(postQpdfSize), String.format("%.1f", qpdfReduction));
} catch (Exception e) {
if (returnCode != null && returnCode.getRc() != 3) {

View File

@ -26,7 +26,7 @@ import lombok.RequiredArgsConstructor;
import stirling.software.SPDF.model.api.misc.AddPageNumbersRequest;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
import stirling.software.common.util.WebResponseUtils;
@RestController
@ -80,7 +80,7 @@ public class PageNumbersController {
customText = "{n}";
}
List<Integer> pagesToNumberList =
GeneralUtil.parsePageList(pagesToNumber.split(","), document.getNumberOfPages());
GeneralUtils.parsePageList(pagesToNumber.split(","), document.getNumberOfPages());
for (int i : pagesToNumberList) {
PDPage page = document.getPage(i);

View File

@ -36,7 +36,7 @@ import stirling.software.SPDF.model.api.security.RedactPdfRequest;
import stirling.software.SPDF.model.api.security.RedactionArea;
import stirling.software.SPDF.pdf.TextFinder;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
import stirling.software.common.util.PdfUtils;
import stirling.software.common.util.WebResponseUtils;
import stirling.software.common.util.propertyeditor.StringToArrayListPropertyEditor;
@ -184,7 +184,8 @@ public class RedactController {
String pageNumbersInput = request.getPageNumbers();
String[] parsedPageNumbers =
pageNumbersInput != null ? pageNumbersInput.split(",") : new String[0];
List<Integer> pageNumbers = GeneralUtil.parsePageList(parsedPageNumbers, pagesCount, false);
List<Integer> pageNumbers =
GeneralUtils.parsePageList(parsedPageNumbers, pagesCount, false);
Collections.sort(pageNumbers);
return pageNumbers;
}

View File

@ -1,6 +1,6 @@
package stirling.software.SPDF.controller.web;
import static stirling.software.common.util.ProviderUtil.validateProvider;
import static stirling.software.common.util.ProviderUtils.validateProvider;
import java.time.Instant;
import java.time.temporal.ChronoUnit;

View File

@ -29,7 +29,7 @@ import stirling.software.SPDF.service.SignatureService;
import stirling.software.common.configuration.InstallationPathConfig;
import stirling.software.common.configuration.RuntimePathConfig;
import stirling.software.common.service.UserServiceInterface;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
@Controller
@Tag(name = "General", description = "General APIs")
@ -240,7 +240,7 @@ public class GeneralWebController {
private List<FontResource> getFontNamesFromLocation(String locationPattern) {
try {
Resource[] resources =
GeneralUtil.getResourcesFromLocationPattern(locationPattern, resourceLoader);
GeneralUtils.getResourcesFromLocationPattern(locationPattern, resourceLoader);
return Arrays.stream(resources)
.map(
resource -> {

View File

@ -12,7 +12,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import stirling.software.common.model.api.PDFFile;
import stirling.software.common.util.GeneralUtil;
import stirling.software.common.util.GeneralUtils;
@Data
@EqualsAndHashCode(callSuper = true)
@ -30,6 +30,6 @@ public class PDFWithPageNums extends PDFFile {
@Hidden
public List<Integer> getPageNumbersList(PDDocument doc, boolean oneBased) {
int pageCount = doc.getNumberOfPages();
return GeneralUtil.parsePageList(pageNumbers, pageCount, oneBased);
return GeneralUtils.parsePageList(pageNumbers, pageCount, oneBased);
}
}