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