Skip to content

Commit

Permalink
removed blocking join
Browse files Browse the repository at this point in the history
  • Loading branch information
ai-republic committed Aug 24, 2024
1 parent 08e0593 commit 5a710cf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
10 changes: 10 additions & 0 deletions webserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
<artifactId>jetty-server</artifactId>
<version>12.0.12</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<version>12.0.12</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-session</artifactId>
<version>12.0.12</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import java.util.ResourceBundle;

import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.security.Constraint;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.security.SecurityHandler;
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
Expand All @@ -17,7 +21,9 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.SecuredRedirectHandler;
import org.eclipse.jetty.session.SessionHandler;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.resource.Resource;
Expand Down Expand Up @@ -51,7 +57,7 @@ public WebServer() {


@Override
public void start(final int httpPort, final int httpsPort, final EnergyStorage energyStorage) throws Exception {
public void start(final int httpPort, final int httpsPort, final EnergyStorage energyStorage) {
server = new Server();

// Setup HTTP Connector
Expand Down Expand Up @@ -94,10 +100,10 @@ public void start(final int httpPort, final int httpsPort, final EnergyStorage e
// Add a Handlers for requests
final Handler.Sequence handlers = new Handler.Sequence();
handlers.addHandler(new SecuredRedirectHandler());
// handlers.addHandler(configureSecurity(server));
handlers.addHandler(new Handler.Abstract() {
@Override
public boolean handle(final Request request, final Response response, final Callback callback) throws Exception {
LOG.info("Hello SSL: " + request.getHeaders());
final String path = request.getHttpURI().getPath();

if (path.isBlank() || path.equals("/") || path.equals("/index.html")) {
Expand Down Expand Up @@ -134,8 +140,13 @@ public boolean handle(final Request request, final Response response, final Call
});
server.setHandler(handlers);

server.start();
server.join();
LOG.info("Starting webserver on ports " + httpPort + ":" + httpsPort);
try {
server.start();
LOG.info("Started webserver on ports " + httpPort + ":" + httpsPort + " successfully!");
} catch (final Exception e) {
LOG.error("FAILED to start webserver on ports " + httpPort + ":" + httpsPort + "!", e);
}
}


Expand All @@ -161,6 +172,29 @@ public void stop() {
}


private SecurityHandler configureSecurity(final Server server) {
final ContextHandler contextHandler = new ContextHandler();
final SessionHandler sessionHandler = new SessionHandler();

contextHandler.setContextPath("/");
server.setHandler(contextHandler);
contextHandler.setHandler(sessionHandler);

final SecurityHandler.PathMapped securityHandler = new SecurityHandler.PathMapped();
sessionHandler.setHandler(securityHandler);

securityHandler.put("/admin/*", Constraint.from("admin"));
securityHandler.put("/any/*", Constraint.ANY_USER);
securityHandler.put("/known/*", Constraint.KNOWN_ROLE);
securityHandler.setAuthenticator(new BasicAuthenticator());

securityHandler.setLoginService(new HashLoginService());
// securityHandler.setHandler(new AuthenticationTestHandler());

return securityHandler;
}


public static void main(final String[] args) throws Exception {
final EnergyStorage energyStorage = new EnergyStorage();
energyStorage.getBatteryPacks().add(new BatteryPack());
Expand Down

0 comments on commit 5a710cf

Please sign in to comment.