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     */
017    package org.apache.activemq.transport.stomp;
018    
019    import java.io.IOException;
020    import java.net.Socket;
021    import java.net.URI;
022    import java.net.URISyntaxException;
023    import java.net.UnknownHostException;
024    
025    import javax.net.ServerSocketFactory;
026    import javax.net.SocketFactory;
027    import javax.net.ssl.SSLContext;
028    
029    import org.apache.activemq.broker.SslContext;
030    import org.apache.activemq.transport.Transport;
031    import org.apache.activemq.transport.TransportServer;
032    import org.apache.activemq.transport.nio.NIOSSLTransportServer;
033    import org.apache.activemq.transport.tcp.TcpTransport;
034    import org.apache.activemq.transport.tcp.TcpTransportServer;
035    import org.apache.activemq.wireformat.WireFormat;
036    
037    public class StompNIOSSLTransportFactory extends StompNIOTransportFactory {
038    
039        protected SSLContext context;
040    
041        @Override
042        protected TcpTransportServer createTcpTransportServer(URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
043            return new NIOSSLTransportServer(context, this, location, serverSocketFactory) {
044    
045                @Override
046                protected Transport createTransport(Socket socket, WireFormat format) throws IOException {
047                    StompNIOSSLTransport transport = new StompNIOSSLTransport(format, socket);
048                    if (context != null) {
049                        transport.setSslContext(context);
050                    }
051    
052                    transport.setNeedClientAuth(isNeedClientAuth());
053                    transport.setWantClientAuth(isWantClientAuth());
054    
055                    return transport;
056                }
057            };
058        }
059    
060        @Override
061        protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException {
062            return new StompNIOSSLTransport(wf, socketFactory, location, localLocation);
063        }
064    
065        @Override
066        public TransportServer doBind(URI location) throws IOException {
067            if (SslContext.getCurrentSslContext() != null) {
068                try {
069                    context = SslContext.getCurrentSslContext().getSSLContext();
070                } catch (Exception e) {
071                    throw new IOException(e);
072                }
073            }
074            return super.doBind(location);
075        }
076    }