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.security.cert.X509Certificate;
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import javax.net.ssl.SSLServerSocketFactory;
028    import javax.net.ssl.SSLSocket;
029    
030    import org.apache.activemq.broker.BrokerContext;
031    import org.apache.activemq.broker.BrokerService;
032    import org.apache.activemq.broker.BrokerServiceAware;
033    import org.apache.activemq.transport.MutexTransport;
034    import org.apache.activemq.transport.Transport;
035    import org.apache.activemq.transport.tcp.SslTransport;
036    import org.apache.activemq.transport.tcp.SslTransportFactory;
037    import org.apache.activemq.transport.tcp.SslTransportServer;
038    import org.apache.activemq.util.IntrospectionSupport;
039    import org.apache.activemq.wireformat.WireFormat;
040    
041    /**
042     * A <a href="http://stomp.codehaus.org/">STOMP</a> over SSL transport factory
043     *
044     *
045     */
046    public class StompSslTransportFactory extends SslTransportFactory implements BrokerServiceAware {
047    
048        private BrokerContext brokerContext = null;
049    
050        protected String getDefaultWireFormatType() {
051            return "stomp";
052        }
053    
054        protected SslTransportServer createSslTransportServer(final URI location, SSLServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
055            return new SslTransportServer(this, location, serverSocketFactory) {
056    
057                @Override
058                protected Transport createTransport(Socket socket, WireFormat format) throws IOException {
059                    return new SslTransport(format, (SSLSocket)socket) {
060    
061                        private X509Certificate[] cachedPeerCerts;
062    
063                        @Override
064                        public void doConsume(Object command) {
065                            StompFrame frame = (StompFrame) command;
066                            if (cachedPeerCerts == null) {
067                                cachedPeerCerts = getPeerCertificates();
068                            }
069                            frame.setTransportContext(cachedPeerCerts);
070                            super.doConsume(command);
071                        }
072                    };
073                }
074            };
075        }
076    
077        @SuppressWarnings("rawtypes")
078        public Transport compositeConfigure(Transport transport, WireFormat format, Map options) {
079            transport = new StompTransportFilter(transport, format, brokerContext);
080            IntrospectionSupport.setProperties(transport, options);
081            return super.compositeConfigure(transport, format, options);
082        }
083    
084        @SuppressWarnings("rawtypes")
085        @Override
086        public Transport serverConfigure(Transport transport, WireFormat format, HashMap options) throws Exception {
087            transport = super.serverConfigure(transport, format, options);
088    
089            MutexTransport mutex = transport.narrow(MutexTransport.class);
090            if (mutex != null) {
091                mutex.setSyncOnCommand(true);
092            }
093    
094            return transport;
095        }
096    
097        public void setBrokerService(BrokerService brokerService) {
098            this.brokerContext = brokerService.getBrokerContext();
099        }
100    
101        @Override
102        protected Transport createInactivityMonitor(Transport transport, WireFormat format) {
103            StompInactivityMonitor monitor = new StompInactivityMonitor(transport, format);
104    
105            StompTransportFilter filter = (StompTransportFilter) transport.narrow(StompTransportFilter.class);
106            filter.setInactivityMonitor(monitor);
107    
108            return monitor;
109        }
110    }