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 javax.net.ssl.KeyManager;
021 import javax.net.ssl.SSLContext;
022 import javax.net.ssl.TrustManager;
023 import java.net.URI;
024 import java.net.UnknownHostException;
025 import java.util.concurrent.Executor;
026 import java.security.NoSuchAlgorithmException;
027
028 /**
029 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
030 */
031
032 public class SslTransportServer extends TcpTransportServer {
033
034 public static SslTransportServer createTransportServer(URI uri) throws Exception {
035 SslTransportServer rc = new SslTransportServer(uri);
036 rc.setSSLContext(SSLContext.getInstance(SslTransport.protocol(uri.getScheme())));
037 return rc;
038 }
039
040 protected KeyManager[] keyManagers;
041 private TrustManager[] trustManagers;
042 protected String protocol = "TLS";
043 protected SSLContext sslContext;
044 protected Executor blockingExecutor;
045
046 public SslTransportServer(URI location) throws UnknownHostException {
047 super(location);
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(Runnable 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.setSSLContext(sslContext);
069 rc.setBlockingExecutor(blockingExecutor);
070 return rc;
071 }
072
073 public SslTransportServer protocol(String value) throws NoSuchAlgorithmException {
074 this.protocol = value;
075 sslContext = SSLContext.getInstance(protocol);
076 return this;
077 }
078
079 public SSLContext getSSLContext() {
080 return sslContext;
081 }
082
083 public void setSSLContext(SSLContext sslContext) {
084 this.sslContext = sslContext;
085 }
086
087 public Executor getBlockingExecutor() {
088 return blockingExecutor;
089 }
090
091 public void setBlockingExecutor(Executor blockingExecutor) {
092 this.blockingExecutor = blockingExecutor;
093 }
094
095 }