SWS-375
This commit is contained in:
@@ -21,7 +21,7 @@ import javax.jms.Message;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import org.springframework.ws.transport.WebServiceConnection;
|
||||
import org.springframework.jms.core.MessagePostProcessor;
|
||||
import org.springframework.ws.transport.WebServiceMessageReceiver;
|
||||
import org.springframework.ws.transport.support.SimpleWebServiceMessageReceiverObjectSupport;
|
||||
|
||||
@@ -43,11 +43,21 @@ public class JmsMessageReceiver extends SimpleWebServiceMessageReceiverObjectSup
|
||||
|
||||
private String textMessageEncoding = DEFAULT_TEXT_MESSAGE_ENCODING;
|
||||
|
||||
private MessagePostProcessor postProcessor;
|
||||
|
||||
/** Sets the encoding used to read from and write to {@link TextMessage} messages. Defaults to <code>UTF-8</code>. */
|
||||
public void setTextMessageEncoding(String textMessageEncoding) {
|
||||
this.textMessageEncoding = textMessageEncoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the optional {@link MessagePostProcessor} to further modify outgoing messages after the XML contents has
|
||||
* been set.
|
||||
*/
|
||||
public void setPostProcessor(MessagePostProcessor postProcessor) {
|
||||
this.postProcessor = postProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles an incoming message. Uses the given session to create a response message.
|
||||
*
|
||||
@@ -56,7 +66,7 @@ public class JmsMessageReceiver extends SimpleWebServiceMessageReceiverObjectSup
|
||||
* @throws IllegalArgumentException when request is not a {@link BytesMessage}
|
||||
*/
|
||||
protected final void handleMessage(Message request, Session session) throws Exception {
|
||||
WebServiceConnection connection;
|
||||
JmsReceiverConnection connection;
|
||||
if (request instanceof BytesMessage) {
|
||||
connection = new JmsReceiverConnection((BytesMessage) request, session);
|
||||
}
|
||||
@@ -67,6 +77,8 @@ public class JmsMessageReceiver extends SimpleWebServiceMessageReceiverObjectSup
|
||||
throw new IllegalArgumentException("Wrong message type: [" + request.getClass() +
|
||||
"]. Only BytesMessages or TextMessages can be handled.");
|
||||
}
|
||||
connection.setPostProcessor(postProcessor);
|
||||
|
||||
handleConnection(connection);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import javax.jms.TextMessage;
|
||||
import javax.jms.Topic;
|
||||
|
||||
import org.springframework.jms.connection.ConnectionFactoryUtils;
|
||||
import org.springframework.jms.core.MessagePostProcessor;
|
||||
import org.springframework.jms.support.JmsUtils;
|
||||
import org.springframework.jms.support.destination.JmsDestinationAccessor;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -70,8 +71,7 @@ import org.springframework.ws.transport.jms.support.JmsTransportUtils;
|
||||
* <tt>jms:RequestQueue?replyToName=ResponseQueueName</tt><br> <tt>jms:Queue?messageType=TEXT_MESSAGE</blockquote>
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see <a href="http://tools.ietf.org/id/draft-merrick-jms-iri-00.txt">IRI Scheme for Java(tm) Message
|
||||
* Service 1.0</a>
|
||||
* @see <a href="http://tools.ietf.org/id/draft-merrick-jms-iri-00.txt">IRI Scheme for Java(tm) Message Service 1.0</a>
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public class JmsMessageSender extends JmsDestinationAccessor implements WebServiceMessageSender {
|
||||
@@ -86,10 +86,7 @@ public class JmsMessageSender extends JmsDestinationAccessor implements WebServi
|
||||
|
||||
private String textMessageEncoding = DEFAULT_TEXT_MESSAGE_ENCODING;
|
||||
|
||||
/** Sets the encoding used to read from {@link TextMessage} messages. Defaults to <code>UTF-8</code>. */
|
||||
public void setTextMessageEncoding(String textMessageEncoding) {
|
||||
this.textMessageEncoding = textMessageEncoding;
|
||||
}
|
||||
private MessagePostProcessor postProcessor;
|
||||
|
||||
/**
|
||||
* Create a new <code>JmsMessageSender</code>
|
||||
@@ -121,6 +118,19 @@ public class JmsMessageSender extends JmsDestinationAccessor implements WebServi
|
||||
this.receiveTimeout = receiveTimeout;
|
||||
}
|
||||
|
||||
/** Sets the encoding used to read from {@link TextMessage} messages. Defaults to <code>UTF-8</code>. */
|
||||
public void setTextMessageEncoding(String textMessageEncoding) {
|
||||
this.textMessageEncoding = textMessageEncoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the optional {@link MessagePostProcessor} to further modify outgoing messages after the XML contents has
|
||||
* been set.
|
||||
*/
|
||||
public void setPostProcessor(MessagePostProcessor postProcessor) {
|
||||
this.postProcessor = postProcessor;
|
||||
}
|
||||
|
||||
public WebServiceConnection createConnection(URI uri) throws IOException {
|
||||
Connection jmsConnection = null;
|
||||
Session jmsSession = null;
|
||||
@@ -129,14 +139,16 @@ public class JmsMessageSender extends JmsDestinationAccessor implements WebServi
|
||||
jmsSession = createSession(jmsConnection);
|
||||
Destination requestDestination = resolveRequestDestination(jmsSession, uri);
|
||||
Message requestMessage = createRequestMessage(jmsSession, uri);
|
||||
JmsSenderConnection wsConnection = new JmsSenderConnection(getConnectionFactory(), jmsConnection,
|
||||
jmsSession, requestDestination, requestMessage);
|
||||
JmsSenderConnection wsConnection =
|
||||
new JmsSenderConnection(getConnectionFactory(), jmsConnection, jmsSession, requestDestination,
|
||||
requestMessage);
|
||||
wsConnection.setDeliveryMode(JmsTransportUtils.getDeliveryMode(uri));
|
||||
wsConnection.setPriority(JmsTransportUtils.getPriority(uri));
|
||||
wsConnection.setReceiveTimeout(receiveTimeout);
|
||||
wsConnection.setResponseDestination(resolveResponseDestination(jmsSession, uri));
|
||||
wsConnection.setTimeToLive(JmsTransportUtils.getTimeToLive(uri));
|
||||
wsConnection.setTextMessageEncoding(textMessageEncoding);
|
||||
wsConnection.setPostProcessor(postProcessor);
|
||||
return wsConnection;
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
|
||||
@@ -29,6 +29,7 @@ import javax.jms.MessageProducer;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import org.springframework.jms.core.MessagePostProcessor;
|
||||
import org.springframework.jms.support.JmsUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
@@ -57,6 +58,8 @@ public class JmsReceiverConnection extends AbstractReceiverConnection {
|
||||
|
||||
private String textMessageEncoding;
|
||||
|
||||
private MessagePostProcessor postProcessor;
|
||||
|
||||
private JmsReceiverConnection(Message requestMessage, Session session) {
|
||||
Assert.notNull(requestMessage, "requestMessage must not be null");
|
||||
Assert.notNull(session, "session must not be null");
|
||||
@@ -85,6 +88,10 @@ public class JmsReceiverConnection extends AbstractReceiverConnection {
|
||||
this.textMessageEncoding = encoding;
|
||||
}
|
||||
|
||||
void setPostProcessor(MessagePostProcessor postProcessor) {
|
||||
this.postProcessor = postProcessor;
|
||||
}
|
||||
|
||||
/** Returns the request message for this connection. Returns either a {@link BytesMessage} or a {@link TextMessage}. */
|
||||
public Message getRequestMessage() {
|
||||
return requestMessage;
|
||||
@@ -207,6 +214,9 @@ public class JmsReceiverConnection extends AbstractReceiverConnection {
|
||||
messageProducer = session.createProducer(requestMessage.getJMSReplyTo());
|
||||
messageProducer.setDeliveryMode(requestMessage.getJMSDeliveryMode());
|
||||
messageProducer.setPriority(requestMessage.getJMSPriority());
|
||||
if (postProcessor != null) {
|
||||
responseMessage = postProcessor.postProcessMessage(responseMessage);
|
||||
}
|
||||
messageProducer.send(responseMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ import javax.jms.TemporaryTopic;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import org.springframework.jms.connection.ConnectionFactoryUtils;
|
||||
import org.springframework.jms.core.MessagePostProcessor;
|
||||
import org.springframework.jms.support.JmsUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
@@ -60,7 +61,7 @@ public class JmsSenderConnection extends AbstractSenderConnection {
|
||||
|
||||
private final Destination requestDestination;
|
||||
|
||||
private final Message requestMessage;
|
||||
private Message requestMessage;
|
||||
|
||||
private Destination responseDestination;
|
||||
|
||||
@@ -76,6 +77,8 @@ public class JmsSenderConnection extends AbstractSenderConnection {
|
||||
|
||||
private String textMessageEncoding;
|
||||
|
||||
private MessagePostProcessor postProcessor;
|
||||
|
||||
/** Constructs a new JMS connection with the given parameters. */
|
||||
protected JmsSenderConnection(ConnectionFactory connectionFactory,
|
||||
Connection connection,
|
||||
@@ -135,6 +138,10 @@ public class JmsSenderConnection extends AbstractSenderConnection {
|
||||
this.textMessageEncoding = textMessageEncoding;
|
||||
}
|
||||
|
||||
void setPostProcessor(MessagePostProcessor postProcessor) {
|
||||
this.postProcessor = postProcessor;
|
||||
}
|
||||
|
||||
/*
|
||||
* URI
|
||||
*/
|
||||
@@ -197,6 +204,9 @@ public class JmsSenderConnection extends AbstractSenderConnection {
|
||||
responseDestination = session.createTemporaryQueue();
|
||||
}
|
||||
requestMessage.setJMSReplyTo(responseDestination);
|
||||
if (postProcessor != null) {
|
||||
requestMessage = postProcessor.postProcessMessage(requestMessage);
|
||||
}
|
||||
connection.start();
|
||||
messageProducer.send(requestMessage);
|
||||
}
|
||||
@@ -296,5 +306,4 @@ public class JmsSenderConnection extends AbstractSenderConnection {
|
||||
JmsUtils.closeSession(session);
|
||||
ConnectionFactoryUtils.releaseConnection(connection, connectionFactory, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import javax.xml.soap.SOAPConstants;
|
||||
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.jms.core.MessageCreator;
|
||||
import org.springframework.jms.core.MessagePostProcessor;
|
||||
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.SoapVersion;
|
||||
@@ -160,4 +161,30 @@ public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjection
|
||||
}
|
||||
}
|
||||
|
||||
public void testPostProcessor() throws Exception {
|
||||
MessagePostProcessor processor = new MessagePostProcessor() {
|
||||
public Message postProcessMessage(Message message) throws JMSException {
|
||||
message.setBooleanProperty("processed", true);
|
||||
return message;
|
||||
}
|
||||
};
|
||||
JmsSenderConnection connection = null;
|
||||
try {
|
||||
URI uri = new URI("jms:SenderRequestQueue?deliveryMode=NON_PERSISTENT");
|
||||
connection = (JmsSenderConnection) messageSender.createConnection(uri);
|
||||
connection.setPostProcessor(processor);
|
||||
SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
|
||||
connection.send(soapRequest);
|
||||
|
||||
BytesMessage request = (BytesMessage) jmsTemplate.receive();
|
||||
assertNotNull("No message received", request);
|
||||
assertTrue("Message not processed", request.getBooleanProperty("processed"));
|
||||
}
|
||||
finally {
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user