View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
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   * The Class WSSOAPMessageImpl.
33   *
34   * @author Michael Odling-Smee
35   * @author Nicholas Jones
36   * @since 0.1
37   */
38  public class WSSOAPMessageImpl extends ITKMessageImpl {
39  
40  	/** The Constant SYNCREQ. */
41  	public static final String SYNCREQ = "SYNCREQ";
42  	
43  	/** The Constant SYNCRESP. */
44  	public static final String SYNCRESP = "SYNCRESP";
45  	
46  	/** The Constant ASYNCREQ. */
47  	public static final String ASYNCREQ = "ASYNCREQ";
48  	
49  	/** The Constant ASYNCRESP. */
50  	public static final String ASYNCRESP = "ASYNCRESP";
51  	
52  	/** The Constant DATE_FORMAT. */
53  	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
54  	
55  	/** The message id. */
56  	private String messageId;
57  	
58  	/** The action. */
59  	private String action;
60  	
61  	/** The from. */
62  	private String from;
63  	
64  	/** The to. */
65  	private String to;
66  	
67  	/** The username. */
68  	private String username;
69  	
70  	/** The created. */
71  	private Calendar created;
72  	
73  	/** The expires. */
74  	private Calendar expires;
75  	
76  	/** The payload. */
77  	private String payload;
78  	
79  	/** The ttl. */
80  	private int ttl;
81  	
82  	/** The msg type. */
83  	private String msgType;
84  
85  	/**
86  	 * Gets the message id.
87  	 *
88  	 * @return the message id
89  	 */
90  	public String getMessageId() {
91  		return messageId;
92  	}
93  
94  	/**
95  	 * Gets the action.
96  	 *
97  	 * @return the action
98  	 */
99  	public String getAction() {
100 		return action;
101 	}
102 
103 	/**
104 	 * Sets the action.
105 	 *
106 	 * @param action the new action
107 	 */
108 	public void setAction(String action) {
109 		this.action = action;
110 	}
111 
112 	/**
113 	 * Gets the from.
114 	 *
115 	 * @return the from
116 	 */
117 	public String getFrom() {
118 		return from;
119 	}
120 
121 	/**
122 	 * Sets the from.
123 	 *
124 	 * @param from the new from
125 	 */
126 	public void setFrom(String from) {
127 		this.from = from;
128 	}
129 
130 	/**
131 	 * Gets the to.
132 	 *
133 	 * @return the to
134 	 */
135 	public String getTo() {
136 		return to;
137 	}
138 
139 	/**
140 	 * Sets the to.
141 	 *
142 	 * @param to the new to
143 	 */
144 	public void setTo(String to) {
145 		this.to = to;
146 	}
147 
148 	/**
149 	 * Gets the username.
150 	 *
151 	 * @return the username
152 	 */
153 	public String getUsername() {
154 		return username;
155 	}
156 
157 	/**
158 	 * Sets the username.
159 	 *
160 	 * @param username the new username
161 	 */
162 	public void setUsername(String username) {
163 		this.username = username;
164 	}
165 
166 	/**
167 	 * Gets the created.
168 	 *
169 	 * @return the created
170 	 */
171 	public Calendar getCreated() {
172 		return created;
173 	}
174 	
175 	/**
176 	 * Gets the created date string.
177 	 *
178 	 * @return the created date string
179 	 */
180 	public String getCreatedDateString() {
181 		return DATE_FORMAT.format(this.created.getTime());
182 	}
183 
184 	/**
185 	 * Gets the expiry date string.
186 	 *
187 	 * @return the expiry date string
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 	 * Gets the expires.
199 	 *
200 	 * @return the expires
201 	 */
202 	public Calendar getExpires() {
203 		return expires;
204 	}
205 
206 	/**
207 	 * Gets the payload.
208 	 *
209 	 * @return the payload
210 	 */
211 	public String getPayload() {
212 		return payload;
213 	}
214 	
215 	/**
216 	 * Gets the msg type.
217 	 *
218 	 * @return the msg type
219 	 */
220 	public String getMsgType() {
221 		return msgType;
222 	}
223 
224 	/**
225 	 * Sets the msg type.
226 	 *
227 	 * @param msgType the new msg type
228 	 */
229 	public void setMsgType(String msgType) {
230 		this.msgType = msgType;
231 	}
232 
233 	/**
234 	 * Gets the reply to.
235 	 *
236 	 * @return the reply to
237 	 */
238 	public String getReplyTo() {
239 		return replyTo;
240 	}
241 
242 	/**
243 	 * Sets the reply to.
244 	 *
245 	 * @param replyTo the new reply to
246 	 */
247 	public void setReplyTo(String replyTo) {
248 		this.replyTo = replyTo;
249 	}
250 
251 	/**
252 	 * Gets the fault to.
253 	 *
254 	 * @return the fault to
255 	 */
256 	public String getFaultTo() {
257 		return faultTo;
258 	}
259 
260 	/**
261 	 * Sets the fault to.
262 	 *
263 	 * @param faultTo the new fault to
264 	 */
265 	public void setFaultTo(String faultTo) {
266 		this.faultTo = faultTo;
267 	}
268 
269 	/** The reply to. */
270 	String replyTo;
271 	
272 	/** The fault to. */
273 	String faultTo;
274 	
275 	/**
276 	 * Gets the time to live.
277 	 *
278 	 * @return the time to live
279 	 */
280 	public int getTimeToLive() {
281 		return ttl;
282 	}
283 
284 	/**
285 	 * Sets the time to live.
286 	 *
287 	 * @param ttl the new time to live
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 	 * Instantiates a new wSSOAP message impl.
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 	// TODO : Look at refactoring the two constructors back into one.
312 	
313 	public WSSOAPMessageImpl(ITKMessage itkMessage, String messageType) throws ITKMessagingException {
314 		this(null, itkMessage, messageType);
315 	}
316 
317 	/**
318 	 * Instantiates a new wSSOAP message impl.
319 	 *
320 	 * @param destination the destination
321 	 * @param itkMessage the itk message
322 	 * @param messageType the message type
323 	 * @throws ITKMessagingException the iTK messaging exception
324 	 */
325 	public WSSOAPMessageImpl(ITKTransportRoute destination, ITKMessage itkMessage, String messageType) throws ITKMessagingException {
326 		//Initialise all variables
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 		//Extract information from the wrapped message
332 		ITKMessageProperties itkMessageProperties = itkMessage.getMessageProperties();
333 		this.setMsgType(messageType);
334 		//Throw an error if the message properties are not present
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 		// COR-DEH-01 : Set the action to the service
340 		this.setAction(itkMessageProperties.getServiceId());
341 
342 		// Set the transport properties
343 		// Sync responses on the inbound channel won't have destination address
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 	/* (non-Javadoc)
355 	 * @see uk.nhs.interoperability.payload.ITKMessageImpl#getPreresolvedRoute()
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 	 * Sets the payload.
367 	 *
368 	 * @param businessPayload the new payload
369 	 */
370 	public void setPayload(String businessPayload) {
371 		if (businessPayload != null) {
372 			if (businessPayload.startsWith("<?xml")) {
373 				//Remove any XML declarations
374 				this.businessPayload = businessPayload.substring(businessPayload.indexOf(">"));
375 			} else {
376 				this.businessPayload = businessPayload;
377 			}
378 		}
379 	}
380 
381 	/* (non-Javadoc)
382 	 * @see uk.nhs.interoperability.payload.ITKMessage#getFullPayload()
383 	 */
384 	@Override
385 	public String getFullPayload() throws ITKMessagingException {
386 		return this.serialise();
387 	}
388 
389 	//@Requirement(traceTo={"WS-STD-03","WS-STD-04","WS-SEC-04","WS-SEC-05"})
390 	/**
391 	 * Serialise.
392 	 *
393 	 * @return the string
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 			// TODO Auto-generated catch block
415 			e.printStackTrace();
416 		}
417 		Logger.trace(SOAPPayload);
418 		return SOAPPayload;
419 	}
420 }