Updated JMS transport to conform to SOAP over JMS SPec.

This commit is contained in:
Arjen Poutsma
2007-06-07 02:10:00 +00:00
parent b525752ec7
commit c86c619f56
17 changed files with 914 additions and 431 deletions

View File

@@ -18,113 +18,113 @@ package org.springframework.ws.transport.jms;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.jms.BytesMessage;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPMessage;
import junit.framework.TestCase;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springframework.util.FileCopyUtils;
import org.springframework.ws.transport.TransportInputStream;
import org.springframework.ws.transport.TransportOutputStream;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.springframework.ws.transport.WebServiceConnection;
public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjectionSpringContextTests {
private static final String REQUEST_HEADER_NAME = "RequestHeader";
private static final String REQUEST_HEADER_VALUE = "RequestHeaderValue";
private static final String RESPONSE_HEADER_NAME = "ResponseHeader";
private static final String RESPONSE_HEADER_VALUE = "ResponseHeaderValue";
private static final String REQUEST = "Request";
private static final String RESPONSE = "Response";
public class JmsMessageSenderIntegrationTest extends TestCase {
private JmsMessageSender messageSender;
private JmsTemplate jmsTemplate;
public void setMessageSender(JmsMessageSender messageSender) {
this.messageSender = messageSender;
private MessageFactory messageFactory;
private static final String URI = "jms:RequestQueue";
private static final String SOAP_ACTION = "http://springframework.org/DoIt";
protected void setUp() throws Exception {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
jmsTemplate = new JmsTemplate(connectionFactory);
jmsTemplate.setDefaultDestinationName("RequestQueue");
messageSender = new JmsMessageSender(connectionFactory);
messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
protected String[] getConfigLocations() {
return new String[]{"classpath:org/springframework/ws/transport/jms/jms-sender-applicationContext.xml"};
}
public void testSendAndReceiveNoResponse() throws Exception {
WebServiceConnection wsConnection = null;
public void testSendAndReceiveQueueNoResponse() throws Exception {
WebServiceConnection connection = null;
try {
wsConnection = messageSender.createConnection();
TransportOutputStream tos = wsConnection.getTransportOutputStream();
tos.addHeader("Content-Type", "text/xml");
tos.addHeader(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE);
FileCopyUtils.copy(REQUEST.getBytes("UTF-8"), tos);
BytesMessage request = (BytesMessage) jmsTemplate.receive();
assertEquals("Invalid header value received on server side", REQUEST_HEADER_VALUE,
request.getStringProperty(REQUEST_HEADER_NAME));
assertEquals("Invalid request received", REQUEST, getMessageContents(request));
assertNull("Response", wsConnection.getTransportInputStream());
connection = messageSender.createConnection(URI);
SOAPMessage saajMessage = messageFactory.createMessage();
SoapMessage soapRequest = new SaajSoapMessage(saajMessage);
soapRequest.setSoapAction(SOAP_ACTION);
connection.send(soapRequest);
BytesMessage jmsRequest = (BytesMessage) jmsTemplate.receive();
validateMessage(jmsRequest);
}
finally {
if (wsConnection != null) {
wsConnection.close();
if (connection != null) {
connection.close();
}
}
}
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));
assertEquals("Invalid service IRI", URI, message.getStringProperty(JmsTransportConstants.PROPERTY_REQUEST_IRI));
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);
}
public void testSendAndReceiveResponse() throws Exception {
WebServiceConnection wsConnection = null;
WebServiceConnection connection = null;
try {
wsConnection = messageSender.createConnection();
TransportOutputStream tos = wsConnection.getTransportOutputStream();
tos.addHeader("Content-Type", "text/xml");
tos.addHeader(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE);
FileCopyUtils.copy(REQUEST.getBytes("UTF-8"), tos);
connection = messageSender.createConnection(URI);
SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
soapRequest.setSoapAction(SOAP_ACTION);
connection.send(soapRequest);
BytesMessage request = (BytesMessage) jmsTemplate.receive();
assertEquals("Invalid header value received on server side", REQUEST_HEADER_VALUE,
request.getStringProperty(REQUEST_HEADER_NAME));
assertEquals("Invalid request received", REQUEST, getMessageContents(request));
final byte[] bytes = RESPONSE.getBytes("UTF-8");
validateMessage(request);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
messageFactory.createMessage().writeTo(bos);
final byte[] buf = bos.toByteArray();
jmsTemplate.send(request.getJMSReplyTo(), new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
BytesMessage response = session.createBytesMessage();
response.setStringProperty(RESPONSE_HEADER_NAME, RESPONSE_HEADER_VALUE);
response.writeBytes(bytes);
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, URI);
response.setStringProperty(JmsTransportConstants.PROPERTY_SOAP_ACTION, SOAP_ACTION);
response.writeBytes(buf);
return response;
}
});
assertNotNull("No response", wsConnection.getTransportInputStream());
TransportInputStream tis = wsConnection.getTransportInputStream();
boolean headerFound = false;
for (Iterator iterator = tis.getHeaderNames(); iterator.hasNext();) {
String headerName = (String) iterator.next();
if (RESPONSE_HEADER_NAME.equals(headerName)) {
headerFound = true;
}
}
assertTrue("Response has invalid header", headerFound);
Iterator headerValues = tis.getHeaders(RESPONSE_HEADER_NAME);
assertTrue("Response has no header values", headerValues.hasNext());
assertEquals("Response has invalid header values", RESPONSE_HEADER_VALUE, headerValues.next());
String result = new String(FileCopyUtils.copyToByteArray(tis), "UTF-8");
assertEquals("Invalid response", RESPONSE, result);
SoapMessage response = (SoapMessage) connection.receive(new SaajSoapMessageFactory(messageFactory));
assertNotNull("No response received", response);
assertEquals("Invalid SOAPAction", SOAP_ACTION, response.getSoapAction());
assertFalse("Message is fault", response.hasFault());
}
finally {
if (wsConnection != null) {
wsConnection.close();
if (connection != null) {
connection.close();
}
}
}
@@ -139,5 +139,4 @@ public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjection
out.flush();
return out.toString("UTF-8");
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2007 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport.jms;
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);
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2007 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport.jms;
import junit.framework.TestCase;
public class JmsUriTest extends TestCase {
public void testJmsUri() {
JmsUri uri = new JmsUri("jms:news?connectionFactoryName=SOAPJMSFactory&" + "deliveryMode=2&" +
"destinationType=topic&" + "initialContextFactory=com.sun.jndi.ldap.LdapCtxFactory&" +
"jndiURL=theJndiURL&" + "priority=8&" + "timeToLive=10&" + "replyToName=interested&" +
"userprop=mystuff");
assertEquals("Invalid connection factory name", "SOAPJMSFactory", uri.getConnectionFactoryName());
assertEquals("Invalid delivery mode", 2, uri.getDeliveryMode());
assertEquals("Invalid destination", "news", uri.getDestination());
assertEquals("Invalid destination type", "topic", uri.getDestinationType());
assertTrue("Invalid pub sub domain", uri.isPubSubDomain());
assertEquals("Invalid initial context factory", "com.sun.jndi.ldap.LdapCtxFactory",
uri.getInitialContextFactory());
assertEquals("Invalid prority", 8, uri.getPriority());
assertEquals("Invalid time to live", 10, uri.getTimeToLive());
assertEquals("Invalid reply to name", "interested", uri.getReplyTo());
assertEquals("Invalid custom property", "mystuff", uri.getCustomParameter("userprop"));
}
public void testGetDestinationNoParams() {
JmsUri uri = new JmsUri("jms:news");
assertEquals("Invalid destination", "news", uri.getDestination());
}
public void testInvalidDeliveryMode() {
testIllegalArgument("jms:news?deliveryMode=abc");
}
public void testInvalidPriority() {
testIllegalArgument("jms:news?priority=abc");
}
public void testInvalidTimeToLive() {
testIllegalArgument("jms:news?timeToLive=abc");
}
public void testInvalidDestinationType() {
testIllegalArgument("jms:news?destinationType=abc");
}
public void testEmpty() {
testIllegalArgument("");
}
public void testInvalidScheme() {
testIllegalArgument("http://localhost");
}
public void testNoDestination() {
testIllegalArgument("jms:");
}
public void testIllegalParam() {
testIllegalArgument("jms:news?bla");
}
private void testIllegalArgument(String uri) {
try {
new JmsUri(uri);
fail("Expected IllegalArgumentException for uri [" + uri + "]");
}
catch (IllegalArgumentException ex) {
//expected
}
}
}

