1616
1717package org .springframework .ws .transport .jms ;
1818
19+ import java .io .ByteArrayInputStream ;
20+ import java .io .ByteArrayOutputStream ;
1921import java .io .IOException ;
2022import java .io .InputStream ;
2123import java .io .OutputStream ;
2224import java .util .Collections ;
2325import java .util .Iterator ;
2426import javax .jms .BytesMessage ;
2527import javax .jms .JMSException ;
28+ import javax .jms .Message ;
2629import javax .jms .MessageProducer ;
2730import javax .jms .Session ;
31+ import javax .jms .TextMessage ;
2832
2933import org .springframework .jms .support .JmsUtils ;
3034import org .springframework .util .Assert ;
3539
3640/**
3741 * Implementation of {@link WebServiceConnection} that is used for server-side JMS access. Exposes a {@link
38- * BytesMessage} request and response message.
42+ * BytesMessage} or {@link TextMessage} request and response message.
43+ * <p/>
44+ * The response message type is equal to the request message type, i.e. if a <code>BytesMessage</code> is received as
45+ * request, a <code>BytesMessage</code> is created as response, and if a <code>TextMessage</code> is received, a
46+ * <code>TextMessage</code> response is created.
3947 *
4048 * @author Arjen Poutsma
4149 * @since 1.5.0
4250 */
4351public class JmsReceiverConnection extends AbstractReceiverConnection {
4452
45- private final BytesMessage requestMessage ;
53+ private final Message requestMessage ;
4654
4755 private final Session session ;
4856
49- private BytesMessage responseMessage ;
57+ private Message responseMessage ;
5058
51- /** Constructs a new JMS connection with the given parameters. */
52- protected JmsReceiverConnection (BytesMessage requestMessage , Session session ) {
59+ private String textMessageEncoding ;
60+
61+ private JmsReceiverConnection (Message requestMessage , Session session ) {
5362 Assert .notNull (requestMessage , "requestMessage must not be null" );
5463 Assert .notNull (session , "session must not be null" );
5564 this .requestMessage = requestMessage ;
5665 this .session = session ;
5766 }
5867
59- /** Returns the request message for this connection. */
60- public BytesMessage getRequestMessage () {
68+ /**
69+ * Constructs a new JMS connection with the given {@link BytesMessage}.
70+ *
71+ * @param requestMessage the JMS request message
72+ * @param session the JMS session
73+ */
74+ protected JmsReceiverConnection (BytesMessage requestMessage , Session session ) {
75+ this ((Message ) requestMessage , session );
76+ }
77+
78+ /**
79+ * Constructs a new JMS connection with the given {@link TextMessage}.
80+ *
81+ * @param requestMessage the JMS request message
82+ * @param session the JMS session
83+ */
84+ protected JmsReceiverConnection (TextMessage requestMessage , String encoding , Session session ) {
85+ this (requestMessage , session );
86+ this .textMessageEncoding = encoding ;
87+ }
88+
89+ /** Returns the request message for this connection. Returns either a {@link BytesMessage} or a {@link TextMessage}. */
90+ public Message getRequestMessage () {
6191 return requestMessage ;
6292 }
6393
64- /** Returns the response message, if any, for this connection. */
65- public BytesMessage getResponseMessage () {
94+ /**
95+ * Returns the response message, if any, for this connection. Returns either a {@link BytesMessage} or a {@link
96+ * TextMessage}.
97+ */
98+ public Message getResponseMessage () {
6699 return responseMessage ;
67100 }
68101
102+ /*
103+ * Errors
104+ */
105+
69106 public String getErrorMessage () throws IOException {
70107 return null ;
71108 }
@@ -98,7 +135,20 @@ protected Iterator getRequestHeaders(String name) throws IOException {
98135 }
99136
100137 protected InputStream getRequestInputStream () throws IOException {
101- return new BytesMessageInputStream (requestMessage );
138+ if (requestMessage instanceof BytesMessage ) {
139+ return new BytesMessageInputStream ((BytesMessage ) requestMessage );
140+ }
141+ else {
142+ TextMessage textMessage = (TextMessage ) requestMessage ;
143+ try {
144+ String text = textMessage .getText ();
145+ byte [] contents = text != null ? text .getBytes (textMessageEncoding ) : new byte [0 ];
146+ return new ByteArrayInputStream (contents );
147+ }
148+ catch (JMSException ex ) {
149+ throw new JmsTransportException (ex );
150+ }
151+ }
102152 }
103153
104154 /*
@@ -107,7 +157,12 @@ protected InputStream getRequestInputStream() throws IOException {
107157
108158 protected void onSendBeforeWrite (WebServiceMessage message ) throws IOException {
109159 try {
110- responseMessage = session .createBytesMessage ();
160+ if (requestMessage instanceof BytesMessage ) {
161+ responseMessage = session .createBytesMessage ();
162+ }
163+ else {
164+ responseMessage = session .createTextMessage ();
165+ }
111166 responseMessage .setJMSCorrelationID (requestMessage .getJMSMessageID ());
112167 }
113168 catch (JMSException ex ) {
@@ -125,7 +180,23 @@ protected void addResponseHeader(String name, String value) throws IOException {
125180 }
126181
127182 protected OutputStream getResponseOutputStream () throws IOException {
128- return new BytesMessageOutputStream (responseMessage );
183+ if (responseMessage instanceof BytesMessage ) {
184+ return new BytesMessageOutputStream ((BytesMessage ) responseMessage );
185+ }
186+ else {
187+ return new ByteArrayOutputStream () {
188+
189+ public void close () throws IOException {
190+ String text = new String (toByteArray (), textMessageEncoding );
191+ try {
192+ ((TextMessage ) responseMessage ).setText (text );
193+ }
194+ catch (JMSException ex ) {
195+ throw new JmsTransportException (ex );
196+ }
197+ }
198+ };
199+ }
129200 }
130201
131202 protected void onSendAfterWrite (WebServiceMessage message ) throws IOException {
0 commit comments