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 protected Iterator getRequestHeaderNames() throws IOException {
140 try {
141 return JmsTransportUtils.getHeaderNames(requestMessage);
142 }
143 catch (JMSException ex) {
144 throw new JmsTransportException("Could not get property names", ex);
145 }
146 }
147
148 protected Iterator getRequestHeaders(String name) throws IOException {
149 try {
150 return JmsTransportUtils.getHeaders(requestMessage, name);
151 }
152 catch (JMSException ex) {
153 throw new JmsTransportException("Could not get property value", ex);
154 }
155 }
156
157 protected InputStream getRequestInputStream() throws IOException {
158 if (requestMessage instanceof BytesMessage) {
159 return new BytesMessageInputStream((BytesMessage) requestMessage);
160 }
161 else if (requestMessage instanceof TextMessage) {
162 return new TextMessageInputStream((TextMessage) requestMessage, textMessageEncoding);
163 }
164 else {
165 throw new IllegalStateException("Unknown request message type [" + requestMessage + "]");
166 }
167 }
168
169
170
171
172
173 protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
174 try {
175 if (requestMessage instanceof BytesMessage) {
176 responseMessage = session.createBytesMessage();
177 }
178 else if (requestMessage instanceof TextMessage) {
179 responseMessage = session.createTextMessage();
180 }
181 else {
182 throw new IllegalStateException("Unknown request message type [" + requestMessage + "]");
183 }
184 String correlation = requestMessage.getJMSCorrelationID();
185 if (correlation == null) {
186 correlation = requestMessage.getJMSMessageID();
187 }
188 responseMessage.setJMSCorrelationID(correlation);
189 }
190 catch (JMSException ex) {
191 throw new JmsTransportException("Could not create response message", ex);
192 }
193 }
194
195 protected void addResponseHeader(String name, String value) throws IOException {
196 try {
197 JmsTransportUtils.addHeader(responseMessage, name, value);
198 }
199 catch (JMSException ex) {
200 throw new JmsTransportException("Could not set property", ex);
201 }
202 }
203
204 protected OutputStream getResponseOutputStream() throws IOException {
205 if (responseMessage instanceof BytesMessage) {
206 return new BytesMessageOutputStream((BytesMessage) responseMessage);
207 }
208 else if (responseMessage instanceof TextMessage) {
209 return new TextMessageOutputStream((TextMessage) responseMessage, textMessageEncoding);
210 }
211 else {
212 throw new IllegalStateException("Unknown response message type [" + responseMessage + "]");
213 }
214 }
215
216 protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
217 MessageProducer messageProducer = null;
218 try {
219 if (requestMessage.getJMSReplyTo() != null) {
220 messageProducer = session.createProducer(requestMessage.getJMSReplyTo());
221 messageProducer.setDeliveryMode(requestMessage.getJMSDeliveryMode());
222 messageProducer.setPriority(requestMessage.getJMSPriority());
223 if (postProcessor != null) {
224 responseMessage = postProcessor.postProcessMessage(responseMessage);
225 }
226 messageProducer.send(responseMessage);
227 }
228 }
229 catch (JMSException ex) {
230 throw new JmsTransportException(ex);
231 }
232 finally {
233 JmsUtils.closeMessageProducer(messageProducer);
234 }
235 }
236
237 }