1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.transport.jms;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.util.Iterator;
25 import javax.jms.BytesMessage;
26 import javax.jms.JMSException;
27 import javax.jms.Message;
28 import javax.jms.MessageProducer;
29 import javax.jms.Session;
30 import javax.jms.TextMessage;
31
32 import org.springframework.jms.core.MessagePostProcessor;
33 import org.springframework.jms.support.JmsUtils;
34 import org.springframework.util.Assert;
35 import org.springframework.ws.WebServiceMessage;
36 import org.springframework.ws.transport.AbstractReceiverConnection;
37 import org.springframework.ws.transport.WebServiceConnection;
38 import org.springframework.ws.transport.jms.support.JmsTransportUtils;
39
40
41
42
43
44
45
46
47
48
49
50
51 public class JmsReceiverConnection extends AbstractReceiverConnection {
52
53 private final Message requestMessage;
54
55 private final Session session;
56
57 private Message responseMessage;
58
59 private String textMessageEncoding;
60
61 private MessagePostProcessor postProcessor;
62
63 private JmsReceiverConnection(Message requestMessage, Session session) {
64 Assert.notNull(requestMessage, "requestMessage must not be null");
65 Assert.notNull(session, "session must not be null");
66 this.requestMessage = requestMessage;
67 this.session = session;
68 }
69
70
71
72
73
74
75
76 protected JmsReceiverConnection(BytesMessage requestMessage, Session session) {
77 this((Message) requestMessage, session);
78 }
79
80
81
82
83
84
85
86 protected JmsReceiverConnection(TextMessage requestMessage, String encoding, Session session) {
87 this(requestMessage, session);
88 this.textMessageEncoding = encoding;
89 }
90
91 void setPostProcessor(MessagePostProcessor postProcessor) {
92 this.postProcessor = postProcessor;
93 }
94
95
96
97
98 public Message getRequestMessage() {
99 return requestMessage;
100 }
101
102
103
104
105
106 public Message getResponseMessage() {
107 return responseMessage;
108 }
109
110
111
112
113
114 public URI getUri() throws URISyntaxException {
115 try {
116 return JmsTransportUtils.toUri(requestMessage.getJMSDestination());
117 }
118 catch (JMSException ex) {
119 throw new URISyntaxException("", ex.getMessage());
120 }
121 }
122
123
124
125
126
127 public String getErrorMessage() throws IOException {
128 return null;
129 }
130
131 public boolean hasError() throws IOException {
132 return false;
133 }
134
135
136
137
138
139 @Override
140 protected Iterator<String> getRequestHeaderNames() throws IOException {
141 try {
142 return JmsTransportUtils.getHeaderNames(requestMessage);
143 }
144 catch (JMSException ex) {
145 throw new JmsTransportException("Could not get property names", ex);
146 }
147 }
148
149 @Override
150 protected Iterator<String> getRequestHeaders(String name) throws IOException {
151 try {
152 return JmsTransportUtils.getHeaders(requestMessage, name);
153 }
154 catch (JMSException ex) {
155 throw new JmsTransportException("Could not get property value", ex);
156 }
157 }
158
159 @Override
160 protected InputStream getRequestInputStream() throws IOException {
161 if (requestMessage instanceof BytesMessage) {
162 return new BytesMessageInputStream((BytesMessage) requestMessage);
163 }
164 else if (requestMessage instanceof TextMessage) {
165 return new TextMessageInputStream((TextMessage) requestMessage, textMessageEncoding);
166 }
167 else {
168 throw new IllegalStateException("Unknown request message type [" + requestMessage + "]");
169 }
170 }
171
172
173
174
175
176 @Override
177 protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
178 try {
179 if (requestMessage instanceof BytesMessage) {
180 responseMessage = session.createBytesMessage();
181 }
182 else if (requestMessage instanceof TextMessage) {
183 responseMessage = session.createTextMessage();
184 }
185 else {
186 throw new IllegalStateException("Unknown request message type [" + requestMessage + "]");
187 }
188 String correlation = requestMessage.getJMSCorrelationID();
189 if (correlation == null) {
190 correlation = requestMessage.getJMSMessageID();
191 }
192 responseMessage.setJMSCorrelationID(correlation);
193 }
194 catch (JMSException ex) {
195 throw new JmsTransportException("Could not create response message", ex);
196 }
197 }
198
199 @Override
200 protected void addResponseHeader(String name, String value) throws IOException {
201 try {
202 JmsTransportUtils.addHeader(responseMessage, name, value);
203 }
204 catch (JMSException ex) {
205 throw new JmsTransportException("Could not set property", ex);
206 }
207 }
208
209 @Override
210 protected OutputStream getResponseOutputStream() throws IOException {
211 if (responseMessage instanceof BytesMessage) {
212 return new BytesMessageOutputStream((BytesMessage) responseMessage);
213 }
214 else if (responseMessage instanceof TextMessage) {
215 return new TextMessageOutputStream((TextMessage) responseMessage, textMessageEncoding);
216 }
217 else {
218 throw new IllegalStateException("Unknown response message type [" + responseMessage + "]");
219 }
220 }
221
222 @Override
223 protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
224 MessageProducer messageProducer = null;
225 try {
226 if (requestMessage.getJMSReplyTo() != null) {
227 messageProducer = session.createProducer(requestMessage.getJMSReplyTo());
228 messageProducer.setDeliveryMode(requestMessage.getJMSDeliveryMode());
229 messageProducer.setPriority(requestMessage.getJMSPriority());
230 if (postProcessor != null) {
231 responseMessage = postProcessor.postProcessMessage(responseMessage);
232 }
233 messageProducer.send(responseMessage);
234 }
235 }
236 catch (JMSException ex) {
237 throw new JmsTransportException(ex);
238 }
239 finally {
240 JmsUtils.closeMessageProducer(messageProducer);
241 }
242 }
243
244 }