Skip to content

Commit

Permalink
Fixes issue piranhacloud#3466 - Change distributions to use HttpsFeat…
Browse files Browse the repository at this point in the history
…ure where applicable (piranhacloud#3467)
  • Loading branch information
mnriem authored Jun 30, 2023
1 parent e765376 commit 1b97c8c
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,17 @@ public class CoreProfilePiranha implements Piranha, Runnable {
/**
* Stores the HTTPS feature.
*/
private final HttpsFeature httpsFeature = new HttpsFeature();
private HttpsFeature httpsFeature;

/**
* Stores the HTTP port.
*/
private int httpsPort = 8080;

/**
* Stores the HTTP server class.
*/
private String httpsServerClass;

/**
* Stores the JMPS enabled flag.
Expand Down Expand Up @@ -222,9 +232,12 @@ public void run() {
}

/**
* Initialize and start the HTTPS endpoint (if applicable).
* Construct, initialize and start the HTTPS endpoint (if applicable).
*/
if (httpsFeature.getPort() > 0) {
if (httpsPort > 0) {
httpsFeature = new HttpsFeature();
httpsFeature.setHttpsServerClass(httpsServerClass);
httpsFeature.setPort(httpsPort);
httpsFeature.init();
httpsFeature.getHttpsServer().setHttpServerProcessor(webApplicationServer);
httpsFeature.start();
Expand Down Expand Up @@ -369,7 +382,7 @@ public void setHttpsKeystorePassword(String httpsKeystorePassword) {
* @param httpsPort the HTTPS server port.
*/
public void setHttpsPort(int httpsPort) {
this.httpsFeature.setPort(httpsPort);
this.httpsPort = httpsPort;
}

/**
Expand All @@ -378,7 +391,7 @@ public void setHttpsPort(int httpsPort) {
* @param httpsServerClass the HTTPS server class.
*/
public void setHttpsServerClass(String httpsServerClass) {
this.httpsFeature.setHttpsServerClass(httpsServerClass);
this.httpsServerClass = httpsServerClass;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions dist/isolated/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.feature</groupId>
<artifactId>piranha-feature-https</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.http</groupId>
<artifactId>piranha-http-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import cloud.piranha.core.api.WebApplicationRequest;
import cloud.piranha.core.api.WebApplicationResponse;
import cloud.piranha.feature.http.HttpFeature;
import cloud.piranha.feature.https.HttpsFeature;
import cloud.piranha.http.api.HttpServer;
import cloud.piranha.http.impl.DefaultHttpServer;
import cloud.piranha.http.webapp.HttpWebApplicationServer;
import cloud.piranha.micro.builder.MicroWebApplication;
import cloud.piranha.micro.loader.MicroConfiguration;
Expand Down Expand Up @@ -87,9 +87,19 @@ public class IsolatedPiranha implements Piranha, Runnable {
private String httpServerClass;

/**
* Stores the SSL flag.
* Stores the HTTPS feature.
*/
private boolean ssl = false;
private HttpsFeature httpsFeature;

/**
* Stores the HTTPS port.
*/
private int httpsPort = -1;

/**
* Stores the HTTPS server class.
*/
private String httpsServerClass;

/**
* Stores the HTTP web application server.
Expand Down Expand Up @@ -130,8 +140,15 @@ private void processArguments(String[] arguments) {
if (arguments[i].equals("--http-server-class")) {
httpServerClass = arguments[i + 1];
}
if (arguments[i].equals("--https-port")) {
httpsPort = Integer.parseInt(arguments[i + 1]);
}
if (arguments[i].equals("--https-server-class")) {
httpsServerClass = arguments[i + 1];
}
if (arguments[i].equals("--ssl")) {
ssl = true;
LOGGER.log(WARNING, "The --ssl parameter is deprecated, please use --https-port instead");
httpsPort = 8043;
}
}
}
Expand Down Expand Up @@ -170,28 +187,13 @@ public void run() {
LOGGER.log(WARNING, "Unable to delete deploying file", ioe);
}
}

long finishTime = System.currentTimeMillis();
LOGGER.log(INFO, "Started Piranha");
LOGGER.log(INFO, "It took {0} milliseconds", finishTime - startTime);

File startedFile = createStartedFile();

File pidFile = new File("tmp/piranha.pid");

HttpServer httpServer = null;

if (ssl) {
httpServer = new DefaultHttpServer();
httpServer.setServerPort(8080);
httpServer.setHttpServerProcessor(webApplicationServer);
httpServer.setSSL(ssl);
httpServer.start();
}

/*
* Construct, initialize and start HTTP endpoint (if applicable).
*/
if (!ssl && httpPort > 0) {
if (httpPort > 0) {
httpFeature = new HttpFeature();
httpFeature.setHttpServerClass(httpServerClass);
httpFeature.setPort(httpPort);
Expand All @@ -201,6 +203,28 @@ public void run() {
httpServer = httpFeature.getHttpServer();
}

/*
* Construct, initialize and start HTTPS endpoint (if applicable).
*/
if (httpsPort > 0) {
httpsFeature = new HttpsFeature();
httpsFeature.setHttpsServerClass(httpsServerClass);
httpsFeature.setPort(httpsPort);
httpsFeature.init();
httpsFeature.getHttpsServer().setHttpServerProcessor(webApplicationServer);
httpsFeature.start();
if (httpServer == null) {
httpServer = httpsFeature.getHttpsServer();
}
}

long finishTime = System.currentTimeMillis();
LOGGER.log(INFO, "Started Piranha");
LOGGER.log(INFO, "It took {0} milliseconds", finishTime - startTime);

File startedFile = createStartedFile();
File pidFile = new File("tmp/piranha.pid");

while (httpServer.isRunning()) {
try {
Thread.sleep(2000);
Expand Down
1 change: 1 addition & 0 deletions dist/isolated/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
requires cloud.piranha.embedded;
requires cloud.piranha.extension.servlet;
requires cloud.piranha.feature.http;
requires cloud.piranha.feature.https;
requires cloud.piranha.http.api;
requires cloud.piranha.http.impl;
requires cloud.piranha.http.webapp;
Expand Down
6 changes: 6 additions & 0 deletions dist/microprofile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.feature</groupId>
<artifactId>piranha-feature-https</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.http</groupId>
<artifactId>piranha-http-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
import static cloud.piranha.core.impl.WarFileExtractor.extractWarFile;
import cloud.piranha.extension.microprofile.MicroProfileExtension;
import cloud.piranha.feature.http.HttpFeature;
import cloud.piranha.http.api.HttpServer;
import cloud.piranha.http.impl.DefaultHttpServer;
import cloud.piranha.feature.https.HttpsFeature;
import cloud.piranha.http.webapp.HttpWebApplicationServer;
import cloud.piranha.resource.impl.DirectoryResource;
import jakarta.servlet.ServletException;
Expand All @@ -56,7 +55,6 @@
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.ServiceLoader;

Expand Down Expand Up @@ -102,6 +100,11 @@ public class MicroProfilePiranha implements Piranha, Runnable {
*/
private String httpServerClass;

/**
* Stores the HTTPS feature.
*/
private HttpsFeature httpsFeature;

/**
* Stores the HTTPS port.
*/
Expand Down Expand Up @@ -157,28 +160,6 @@ public void run() {

webApplicationServer = new HttpWebApplicationServer();

if (httpsPort > 0) {
HttpServer httpsServer = null;
if (httpsServerClass == null) {
httpsServerClass = DefaultHttpServer.class.getName();
}
try {
httpsServer = (HttpServer) Class.forName(httpsServerClass)
.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException | IllegalAccessException
| IllegalArgumentException | InstantiationException
| NoSuchMethodException | SecurityException
| InvocationTargetException t) {
LOGGER.log(ERROR, "Unable to construct HTTPS server", t);
}
if (httpsServer != null) {
httpsServer.setHttpServerProcessor(webApplicationServer);
httpsServer.setServerPort(httpsPort);
httpsServer.setSSL(true);
httpsServer.start();
}
}

if (warFile != null && warFile.getName().toLowerCase().endsWith(".war")) {
if (contextPath == null) {
contextPath = warFile.getName().substring(0, warFile.getName().length() - 4);
Expand Down Expand Up @@ -249,6 +230,18 @@ public void run() {
httpFeature.getHttpServer().setHttpServerProcessor(webApplicationServer);
httpFeature.start();
}

/*
* Construct, initialize and start HTTP endpoint (if applicable).
*/
if (httpsPort > 0) {
httpsFeature = new HttpsFeature();
httpsFeature.setHttpsServerClass(httpsServerClass);
httpsFeature.setPort(httpsPort);
httpsFeature.init();
httpsFeature.getHttpsServer().setHttpServerProcessor(webApplicationServer);
httpsFeature.start();
}

long finishTime = System.currentTimeMillis();
LOGGER.log(INFO, "Started Piranha");
Expand Down
1 change: 1 addition & 0 deletions dist/microprofile/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
requires transitive cloud.piranha.core.api;
requires cloud.piranha.core.impl;
requires cloud.piranha.feature.http;
requires cloud.piranha.feature.https;
requires cloud.piranha.extension.microprofile;
requires cloud.piranha.http.impl;
requires cloud.piranha.http.webapp;
Expand Down
6 changes: 6 additions & 0 deletions dist/platform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.feature</groupId>
<artifactId>piranha-feature-https</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.http</groupId>
<artifactId>piranha-http-impl</artifactId>
Expand Down
Loading

0 comments on commit 1b97c8c

Please sign in to comment.