From 63446dc6ee848bdc6854725df010b38698aaadb5 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 5 Jun 2008 13:41:36 +0000 Subject: [PATCH] SWS-367 --- .../transport/jms/JmsReceiverConnection.java | 27 +-------- .../ws/transport/jms/JmsSenderConnection.java | 38 +++++------- .../transport/jms/TextMessageInputStream.java | 52 +++++++++++++++++ .../jms/TextMessageOutputStream.java | 58 +++++++++++++++++++ .../jms/JmsMessageSenderIntegrationTest.java | 2 + 5 files changed, 128 insertions(+), 49 deletions(-) create mode 100644 support/src/main/java/org/springframework/ws/transport/jms/TextMessageInputStream.java create mode 100644 support/src/main/java/org/springframework/ws/transport/jms/TextMessageOutputStream.java diff --git a/support/src/main/java/org/springframework/ws/transport/jms/JmsReceiverConnection.java b/support/src/main/java/org/springframework/ws/transport/jms/JmsReceiverConnection.java index 394a7786..cf7366e6 100644 --- a/support/src/main/java/org/springframework/ws/transport/jms/JmsReceiverConnection.java +++ b/support/src/main/java/org/springframework/ws/transport/jms/JmsReceiverConnection.java @@ -16,8 +16,6 @@ package org.springframework.ws.transport.jms; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -152,15 +150,7 @@ public class JmsReceiverConnection extends AbstractReceiverConnection { return new BytesMessageInputStream((BytesMessage) requestMessage); } else if (requestMessage instanceof TextMessage) { - TextMessage textMessage = (TextMessage) requestMessage; - try { - String text = textMessage.getText(); - byte[] contents = text != null ? text.getBytes(textMessageEncoding) : new byte[0]; - return new ByteArrayInputStream(contents); - } - catch (JMSException ex) { - throw new JmsTransportException(ex); - } + return new TextMessageInputStream((TextMessage) requestMessage, textMessageEncoding); } else { throw new IllegalStateException("Unknown request message type [" + requestMessage + "]"); @@ -203,21 +193,10 @@ public class JmsReceiverConnection extends AbstractReceiverConnection { return new BytesMessageOutputStream((BytesMessage) responseMessage); } else if (responseMessage instanceof TextMessage) { - return new ByteArrayOutputStream() { - - public void close() throws IOException { - String text = new String(toByteArray(), textMessageEncoding); - try { - ((TextMessage) responseMessage).setText(text); - } - catch (JMSException ex) { - throw new JmsTransportException(ex); - } - } - }; + return new TextMessageOutputStream((TextMessage) responseMessage, textMessageEncoding); } else { - throw new IllegalStateException("Unknown request message type [" + responseMessage + "]"); + throw new IllegalStateException("Unknown response message type [" + responseMessage + "]"); } } diff --git a/support/src/main/java/org/springframework/ws/transport/jms/JmsSenderConnection.java b/support/src/main/java/org/springframework/ws/transport/jms/JmsSenderConnection.java index 475819b4..d6528fe5 100644 --- a/support/src/main/java/org/springframework/ws/transport/jms/JmsSenderConnection.java +++ b/support/src/main/java/org/springframework/ws/transport/jms/JmsSenderConnection.java @@ -16,8 +16,6 @@ package org.springframework.ws.transport.jms; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -178,20 +176,13 @@ public class JmsSenderConnection extends AbstractSenderConnection { if (requestMessage instanceof BytesMessage) { return new BytesMessageOutputStream((BytesMessage) requestMessage); } - else { - return new ByteArrayOutputStream() { - - public void close() throws IOException { - String text = new String(toByteArray(), textMessageEncoding); - try { - ((TextMessage) requestMessage).setText(text); - } - catch (JMSException ex) { - throw new JmsTransportException(ex); - } - } - }; + else if (requestMessage instanceof TextMessage) { + return new TextMessageOutputStream((TextMessage) requestMessage, textMessageEncoding); } + else { + throw new IllegalStateException("Unknown request message type [" + requestMessage + "]"); + } + } protected void onSendAfterWrite(WebServiceMessage message) throws IOException { @@ -275,17 +266,14 @@ public class JmsSenderConnection extends AbstractSenderConnection { if (responseMessage instanceof BytesMessage) { return new BytesMessageInputStream((BytesMessage) responseMessage); } - else { - TextMessage textMessage = (TextMessage) responseMessage; - try { - String text = textMessage.getText(); - byte[] contents = text != null ? text.getBytes(textMessageEncoding) : new byte[0]; - return new ByteArrayInputStream(contents); - } - catch (JMSException ex) { - throw new JmsTransportException(ex); - } + else if (responseMessage instanceof TextMessage) { + return new TextMessageInputStream((TextMessage) responseMessage, textMessageEncoding); } + else { + throw new IllegalStateException("Unknown response message type [" + responseMessage + "]"); + } + + } protected void onClose() throws IOException { diff --git a/support/src/main/java/org/springframework/ws/transport/jms/TextMessageInputStream.java b/support/src/main/java/org/springframework/ws/transport/jms/TextMessageInputStream.java new file mode 100644 index 00000000..69696091 --- /dev/null +++ b/support/src/main/java/org/springframework/ws/transport/jms/TextMessageInputStream.java @@ -0,0 +1,52 @@ +/* + * Copyright 2008 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 java.io.ByteArrayInputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import javax.jms.JMSException; +import javax.jms.TextMessage; + +import org.springframework.util.Assert; + +/** + * Input stream that wraps a {@link javax.jms.TextMessage}. + * + * @author Arjen Poutsma + * @since 1.5.3 + */ +class TextMessageInputStream extends FilterInputStream { + + TextMessageInputStream(TextMessage message, String encoding) throws IOException { + super(createInputStream(message, encoding)); + } + + private static InputStream createInputStream(TextMessage message, String encoding) throws IOException { + Assert.notNull(message, "'message' must not be null"); + Assert.notNull(encoding, "'encoding' must not be null"); + try { + String text = message.getText(); + byte[] contents = text != null ? text.getBytes(encoding) : new byte[0]; + return new ByteArrayInputStream(contents); + } + catch (JMSException ex) { + throw new JmsTransportException(ex); + } + } +} diff --git a/support/src/main/java/org/springframework/ws/transport/jms/TextMessageOutputStream.java b/support/src/main/java/org/springframework/ws/transport/jms/TextMessageOutputStream.java new file mode 100644 index 00000000..bec30c0f --- /dev/null +++ b/support/src/main/java/org/springframework/ws/transport/jms/TextMessageOutputStream.java @@ -0,0 +1,58 @@ +/* + * Copyright 2008 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 java.io.ByteArrayOutputStream; +import java.io.FilterOutputStream; +import java.io.IOException; +import javax.jms.JMSException; +import javax.jms.TextMessage; + +import org.springframework.util.Assert; + +/** + * Writer that wraps a {@link javax.jms.TextMessage}. + * + * @author Arjen Poutsma + * @since 1.5.3 + */ +class TextMessageOutputStream extends FilterOutputStream { + + private final TextMessage message; + + private final String encoding; + + TextMessageOutputStream(TextMessage message, String encoding) { + super(new ByteArrayOutputStream()); + Assert.notNull(message, "'message' must not be null"); + Assert.notNull(encoding, "'encoding' must not be null"); + this.message = message; + this.encoding = encoding; + } + + public void flush() throws IOException { + super.flush(); + try { + ByteArrayOutputStream baos = (ByteArrayOutputStream) out; + String text = new String(baos.toByteArray(), encoding); + message.setText(text); + } + catch (JMSException ex) { + throw new JmsTransportException(ex); + } + } +} diff --git a/support/src/test/java/org/springframework/ws/transport/jms/JmsMessageSenderIntegrationTest.java b/support/src/test/java/org/springframework/ws/transport/jms/JmsMessageSenderIntegrationTest.java index 8b7384b0..638643c1 100644 --- a/support/src/test/java/org/springframework/ws/transport/jms/JmsMessageSenderIntegrationTest.java +++ b/support/src/test/java/org/springframework/ws/transport/jms/JmsMessageSenderIntegrationTest.java @@ -72,6 +72,7 @@ public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjection BytesMessage request = (BytesMessage) jmsTemplate.receive(); assertNotNull("No message received", request); + assertTrue("No message content received", request.readByte() != -1); ByteArrayOutputStream bos = new ByteArrayOutputStream(); messageFactory.createMessage().writeTo(bos); final byte[] buf = bos.toByteArray(); @@ -109,6 +110,7 @@ public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjection TextMessage request = (TextMessage) jmsTemplate.receive(); assertNotNull("No message received", request); + assertNotNull("No message content received", request.getText()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); messageFactory.createMessage().writeTo(bos); final String text = new String(bos.toByteArray(), "UTF-8");