From 33f8d609009d2b18c4e3b9961d5b715233f6f0c4 Mon Sep 17 00:00:00 2001 From: Parth P Shah Date: Fri, 23 Feb 2024 23:46:36 +0530 Subject: [PATCH 1/3] Update SPdfApplication.java --- .../software/SPDF/SPdfApplication.java | 103 +++++++++--------- 1 file changed, 51 insertions(+), 52 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/SPdfApplication.java b/src/main/java/stirling/software/SPDF/SPdfApplication.java index 62cbfe10..673bbf16 100644 --- a/src/main/java/stirling/software/SPDF/SPdfApplication.java +++ b/src/main/java/stirling/software/SPDF/SPdfApplication.java @@ -1,36 +1,72 @@ -package stirling.software.SPDF; - import java.nio.file.Files; import java.nio.file.Paths; import java.util.Collections; -import org.springframework.beans.factory.annotation.Autowired; +import io.github.pixee.security.SystemCommand; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.EnableScheduling; -import io.github.pixee.security.SystemCommand; - -import jakarta.annotation.PostConstruct; -import stirling.software.SPDF.config.ConfigInitializer; -import stirling.software.SPDF.utils.GeneralUtils; +import static java.nio.file.Files.createDirectories; @SpringBootApplication @EnableScheduling public class SPdfApplication { - @Autowired private Environment env; + private static Environment env; - @PostConstruct - public void init() { - // Check if the BROWSER_OPEN environment variable is set to true - String browserOpenEnv = env.getProperty("BROWSER_OPEN"); + public SPdfApplication(Environment env) { + SPdfApplication.env = env; + } + + public static void main(String[] args) { + SpringApplication app = new SpringApplication(SPdfApplication.class); + app.addInitializers((ApplicationContextInitializer) applicationContext -> { + if (Files.exists(Paths.get("configs/settings.yml"))) { + app.setDefaultProperties( + Collections.singletonMap( + "spring.config.additional-location", "file:configs/settings.yml")); + } else { + System.out.println( + "External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead."); + } + ConfigurableApplicationContext context = app.run(args); + + createDirectories(); + printStartupMessage(context); + } + } + + private static void createDirectories() { + try { + createDirectories(Paths.get("customFiles/static/")); + createDirectories(Paths.get("customFiles/templates/")); + } catch (Exception e) { + System.err.println("Error creating directories: " + e.getMessage()); + } + } + + private static void printStartupMessage(ConfigurableApplicationContext context) { + String port = context.getEnvironment().getProperty("local.server.port", "8080"); + String url = "http://localhost:" + port; + System.out.println("Stirling-PDF Started."); + System.out.println("Navigate to " + url); + + // Open browser if BROWSER_OPEN environment variable is set to true + openBrowserIfRequired(context); + } + + private static void openBrowserIfRequired(ConfigurableApplicationContext context) { + Environment environment = context.getEnvironment(); + String browserOpenEnv = environment.getProperty("BROWSER_OPEN"); boolean browserOpen = browserOpenEnv != null && "true".equalsIgnoreCase(browserOpenEnv); if (browserOpen) { try { - String url = "http://localhost:" + getPort(); + String url = "http://localhost:" + context.getEnvironment().getProperty("local.server.port", "8080"); String os = System.getProperty("os.name").toLowerCase(); Runtime rt = Runtime.getRuntime(); @@ -39,45 +75,8 @@ public class SPdfApplication { SystemCommand.runCommand(rt, "rundll32 url.dll,FileProtocolHandler " + url); } } catch (Exception e) { - e.printStackTrace(); + System.err.println("Error opening browser: " + e.getMessage()); } } } - - public static void main(String[] args) { - SpringApplication app = new SpringApplication(SPdfApplication.class); - app.addInitializers(new ConfigInitializer()); - if (Files.exists(Paths.get("configs/settings.yml"))) { - app.setDefaultProperties( - Collections.singletonMap( - "spring.config.additional-location", "file:configs/settings.yml")); - } else { - System.out.println( - "External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead."); - } - app.run(args); - - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - GeneralUtils.createDir("customFiles/static/"); - GeneralUtils.createDir("customFiles/templates/"); - - System.out.println("Stirling-PDF Started."); - - String url = "http://localhost:" + getPort(); - System.out.println("Navigate to " + url); - } - - public static String getPort() { - String port = System.getProperty("local.server.port"); - if (port == null || port.isEmpty()) { - port = "8080"; - } - return port; - } } From 8670afb96f52cce3d172f48813546355fac08ba2 Mon Sep 17 00:00:00 2001 From: Parth P Shah Date: Fri, 23 Feb 2024 23:46:42 +0530 Subject: [PATCH 2/3] Revert "Update SPdfApplication.java" This reverts commit 33f8d609009d2b18c4e3b9961d5b715233f6f0c4. --- .../software/SPDF/SPdfApplication.java | 103 +++++++++--------- 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/SPdfApplication.java b/src/main/java/stirling/software/SPDF/SPdfApplication.java index 673bbf16..62cbfe10 100644 --- a/src/main/java/stirling/software/SPDF/SPdfApplication.java +++ b/src/main/java/stirling/software/SPDF/SPdfApplication.java @@ -1,72 +1,36 @@ +package stirling.software.SPDF; + import java.nio.file.Files; import java.nio.file.Paths; import java.util.Collections; -import io.github.pixee.security.SystemCommand; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.EnableScheduling; -import static java.nio.file.Files.createDirectories; +import io.github.pixee.security.SystemCommand; + +import jakarta.annotation.PostConstruct; +import stirling.software.SPDF.config.ConfigInitializer; +import stirling.software.SPDF.utils.GeneralUtils; @SpringBootApplication @EnableScheduling public class SPdfApplication { - private static Environment env; + @Autowired private Environment env; - public SPdfApplication(Environment env) { - SPdfApplication.env = env; - } - - public static void main(String[] args) { - SpringApplication app = new SpringApplication(SPdfApplication.class); - app.addInitializers((ApplicationContextInitializer) applicationContext -> { - if (Files.exists(Paths.get("configs/settings.yml"))) { - app.setDefaultProperties( - Collections.singletonMap( - "spring.config.additional-location", "file:configs/settings.yml")); - } else { - System.out.println( - "External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead."); - } - ConfigurableApplicationContext context = app.run(args); - - createDirectories(); - printStartupMessage(context); - } - } - - private static void createDirectories() { - try { - createDirectories(Paths.get("customFiles/static/")); - createDirectories(Paths.get("customFiles/templates/")); - } catch (Exception e) { - System.err.println("Error creating directories: " + e.getMessage()); - } - } - - private static void printStartupMessage(ConfigurableApplicationContext context) { - String port = context.getEnvironment().getProperty("local.server.port", "8080"); - String url = "http://localhost:" + port; - System.out.println("Stirling-PDF Started."); - System.out.println("Navigate to " + url); - - // Open browser if BROWSER_OPEN environment variable is set to true - openBrowserIfRequired(context); - } - - private static void openBrowserIfRequired(ConfigurableApplicationContext context) { - Environment environment = context.getEnvironment(); - String browserOpenEnv = environment.getProperty("BROWSER_OPEN"); + @PostConstruct + public void init() { + // Check if the BROWSER_OPEN environment variable is set to true + String browserOpenEnv = env.getProperty("BROWSER_OPEN"); boolean browserOpen = browserOpenEnv != null && "true".equalsIgnoreCase(browserOpenEnv); if (browserOpen) { try { - String url = "http://localhost:" + context.getEnvironment().getProperty("local.server.port", "8080"); + String url = "http://localhost:" + getPort(); String os = System.getProperty("os.name").toLowerCase(); Runtime rt = Runtime.getRuntime(); @@ -75,8 +39,45 @@ public class SPdfApplication { SystemCommand.runCommand(rt, "rundll32 url.dll,FileProtocolHandler " + url); } } catch (Exception e) { - System.err.println("Error opening browser: " + e.getMessage()); + e.printStackTrace(); } } } + + public static void main(String[] args) { + SpringApplication app = new SpringApplication(SPdfApplication.class); + app.addInitializers(new ConfigInitializer()); + if (Files.exists(Paths.get("configs/settings.yml"))) { + app.setDefaultProperties( + Collections.singletonMap( + "spring.config.additional-location", "file:configs/settings.yml")); + } else { + System.out.println( + "External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead."); + } + app.run(args); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + GeneralUtils.createDir("customFiles/static/"); + GeneralUtils.createDir("customFiles/templates/"); + + System.out.println("Stirling-PDF Started."); + + String url = "http://localhost:" + getPort(); + System.out.println("Navigate to " + url); + } + + public static String getPort() { + String port = System.getProperty("local.server.port"); + if (port == null || port.isEmpty()) { + port = "8080"; + } + return port; + } } From 6fef4ea82c1ce5b0f037803f06b51c85c41d7077 Mon Sep 17 00:00:00 2001 From: Parth P Shah Date: Sat, 24 Feb 2024 00:01:20 +0530 Subject: [PATCH 3/3] Update SPdfApplication.java --- .../software/SPDF/SPdfApplication.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/SPdfApplication.java b/src/main/java/stirling/software/SPDF/SPdfApplication.java index 62cbfe10..b2291186 100644 --- a/src/main/java/stirling/software/SPDF/SPdfApplication.java +++ b/src/main/java/stirling/software/SPDF/SPdfApplication.java @@ -1,6 +1,8 @@ package stirling.software.SPDF; +import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; @@ -9,7 +11,8 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.EnableScheduling; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import io.github.pixee.security.SystemCommand; import jakarta.annotation.PostConstruct; @@ -20,6 +23,8 @@ import stirling.software.SPDF.utils.GeneralUtils; @EnableScheduling public class SPdfApplication { + private static final Logger logger = LoggerFactory.getLogger(SPdfApplication.class); + @Autowired private Environment env; @PostConstruct @@ -39,12 +44,12 @@ public class SPdfApplication { SystemCommand.runCommand(rt, "rundll32 url.dll,FileProtocolHandler " + url); } } catch (Exception e) { - e.printStackTrace(); + logger.error("Error opening browser: {}", e.getMessage()); } } } - public static void main(String[] args) { + public static void main(String[] args) throws IOException, InterruptedException { SpringApplication app = new SpringApplication(SPdfApplication.class); app.addInitializers(new ConfigInitializer()); if (Files.exists(Paths.get("configs/settings.yml"))) { @@ -52,25 +57,30 @@ public class SPdfApplication { Collections.singletonMap( "spring.config.additional-location", "file:configs/settings.yml")); } else { - System.out.println( - "External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead."); + logger.warn("External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead."); } app.run(args); try { Thread.sleep(1000); } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new InterruptedException("Error"); } - GeneralUtils.createDir("customFiles/static/"); - GeneralUtils.createDir("customFiles/templates/"); + try { + Files.createDirectories(Path.of("customFiles/static/")); + Files.createDirectories(Path.of("customFiles/templates/")); + } catch (Exception e) { + logger.error("Error creating directories: {}", e.getMessage()); + } + printStartupLogs(); + } - System.out.println("Stirling-PDF Started."); + private static void printStartupLogs() { + logger.info("Stirling-PDF Started."); String url = "http://localhost:" + getPort(); - System.out.println("Navigate to " + url); + logger.info("Navigate to {}", url); } public static String getPort() {