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
018package org.fusesource.hawtdispatch.transport;
019
020import org.fusesource.hawtdispatch.Task;
021
022import javax.net.ssl.KeyManager;
023import javax.net.ssl.SSLContext;
024import javax.net.ssl.TrustManager;
025import java.net.URI;
026import java.security.NoSuchAlgorithmException;
027
028/**
029 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
030 */
031
032public class SslTransportServer extends TcpTransportServer {
033
034    public static SslTransportServer createTransportServer(URI uri) throws Exception {
035        return new SslTransportServer(uri);
036    }
037
038    protected KeyManager[] keyManagers;
039    private TrustManager[] trustManagers;
040    protected String protocol = "TLS";
041    protected SSLContext sslContext;
042    private String clientAuth = "want";
043    private String disabledCypherSuites = null;
044
045    public SslTransportServer(URI location) throws Exception {
046        super(location);
047        setSSLContext(SSLContext.getInstance(SslTransport.protocol(location.getScheme())));
048    }
049
050    public void setKeyManagers(KeyManager[] keyManagers) {
051        this.keyManagers = keyManagers;
052    }
053    public void setTrustManagers(TrustManager[] trustManagers) {
054        this.trustManagers = trustManagers;
055    }
056
057    public void start(Task onCompleted) throws Exception {
058        if( keyManagers!=null ) {
059            sslContext.init(keyManagers, trustManagers, null);
060        } else {
061            sslContext = SSLContext.getDefault();
062        }
063        super.start(onCompleted);
064    }
065
066    protected TcpTransport createTransport() {
067        SslTransport rc = new SslTransport();
068        rc.setDispatchQueue(dispatchQueue);
069        rc.setBlockingExecutor(blockingExecutor);
070        rc.setSSLContext(sslContext);
071        rc.setClientAuth(clientAuth);
072        rc.setDisabledCypherSuites(disabledCypherSuites);
073        return rc;
074    }
075
076    public SslTransportServer protocol(String value) throws NoSuchAlgorithmException {
077        this.protocol = value;
078        sslContext = SSLContext.getInstance(protocol);
079        return this;
080    }
081
082    public SSLContext getSSLContext() {
083        return sslContext;
084    }
085
086    public void setSSLContext(SSLContext sslContext) {
087        this.sslContext = sslContext;
088    }
089
090    public String getClientAuth() {
091        return clientAuth;
092    }
093
094    public void setClientAuth(String clientAuth) {
095        this.clientAuth = clientAuth;
096    }
097
098    public String getDisabledCypherSuites() {
099        return disabledCypherSuites;
100    }
101
102    public void setDisabledCypherSuites(String disabledCypherSuites) {
103        this.disabledCypherSuites = disabledCypherSuites;
104    }
105}