Created
September 28, 2017 19:26
-
-
Save hasalex/50644abf741c35f8ee0eaebcd33a9366 to your computer and use it in GitHub Desktop.
HTTP, HTTPS, HTTP2 avec Undertow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.sewatech.demo.http2.undertow; | |
import io.undertow.Undertow; | |
import io.undertow.UndertowOptions; | |
import io.undertow.server.HttpServerExchange; | |
import io.undertow.util.Headers; | |
import javax.net.ssl.KeyManagerFactory; | |
import javax.net.ssl.SSLContext; | |
import java.security.KeyStore; | |
public class StartHttp2 { | |
private static final String KEYSTORE_TYPE = "PKCS12"; | |
private static final String KEYSTORE_FILE = "undertow.pfx"; | |
private static final String KEYSTORE_PASSWORD = "tomcat"; | |
private static final String TLS_VERSION = "TLSv1.2"; | |
public static void main(final String[] args) throws Exception { | |
Undertow simpleServer = Undertow.builder() | |
.addHttpListener(8000, "localhost") | |
.setHandler(StartHttp2::hello) | |
.build(); | |
simpleServer.start(); | |
Undertow sslServer = Undertow.builder() | |
.addHttpsListener(8001, "localhost", buildSslContext()) | |
.setHandler(StartHttp2::hello) | |
.build(); | |
sslServer.start(); | |
Undertow http2Server = Undertow.builder() | |
.addHttpsListener(8002, "localhost", buildSslContext()) | |
.setServerOption(UndertowOptions.ENABLE_HTTP2, true) | |
.setHandler(StartHttp2::hello) | |
.build(); | |
http2Server.start(); | |
} | |
private static SSLContext buildSslContext() throws Exception { | |
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | |
KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE); | |
keyStore.load( | |
classLoader.getResourceAsStream(KEYSTORE_FILE), | |
KEYSTORE_PASSWORD.toCharArray()); | |
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | |
keyManagerFactory.init(keyStore, KEYSTORE_PASSWORD.toCharArray()); | |
SSLContext sslContext = SSLContext.getInstance(TLS_VERSION); | |
sslContext.init(keyManagerFactory.getKeyManagers(), null, null); | |
return sslContext; | |
} | |
private static void hello(HttpServerExchange exchange) { | |
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); | |
exchange.getResponseSender().send("Hello World"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment