1
2
3
4
5
6
7
8
9
10
11
12
13
14 package uk.nhs.interoperability.infrastructure;
15
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import javax.xml.parsers.ParserConfigurationException;
20 import javax.xml.xpath.XPathConstants;
21 import javax.xml.xpath.XPathExpressionException;
22
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Node;
25
26 import uk.nhs.interoperability.transport.ITKTransportProperties;
27 import uk.nhs.interoperability.util.Logger;
28 import uk.nhs.interoperability.util.xml.DomUtils;
29 import uk.nhs.interoperability.util.xml.XPaths;
30
31
32
33
34
35
36
37
38
39 public class ITKMessagePropertiesImpl implements ITKMessageProperties {
40
41
42
43 private ITKAddress fromAddress;
44
45
46 private ITKAddress toAddress;
47
48
49 private ITKIdentity auditIdentity ;
50
51
52 private String serviceId;
53
54
55 private String businessPayloadId;
56
57
58 private String profileId;
59
60
61 private String trackingId;
62
63
64 private ITKTransportProperties inboundTransportProperties;
65
66
67
68 private Map<String, String> handlingSpecification = new HashMap<String, String>();
69
70
71
72
73 public String getTrackingId() {
74 return trackingId;
75 }
76
77
78
79
80 public void setTrackingId(String trackingId) {
81 this.trackingId = trackingId;
82 }
83
84
85
86
87 public ITKAddress getFromAddress() {
88 return fromAddress;
89 }
90
91
92
93
94 public void setFromAddress(ITKAddress fromAddress) {
95 this.fromAddress = fromAddress;
96 }
97
98
99
100
101 public ITKAddress getToAddress() {
102 return toAddress;
103 }
104
105
106
107
108 public void setToAddress(ITKAddress toAddress) {
109 this.toAddress = toAddress;
110 }
111
112
113
114
115 public ITKIdentity getAuditIdentity() {
116 return auditIdentity;
117 }
118
119
120
121
122 public void setAuditIdentity(ITKIdentity auditIdentity) {
123 this.auditIdentity = auditIdentity;
124 }
125
126
127
128
129 public String getServiceId() {
130 return serviceId;
131 }
132
133
134
135
136 public void setServiceId(String serviceId) {
137 this.serviceId = serviceId;
138 }
139
140
141
142
143 public String getBusinessPayloadId() {
144 return businessPayloadId;
145 }
146
147
148
149
150 public void setBusinessPayloadId(String businessPayloadId) {
151 this.businessPayloadId = businessPayloadId;
152 }
153
154
155
156
157 public String getProfileId() {
158 return profileId;
159 }
160
161
162
163
164 public void setProfileId(String profileId) {
165 this.profileId = profileId;
166 }
167
168
169
170
171 public ITKTransportProperties getInboundTransportProperties() {
172 return inboundTransportProperties;
173 }
174
175
176
177
178 public void setInboundTransportProperties(
179 ITKTransportProperties inboundTransportProperties) {
180 this.inboundTransportProperties = inboundTransportProperties;
181 }
182
183
184
185
186 @Override
187 public Map<String, String> getHandlingSpecifications() {
188 return this.handlingSpecification;
189 }
190
191
192
193
194 @Override
195 public void addHandlingSpecification(String key, String value) {
196 this.handlingSpecification.put(key, value);
197 }
198
199
200
201
202 @Override
203 public String getHandlingSpecification(String key) {
204 return this.handlingSpecification != null ? this.handlingSpecification.get(key) : null;
205 }
206
207
208
209
210
211
212
213
214 public static Document extractDistributionEnvelopeFromSoap(Document doc) throws ITKMessagingException {
215 try {
216 return DomUtils.createDocumentFromNode((Node)XPaths.SOAP_BODY_CONTENT_XPATH.evaluate(doc, XPathConstants.NODE));
217 } catch (XPathExpressionException e) {
218 throw new ITKMessagingException(ITKMessagingException.PROCESSING_ERROR_NOT_RETRYABLE_CODE, "Could not extract values from request", e);
219 } catch (ParserConfigurationException e) {
220 throw new ITKMessagingException(ITKMessagingException.PROCESSING_ERROR_NOT_RETRYABLE_CODE, "XML parser configuration error", e);
221 }
222 }
223
224
225
226
227
228
229
230
231 public static ITKMessageProperties build(Document distributionEnvelope) throws ITKMessagingException {
232
233 ITKMessagePropertiesImpl itkMessageProperties = new ITKMessagePropertiesImpl();
234
235 try {
236
237
238
239 Double payloadCount = (Double)XPaths.ITK_PAYLOAD_COUNT_XPATH.evaluate(distributionEnvelope, XPathConstants.NUMBER);
240 Logger.debug("Number of payloads " + payloadCount);
241
242
243
244 if (payloadCount != 1) {
245 throw new ITKMessagingException(itkMessageProperties, ITKMessagingException.PROCESSING_ERROR_NOT_RETRYABLE_CODE, "The ITK reference implementation currently only supports a single payload within a distribution envelope");
246 }
247
248 String itkService = XPaths.ITK_SERVICE_XPATH.evaluate(distributionEnvelope);
249 Logger.debug("ITK service " + itkService);
250
251 String itkAuditIdentityURI = XPaths.ITK_AUDIT_IDENTITY_URI_XPATH.evaluate(distributionEnvelope);
252 String itkAuditIdentityType = XPaths.ITK_AUDIT_IDENTITY_TYPE_XPATH.evaluate(distributionEnvelope);
253 Logger.debug("ITK audit identity " + itkAuditIdentityType + ":" + itkAuditIdentityURI);
254
255 String itkFromAddress = XPaths.ITK_FROM_ADDRESS_XPATH.evaluate(distributionEnvelope);
256 Logger.debug("ITK from address " + itkFromAddress);
257
258 String itkToAddress = XPaths.ITK_FIRST_TO_ADDRESS_XPATH.evaluate(distributionEnvelope);
259 Logger.debug("ITK to address " + itkToAddress);
260
261 String itkTrackingId = XPaths.ITK_TRACKING_ID_XPATH.evaluate(distributionEnvelope);
262 Logger.debug("ITK tracking id " + itkTrackingId);
263
264 String itkProfileId = XPaths.ITK_PROFILE_ID_XPATH.evaluate(distributionEnvelope);
265 Logger.debug("ITK profile id " + itkProfileId);
266
267 String businessPayloadId = XPaths.ITK_FIRST_PAYLOAD_ID_XPATH.evaluate(distributionEnvelope);
268 Logger.debug("ITK payload id " + businessPayloadId);
269
270 String businessAckHandlingSpecification = XPaths.ITK_BUSINESS_ACK_HANDLING_SPECIFICATIONS_XPATH.evaluate(distributionEnvelope);
271 Logger.debug("ITK businessAck handling specification " + businessAckHandlingSpecification);
272
273
274 itkMessageProperties.setBusinessPayloadId(businessPayloadId);
275 itkMessageProperties.setToAddress(new ITKAddressImpl(itkToAddress));
276 itkMessageProperties.setFromAddress(new ITKAddressImpl(itkFromAddress));
277 itkMessageProperties.setServiceId(itkService);
278 itkMessageProperties.setAuditIdentity(new ITKIdentityImpl(itkAuditIdentityURI, itkAuditIdentityType));
279 itkMessageProperties.setProfileId(itkProfileId);
280 itkMessageProperties.setTrackingId(itkTrackingId);
281 itkMessageProperties.addHandlingSpecification(ITKMessageProperties.BUSINESS_ACK_HANDLING_SPECIFICATION_KEY, businessAckHandlingSpecification);
282
283 return itkMessageProperties;
284
285 } catch (XPathExpressionException e) {
286 throw new ITKMessagingException(itkMessageProperties, ITKMessagingException.PROCESSING_ERROR_NOT_RETRYABLE_CODE, "Could not extract values from request", e);
287 }
288 }
289
290 }