This commit is contained in:
Arjen Poutsma
2008-06-05 13:41:36 +00:00
parent b8f46c6229
commit 63446dc6ee
5 changed files with 128 additions and 49 deletions

View File

@@ -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 + "]");
}
}

View File

@@ -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 {

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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");