Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 package io.undertow;
 
 import org.xnio.Pool;
 import org.xnio.Xnio;
 
 import java.util.List;

Convenience class used to build an Undertow server.

TODO: This API is still a work in progress

Author(s):
Stuart Douglas
 
 public class Undertow {
 
     private final int bufferSize;
     private final int buffersPerRegion;
     private final int ioThreads;
     private final int workerThreads;
     private final boolean directBuffers;
     private final List<ListenerConfiglisteners = new ArrayList<>();
     private final HttpHandler rootHandler;
     private final OptionMap workerOptions;
     private final OptionMap socketOptions;
     private final OptionMap serverOptions;
 
     private XnioWorker worker;
     private List<AcceptingChannel<? extends StreamConnection>> channels;
     private Xnio xnio;
 
     private Undertow(Builder builder) {
         this. = builder.bufferSize;
         this. = builder.buffersPerRegion;
         this. = builder.ioThreads;
         this. = builder.workerThreads;
         this. = builder.directBuffers;
         this..addAll(builder.listeners);
         this. = builder.handler;
         this. = builder.workerOptions.getMap();
         this. = builder.socketOptions.getMap();
         this. = builder.serverOptions.getMap();
     }

    

Returns:
A builder that can be used to create an Undertow server instance
 
     public static Builder builder() {
         return new Builder();
     }
 
     public synchronized void start() {
         = Xnio.getInstance(Undertow.class.getClassLoader());
         = new ArrayList<>();
        try {
             = .createWorker(OptionMap.builder()
                    .set(.)
                    .set(., 1000000)
                    .set(., 1000000)
                    .set(.)
                    .set(.)
                    .set(.true)
                    .set(.true)
                    .addAll()
                    .getMap());
            OptionMap socketOptions = OptionMap.builder()
                    .set(.)
                    .set(.true)
                    .set(.true)
                    .set(., 1)
                    .set(., 2)
                    .addAll(this.)
                    .getMap();
            for (ListenerConfig listener : ) {
                if (listener.type == .) {
                    AjpOpenListener openListener = new AjpOpenListener(buffers);
                    openListener.setRootHandler();
                    ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                    AcceptingChannel<? extends StreamConnectionserver = .createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), acceptListenersocketOptions);
                    server.resumeAccepts();
                    .add(server);
                } else {
                    OptionMap undertowOptions = OptionMap.builder().set(.true).addAll().getMap();
                    if (listener.type == .) {
                        HttpOpenListener openListener = new HttpOpenListener(buffersundertowOptions);
                        openListener.setRootHandler();
                        ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                        AcceptingChannel<? extends StreamConnectionserver = .createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), acceptListenersocketOptions);
                        server.resumeAccepts();
                        .add(server);
                    } else if (listener.type == .) {
                        ChannelListener<StreamConnectionopenListener;
                        HttpOpenListener httpOpenListener = new HttpOpenListener(buffersundertowOptions);
                        httpOpenListener.setRootHandler();
                        boolean spdy = .get(.false);
                        if(spdy) {
                            AlpnOpenListener alpn = new AlpnOpenListener(buffershttpOpenListener);
                            if(spdy) {
                                SpdyOpenListener spdyListener = new SpdyOpenListener(buffersnew ByteBufferSlicePool(., 1024, 1024), undertowOptions);
                                spdyListener.setRootHandler();
                                alpn.addProtocol(.spdyListener, 5);
                            }
                            openListener = alpn;
                        } else {
                            openListener = httpOpenListener;
                        }
                        ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                        XnioSsl xnioSsl;
                        if (listener.sslContext != null) {
                            xnioSsl = new JsseXnioSsl(, OptionMap.create(.true), listener.sslContext);
                        } else {
                            xnioSsl = .getSslProvider(listener.keyManagerslistener.trustManagers, OptionMap.create(.true));
                        }
                        AcceptingChannel<SslConnectionsslServer = xnioSsl.createSslConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), (ChannelListeneracceptListenersocketOptions);
                        sslServer.resumeAccepts();
                        .add(sslServer);
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public synchronized void stop() {
        for (AcceptingChannel<? extends StreamConnectionchannel : ) {
            IoUtils.safeClose(channel);
        }
         = null;
        .shutdownNow();
         = null;
         = null;
    }
    public static enum ListenerType {
        HTTP,
        HTTPS,
        AJP
    }
    private static class ListenerConfig {
        final ListenerType type;
        final int port;
        final String host;
        final KeyManager[] keyManagers;
        final TrustManager[] trustManagers;
        final SSLContext sslContext;
        private ListenerConfig(final ListenerType typefinal int portfinal String hostKeyManager[] keyManagersTrustManager[] trustManagers) {
            this. = type;
            this. = port;
            this. = host;
            this. = keyManagers;
            this. = trustManagers;
            this. = null;
        }
        private ListenerConfig(final ListenerType typefinal int portfinal String hostSSLContext sslContext) {
            this. = type;
            this. = port;
            this. = host;
            this. = null;
            this. = null;
            this. = sslContext;
        }
    }
    public static class LoginConfig {
        private final IdentityManager identityManager;
        private boolean basic;
        private boolean digest;
        private boolean kerberos;
        private boolean form;
        private String realmName;
        private String errorPageloginPage;
        public LoginConfig(final IdentityManager identityManager) {
            this. = identityManager;
        }
        public LoginConfig basicAuth(final String realmName) {
            if () {
                throw ..authTypeCannotBeCombined("basic""digest");
            } else if () {
                throw ..authTypeCannotBeCombined("basic""form");
            }
             = true;
            this. = realmName;
            return this;
        }
        public LoginConfig digestAuth(final String realmName) {
            if () {
                throw ..authTypeCannotBeCombined("digest""basic");
            } else if () {
                throw ..authTypeCannotBeCombined("digest""form");
            }
             = true;
            this. = realmName;
            return this;
        }
        public LoginConfig kerberosAuth(GSSAPIServerSubjectFactory subjectFactory) {
             = true;
            this. = subjectFactory;
            return this;
        }
        public LoginConfig formAuth(final String loginPagefinal String errorPage) {
            if () {
                throw ..authTypeCannotBeCombined("form""digest");
            } else if () {
                throw ..authTypeCannotBeCombined("form""basic");
            }
            this. = loginPage;
            this. = errorPage;
             = true;
            return this;
        }
        public LoginConfig setAuthenticationMode(final AuthenticationMode authenticationMode) {
            this. = authenticationMode;
            return this;
        }
    }
    public static final class Builder {
        private int bufferSize;
        private int buffersPerRegion;
        private int ioThreads;
        private int workerThreads;
        private boolean directBuffers;
        private final List<ListenerConfiglisteners = new ArrayList<>();
        private HttpHandler handler;
        private final OptionMap.Builder workerOptions = OptionMap.builder();
        private final OptionMap.Builder socketOptions = OptionMap.builder();
        private final OptionMap.Builder serverOptions = OptionMap.builder();
        private Builder() {
             = Math.max(Runtime.getRuntime().availableProcessors(), 2);
             =  * 8;
            long maxMemory = Runtime.getRuntime().maxMemory();
            //smaller than 64mb of ram we use 512b buffers
            if (maxMemory < 64 * 1024 * 1024) {
                //use 512b buffers
                 = false;
                 = 512;
                 = 10;
            } else if (maxMemory < 128 * 1024 * 1024) {
                //use 1k buffers
                 = true;
                 = 1024;
                 = 10;
            } else {
                //use 16k buffers for best performance
                //as 16k is generally the max amount of data that can be sent in a single write() call
                 = true;
                 = 1024 * 16;
                 = 20;
            }
        }
        public Undertow build() {
            return new Undertow(this);
        }
        @Deprecated
        public Builder addListener(int portString host) {
            .add(new ListenerConfig(.porthostnullnull));
            return this;
        }
        public Builder addHttpListener(int portString host) {
            .add(new ListenerConfig(.porthostnullnull));
            return this;
        }
        public Builder addHttpsListener(int portString hostKeyManager[] keyManagersTrustManager[] trustManagers) {
            .add(new ListenerConfig(.porthostkeyManagerstrustManagers));
            return this;
        }
        public Builder addHttpsListener(int portString hostSSLContext sslContext) {
            .add(new ListenerConfig(.porthostsslContext));
            return this;
        }
        public Builder addAjpListener(int portString host) {
            .add(new ListenerConfig(.porthostnullnull));
            return this;
        }
        @Deprecated
        public Builder addListener(int portString hostListenerType listenerType) {
            .add(new ListenerConfig(listenerTypeporthostnullnull));
            return this;
        }
        public Builder setBufferSize(final int bufferSize) {
            this. = bufferSize;
            return this;
        }
        public Builder setBuffersPerRegion(final int buffersPerRegion) {
            this. = buffersPerRegion;
            return this;
        }
        public Builder setIoThreads(final int ioThreads) {
            this. = ioThreads;
            return this;
        }
        public Builder setWorkerThreads(final int workerThreads) {
            this. = workerThreads;
            return this;
        }
        public Builder setDirectBuffers(final boolean directBuffers) {
            this. = directBuffers;
            return this;
        }
        public Builder setHandler(final HttpHandler handler) {
            this. = handler;
            return this;
        }
        public <T> Builder setServerOption(final Option<T> optionfinal T value) {
            .set(optionvalue);
            return this;
        }
        public <T> Builder setSocketOption(final Option<T> optionfinal T value) {
            .set(optionvalue);
            return this;
        }
        public <T> Builder setWorkerOption(final Option<T> optionfinal T value) {
            .set(optionvalue);
            return this;
        }
    }
New to GrepCode? Check out our FAQ X