SWS-798 - Ensure that only valid Java Identifiers are added to JMS headers

This commit is contained in:
Arjen Poutsma
2012-09-25 11:20:18 +00:00
parent de42ef21e2
commit d5107c5b00
4 changed files with 87 additions and 33 deletions

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2005-2012 the original author or authors.
*
* Licensed 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
* 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,
@@ -27,6 +27,9 @@ public interface TransportConstants {
/** The "Accept" header. */
String HEADER_ACCEPT = "Accept";
/** The "Accept-Encoding" header. */
String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
/** The "Content-Id" header. */
String HEADER_CONTENT_ID = "Content-Id";

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2007 the original author or authors.
* Copyright 2005-2012 the original author or authors.
*
* Licensed 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
* 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,
@@ -41,6 +41,9 @@ public interface JmsTransportConstants extends TransportConstants {
/** Prefix for JMS properties that map to transport headers. */
String PROPERTY_PREFIX = "SOAPJMS_";
/** JMS property used for storing {@link #HEADER_ACCEPT_ENCODING}. */
String PROPERTY_ACCEPT_ENCODING = PROPERTY_PREFIX + "acceptEncoding";
/** JMS property used for storing {@link #HEADER_SOAP_ACTION}. */
String PROPERTY_SOAP_ACTION = PROPERTY_PREFIX + "soapAction";

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2012 the original author or authors.
*
* Licensed 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
* 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,
@@ -46,7 +46,8 @@ public abstract class JmsTransportUtils {
private static final String[] CONVERSION_TABLE = new String[]{JmsTransportConstants.HEADER_CONTENT_TYPE,
JmsTransportConstants.PROPERTY_CONTENT_TYPE, JmsTransportConstants.HEADER_CONTENT_LENGTH,
JmsTransportConstants.PROPERTY_CONTENT_LENGTH, JmsTransportConstants.HEADER_SOAP_ACTION,
JmsTransportConstants.PROPERTY_SOAP_ACTION};
JmsTransportConstants.PROPERTY_SOAP_ACTION, JmsTransportConstants.HEADER_ACCEPT_ENCODING,
JmsTransportConstants.PROPERTY_ACCEPT_ENCODING};
private static final Pattern DESTINATION_NAME_PATTERN = Pattern.compile("^([^\\?]+)");
@@ -75,7 +76,18 @@ public abstract class JmsTransportUtils {
return CONVERSION_TABLE[i + 1];
}
}
return headerName;
// fall-back
StringBuilder builder = new StringBuilder(JmsTransportConstants.PROPERTY_PREFIX);
for (int i = 0; i < headerName.length(); i++) {
char ch = headerName.charAt(i);
if (i == 0) {
builder.append(Character.toLowerCase(ch));
}
else if (Character.isJavaIdentifierPart(ch)) {
builder.append(ch);
}
}
return builder.toString();
}
/**
@@ -91,7 +103,27 @@ public abstract class JmsTransportUtils {
return CONVERSION_TABLE[i - 1];
}
}
return propertyName;
// fall-back
if (propertyName.startsWith(JmsTransportConstants.PROPERTY_PREFIX)) {
StringBuilder builder = new StringBuilder(propertyName.length());
int start = JmsTransportConstants.PROPERTY_PREFIX.length();
for (int i = start; i < propertyName.length(); i++) {
char ch = propertyName.charAt(i);
if (i == start) {
builder.append(Character.toUpperCase(ch));
}
else {
if (Character.isUpperCase(ch)) {
builder.append('-');
}
builder.append(ch);
}
}
return builder.toString();
}
else {
return propertyName;
}
}
/**

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2012 the original author or authors.
*
* Licensed 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
* 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,
@@ -25,76 +25,78 @@ import org.springframework.ws.transport.jms.JmsTransportConstants;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class JmsTransportUtilsTest {
@Test
public void testGetDestinationName() throws Exception {
public void getDestinationName() throws Exception {
URI uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
String destinationName = JmsTransportUtils.getDestinationName(uri);
Assert.assertEquals("Invalid destination", "RequestQueue", destinationName);
assertEquals("Invalid destination", "RequestQueue", destinationName);
uri = new URI("jms:RequestQueue");
destinationName = JmsTransportUtils.getDestinationName(uri);
Assert.assertEquals("Invalid destination", "RequestQueue", destinationName);
assertEquals("Invalid destination", "RequestQueue", destinationName);
}
@Test
public void testGetDeliveryMode() throws Exception {
public void getDeliveryMode() throws Exception {
URI uri = new URI("jms:RequestQueue?deliveryMode=NON_PERSISTENT");
int deliveryMode = JmsTransportUtils.getDeliveryMode(uri);
Assert.assertEquals("Invalid deliveryMode", DeliveryMode.NON_PERSISTENT, deliveryMode);
assertEquals("Invalid deliveryMode", DeliveryMode.NON_PERSISTENT, deliveryMode);
uri = new URI("jms:RequestQueue?deliveryMode=PERSISTENT");
deliveryMode = JmsTransportUtils.getDeliveryMode(uri);
Assert.assertEquals("Invalid deliveryMode", DeliveryMode.PERSISTENT, deliveryMode);
assertEquals("Invalid deliveryMode", DeliveryMode.PERSISTENT, deliveryMode);
uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
deliveryMode = JmsTransportUtils.getDeliveryMode(uri);
Assert.assertEquals("Invalid deliveryMode", Message.DEFAULT_DELIVERY_MODE, deliveryMode);
assertEquals("Invalid deliveryMode", Message.DEFAULT_DELIVERY_MODE, deliveryMode);
}
@Test
public void testGetMessageType() throws Exception {
public void getMessageType() throws Exception {
URI uri = new URI("jms:RequestQueue?messageType=BYTESMESSAGE");
int messageType = JmsTransportUtils.getMessageType(uri);
Assert.assertEquals("Invalid messageType", JmsTransportConstants.BYTES_MESSAGE_TYPE, messageType);
assertEquals("Invalid messageType", JmsTransportConstants.BYTES_MESSAGE_TYPE, messageType);
uri = new URI("jms:RequestQueue?messageType=TEXT_MESSAGE");
messageType = JmsTransportUtils.getMessageType(uri);
Assert.assertEquals("Invalid messageType", JmsTransportConstants.TEXT_MESSAGE_TYPE, messageType);
assertEquals("Invalid messageType", JmsTransportConstants.TEXT_MESSAGE_TYPE, messageType);
uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
messageType = JmsTransportUtils.getMessageType(uri);
Assert.assertEquals("Invalid messageType", JmsTransportConstants.BYTES_MESSAGE_TYPE, messageType);
assertEquals("Invalid messageType", JmsTransportConstants.BYTES_MESSAGE_TYPE, messageType);
}
@Test
public void testGetTimeToLive() throws Exception {
public void getTimeToLive() throws Exception {
URI uri = new URI("jms:RequestQueue?timeToLive=100");
long timeToLive = JmsTransportUtils.getTimeToLive(uri);
Assert.assertEquals("Invalid timeToLive", 100, timeToLive);
assertEquals("Invalid timeToLive", 100, timeToLive);
uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
timeToLive = JmsTransportUtils.getTimeToLive(uri);
Assert.assertEquals("Invalid timeToLive", Message.DEFAULT_TIME_TO_LIVE, timeToLive);
assertEquals("Invalid timeToLive", Message.DEFAULT_TIME_TO_LIVE, timeToLive);
}
@Test
public void testGetPriority() throws Exception {
public void getPriority() throws Exception {
URI uri = new URI("jms:RequestQueue?priority=5");
int priority = JmsTransportUtils.getPriority(uri);
Assert.assertEquals("Invalid priority", 5, priority);
assertEquals("Invalid priority", 5, priority);
uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
priority = JmsTransportUtils.getPriority(uri);
Assert.assertEquals("Invalid priority", Message.DEFAULT_PRIORITY, priority);
assertEquals("Invalid priority", Message.DEFAULT_PRIORITY, priority);
}
@Test
public void testGetReplyToName() throws Exception {
public void getReplyToName() throws Exception {
URI uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
String replyToName = JmsTransportUtils.getReplyToName(uri);
Assert.assertEquals("Invalid replyToName", "RESP_QUEUE", replyToName);
assertEquals("Invalid replyToName", "RESP_QUEUE", replyToName);
uri = new URI("jms:RequestQueue?priority=5");
replyToName = JmsTransportUtils.getReplyToName(uri);
@@ -102,12 +104,26 @@ public class JmsTransportUtilsTest {
}
@Test
public void testJndi() throws Exception {
public void jndi() throws Exception {
URI uri = new URI("jms:jms/REQUEST_QUEUE?replyToName=jms/REPLY_QUEUE");
String destination = JmsTransportUtils.getDestinationName(uri);
Assert.assertEquals("Invalid destination name", "jms/REQUEST_QUEUE", destination);
assertEquals("Invalid destination name", "jms/REQUEST_QUEUE", destination);
String replyTo = JmsTransportUtils.getReplyToName(uri);
Assert.assertEquals("Invalid reply to name", "jms/REPLY_QUEUE", replyTo);
assertEquals("Invalid reply to name", "jms/REPLY_QUEUE", replyTo);
}
@Test
public void headerToJmsPropertyFallback() {
String expected = "SOAPJMS_fooBarBaz";
String result = JmsTransportUtils.headerToJmsProperty("Foo-Bar-Baz");
assertEquals(expected, result);
}
@Test
public void jmsPropertyToHeader() {
String expected = "Foo-Bar-Baz";
String result = JmsTransportUtils.jmsPropertyToHeader("SOAPJMS_fooBarBaz");
assertEquals(expected, result);
}
}