Java Code Examples for io.undertow.Undertow.Builder#addHttpsListener()

The following examples show how to use io.undertow.Undertow.Builder#addHttpsListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UndertowServer.java    From pippo with Apache License 2.0 4 votes vote down vote up
protected Undertow createServer(HttpHandler contextHandler) {
    Builder builder = Undertow.builder();
    // TODO is it a better option?
    if (getSettings().getBufferSize() > 0) {
        builder.setBufferSize(getSettings().getBufferSize());
    }
    // method builder.setBuffersPerRegion is deprecated
    /*
    if (getSettings().getBuffersPerRegion() > 0) {
        builder.setBuffersPerRegion(getSettings().getBuffersPerRegion());
    }
    */
    if (getSettings().getDirectBuffers()) {
        builder.setDirectBuffers(getSettings().getDirectBuffers());
    }
    if (getSettings().getIoThreads() > 0) {
        builder.setIoThreads(getSettings().getIoThreads());
    }
    if (getSettings().getWorkerThreads() > 0) {
        builder.setWorkerThreads(getSettings().getWorkerThreads());
    }

    if (getSettings().getKeystoreFile() == null) {
        // HTTP
        builder.addHttpListener(getSettings().getPort(), getSettings().getHost());
    } else {
        // HTTPS
        builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true);
        try {
            KeyStore keyStore = loadKeyStore(getSettings().getKeystoreFile(), getSettings().getKeystorePassword());
            KeyStore trustStore = loadKeyStore(getSettings().getTruststoreFile(), getSettings().getTruststorePassword());
            SSLContext sslContext = createSSLContext(keyStore, trustStore);
            builder.addHttpsListener(getSettings().getPort(), getSettings().getHost(), sslContext);
        } catch (Exception e) {
            throw new PippoRuntimeException(e, "Failed to setup an Undertow SSL listener!");
        }
    }

    // add undertow options
    getSettings().addUndertowOptions(builder);
    builder.setHandler(contextHandler);

    return builder.build();
}
 
Example 2
Source File: UndertowWebServer.java    From hammock with Apache License 2.0 4 votes vote down vote up
@Override
public void start() {
    DeploymentInfo di = new DeploymentInfo()
            .setContextPath("/")
            .setDeploymentName("Undertow")
            .setResourceManager(new ClassPathResourceManager(getClass().getClassLoader()))
            .setClassLoader(ClassLoader.getSystemClassLoader());

    super.getListeners().forEach(c ->di.addListener(listener(c)));

    Collection<Class<?>> endpoints = extension.getEndpointClasses();
    if(!endpoints.isEmpty()) {
        WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
        endpoints.forEach(webSocketDeploymentInfo::addEndpoint);
        di.addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, webSocketDeploymentInfo);
    }

    getServletContextAttributes().forEach(di::addServletContextAttribute);
    servlets.forEach(di::addServlet);
    getFilterDescriptors().forEach(filterDescriptor -> {
        FilterInfo filterInfo = filter(filterDescriptor.displayName(), filterDescriptor.getClazz()).setAsyncSupported(filterDescriptor.asyncSupported());
        if(filterDescriptor.initParams() != null) {
            for (WebInitParam param : filterDescriptor.initParams()) {
                filterInfo.addInitParam(param.name(), param.value());
            }
        }
        di.addFilter(filterInfo);
        for(String url : filterDescriptor.urlPatterns()) {
            for(DispatcherType dispatcherType : filterDescriptor.dispatcherTypes()) {
                di.addFilterUrlMapping(filterDescriptor.displayName(), url, dispatcherType);
            }
        }
    });

    getInitParams().forEach(di::addInitParameter);

    DeploymentManager deploymentManager = Servlets.defaultContainer().addDeployment(di);
    deploymentManager.deploy();
    try {
        HttpHandler servletHandler = deploymentManager.start();
        PathHandler path = path(Handlers.redirect("/"))
                .addPrefixPath("/", servletHandler);
        Builder undertowBuilder = Undertow.builder()
                .addHttpListener(webServerConfiguration.getPort(), webServerConfiguration.getAddress())
                .setHandler(path);
        if (hammockRuntime.isSecuredConfigured()){
        	KeyManager[] keyManagers = loadKeyManager();
        	TrustManager[] trustManagers = loadTrustManager();
        	SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(keyManagers, trustManagers, null);
        	undertowBuilder = undertowBuilder.addHttpsListener(webServerConfiguration.getSecuredPort(), webServerConfiguration.getAddress(), sslContext);
        }
        this.undertow = undertowBuilder.build();
        this.undertow.start();
    } catch (ServletException | GeneralSecurityException | IOException e) {
        throw new RuntimeException("Unable to start server", e);
    }
}