Skip to content

Commit d5107c5

Browse files
committed
SWS-798 - Ensure that only valid Java Identifiers are added to JMS headers
1 parent de42ef2 commit d5107c5

File tree

4 files changed

+87
-33
lines changed

4 files changed

+87
-33
lines changed

core/src/main/java/org/springframework/ws/transport/TransportConstants.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -27,6 +27,9 @@ public interface TransportConstants {
2727
/** The "Accept" header. */
2828
String HEADER_ACCEPT = "Accept";
2929

30+
/** The "Accept-Encoding" header. */
31+
String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
32+
3033
/** The "Content-Id" header. */
3134
String HEADER_CONTENT_ID = "Content-Id";
3235

support/src/main/java/org/springframework/ws/transport/jms/JmsTransportConstants.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2007 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -41,6 +41,9 @@ public interface JmsTransportConstants extends TransportConstants {
4141
/** Prefix for JMS properties that map to transport headers. */
4242
String PROPERTY_PREFIX = "SOAPJMS_";
4343

44+
/** JMS property used for storing {@link #HEADER_ACCEPT_ENCODING}. */
45+
String PROPERTY_ACCEPT_ENCODING = PROPERTY_PREFIX + "acceptEncoding";
46+
4447
/** JMS property used for storing {@link #HEADER_SOAP_ACTION}. */
4548
String PROPERTY_SOAP_ACTION = PROPERTY_PREFIX + "soapAction";
4649

support/src/main/java/org/springframework/ws/transport/jms/support/JmsTransportUtils.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -46,7 +46,8 @@ public abstract class JmsTransportUtils {
4646
private static final String[] CONVERSION_TABLE = new String[]{JmsTransportConstants.HEADER_CONTENT_TYPE,
4747
JmsTransportConstants.PROPERTY_CONTENT_TYPE, JmsTransportConstants.HEADER_CONTENT_LENGTH,
4848
JmsTransportConstants.PROPERTY_CONTENT_LENGTH, JmsTransportConstants.HEADER_SOAP_ACTION,
49-
JmsTransportConstants.PROPERTY_SOAP_ACTION};
49+
JmsTransportConstants.PROPERTY_SOAP_ACTION, JmsTransportConstants.HEADER_ACCEPT_ENCODING,
50+
JmsTransportConstants.PROPERTY_ACCEPT_ENCODING};
5051

5152
private static final Pattern DESTINATION_NAME_PATTERN = Pattern.compile("^([^\\?]+)");
5253

@@ -75,7 +76,18 @@ public static String headerToJmsProperty(String headerName) {
7576
return CONVERSION_TABLE[i + 1];
7677
}
7778
}
78-
return headerName;
79+
// fall-back
80+
StringBuilder builder = new StringBuilder(JmsTransportConstants.PROPERTY_PREFIX);
81+
for (int i = 0; i < headerName.length(); i++) {
82+
char ch = headerName.charAt(i);
83+
if (i == 0) {
84+
builder.append(Character.toLowerCase(ch));
85+
}
86+
else if (Character.isJavaIdentifierPart(ch)) {
87+
builder.append(ch);
88+
}
89+
}
90+
return builder.toString();
7991
}
8092

