1
2
3
4
5
6
7
8
9
10
11
12
13
14 package uk.nhs.interoperability.transport.WS;
15
16 import java.text.SimpleDateFormat;
17 import java.util.Calendar;
18 import java.util.UUID;
19
20 import com.xmlsolutions.annotation.Requirement;
21
22 import uk.nhs.interoperability.infrastructure.ITKMessageProperties;
23 import uk.nhs.interoperability.infrastructure.ITKMessagingException;
24 import uk.nhs.interoperability.payload.ITKMessage;
25 import uk.nhs.interoperability.payload.ITKMessageImpl;
26 import uk.nhs.interoperability.transform.TransformManager;
27 import uk.nhs.interoperability.transport.ITKTransportRoute;
28 import uk.nhs.interoperability.transport.ITKTransportRouteImpl;
29 import uk.nhs.interoperability.util.Logger;
30
31
32
33
34
35
36
37
38 public class WSSOAPMessageImpl extends ITKMessageImpl {
39
40
41 public static final String SYNCREQ = "SYNCREQ";
42
43
44 public static final String SYNCRESP = "SYNCRESP";
45
46
47 public static final String ASYNCREQ = "ASYNCREQ";
48
49
50 public static final String ASYNCRESP = "ASYNCRESP";
51
52
53 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
54
55
56 private String messageId;
57
58
59 private String action;
60
61
62 private String from;
63
64
65 private String to;
66
67
68 private String username;
69
70
71 private Calendar created;
72
73
74 private Calendar expires;
75
76
77 private String payload;
78
79
80 private int ttl;
81
82
83 private String msgType;
84
85
86
87
88
89
90 public String getMessageId() {
91 return messageId;
92 }
93
94
95
96
97
98
99 public String getAction() {
100 return action;
101 }
102
103
104
105
106
107
108 public void setAction(String action) {
109 this.action = action;
110 }
111
112
113
114
115
116
117 public String getFrom() {
118 return from;
119 }
120
121
122
123
124
125
126 public void setFrom(String from) {
127 this.from = from;
128 }
129
130
131
132
133
134
135 public String getTo() {
136 return to;
137 }
138
139
140
141
142
143
144 public void setTo(String to) {
145 this.to = to;
146 }
147
148
149
150
151
152
153 public String getUsername() {
154 return username;
155 }
156
157
158
159
160
161
162 public void setUsername(String username) {
163 this.username = username;
164 }
165
166
167
168
169
170
171 public Calendar getCreated() {
172 return created;
173 }
174
175
176
177
178
179
180 public String getCreatedDateString() {
181 return DATE_FORMAT.format(this.created.getTime());
182 }
183
184
185
186
187
188
189 public String getExpiryDateString() {
190 if (null==expires){
191 return "";
192 } else {
193 return DATE_FORMAT.format(this.expires.getTime());
194 }
195 }
196
197
198
199
200
201
202 public Calendar getExpires() {
203 return expires;
204 }
205
206
207
208
209
210
211 public String getPayload() {
212 return payload;
213 }
214
215
216
217
218
219
220 public String getMsgType() {
221 return msgType;
222 }
223
224
225
226
227
228
229 public void setMsgType(String msgType) {
230 this.msgType = msgType;
231 }
232
233
234
235
236
237
238 public String getReplyTo() {
239 return replyTo;
240 }
241
242
243
244
245
246
247 public void setReplyTo(String replyTo) {
248 this.replyTo = replyTo;
249 }
250
251
252
253
254
255
256 public String getFaultTo() {
257 return faultTo;
258 }
259
260
261
262
263
264
265 public void setFaultTo(String faultTo) {
266 this.faultTo = faultTo;
267 }
268
269
270 String replyTo;
271
272
273 String faultTo;
274
275
276
277
278
279
280 public int getTimeToLive() {
281 return ttl;
282 }
283
284
285
286
287
288
289 public void setTimeToLive(int ttl) {
290 this.ttl = ttl;
291 expires = (Calendar)created.clone();
292 expires.add(Calendar.SECOND,ttl);
293 }
294
295
296
297
298 public WSSOAPMessageImpl(){
299 UUID soapMessageId = UUID.randomUUID();
300 this.messageId = soapMessageId.toString().toUpperCase();
301 this.created = Calendar.getInstance();
302 this.username = "";
303 this.action = "";
304 this.faultTo = "";
305 this.replyTo = "";
306 this.from = "";
307 this.payload = "";
308 this.to = "";
309 }
310
311
312
313 public WSSOAPMessageImpl(ITKMessage itkMessage, String messageType) throws ITKMessagingException {
314 this(null, itkMessage, messageType);
315 }
316
317
318
319
320
321
322
323
324
325 public WSSOAPMessageImpl(ITKTransportRoute destination, ITKMessage itkMessage, String messageType) throws ITKMessagingException {
326
327 this();
328 if (itkMessage == null || messageType == null) {
329 throw new ITKMessagingException(ITKMessagingException.PROCESSING_ERROR_NOT_RETRYABLE_CODE, "Could not add soap wrappers as message was null");
330 }
331
332 ITKMessageProperties itkMessageProperties = itkMessage.getMessageProperties();
333 this.setMsgType(messageType);
334
335 if (itkMessageProperties == null) {
336 throw new ITKMessagingException(ITKMessagingException.PROCESSING_ERROR_NOT_RETRYABLE_CODE, "Could not add soap wrappers as the message did not contain the correct message properties");
337 }
338
339
340 this.setAction(itkMessageProperties.getServiceId());
341
342
343
344 if (destination!=null){
345 this.setReplyTo(destination.getReplyToAddress());
346 this.setFaultTo(destination.getExceptionToAddress());
347 this.setTo(destination.getPhysicalAddress());
348 }
349
350 this.setPayload(itkMessage.getFullPayload());
351
352 }
353
354
355
356
357 @Override
358 public ITKTransportRoute getPreresolvedRoute() {
359 if (this.to != null) {
360 return new ITKTransportRouteImpl(ITKTransportRoute.HTTP_WS, this.to);
361 }
362 return null;
363 }
364
365
366
367
368
369
370 public void setPayload(String businessPayload) {
371 if (businessPayload != null) {
372 if (businessPayload.startsWith("<?xml")) {
373
374 this.businessPayload = businessPayload.substring(businessPayload.indexOf(">"));
375 } else {
376 this.businessPayload = businessPayload;
377 }
378 }
379 }
380
381
382
383
384 @Override
385 public String getFullPayload() throws ITKMessagingException {
386 return this.serialise();
387 }
388
389
390
391
392
393
394
395 @Requirement(traceTo="WS-STD-03", status="implemented")
396 public String serialise(){
397 String soapXML = "<SOAPMessage>";
398 soapXML += "<MessageType>"+msgType+"</MessageType>";
399 soapXML += "<MessageId>"+messageId+"</MessageId>";
400 soapXML += "<Action>"+action+"</Action>";
401 soapXML += "<From>"+from+"</From>";
402 soapXML += "<To>"+to+"</To>";
403 soapXML += "<FaultTo>"+faultTo+"</FaultTo>";
404 soapXML += "<ReplyTo>"+replyTo+"</ReplyTo>";
405 soapXML += "<Username>"+username+"</Username>";
406 soapXML += "<Created>"+this.getCreatedDateString()+"</Created>";
407 soapXML += "<Expires>"+this.getExpiryDateString()+"</Expires>";
408 soapXML += "<Payload>" + this.businessPayload + "</Payload>";
409 soapXML += "</SOAPMessage>";
410 String SOAPPayload ="";
411 try {
412 SOAPPayload = TransformManager.doTransform("ToSOAP.xslt", soapXML);
413 } catch (Exception e) {
414
415 e.printStackTrace();
416 }
417 Logger.trace(SOAPPayload);
418 return SOAPPayload;
419 }
420 }