Skip to content

Commit

Permalink
#141 Change to Grizzly Http Server
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderNeumann committed Jun 15, 2022
1 parent b83c5f4 commit f055807
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions webconnector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ dependencies {

implementation "com.nimbusds:oauth2-oidc-sdk:4.17"
implementation "org.glassfish.jersey.containers:jersey-container-jdk-http:${project.property('jersey.version')}"
implementation "org.glassfish.jersey.containers:jersey-container-grizzly2-http:${project.property('jersey.version')}"
implementation "net.minidev:json-smart:2.3"
implementation "org.webjars:swagger-ui:3.6.1"
implementation "javax.xml.bind:jaxb-api:2.3.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.StreamHandler;
Expand All @@ -27,15 +26,16 @@
import javax.net.ssl.SSLException;
import javax.ws.rs.core.MultivaluedMap;

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;

import com.nimbusds.oauth2.sdk.http.HTTPRequest;
import com.nimbusds.oauth2.sdk.http.HTTPRequest.Method;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsServer;

import i5.las2peer.api.logging.MonitoringEvent;
import i5.las2peer.connectors.Connector;
Expand Down Expand Up @@ -134,7 +134,7 @@ public class WebConnector extends Connector {
public static final String SSL_INSTANCE_NAME = "TLSv1.2";

private HttpServer http;
private HttpsServer https;
private HttpServer https;

private Node myNode = null;

Expand Down Expand Up @@ -396,7 +396,7 @@ public void start(Node node) throws ConnectorException {
if (http != null) {
// try to cleanup mess
try {
http.stop(0);
http.shutdownNow();
} catch (Exception e2) {
logger.log(Level.SEVERE, "HTTPS server cleanup failed after failed connector start", e2);
} finally {
Expand All @@ -406,7 +406,7 @@ public void start(Node node) throws ConnectorException {
if (https != null) {
// try to cleanup mess
try {
https.stop(0);
https.shutdownNow();
} catch (Exception e2) {
logger.log(Level.SEVERE, "HTTPS server cleanup failed after failed connector start", e2);
} finally {
Expand All @@ -418,9 +418,10 @@ public void start(Node node) throws ConnectorException {
}

private void startHttpServer(ResourceConfig config) throws Exception {
http = JdkHttpServerFactory.createHttpServer(new URI(getHttpEndpoint() + "/"), config, null, false);
httpPort = http.getAddress().getPort();
http.setExecutor(Executors.newFixedThreadPool(maxThreads));
http = GrizzlyHttpServerFactory.createHttpServer(new URI(getHttpEndpoint() + "/"), config, false);
final TCPNIOTransport transport = http.getListener("grizzly").getTransport();
transport.setWorkerThreadPoolConfig(ThreadPoolConfig.defaultConfig().setCorePoolSize(maxThreads).setMaxPoolSize(maxThreads));
httpPort = http.getListener("grizzly").getPort();
http.start();
logMessage("Web-Connector in HTTP mode running at " + getHttpEndpoint());
}
Expand Down Expand Up @@ -452,11 +453,14 @@ private void startHttpsServer(ResourceConfig config) throws Exception {
kmf.init(keystore, keystoreSecret);
SSLContext sslContext = SSLContext.getInstance(SSL_INSTANCE_NAME);
sslContext.init(kmf.getKeyManagers(), null, null);
https = (HttpsServer) JdkHttpServerFactory.createHttpServer(new URI(getHttpsEndpoint() + "/"), config,
sslContext, false);
httpsPort = https.getAddress().getPort();
https.setExecutor(Executors.newFixedThreadPool(maxThreads));

https = GrizzlyHttpServerFactory.createHttpServer(new URI(getHttpEndpoint() + "/"), config,
sslContext);
final TCPNIOTransport httpsTransport = https.getListener("grizzly").getTransport();
httpsPort = https.getListener("grizzly").getPort();
httpsTransport.setWorkerThreadPoolConfig(ThreadPoolConfig.defaultConfig().setCorePoolSize(maxThreads).setMaxPoolSize(maxThreads));
https.start();

logMessage("Web-Connector in HTTPS mode running at " + getHttpsEndpoint());
}

Expand Down Expand Up @@ -484,10 +488,10 @@ public String getHttpEndpoint() {
public synchronized void stop() throws ConnectorException {
// stop the HTTP server
if (https != null) {
https.stop(0);
https.shutdownNow();
}
if (http != null) {
http.stop(0);
http.shutdownNow();
}
caCert = null;
agentIdToSessionId.clear();
Expand Down

0 comments on commit f055807

Please sign in to comment.