001    /**
002     * Copyright (C) 2012 FuseSource, Inc.
003     * http://fusesource.com
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *    http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.fusesource.hawtdispatch.transport;
019    
020    import org.fusesource.hawtdispatch.Task;
021    
022    import javax.net.ssl.KeyManager;
023    import javax.net.ssl.SSLContext;
024    import javax.net.ssl.TrustManager;
025    import java.net.URI;
026    import java.net.UnknownHostException;
027    import java.util.concurrent.Executor;
028    import java.security.NoSuchAlgorithmException;
029    
030    /**
031     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
032     */
033    
034    public class SslTransportServer extends TcpTransportServer {
035    
036        public static SslTransportServer createTransportServer(URI uri) throws Exception {
037            SslTransportServer rc = new SslTransportServer(uri);
038            rc.setSSLContext(SSLContext.getInstance(SslTransport.protocol(uri.getScheme())));
039            return rc;
040        }
041    
042        protected KeyManager[] keyManagers;
043        private TrustManager[] trustManagers;
044        protected String protocol = "TLS";
045        protected SSLContext sslContext;
046        protected Executor blockingExecutor;
047    
048        public SslTransportServer(URI location) throws UnknownHostException {
049            super(location);
050        }
051    
052        public void setKeyManagers(KeyManager[] keyManagers) {
053            this.keyManagers = keyManagers;
054        }
055        public void setTrustManagers(TrustManager[] trustManagers) {
056            this.trustManagers = trustManagers;
057        }
058    
059        public void start(Task onCompleted) throws Exception {
060            if( keyManagers!=null ) {
061                sslContext.init(keyManagers, trustManagers, null);
062            } else {
063                sslContext = SSLContext.getDefault();
064            }
065            super.start(onCompleted);
066        }
067    
068        protected TcpTransport createTransport() {
069            SslTransport rc = new SslTransport();
070            rc.setSSLContext(sslContext);
071            rc.setBlockingExecutor(blockingExecutor);
072            return rc;
073        }
074    
075        public SslTransportServer protocol(String value) throws NoSuchAlgorithmException {
076            this.protocol = value;
077            sslContext = SSLContext.getInstance(protocol);
078            return this;
079        }
080    
081        public SSLContext getSSLContext() {
082            return sslContext;
083        }
084    
085        public void setSSLContext(SSLContext sslContext) {
086            this.sslContext = sslContext;
087        }
088    
089        public Executor getBlockingExecutor() {
090            return blockingExecutor;
091        }
092    
093        public void setBlockingExecutor(Executor blockingExecutor) {
094            this.blockingExecutor = blockingExecutor;
095        }
096    
097    }