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 return new SslTransportServer(uri);
038 }
039
040 protected KeyManager[] keyManagers;
041 private TrustManager[] trustManagers;
042 protected String protocol = "TLS";
043 protected SSLContext sslContext;
044 protected Executor blockingExecutor;
045 private String clientAuth = "want";
046
047 public SslTransportServer(URI location) throws Exception {
048 super(location);
049 setSSLContext(SSLContext.getInstance(SslTransport.protocol(location.getScheme())));
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 rc.setClientAuth(clientAuth);
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 Executor getBlockingExecutor() {
091 return blockingExecutor;
092 }
093
094 public void setBlockingExecutor(Executor blockingExecutor) {
095 this.blockingExecutor = blockingExecutor;
096 }
097
098 public String getClientAuth() {
099 return clientAuth;
100 }
101
102 public void setClientAuth(String clientAuth) {
103 this.clientAuth = clientAuth;
104 }
105
106 }