Skip to content

Commit 498e21a

Browse files
committed
SWS-258
1 parent cf8c802 commit 498e21a

File tree

5 files changed

+116
-25
lines changed

5 files changed

+116
-25
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.io.OutputStream;
24-
import java.util.Collections;
2524
import java.util.Iterator;
2625
import javax.jms.BytesMessage;
2726
import javax.jms.JMSException;
@@ -35,7 +34,7 @@
3534
import org.springframework.ws.WebServiceMessage;
3635
import org.springframework.ws.transport.AbstractReceiverConnection;
3736
import org.springframework.ws.transport.WebServiceConnection;
38-
import org.springframework.ws.transport.support.EnumerationIterator;
37+
import org.springframework.ws.transport.jms.support.JmsTransportUtils;
3938

4039
/**
4140
* Implementation of {@link WebServiceConnection} that is used for server-side JMS access. Exposes a {@link
@@ -117,7 +116,7 @@ public boolean hasError() throws IOException {
117116

118117
protected Iterator getRequestHeaderNames() throws IOException {
119118
try {
120-
return new EnumerationIterator(requestMessage.getPropertyNames());
119+
return JmsTransportUtils.getHeaderNames(requestMessage);
121120
}
122121
catch (JMSException ex) {
123122
throw new JmsTransportException("Could not get property names", ex);
@@ -126,8 +125,7 @@ protected Iterator getRequestHeaderNames() throws IOException {
126125

127126
protected Iterator getRequestHeaders(String name) throws IOException {
128127
try {
129-
String value = requestMessage.getStringProperty(name);
130-
return Collections.singletonList(value).iterator();
128+
return JmsTransportUtils.getHeaders(requestMessage, name);
131129
}
132130
catch (JMSException ex) {
133131
throw new JmsTransportException("Could not get property value", ex);
@@ -138,7 +136,7 @@ protected InputStream getRequestInputStream() throws IOException {
138136
if (requestMessage instanceof BytesMessage) {
139137
return new BytesMessageInputStream((BytesMessage) requestMessage);
140138
}
141-
else {
139+
else if (requestMessage instanceof TextMessage) {
142140
TextMessage textMessage = (TextMessage) requestMessage;
143141
try {
144142
String text = textMessage.getText();
@@ -149,6 +147,9 @@ protected InputStream getRequestInputStream() throws IOException {
149147
throw new JmsTransportException(ex);
150148
}
151149
}
150+
else {
151+
throw new IllegalStateException("Unknown request message type [" + requestMessage + "]");
152+
}
152153
}
153154

154155
/*
@@ -160,9 +161,12 @@ protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
160161
if (requestMessage instanceof BytesMessage) {
161162
responseMessage = session.createBytesMessage();
162163
}
163-
else {
164+
else if (requestMessage instanceof TextMessage) {
164165
responseMessage = session.createTextMessage();
165166
}
167+
else {
168+
throw new IllegalStateException("Unknown request message type [" + requestMessage + "]");
169+
}
166170
responseMessage.setJMSCorrelationID(requestMessage.getJMSMessageID());
167171
}
168172
catch (JMSException ex) {
@@ -172,7 +176,7 @@ protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
172176

173177
protected void addResponseHeader(String name, String value) throws IOException {
174178
try {
175-
responseMessage.setStringProperty(name, value);
179+
JmsTransportUtils.addHeader(responseMessage, name, value);
176180
}
177181
catch (JMSException ex) {
178182
throw new JmsTransportException("Could not set property", ex);
@@ -183,7 +187,7 @@ protected OutputStream getResponseOutputStream() throws IOException {
183187
if (responseMessage instanceof BytesMessage) {
184188
return new BytesMessageOutputStream((BytesMessage) responseMessage);
185189
}
186-
else {
190+
else if (responseMessage instanceof TextMessage) {
187191
return new ByteArrayOutputStream() {
188192

189193
public void close() throws IOException {
@@ -197,6 +201,9 @@ public void close() throws IOException {
197201
}
198202
};
199203
}
204+
else {
205+
throw new IllegalStateException("Unknown request message type [" + responseMessage + "]");
206+
}
200207
}
201208

202209
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.io.OutputStream;
24-
import java.util.Collections;
2524
import java.util.Iterator;
2625
import javax.jms.BytesMessage;
2726
import javax.jms.Connection;
@@ -41,7 +40,7 @@
4140
import org.springframework.ws.WebServiceMessage;
4241
import org.springframework.ws.transport.AbstractSenderConnection;
4342
import org.springframework.ws.transport.WebServiceConnection;
44-
import org.springframework.ws.transport.support.EnumerationIterator;
43+
import org.springframework.ws.transport.jms.support.JmsTransportUtils;
4544

4645
/**
4746
* Implementation of {@link WebServiceConnection} that is used for client-side JMS access. Exposes a {@link
@@ -169,7 +168,7 @@ protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
169168

170169
protected void addRequestHeader(String name, String value) throws IOException {
171170
try {
172-
requestMessage.setStringProperty(name, value);
171+
JmsTransportUtils.addHeader(requestMessage, name, value);
173172
}
174173
catch (JMSException ex) {
175174
throw new JmsTransportException("Could not set property", ex);
@@ -257,7 +256,7 @@ protected boolean hasResponse() throws IOException {
257256

258257
protected Iterator getResponseHeaderNames() throws IOException {
259258
try {
260-
return new EnumerationIterator(responseMessage.getPropertyNames());
259+
return JmsTransportUtils.getHeaderNames(responseMessage);
261260
}
262261
catch (JMSException ex) {
263262
throw new JmsTransportException("Could not get property names", ex);
@@ -266,13 +265,7 @@ protected Iterator getResponseHeaderNames() throws IOException {
266265

267266
protected Iterator getResponseHeaders(String name) throws IOException {
268267
try {
269-
String value = responseMessage.getStringProperty(name);
270-
if (value != null) {
271-
return Collections.singletonList(value).iterator();
272-
}
273-
else {
274-
return Collections.EMPTY_LIST.iterator();
275-
}
268+
return JmsTransportUtils.getHeaders(responseMessage, name);
276269
}
277270
catch (JMSException ex) {
278271
throw new JmsTransportException("Could not get property value", ex);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,16 @@ public interface JmsTransportConstants extends TransportConstants {
3838
/** Indicates a {@link TextMessage} type. */
3939
int TEXT_MESSAGE_TYPE = 2;
4040

