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;
024import java.nio.ByteBuffer;
025
026import javax.net.ServerSocketFactory;
027import javax.net.SocketFactory;
028import javax.net.ssl.SSLContext;
029import javax.net.ssl.SSLEngine;
030
031import org.apache.activemq.broker.SslContext;
032import org.apache.activemq.transport.Transport;
033import org.apache.activemq.transport.TransportServer;
034import org.apache.activemq.transport.nio.NIOSSLTransportServer;
035import org.apache.activemq.transport.tcp.TcpTransport;
036import org.apache.activemq.transport.tcp.TcpTransport.InitBuffer;
037import org.apache.activemq.transport.tcp.TcpTransportServer;
038import org.apache.activemq.util.IntrospectionSupport;
039import org.apache.activemq.wireformat.WireFormat;
040
041public class MQTTNIOSSLTransportFactory extends MQTTNIOTransportFactory {
042
043    SSLContext context;
044
045    @Override
046    protected TcpTransportServer createTcpTransportServer(URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
047        NIOSSLTransportServer result = new NIOSSLTransportServer(context, this, location, serverSocketFactory) {
048            @Override
049            protected Transport createTransport(Socket socket, WireFormat format) throws IOException {
050                MQTTNIOSSLTransport transport = new MQTTNIOSSLTransport(format, socket);
051                if (context != null) {
052                    transport.setSslContext(context);
053                }
054
055                transport.setNeedClientAuth(isNeedClientAuth());
056                transport.setWantClientAuth(isWantClientAuth());
057
058                return transport;
059            }
060        };
061        result.setAllowLinkStealing(true);
062        return result;
063    }
064
065    @Override
066    protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException {
067        return new MQTTNIOSSLTransport(wf, socketFactory, location, localLocation);
068    }
069
070    @Override
071    public TcpTransport createTransport(WireFormat wireFormat, Socket socket,
072            SSLEngine engine, InitBuffer initBuffer, ByteBuffer inputBuffer)
073            throws IOException {
074        return new MQTTNIOSSLTransport(wireFormat, socket, engine, initBuffer, inputBuffer);
075    }
076
077    @Override
078    public TransportServer doBind(URI location) throws IOException {
079        if (SslContext.getCurrentSslContext() != null) {
080            try {
081                context = SslContext.getCurrentSslContext().getSSLContext();
082            } catch (Exception e) {
083                throw new IOException(e);
084            }
085        }
086        return super.doBind(location);
087    }
088
089}