8193
/**
@@ -91,7 +103,27 @@ public static String jmsPropertyToHeader(String propertyName) {
91103
return CONVERSION_TABLE[i - 1];
92104
}
93105
}
94-
return propertyName;
106+
// fall-back
107+
if (propertyName.startsWith(JmsTransportConstants.PROPERTY_PREFIX)) {
108+
StringBuilder builder = new StringBuilder(propertyName.length());
109+
int start = JmsTransportConstants.PROPERTY_PREFIX.length();
110+
for (int i = start; i < propertyName.length(); i++) {
111+
char ch = propertyName.charAt(i);
112+
if (i == start) {
113+
builder.append(Character.toUpperCase(ch));
114+
}
115+
else {
116+
if (Character.isUpperCase(ch)) {
117+
builder.append('-');
118+
}
119+
builder.append(ch);
120+
}
121+
}
122+
return builder.toString();
123+
}
124+
else {
125+
return propertyName;
126+
}
95127
}
96128

97129
/**
Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -25,89 +25,105 @@
2525
import org.junit.Assert;
2626
import org.junit.Test;
2727

28+
import static org.junit.Assert.assertEquals;
29+
2830
public class JmsTransportUtilsTest {
2931

3032
@Test
31-
public void testGetDestinationName() throws Exception {
33+
public void getDestinationName() throws Exception {
3234
URI uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
3335
String destinationName = JmsTransportUtils.getDestinationName(uri);
34-
Assert.assertEquals("Invalid destination", "RequestQueue", destinationName);
36+
assertEquals("Invalid destination", "RequestQueue", destinationName);
3537

3638
uri = new URI("jms:RequestQueue");
3739
destinationName = JmsTransportUtils.getDestinationName(uri);
38-
Assert.assertEquals("Invalid destination", "RequestQueue", destinationName);
40+
assertEquals("Invalid destination", "RequestQueue", destinationName);
3941
}
4042

4143
@Test
42-
public void testGetDeliveryMode() throws Exception {
44+
public void getDeliveryMode() throws Exception {
4345
URI uri = new URI("jms:RequestQueue?deliveryMode=NON_PERSISTENT");
4446
int deliveryMode = JmsTransportUtils.getDeliveryMode(uri);
45-
Assert.assertEquals("Invalid deliveryMode", DeliveryMode.NON_PERSISTENT, deliveryMode);
47+
assertEquals("Invalid deliveryMode", DeliveryMode.NON_PERSISTENT, deliveryMode);
4648

4749
uri = new URI("jms:RequestQueue?deliveryMode=PERSISTENT");
4850
deliveryMode = JmsTransportUtils.getDeliveryMode(uri);
49-
Assert.assertEquals("Invalid deliveryMode", DeliveryMode.PERSISTENT, deliveryMode);
51+
assertEquals("Invalid deliveryMode", DeliveryMode.PERSISTENT, deliveryMode);
5052

5153
uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
5254
deliveryMode = JmsTransportUtils.getDeliveryMode(uri);
53-
Assert.assertEquals("Invalid deliveryMode", Message.DEFAULT_DELIVERY_MODE, deliveryMode);
55+
assertEquals("Invalid deliveryMode", Message.DEFAULT_DELIVERY_MODE, deliveryMode);
5456
}
5557

5658
@Test
57-
public void testGetMessageType() throws Exception {
59+
public void getMessageType() throws Exception {
5860
URI uri = new URI("jms:RequestQueue?messageType=BYTESMESSAGE");
5961
int messageType = JmsTransportUtils.getMessageType(uri);
60-
Assert.assertEquals("Invalid messageType", JmsTransportConstants.BYTES_MESSAGE_TYPE, messageType);
62+
assertEquals("Invalid messageType", JmsTransportConstants.BYTES_MESSAGE_TYPE, messageType);
6163

6264
uri = new URI("jms:RequestQueue?messageType=TEXT_MESSAGE");
6365
messageType = JmsTransportUtils.getMessageType(uri);
64-
Assert.assertEquals("Invalid messageType", JmsTransportConstants.TEXT_MESSAGE_TYPE, messageType);
66+
assertEquals("Invalid messageType", JmsTransportConstants.TEXT_MESSAGE_TYPE, messageType);
6567

6668
uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
6769
messageType = JmsTransportUtils.getMessageType(uri);
68-
Assert.assertEquals("Invalid messageType", JmsTransportConstants.BYTES_MESSAGE_TYPE, messageType);
70+
assertEquals("Invalid messageType", JmsTransportConstants.BYTES_MESSAGE_TYPE, messageType);
6971
}
7072

7173
@Test
72-
public void testGetTimeToLive() throws Exception {
74+
public void getTimeToLive() throws Exception {
7375
URI uri = new URI("jms:RequestQueue?timeToLive=100");
7476
long timeToLive = JmsTransportUtils.getTimeToLive(uri);
75-
Assert.assertEquals("Invalid timeToLive", 100, timeToLive);
77+
assertEquals("Invalid timeToLive", 100, timeToLive);
7678

7779
uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
7880
timeToLive = JmsTransportUtils.getTimeToLive(uri);
79-
Assert.assertEquals("Invalid timeToLive", Message.DEFAULT_TIME_TO_LIVE, timeToLive);
81+
assertEquals("Invalid timeToLive", Message.DEFAULT_TIME_TO_LIVE, timeToLive);
8082
}
8183

8284
@Test
83-
public void testGetPriority() throws Exception {
85+
public void getPriority() throws Exception {
8486
URI uri = new URI("jms:RequestQueue?priority=5");
8587
int priority = JmsTransportUtils.getPriority(uri);
86-
Assert.assertEquals("Invalid priority", 5, priority);
88+
assertEquals("Invalid priority", 5, priority);
8789

8890
uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
8991
priority = JmsTransportUtils.getPriority(uri);
90-
Assert.assertEquals("Invalid priority", Message.DEFAULT_PRIORITY, priority);
92+
assertEquals("Invalid priority", Message.DEFAULT_PRIORITY, priority);
9193
}
9294

9395
@Test
94-
public void testGetReplyToName() throws Exception {
96+
public void getReplyToName() throws Exception {
9597
URI uri = new URI("jms:RequestQueue?replyToName=RESP_QUEUE");
9698
String replyToName = JmsTransportUtils.getReplyToName(uri);
97-
Assert.assertEquals("Invalid replyToName", "RESP_QUEUE", replyToName);
99+
assertEquals("Invalid replyToName", "RESP_QUEUE", replyToName);
98100

99101
uri = new URI("jms:RequestQueue?priority=5");
100102
replyToName = JmsTransportUtils.getReplyToName(uri);
101103
Assert.assertNull("Invalid replyToName", replyToName);
102104
}
103105

104106
@Test
105-
public void testJndi() throws Exception {
107+
public void jndi() throws Exception {
106108
URI uri = new URI("jms:jms/REQUEST_QUEUE?replyToName=jms/REPLY_QUEUE");
107109
String destination = JmsTransportUtils.getDestinationName(uri);
108-
Assert.assertEquals("Invalid destination name", "jms/REQUEST_QUEUE", destination);
110+
assertEquals("Invalid destination name", "jms/REQUEST_QUEUE", destination);
109111

110112
String replyTo = JmsTransportUtils.getReplyToName(uri);
111-
Assert.assertEquals("Invalid reply to name", "jms/REPLY_QUEUE", replyTo);
113+
assertEquals("Invalid reply to name", "jms/REPLY_QUEUE", replyTo);
114+
}
115+
116+
@Test
117+
public void headerToJmsPropertyFallback() {
118+
String expected = "SOAPJMS_fooBarBaz";
119+
String result = JmsTransportUtils.headerToJmsProperty("Foo-Bar-Baz");
120+
assertEquals(expected, result);
121+
}
122+
123+
@Test
124+
public void jmsPropertyToHeader() {
125+
String expected = "Foo-Bar-Baz";
126+
String result = JmsTransportUtils.jmsPropertyToHeader("SOAPJMS_fooBarBaz");
127+
assertEquals(expected, result);
112128
}
113129
}

0 commit comments

Comments
 (0)