41+
/** Prefix for JMS properties that map to transport headers. */
42+
String PROPERTY_PREFIX = "SOAPJMS_";
43+
44+
/** JMS property used for storing {@link #HEADER_SOAP_ACTION}. */
45+
String PROPERTY_SOAP_ACTION = PROPERTY_PREFIX + "soapAction";
46+
47+
/** JMS property used for storing {@link #HEADER_CONTENT_LENGTH}. */
48+
String PROPERTY_CONTENT_LENGTH = PROPERTY_PREFIX + "contentLength";
49+
50+
/** JMS property used for storing {@link #HEADER_CONTENT_TYPE}. */
51+
String PROPERTY_CONTENT_TYPE = PROPERTY_PREFIX + "contentType";
4152

4253
}

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
package org.springframework.ws.transport.jms.support;
1818

1919
import java.net.URI;
20+
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.Enumeration;
23+
import java.util.Iterator;
24+
import java.util.List;
2025
import java.util.regex.Matcher;
2126
import java.util.regex.Pattern;
2227
import javax.jms.DeliveryMode;
2328
import javax.jms.Destination;
29+
import javax.jms.JMSException;
2430
import javax.jms.Message;
2531

2632
import org.springframework.ws.transport.jms.JmsTransportConstants;
@@ -34,6 +40,11 @@
3440
*/
3541
public class JmsTransportUtils {
3642

43+
private static final String[] CONVERSION_TABLE = new String[]{JmsTransportConstants.HEADER_CONTENT_TYPE,
44+
JmsTransportConstants.PROPERTY_CONTENT_TYPE, JmsTransportConstants.HEADER_CONTENT_LENGTH,
45+
JmsTransportConstants.PROPERTY_CONTENT_LENGTH, JmsTransportConstants.HEADER_SOAP_ACTION,
46+
JmsTransportConstants.PROPERTY_SOAP_ACTION};
47+
3748
private static final Pattern DESTINATION_NAME_PATTERN = Pattern.compile("^([^\\?]+)");
3849

3950
private static final Pattern DELIVERY_MODE_PATTERN = Pattern.compile("deliveryMode=(PERSISTENT|NON_PERSISTENT)");
@@ -49,10 +60,80 @@ public class JmsTransportUtils {
4960
private JmsTransportUtils() {
5061
}
5162

63+
/**
64+
* Converts the given transport header to a JMS property name. Returns the given header name if no match is found.
65+
*
66+
* @param headerName the header name to transform
67+
* @return the JMS property name
68+
*/
69+
public static String headerToJmsProperty(String headerName) {
70+
for (int i = 0; i < CONVERSION_TABLE.length; i = i + 2) {
71+
if (CONVERSION_TABLE[i].equals(headerName)) {
72+
return CONVERSION_TABLE[i + 1];
73+
}
74+
}
75+
return headerName;
76+
}
77+
78+
/**
79+
* Converts the given JMS property name to a transport header name. Returns the given property name if no match is
80+
* found.
81+
*
82+
* @param propertyName the JMS property name to transform
83+
* @return the transport header name
84+
*/
85+
public static String jmsPropertyToHeader(String propertyName) {
86+
for (int i = 1; i < CONVERSION_TABLE.length; i = i + 2) {
87+
if (CONVERSION_TABLE[i].equals(propertyName)) {
88+
return CONVERSION_TABLE[i - 1];
89+
}
90+
}
91+
return propertyName;
92+
}
93+
94+
/** Returns the destination name of the given URI. */
5295
public static String getDestinationName(URI uri) {
5396
return getStringParameter(DESTINATION_NAME_PATTERN, uri);
5497
}
5598

99+
/** Adds the given header to the specified message. */
100+
public static void addHeader(Message message, String name, String value) throws JMSException {
101+
String propertyName = JmsTransportUtils.headerToJmsProperty(name);
102+
message.setStringProperty(propertyName, value);
103+
}
104+
105+
/**
106+
* Returns an iterator over all header names in the given message. Delegates to {@link
107+
* #jmsPropertyToHeader(String)}.
108+
*/
109+
public static Iterator getHeaderNames(Message message) throws JMSException {
110+
Enumeration properties = message.getPropertyNames();
111+
List results = new ArrayList();
112+
while (properties.hasMoreElements()) {
113+
String property = (String) properties.nextElement();
114+
if (property.startsWith(JmsTransportConstants.PROPERTY_PREFIX)) {
115+
String header = jmsPropertyToHeader(property);
116+
results.add(header);
117+
}
118+
}
119+
return results.iterator();
120+
}
121+
122+
/**
123+
* Returns an iterator over all the header values of the given message and header name. Delegates to {@link
124+
* #headerToJmsProperty(String)}.
125+
*/
126+
public static Iterator getHeaders(Message message, String name) throws JMSException {
127+
String propertyName = headerToJmsProperty(name);
128+
String value = message.getStringProperty(propertyName);
129+
if (value != null) {
130+
return Collections.singletonList(value).iterator();
131+
}
132+
else {
133+
return Collections.EMPTY_LIST.iterator();
134+
}
135+
}
136+
56137
/**
57138
* Returns the delivery mode of the given URI.
58139
*

support/src/test/java/org/springframework/ws/transport/jms/JmsMessageSenderIntegrationTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.ws.soap.SoapVersion;
3434
import org.springframework.ws.soap.saaj.SaajSoapMessage;
3535
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
36-
import org.springframework.ws.transport.TransportConstants;
3736
import org.springframework.ws.transport.WebServiceConnection;
3837

3938
public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjectionSpringContextTests {
@@ -80,8 +79,8 @@ public void testSendAndReceiveQueueBytesMessage() throws Exception {
8079

8180
public Message createMessage(Session session) throws JMSException {
8281
BytesMessage response = session.createBytesMessage();
83-
response.setStringProperty(TransportConstants.HEADER_SOAP_ACTION, SOAP_ACTION);
84-
response.setStringProperty(TransportConstants.HEADER_CONTENT_TYPE,
82+
response.setStringProperty(JmsTransportConstants.PROPERTY_SOAP_ACTION, SOAP_ACTION);
83+
response.setStringProperty(JmsTransportConstants.PROPERTY_CONTENT_TYPE,
8584
SoapVersion.SOAP_11.getContentType());
8685
response.writeBytes(buf);
8786
return response;
@@ -117,8 +116,8 @@ public void testSendAndReceiveQueueTextMessage() throws Exception {
117116

118117
public Message createMessage(Session session) throws JMSException {
119118
TextMessage response = session.createTextMessage();
120-
response.setStringProperty(TransportConstants.HEADER_SOAP_ACTION, SOAP_ACTION);
121-
response.setStringProperty(TransportConstants.HEADER_CONTENT_TYPE,
119+
response.setStringProperty(JmsTransportConstants.PROPERTY_SOAP_ACTION, SOAP_ACTION);
120+
response.setStringProperty(JmsTransportConstants.PROPERTY_CONTENT_TYPE,
122121
SoapVersion.SOAP_11.getContentType());
123122
response.setText(text);
124123
return response;

0 commit comments

Comments
 (0)