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.auto; 018 019import java.io.IOException; 020import java.net.Socket; 021import java.net.URI; 022import java.net.URISyntaxException; 023import java.util.HashMap; 024import java.util.Map; 025import java.util.Set; 026 027import javax.net.ServerSocketFactory; 028import javax.net.ssl.SSLServerSocketFactory; 029 030import org.apache.activemq.broker.BrokerService; 031import org.apache.activemq.broker.BrokerServiceAware; 032import org.apache.activemq.transport.TransportServer; 033import org.apache.activemq.transport.tcp.SslTransportFactory; 034import org.apache.activemq.transport.tcp.TcpTransport; 035import org.apache.activemq.transport.tcp.TcpTransport.InitBuffer; 036import org.apache.activemq.transport.tcp.TcpTransportFactory; 037import org.apache.activemq.transport.tcp.TcpTransportServer; 038import org.apache.activemq.util.IOExceptionSupport; 039import org.apache.activemq.util.IntrospectionSupport; 040import org.apache.activemq.util.URISupport; 041import org.apache.activemq.wireformat.WireFormat; 042 043 044public class AutoSslTransportFactory extends SslTransportFactory implements BrokerServiceAware { 045 046 protected BrokerService brokerService; 047 /* (non-Javadoc) 048 * @see org.apache.activemq.broker.BrokerServiceAware#setBrokerService(org.apache.activemq.broker.BrokerService) 049 */ 050 @Override 051 public void setBrokerService(BrokerService brokerService) { 052 this.brokerService = brokerService; 053 } 054 055 private Set<String> enabledProtocols; 056 057 /** 058 * Overriding to use SslTransportServer and allow for proper reflection. 059 */ 060 @Override 061 public TransportServer doBind(final URI location) throws IOException { 062 try { 063 Map<String, String> options = new HashMap<String, String>(URISupport.parseParameters(location)); 064 065 Map<String, Object> autoProperties = IntrospectionSupport.extractProperties(options, "auto."); 066 this.enabledProtocols = AutoTransportUtils.parseProtocols((String) autoProperties.get("protocols")); 067 068 ServerSocketFactory serverSocketFactory = createServerSocketFactory(); 069 AutoSslTransportServer server = createAutoSslTransportServer(location, (SSLServerSocketFactory)serverSocketFactory); 070 if (options.get("allowLinkStealing") != null){ 071 allowLinkStealingSet = true; 072 } 073 IntrospectionSupport.setProperties(server, options); 074 server.setAutoTransportOptions(IntrospectionSupport.extractProperties(options, "auto.")); 075 server.setTransportOption(IntrospectionSupport.extractProperties(options, "transport.")); 076 server.setWireFormatOptions(AutoTransportUtils.extractWireFormatOptions(options)); 077 server.bind(); 078 079 return server; 080 } catch (URISyntaxException e) { 081 throw IOExceptionSupport.create(e); 082 } 083 } 084 085 boolean allowLinkStealingSet = false; 086 087 /** 088 * Allows subclasses of SslTransportFactory to create custom instances of 089 * SslTransportServer. 090 * 091 * @param location 092 * @param serverSocketFactory 093 * @return 094 * @throws IOException 095 * @throws URISyntaxException 096 */ 097 protected AutoSslTransportServer createAutoSslTransportServer(final URI location, SSLServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException { 098 AutoSslTransportServer server = new AutoSslTransportServer(this, location, serverSocketFactory, 099 this.brokerService, enabledProtocols) { 100 101 @Override 102 protected TcpTransport createTransport(Socket socket, WireFormat format, 103 TcpTransportFactory detectedTransportFactory, InitBuffer initBuffer) throws IOException { 104 setDefaultLinkStealing(format, this); 105 return super.createTransport(socket, format, detectedTransportFactory, initBuffer); 106 } 107 }; 108 return server; 109 } 110 111 private void setDefaultLinkStealing(WireFormat format, TcpTransportServer server) { 112 if (format.getClass().toString().contains("MQTT") && !allowLinkStealingSet) { 113 server.setAllowLinkStealing(true); 114 } 115 } 116 117}