16
16
17
17
package org .springframework .ws .transport .jms ;
18
18
19
+ import java .io .ByteArrayInputStream ;
20
+ import java .io .ByteArrayOutputStream ;
19
21
import java .io .IOException ;
20
22
import java .io .InputStream ;
21
23
import java .io .OutputStream ;
22
24
import java .util .Collections ;
23
25
import java .util .Iterator ;
24
26
import javax .jms .BytesMessage ;
25
27
import javax .jms .JMSException ;
28
+ import javax .jms .Message ;
26
29
import javax .jms .MessageProducer ;
27
30
import javax .jms .Session ;
31
+ import javax .jms .TextMessage ;
28
32
29
33
import org .springframework .jms .support .JmsUtils ;
30
34
import org .springframework .util .Assert ;
35
39
36
40
/**
37
41
* 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.
39
47
*
40
48
* @author Arjen Poutsma
41
49
* @since 1.5.0
42
50
*/
43
51
public class JmsReceiverConnection extends AbstractReceiverConnection {
44
52
45
- private final BytesMessage requestMessage ;
53
+ private final Message requestMessage ;
46
54
47
55
private final Session session ;
48
56
49
- private BytesMessage responseMessage ;
57
+ private Message responseMessage ;
50
58
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 ) {
53
62
Assert .notNull (requestMessage , "requestMessage must not be null" );
54
63
Assert .notNull (session , "session must not be null" );
55
64
this .requestMessage = requestMessage ;
56
65
this .session = session ;
57
66
}
58
67
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 () {
61
91
return requestMessage ;
62
92
}
63
93
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 () {
66
99
return responseMessage ;
67
100
}
68
101
102
+ /*
103
+ * Errors
104
+ */
105
+
69
106
public String getErrorMessage () throws IOException {
70
107
return null ;
71
108
}
@@ -98,7 +135,20 @@ protected Iterator getRequestHeaders(String name) throws IOException {
98
135
}
99
136
100
137
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
+ }
102
152
}
103
153
104
154
/*
@@ -107,7 +157,12 @@ protected InputStream getRequestInputStream() throws IOException {
107
157
108
158
protected void onSendBeforeWrite (WebServiceMessage message ) throws IOException {
109
159
try {
110
- responseMessage = session .createBytesMessage ();
160
+ if (requestMessage instanceof BytesMessage ) {
161
+ responseMessage = session .createBytesMessage ();
162
+ }
163
+ else {
164
+ responseMessage = session .createTextMessage ();
165
+ }
111
166
responseMessage .setJMSCorrelationID (requestMessage .getJMSMessageID ());
112
167
}
113
168
catch (JMSException ex ) {
@@ -125,7 +180,23 @@ protected void addResponseHeader(String name, String value) throws IOException {
125
180
}
126
181
127
182
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
+ }
129
200
}
130
201
131
202
protected void onSendAfterWrite (WebServiceMessage message ) throws IOException {
0 commit comments