View File

@@ -41,7 +41,7 @@ public class MessageEndpointMessageListenerTest extends TestCase {
" <m:GetLastTradePrice xmlns:m=\"Some-URI\">\n" + " <symbol>DIS</symbol>\n" +
" </m:GetLastTradePrice>\n" + " </SOAP-ENV:Body>\n" + "</SOAP-ENV:Envelope>";
private WebServiceMessageReceiverMessageListener messageListener;
private WebServiceMessageListener messageListener;
private BytesMessage request;
@@ -50,7 +50,7 @@ public class MessageEndpointMessageListenerTest extends TestCase {
private Session sessionMock;
protected void setUp() throws Exception {
messageListener = new WebServiceMessageReceiverMessageListener();
messageListener = new WebServiceMessageListener();
request = new ActiveMQBytesMessage();
request.writeBytes(REQUEST.getBytes("UTF-8"));
messageListener.setMessageFactory(new MockWebServiceMessageFactory());

View File

@@ -21,7 +21,7 @@
<property name="messageListener" ref="messageListener"/>
</bean>
<bean id="messageListener" class="org.springframework.ws.transport.jms.WebServiceMessageReceiverMessageListener">
<bean id="messageListener" class="org.springframework.ws.transport.jms.WebServiceMessageListener">
<property name="messageFactory">
<bean class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>
</property>

View File

@@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost?broker.persistent=false"/>
</bean>
<bean id="messageSender" class="org.springframework.ws.transport.jms.JmsMessageSender">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="queueName" value="RequestQueue"/>
<property name="receiveTimeout" value="5"/>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestinationName" value="RequestQueue"/>
</bean>
</beans>