Simplified the JMS support
This commit is contained in:
@@ -36,7 +36,7 @@ import org.springframework.ws.transport.support.SimpleWebServiceMessageReceiverO
|
||||
public class JmsMessageReceiver extends SimpleWebServiceMessageReceiverObjectSupport {
|
||||
|
||||
/**
|
||||
* Handles an incoming messages. Uses the given session to create a response message.
|
||||
* Handles an incoming message. Uses the given session to create a response message.
|
||||
*
|
||||
* @param request the incoming message
|
||||
* @param session the JMS session used to create a response
|
||||
|
||||
@@ -19,11 +19,8 @@ package org.springframework.ws.transport.jms;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import javax.jms.BytesMessage;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.MessageProducer;
|
||||
@@ -31,21 +28,19 @@ import javax.jms.Session;
|
||||
|
||||
import org.springframework.jms.support.JmsUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.FaultAwareWebServiceMessage;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.transport.AbstractReceiverConnection;
|
||||
import org.springframework.ws.transport.FaultAwareWebServiceConnection;
|
||||
import org.springframework.ws.transport.WebServiceConnection;
|
||||
import org.springframework.ws.transport.jms.support.JmsTransportUtils;
|
||||
import org.springframework.ws.transport.support.EnumerationIterator;
|
||||
|
||||
/**
|
||||
* Implementation of {@link WebServiceConnection} that is used for server-side JMS access.
|
||||
* Implementation of {@link WebServiceConnection} that is used for server-side JMS access. Exposes a {@link
|
||||
* BytesMessage} request and response message.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class JmsReceiverConnection extends AbstractReceiverConnection
|
||||
implements JmsTransportConstants, FaultAwareWebServiceConnection {
|
||||
public class JmsReceiverConnection extends AbstractReceiverConnection implements WebServiceConnection {
|
||||
|
||||
private final BytesMessage requestMessage;
|
||||
|
||||
@@ -53,9 +48,7 @@ public class JmsReceiverConnection extends AbstractReceiverConnection
|
||||
|
||||
private BytesMessage responseMessage;
|
||||
|
||||
/**
|
||||
* Constructs a new JMS connection with the given parameters.
|
||||
*/
|
||||
/** Constructs a new JMS connection with the given parameters. */
|
||||
protected JmsReceiverConnection(BytesMessage requestMessage, Session session) {
|
||||
Assert.notNull(requestMessage, "requestMessage must not be null");
|
||||
Assert.notNull(session, "session must not be null");
|
||||
@@ -63,16 +56,12 @@ public class JmsReceiverConnection extends AbstractReceiverConnection
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the request message for this connection.
|
||||
*/
|
||||
/** Returns the request message for this connection. */
|
||||
public BytesMessage getRequestMessage() {
|
||||
return requestMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the response message, if any, for this connection.
|
||||
*/
|
||||
/** Returns the response message, if any, for this connection. */
|
||||
public BytesMessage getResponseMessage() {
|
||||
return responseMessage;
|
||||
}
|
||||
@@ -91,15 +80,7 @@ public class JmsReceiverConnection extends AbstractReceiverConnection
|
||||
|
||||
protected Iterator getRequestHeaderNames() throws IOException {
|
||||
try {
|
||||
Enumeration headers = requestMessage.getPropertyNames();
|
||||
List results = new ArrayList();
|
||||
while (headers.hasMoreElements()) {
|
||||
String header = (String) headers.nextElement();
|
||||
if (header.startsWith(JmsTransportConstants.PROPERTY_PREFIX)) {
|
||||
results.add(header);
|
||||
}
|
||||
}
|
||||
return results.iterator();
|
||||
return new EnumerationIterator(requestMessage.getPropertyNames());
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new JmsTransportException("Could not get property names", ex);
|
||||
@@ -128,11 +109,6 @@ public class JmsReceiverConnection extends AbstractReceiverConnection
|
||||
try {
|
||||
responseMessage = session.createBytesMessage();
|
||||
responseMessage.setJMSCorrelationID(requestMessage.getJMSMessageID());
|
||||
responseMessage.setStringProperty(PROPERTY_BINDING_VERSION, "1.0");
|
||||
if (message instanceof FaultAwareWebServiceMessage) {
|
||||
FaultAwareWebServiceMessage faultMessage = (FaultAwareWebServiceMessage) message;
|
||||
responseMessage.setBooleanProperty(PROPERTY_IS_FAULT, faultMessage.hasFault());
|
||||
}
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new JmsTransportException("Could not create response message", ex);
|
||||
@@ -141,8 +117,7 @@ public class JmsReceiverConnection extends AbstractReceiverConnection
|
||||
|
||||
protected void addResponseHeader(String name, String value) throws IOException {
|
||||
try {
|
||||
String property = JmsTransportUtils.headerToJmsProperty(name);
|
||||
responseMessage.setStringProperty(property, value);
|
||||
responseMessage.setStringProperty(name, value);
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new JmsTransportException("Could not set property", ex);
|
||||
@@ -174,29 +149,4 @@ public class JmsReceiverConnection extends AbstractReceiverConnection
|
||||
public void close() throws IOException {
|
||||
}
|
||||
|
||||
/*
|
||||
* Faults
|
||||
*/
|
||||
|
||||
public boolean hasFault() throws IOException {
|
||||
try {
|
||||
return requestMessage.getBooleanProperty(JmsTransportConstants.PROPERTY_IS_FAULT);
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new JmsTransportException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void setFault(boolean fault) throws IOException {
|
||||
if (responseMessage != null) {
|
||||
try {
|
||||
responseMessage.setBooleanProperty(JmsTransportConstants.PROPERTY_IS_FAULT, fault);
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new JmsTransportException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -19,11 +19,8 @@ package org.springframework.ws.transport.jms;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import javax.jms.BytesMessage;
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
@@ -37,12 +34,10 @@ import javax.jms.TemporaryQueue;
|
||||
import org.springframework.jms.connection.ConnectionFactoryUtils;
|
||||
import org.springframework.jms.support.JmsUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.FaultAwareWebServiceMessage;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.transport.AbstractSenderConnection;
|
||||
import org.springframework.ws.transport.FaultAwareWebServiceConnection;
|
||||
import org.springframework.ws.transport.WebServiceConnection;
|
||||
import org.springframework.ws.transport.jms.support.JmsTransportUtils;
|
||||
import org.springframework.ws.transport.support.EnumerationIterator;
|
||||
|
||||
/**
|
||||
* Implementation of {@link WebServiceConnection} that is used for client-side JMS access.
|
||||
@@ -50,8 +45,7 @@ import org.springframework.ws.transport.jms.support.JmsTransportUtils;
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class JmsSenderConnection extends AbstractSenderConnection
|
||||
implements FaultAwareWebServiceConnection, JmsTransportConstants {
|
||||
public class JmsSenderConnection extends AbstractSenderConnection implements WebServiceConnection {
|
||||
|
||||
private final ConnectionFactory connectionFactory;
|
||||
|
||||
@@ -142,12 +136,6 @@ public class JmsSenderConnection extends AbstractSenderConnection
|
||||
protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
|
||||
try {
|
||||
requestMessage = session.createBytesMessage();
|
||||
requestMessage.setStringProperty(PROPERTY_BINDING_VERSION, "1.0");
|
||||
if (message instanceof FaultAwareWebServiceMessage) {
|
||||
FaultAwareWebServiceMessage faultMessage = (FaultAwareWebServiceMessage) message;
|
||||
requestMessage.setBooleanProperty(PROPERTY_IS_FAULT, faultMessage.hasFault());
|
||||
}
|
||||
// requestMessage.setStringProperty(PROPERTY_REQUEST_IRI, uri.toString());
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new JmsTransportException(ex);
|
||||
@@ -156,8 +144,7 @@ public class JmsSenderConnection extends AbstractSenderConnection
|
||||
|
||||
protected void addRequestHeader(String name, String value) throws IOException {
|
||||
try {
|
||||
String property = JmsTransportUtils.headerToJmsProperty(name);
|
||||
requestMessage.setStringProperty(property, value);
|
||||
requestMessage.setStringProperty(name, value);
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new JmsTransportException("Could not set property", ex);
|
||||
@@ -223,13 +210,7 @@ public class JmsSenderConnection extends AbstractSenderConnection
|
||||
|
||||
protected Iterator getResponseHeaderNames() throws IOException {
|
||||
try {
|
||||
List headerNames = new ArrayList();
|
||||
Enumeration propertyNames = responseMessage.getPropertyNames();
|
||||
while (propertyNames.hasMoreElements()) {
|
||||
String propertyName = (String) propertyNames.nextElement();
|
||||
headerNames.add(JmsTransportUtils.jmsPropertyToHeader(propertyName));
|
||||
}
|
||||
return headerNames.iterator();
|
||||
return new EnumerationIterator(responseMessage.getPropertyNames());
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new JmsTransportException("Could not get property names", ex);
|
||||
@@ -238,8 +219,7 @@ public class JmsSenderConnection extends AbstractSenderConnection
|
||||
|
||||
protected Iterator getResponseHeaders(String name) throws IOException {
|
||||
try {
|
||||
String propertyName = JmsTransportUtils.headerToJmsProperty(name);
|
||||
String value = responseMessage.getStringProperty(propertyName);
|
||||
String value = responseMessage.getStringProperty(name);
|
||||
if (value != null) {
|
||||
return Collections.singletonList(value).iterator();
|
||||
}
|
||||
@@ -261,31 +241,5 @@ public class JmsSenderConnection extends AbstractSenderConnection
|
||||
ConnectionFactoryUtils.releaseConnection(connection, connectionFactory, true);
|
||||
}
|
||||
|
||||
/*
|
||||
* Faults
|
||||
*/
|
||||
|
||||
public boolean hasFault() throws IOException {
|
||||
if (responseMessage != null) {
|
||||
try {
|
||||
return responseMessage.getBooleanProperty(JmsTransportConstants.PROPERTY_IS_FAULT);
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new JmsTransportException(ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void setFault(boolean fault) throws IOException {
|
||||
try {
|
||||
requestMessage.setBooleanProperty(JmsTransportConstants.PROPERTY_IS_FAULT, fault);
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new JmsTransportException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,41 +29,4 @@ public interface JmsTransportConstants extends TransportConstants {
|
||||
/** The "jms" URI scheme" */
|
||||
String JMS_URI_SCHEME = "jms";
|
||||
|
||||
String PARAM_DELIVERY_MODE = "deliveryMode";
|
||||
|
||||
String PARAM_CONNECTION_FACTORY_NAME = "connectionFactoryName";
|
||||
|
||||
String PARAM_INITIAL_CONTEXT_FACTORY = "initialContextFactory";
|
||||
|
||||
String PARAM_JNDI_URL = "jndiURL";
|
||||
|
||||
String PARAM_TIME_TO_LIVE = "timeToLive";
|
||||
|
||||
String PARAM_PRIORITY = "priority";
|
||||
|
||||
String PARAM_DESTINATION_TYPE = "destinationType";
|
||||
|
||||
String PARAM_REPLY_TO_NAME = "replyToName";
|
||||
|
||||
String DESTINATION_TYPE_QUEUE = "queue";
|
||||
|
||||
String DESTINATION_TYPE_TOPIC = "topic";
|
||||
|
||||
String PROPERTY_PREFIX = "SOAPJMS_";
|
||||
|
||||
String PROPERTY_IS_FAULT = PROPERTY_PREFIX + "isFault";
|
||||
|
||||
String PROPERTY_SOAP_ACTION = PROPERTY_PREFIX + "soapAction";
|
||||
|
||||
String PROPERTY_CONTENT_LENGTH = PROPERTY_PREFIX + "contentLength";
|
||||
|
||||
String PROPERTY_CONTENT_TYPE = PROPERTY_PREFIX + "contentType";
|
||||
|
||||
String PROPERTY_BINDING_VERSION = PROPERTY_PREFIX + "bindingVersion";
|
||||
|
||||
String PROPERTY_TARGET_SERVICE = PROPERTY_PREFIX + "targetService";
|
||||
|
||||
String PROPERTY_REQUEST_IRI = PROPERTY_PREFIX + "requestIRI";
|
||||
|
||||
String PROPERTY_SOAP_MEP = PROPERTY_PREFIX + "soapMEP";
|
||||
}
|
||||
|
||||
@@ -23,11 +23,9 @@ import javax.jms.DeliveryMode;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.Message;
|
||||
|
||||
import org.springframework.ws.transport.jms.JmsTransportConstants;
|
||||
|
||||
/**
|
||||
* Collection of utility methods to work with JMS transports. Includes methods to convert from transport header names to
|
||||
* JMS Properties and vice-versa.
|
||||
* Collection of utility methods to work with JMS transports. Includes methods to retrieve JMS properties from an {@link
|
||||
* URI}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.1.0
|
||||
@@ -44,45 +42,9 @@ public class JmsTransportUtils {
|
||||
|
||||
private static final Pattern REPLY_TO_NAME_PATTERN = Pattern.compile("replyToName=(\\w+)");
|
||||
|
||||
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};
|
||||
|
||||
private JmsTransportUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given transport header to a JMS property name. Returns the given header name if no match is found.
|
||||
*
|
||||
* @param headerName the header name to transform
|
||||
* @return the JMS property name
|
||||
*/
|
||||
public static String headerToJmsProperty(String headerName) {
|
||||
for (int i = 0; i < CONVERSION_TABLE.length; i = i + 2) {
|
||||
if (CONVERSION_TABLE[i].equals(headerName)) {
|
||||
return CONVERSION_TABLE[i + 1];
|
||||
}
|
||||
}
|
||||
return headerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given JMS property name to a transport header name. Returns the given property name if no match is
|
||||
* found.
|
||||
*
|
||||
* @param propertyName the JMS property name to transform
|
||||
* @return the transport header name
|
||||
*/
|
||||
public static String jmsPropertyToHeader(String propertyName) {
|
||||
for (int i = 1; i < CONVERSION_TABLE.length; i = i + 2) {
|
||||
if (CONVERSION_TABLE[i].equals(propertyName)) {
|
||||
return CONVERSION_TABLE[i - 1];
|
||||
}
|
||||
}
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
public static String getDestinationName(URI uri) {
|
||||
return getStringParameter(DESTINATION_NAME_PATTERN, uri);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.ws.transport.jms;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import javax.jms.BytesMessage;
|
||||
import javax.jms.JMSException;
|
||||
@@ -30,8 +29,10 @@ import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.jms.core.MessageCreator;
|
||||
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.SoapVersion;
|
||||
import org.springframework.ws.soap.saaj.SaajSoapMessage;
|
||||
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
|
||||
import org.springframework.ws.transport.TransportConstants;
|
||||
import org.springframework.ws.transport.WebServiceConnection;
|
||||
|
||||
public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjectionSpringContextTests {
|
||||
@@ -72,7 +73,6 @@ public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjection
|
||||
connection.send(soapRequest);
|
||||
|
||||
BytesMessage request = (BytesMessage) jmsTemplate.receive();
|
||||
validateMessage(request);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
messageFactory.createMessage().writeTo(bos);
|
||||
final byte[] buf = bos.toByteArray();
|
||||
@@ -80,13 +80,9 @@ public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjection
|
||||
|
||||
public Message createMessage(Session session) throws JMSException {
|
||||
BytesMessage response = session.createBytesMessage();
|
||||
response.setStringProperty(JmsTransportConstants.PROPERTY_BINDING_VERSION, "1.0");
|
||||
response.setIntProperty(JmsTransportConstants.PROPERTY_CONTENT_LENGTH, buf.length);
|
||||
response.setStringProperty(JmsTransportConstants.PROPERTY_CONTENT_TYPE, "text/xml");
|
||||
response.setBooleanProperty(JmsTransportConstants.PROPERTY_IS_FAULT, false);
|
||||
response.setStringProperty(JmsTransportConstants.PROPERTY_REQUEST_IRI, requestQueueUri.toString());
|
||||
response.setStringProperty(JmsTransportConstants.PROPERTY_SOAP_ACTION, SOAP_ACTION);
|
||||
|
||||
response.setStringProperty(TransportConstants.HEADER_SOAP_ACTION, SOAP_ACTION);
|
||||
response.setStringProperty(TransportConstants.HEADER_CONTENT_TYPE,
|
||||
SoapVersion.SOAP_11.getContentType());
|
||||
response.writeBytes(buf);
|
||||
return response;
|
||||
}
|
||||
@@ -103,28 +99,4 @@ public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjection
|
||||
}
|
||||
}
|
||||
|
||||
private void validateMessage(BytesMessage message) throws JMSException, IOException {
|
||||
assertEquals("Invalid SOAPAction", SOAP_ACTION,
|
||||
message.getStringProperty(JmsTransportConstants.PROPERTY_SOAP_ACTION));
|
||||
assertEquals("Invalid binding version", "1.0",
|
||||
message.getStringProperty(JmsTransportConstants.PROPERTY_BINDING_VERSION));
|
||||
assertFalse("Message is Fault", message.getBooleanProperty(JmsTransportConstants.PROPERTY_IS_FAULT));
|
||||
assertTrue("Invalid Content Type",
|
||||
message.getStringProperty(JmsTransportConstants.PROPERTY_CONTENT_TYPE).indexOf("text/xml") != -1);
|
||||
assertTrue("No Content Length", message.getIntProperty(JmsTransportConstants.PROPERTY_CONTENT_LENGTH) > 0);
|
||||
|
||||
assertTrue("Message has no contents", getMessageContents(message).length() > 0);
|
||||
|
||||
}
|
||||
|
||||
private String getMessageContents(BytesMessage message) throws JMSException, IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = message.readBytes(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
}
|
||||
out.flush();
|
||||
return out.toString("UTF-8");
|
||||
}
|
||||
}
|
||||
@@ -24,11 +24,6 @@ import junit.framework.TestCase;
|
||||
|
||||
public class JmsTransportUtilsTest extends TestCase {
|
||||
|
||||
public void testHeaderToJmsProperty() throws Exception {
|
||||
String result = JmsTransportUtils.headerToJmsProperty("SOAPAction");
|
||||
assertEquals("Invalid result", "SOAPJMS_soapAction", result);
|
||||
}
|
||||
|
||||
public void testGetDeliveryMode() throws Exception {
|
||||
URI uri = new URI("jms:RequestQueue?deliveryMode=NON_PERSISTENT");
|
||||
int deliveryMode = JmsTransportUtils.getDeliveryMode(uri);
|
||||
|
||||
Reference in New Issue
Block a user