AMQ-4433: Validate socket.xxx transport connector options, to detect invalud/unused options and fail. Thanks to Christoffer Sawicki for the patch.

This commit is contained in:
Claus Ibsen 2013-10-31 12:13:32 +01:00
parent 3ed52ef8a2
commit 7dfb0a2a3c
2 changed files with 75 additions and 5 deletions

View File

@ -418,14 +418,19 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
/** /**
* Configures the socket for use * Configures the socket for use
* *
* @param sock * @param sock the socket
* @throws SocketException, IllegalArgumentException if setting the options * @throws SocketException, IllegalArgumentException if setting the options
* on the socket failed. * on the socket failed.
*/ */
protected void initialiseSocket(Socket sock) throws SocketException, protected void initialiseSocket(Socket sock) throws SocketException, IllegalArgumentException {
IllegalArgumentException {
if (socketOptions != null) { if (socketOptions != null) {
IntrospectionSupport.setProperties(socket, socketOptions); // copy the map as its used values is being removed when calling setProperties
// and we need to be able to set the options again in case socket is re-initailized
Map<String, Object> copy = new HashMap<String, Object>(socketOptions);
IntrospectionSupport.setProperties(socket, copy);
if (!copy.isEmpty()) {
throw new IllegalArgumentException("Invalid socket parameters: " + copy);
}
} }
try { try {
@ -433,7 +438,7 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
sock.setSendBufferSize(socketBufferSize); sock.setSendBufferSize(socketBufferSize);
} catch (SocketException se) { } catch (SocketException se) {
LOG.warn("Cannot set socket buffer size = " + socketBufferSize); LOG.warn("Cannot set socket buffer size = " + socketBufferSize);
LOG.debug("Cannot set socket buffer size. Reason: " + se, se); LOG.debug("Cannot set socket buffer size. Reason: " + se.getMessage() + ". This exception is ignored.", se);
} }
sock.setSoTimeout(soTimeout); sock.setSoTimeout(soTimeout);

View File

@ -0,0 +1,65 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.transport.tcp;
import javax.jms.JMSException;
import junit.framework.TestCase;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.junit.Test;
public class TransportConnectorInvalidSocketOptionsTest extends TestCase {
@Test
public void testClientParameters() throws Exception {
try {
new ActiveMQConnectionFactory("tcp://localhost:42?foo=bar").createConnection();
fail("Should have thrown an exception");
} catch (Exception e) {
assertEquals(JMSException.class, e.getClass());
assertEquals(IllegalArgumentException.class, e.getCause().getClass());
assertEquals("Invalid connect parameters: {foo=bar}", e.getCause().getMessage());
}
}
@Test
public void testClientSocketParameters() throws Exception {
BrokerService broker = null;
try {
broker = new BrokerService();
broker.setPersistent(false);
broker.addConnector("tcp://localhost:61616");
broker.start();
try {
new ActiveMQConnectionFactory("tcp://localhost:61616?socket.foo=bar").createConnection();
fail("Should have thrown an exception");
} catch (Exception e) {
assertEquals(JMSException.class, e.getClass());
assertEquals(IllegalArgumentException.class, e.getCause().getClass());
assertEquals("Invalid socket parameters: {foo=bar}", e.getCause().getMessage());
}
} finally {
if (broker != null) {
broker.stop();
}
}
}
}