Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HTTPS Support with SSL/TLS and HTTP to HTTPS Redirection #15

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ target/
.settings
.springBeans
.sts4-cache
bin/

### IntelliJ IDEA ###
.idea
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<scope>test</scope>
</dependency>

</dependencies>
</dependencies>

<build>
<plugins>
Expand Down
68 changes: 46 additions & 22 deletions src/main/java/com/httpserver/HttpServerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import java.util.Objects;
import java.io.IOException;
import org.slf4j.LoggerFactory;
import com.httpserver.config.Configuration;
import com.httpserver.core.ServerListenerThread;
import com.httpserver.config.HttpServerConfiguration;
import com.httpserver.core.http.HttpServerListenerThread;
import com.httpserver.core.https.HttpsServerListenerThread;
import com.httpserver.config.ConfigurationManager;
import com.httpserver.config.SSLConfiguration;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
Expand All @@ -25,45 +28,66 @@ public class HttpServerApplication {
* @param args command-line arguments; expects a single argument specifying the configuration file path.
*/
public static void main(String[] args) {

LOGGER.info("Starting HTTP server application...");

// Uncomment if you want to enforce configuration file path argument
LOGGER.info("Starting HTTP server application...");
// Uncomment if you want to enforce configuration file path argument
/*
if (args.length != 1) {
LOGGER.error("No configuration file provided.");
LOGGER.error("Syntax: java -jar simplehttpserver-1.0-SNAPSHOT.jar <config.json>");
return;
}
*/

try {
LOGGER.debug("Loading configuration file from the classpath.");
// Load the configuration from the provided JSON file.
String configFilePath = Objects.requireNonNull(HttpServerApplication.class.getClassLoader().getResource("http.json")).getFile();
ConfigurationManager.getInstance().loadConfigurationFile(configFilePath);
LOGGER.debug("Configuration file loaded from path: {}", configFilePath);
} catch (NullPointerException e) {
LOGGER.error("Configuration file not found: http.json");
return;
LOGGER.debug("Loading configuration files from the classpath.");
loadConfigurations();
} catch (Exception e) {
LOGGER.error("Failed to load configuration file: {}", e.getMessage());
LOGGER.debug("Exception details: ", e); // Log full exception stack trace for debugging
return;
}

HttpServerConfiguration config = ConfigurationManager.getInstance().getConfiguration(HttpServerConfiguration.class);

Configuration config = ConfigurationManager.getInstance().getCurrentConfiguration();
LOGGER.info("Using Port: {}", config.getPort());
LOGGER.info("Using HTTP Port: {}", config.getHttpPort());
LOGGER.info("Using HTTPS Port: {}", config.getHttpsPort());
LOGGER.info("Using Webroot: {}", config.getWebroot());

try {
LOGGER.info("Starting server listener thread...");
ServerListenerThread serverListenerThread = new ServerListenerThread(config.getPort(), config.getWebroot());
LOGGER.info("Starting server listener threads...");
HttpsServerListenerThread serverListenerThread = new HttpsServerListenerThread(config.getHttpsPort() , config.getWebroot());
serverListenerThread.start();
LOGGER.info("Server listener thread started successfully.");
} catch (IOException e) {
LOGGER.error("Error starting server listener thread: {}", e.getMessage());

HttpServerListenerThread httpServerListenerThread = new HttpServerListenerThread(config.getHttpPort() , config.getWebroot());
httpServerListenerThread.start();

LOGGER.info("Server listener threads started successfully.");
} catch (Exception e) {
LOGGER.error("Error starting server listener s: {}", e.getMessage());
LOGGER.debug("Exception details: ", e); // Log full exception stack trace for debugging
}
}

/**
* Load configurations from JSON files.
*
* @throws IOException if configuration files are not found or can't be read.
*/
private static void loadConfigurations() throws IOException {
String httpServerConfigFilePath = Objects.requireNonNull(HttpServerApplication.class.getClassLoader().getResource("http.json")).getFile();
ConfigurationManager.getInstance().loadConfiguration(
httpServerConfigFilePath,
HttpServerConfiguration.class
);
LOGGER.debug("Http Server Configuration file loaded from path: {}", httpServerConfigFilePath);

String sslConfigFilePath = Objects.requireNonNull(HttpServerApplication.class.getClassLoader().getResource("ssl-config.json")).getFile();
ConfigurationManager.getInstance().loadConfiguration(
sslConfigFilePath,
SSLConfiguration.class
);
LOGGER.debug("SSL Configuration file loaded from path: {}", sslConfigFilePath);
}
}
91 changes: 0 additions & 91 deletions src/main/java/com/httpserver/config/Configuration.java

This file was deleted.

Loading