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