From b611f66803dee20970dc2f386783f507a411465a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 28 Mar 2017 11:16:16 +0200 Subject: java : vaadin : add https support to jetty --- java/vaadin/src/run/java/ch/asynk/Main.java | 88 ++++++++++++++++++++++++++-- java/vaadin/src/run/resources/keystore.jks | Bin 0 -> 2230 bytes 2 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 java/vaadin/src/run/resources/keystore.jks diff --git a/java/vaadin/src/run/java/ch/asynk/Main.java b/java/vaadin/src/run/java/ch/asynk/Main.java index a569c85..222fccb 100644 --- a/java/vaadin/src/run/java/ch/asynk/Main.java +++ b/java/vaadin/src/run/java/ch/asynk/Main.java @@ -4,8 +4,15 @@ import com.vaadin.server.DefaultUIProvider; import com.vaadin.server.UIProvider; import com.vaadin.server.VaadinServlet; import com.vaadin.ui.UI; +import org.eclipse.jetty.server.HttpConfiguration; +import org.eclipse.jetty.server.HttpConnectionFactory; +import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.SecureRequestCustomizer; +import org.eclipse.jetty.server.SslConnectionFactory; import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.webapp.WebAppContext; import java.io.IOException; @@ -13,11 +20,21 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -class MyVaadinJettyServer extends Server +// https://github.com/eclipse/jetty.project/blob/jetty-9.3.x/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java + +abstract class VaadinJettyServer extends Server { - public MyVaadinJettyServer(int port, String webappDirectory) throws IOException + public VaadinJettyServer(int port, String webappDirectory) + { + super(); + configure(port); + buildWebapp(webappDirectory); + } + + abstract protected void configure(int port); + + protected void buildWebapp(String webappDirectory) { - super(port); WebAppContext context = new WebAppContext(webappDirectory, "/"); @@ -34,15 +51,76 @@ class MyVaadinJettyServer extends Server if (uiClass != null) servletHolder.setInitParameter("UI", uiClass.getName()); return servletHolder; } + + protected Connector httpConnector(int port) + { + ServerConnector connector = new ServerConnector(this); + connector.setPort(port); + return connector; + } + + protected Connector httpsConnector(int port) + { + HttpConfiguration httpConf = new HttpConfiguration(); + httpConf.addCustomizer(new SecureRequestCustomizer()); + + SslContextFactory sslContextFactory = new SslContextFactory(); + // keytool -keystore src/run/resources/keystore.jks -genkey -alias asynk.ch -keyalg RSA -keysize 2048 + sslContextFactory.setKeyStorePath(VaadinJettyServer.class.getResource("/keystore.jks").toExternalForm()); + sslContextFactory.setKeyStorePassword("123456"); + sslContextFactory.setKeyManagerPassword("123456"); + ServerConnector connector = new ServerConnector(this, + new SslConnectionFactory(sslContextFactory, "http/1.1"), + new HttpConnectionFactory(httpConf)); + connector.setPort(port); + return connector; + } +} + +class HttpVaadinJettyServer extends VaadinJettyServer +{ + public HttpVaadinJettyServer(int port, String webappDirectory) { super(port, webappDirectory); } + + @Override + protected void configure(int port) + { + System.out.println("http://127.0.0.1:" + port + "/hello"); + addConnector(httpConnector(port)); + } +} + +class HttpsVaadinJettyServer extends VaadinJettyServer +{ + public HttpsVaadinJettyServer(int port, String webappDirectory) { super(port, webappDirectory); } + + @Override + protected void configure(int port) + { + System.out.println("https://127.0.0.1:" + port + "/hello"); + addConnector(httpsConnector(port)); + } +} +class HttpHttpsVaadinJettyServer extends HttpsVaadinJettyServer +{ + public HttpHttpsVaadinJettyServer(int port, String webappDirectory) { super(port, webappDirectory); } + + @Override + protected void configure(int port) + { + System.out.println("http://127.0.0.1:" + port + "/hello"); + System.out.println("https://127.0.0.1:" + (port + 1) + "/hello"); + this.setConnectors(new Connector[] { httpConnector(port), httpsConnector(port + 1) }); + } } public class Main { public static void main(String[] args) throws Exception { + int port = 8666; String webRoot = System.getProperty("WEBROOT"); if (webRoot == null) webRoot = "./src/main/WebContent"; - System.out.println("http://127.0.0.1:8666/hello"); - new MyVaadinJettyServer(8666, webRoot).start(); + + new HttpVaadinJettyServer(port, webRoot).start(); } } diff --git a/java/vaadin/src/run/resources/keystore.jks b/java/vaadin/src/run/resources/keystore.jks new file mode 100644 index 0000000..333fa20 Binary files /dev/null and b/java/vaadin/src/run/resources/keystore.jks differ -- cgit v1.1-2-g2b99