package com.perforce.workspace.tjuricek.p4oauth.jetty; import com.perforce.workspace.tjuricek.p4oauth.config.P4OAuthConfig; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.eclipse.jetty.server.*; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.ResourceHandler; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.ssl.SslContextFactory; import java.net.MalformedURLException; import java.util.function.Supplier; /** * Manages the main embedded Jetty server instance */ public class ServerRunnable implements Runnable { private static final Logger logger = LogManager.getFormatterLogger(ServerRunnable.class); private P4OAuthConfig config; private Server server; private HttpConfiguration httpConfiguration; private ServerConnector http; private SslContextFactory sslContextFactory; private HttpConfiguration httpsConfiguration; private ServerConnector https; private HandlerCollection handlerCollection; private HandlerList mainHandlerList; private OAuthHandler oAuthHandler; private boolean failed = false; private Exception exception; private Supplier<OAuthHandler> oAuthHandlerSupplier; private Supplier<OptionsServerReleaseHandler> releaseHandlerSupplier; public ServerRunnable(P4OAuthConfig config, Supplier<OAuthHandler> oAuthHandlerSupplier, Supplier<OptionsServerReleaseHandler> releaseHandlerSupplier) { this.config = config; this.oAuthHandlerSupplier = oAuthHandlerSupplier; this.releaseHandlerSupplier = releaseHandlerSupplier; } public void run() { try { server = new Server(); httpConfiguration = new HttpConfiguration(); httpConfiguration.setSecurePort(config.getHttpsPort()); http = new ServerConnector(server, new HttpConnectionFactory(httpConfiguration)); http.setPort(config.getHttpPort()); http.setIdleTimeout(config.getHttpIdleTimeout()); server.addConnector(http); sslContextFactory = new SslContextFactory(); sslContextFactory.setKeyStorePath(config.getKeyStorePath()); sslContextFactory.setKeyStorePassword(config.getKeyStorePassword()); sslContextFactory.setKeyManagerPassword(config.getKeyManagerPassword()); httpsConfiguration = new HttpConfiguration(httpConfiguration); httpsConfiguration.addCustomizer(new SecureRequestCustomizer()); https = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, "http/1.1"), new HttpConnectionFactory(httpsConfiguration)); https.setPort(config.getHttpsPort()); https.setIdleTimeout(config.getHttpsIdleTimeout()); server.addConnector(https); // We use a handler collection as the main handler, which should // allow for things like logging and timing handlers to be added // that to not inferfere with the main handler. handlerCollection = new HandlerCollection(); handlerCollection.addHandler(new LoggingHandlers.LogStartHandler()); // The main handler list will actually "fall back" until the response // is handled mainHandlerList = new HandlerList(); oAuthHandler = oAuthHandlerSupplier.get(); mainHandlerList.addHandler(oAuthHandler); // This should only apply to webjars third party dependencies. ResourceHandler metaInfResources = new ResourceHandler() { @Override public Resource getResource(String path) throws MalformedURLException { return Resource.newClassPathResource("META-INF/resources" + path); } }; metaInfResources.setDirectoriesListed(false); metaInfResources.setResourceBase("/"); mainHandlerList.addHandler(metaInfResources); mainHandlerList.addHandler(new DefaultHandler()); handlerCollection.addHandler(mainHandlerList); handlerCollection.addHandler(releaseHandlerSupplier.get()); handlerCollection.addHandler(new LoggingHandlers.LogEndHandler()); server.setHandler(handlerCollection); server.start(); logger.info("Started p4oauth, http on port %d, https on port %d", config.getHttpPort(), config.getHttpsPort()); server.join(); } catch (Exception e) { logger.error("The Jetty instance has halted with an error", e); failed = true; exception = e; } } /** * Set to true if run() finishes with an exception. * * Use getException() to retrieve the exception. It's already logged. */ public boolean isFailed() { return failed; } public Exception getException() { return exception; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 9095 | tjuricek | Added some basic test data and renamed "workspace" to "workshop" in package name | ||
#1 | 9089 | tjuricek |
Moving some code that worked via some manual validation to the workshop. This just implements a basic code authorization grant scheme. Automated tests are forthcoming, awating some gradle plugin work that should sit outside of this project. |