001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. 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 */ 017package org.apache.activemq.transport.mqtt; 018 019import java.io.IOException; 020import java.net.Socket; 021import java.net.URI; 022import java.net.URISyntaxException; 023import java.net.UnknownHostException; 024 025import javax.net.ServerSocketFactory; 026import javax.net.SocketFactory; 027import javax.net.ssl.SSLContext; 028import org.apache.activemq.broker.SslContext; 029import org.apache.activemq.transport.Transport; 030import org.apache.activemq.transport.TransportServer; 031import org.apache.activemq.transport.nio.NIOSSLTransportServer; 032import org.apache.activemq.transport.tcp.TcpTransport; 033import org.apache.activemq.transport.tcp.TcpTransportServer; 034import org.apache.activemq.util.IntrospectionSupport; 035import org.apache.activemq.wireformat.WireFormat; 036 037public class MQTTNIOSSLTransportFactory extends MQTTNIOTransportFactory { 038 039 SSLContext context; 040 041 @Override 042 protected TcpTransportServer createTcpTransportServer(URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException { 043 NIOSSLTransportServer result = new NIOSSLTransportServer(context, this, location, serverSocketFactory) { 044 @Override 045 protected Transport createTransport(Socket socket, WireFormat format) throws IOException { 046 MQTTNIOSSLTransport transport = new MQTTNIOSSLTransport(format, socket); 047 if (context != null) { 048 transport.setSslContext(context); 049 } 050 051 transport.setNeedClientAuth(isNeedClientAuth()); 052 transport.setWantClientAuth(isWantClientAuth()); 053 054 return transport; 055 } 056 }; 057 result.setAllowLinkStealing(true); 058 return result; 059 } 060 061 @Override 062 protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException { 063 return new MQTTNIOSSLTransport(wf, socketFactory, location, localLocation); 064 } 065 066 @Override 067 public TransportServer doBind(URI location) throws IOException { 068 if (SslContext.getCurrentSslContext() != null) { 069 try { 070 context = SslContext.getCurrentSslContext().getSSLContext(); 071 } catch (Exception e) { 072 throw new IOException(e); 073 } 074 } 075 return super.doBind(location); 076 } 077 078}