1
2
3
4
5
6
7
8
9
10
11
12
13
14 package uk.nhs.interoperability.util.xml;
15
16 import javax.xml.namespace.NamespaceContext;
17 import javax.xml.xpath.XPath;
18 import javax.xml.xpath.XPathConstants;
19 import javax.xml.xpath.XPathExpression;
20 import javax.xml.xpath.XPathExpressionException;
21 import javax.xml.xpath.XPathFactory;
22 import javax.xml.xpath.XPathFactoryConfigurationException;
23
24 import uk.nhs.interoperability.util.Logger;
25
26 import com.xmlsolutions.annotation.Requirement;
27
28
29
30
31
32
33
34
35 public class XPaths {
36
37
38 private static XPathFactory xpathFactory;
39
40 static {
41 try {
42 xpathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
43 } catch (XPathFactoryConfigurationException e) {
44 Logger.error("Could not create XPath factory", e);
45 }
46 }
47
48
49 public static final String ROOT = "/";
50
51
52 public static final String SOAP_BODY = ROOT + "SOAP:Envelope/SOAP:Body";
53
54
55 public static final String SOAP_BODY_CONTENT = SOAP_BODY + "/*[1]";
56
57
58 public static final String SOAP_HEADER = ROOT + "SOAP:Envelope/SOAP:Header";
59
60
61 public static final String WSA_MSG_ID = SOAP_HEADER + "/wsa:MessageID";
62
63
64 @Requirement(traceTo={"WS-STD-03"})
65 public static final String WSA_TO = SOAP_HEADER + "/wsa:To";
66
67
68 @Requirement(traceTo={"WS-STD-03"})
69 public static final String WSA_FROM = SOAP_HEADER + "/wsa:From/wsa:Address";
70
71
72 @Requirement(traceTo={"WS-STD-03"})
73 public static final String WSA_REPLY_TO = SOAP_HEADER + "/wsa:ReplyTo/wsa:Address";
74
75
76 @Requirement(traceTo={"WS-STD-03"})
77 public static final String WSA_FAULT_TO = SOAP_HEADER + "/wsa:FaultTo/wsa:Address";
78
79
80 @Requirement(traceTo={"WS-STD-03"})
81 public static final String WSA_ACTION = SOAP_HEADER + "/wsa:Action";
82
83
84
85 public static final String WSA_SECURITY_TIMESTAMP = SOAP_HEADER + "/wsse:Security/wsu:Timestamp";
86
87
88 public static final String WSA_SECURITY_CREATED = SOAP_HEADER + "/wsse:Security/wsu:Timestamp/wsu:Created";
89
90
91 public static final String WSA_SECURITY_EXPIRES = SOAP_HEADER + "/wsse:Security/wsu:Timestamp/wsu:Expires";
92
93
94 public static final String WSA_SECURITY_USERNAME = SOAP_HEADER + "/wsse:Security/wsse:UsernameToken/wsse:Username";
95
96
97 public static final String ITK_PAYLOAD_COUNT = "count(itk:DistributionEnvelope/itk:payloads/itk:payload)";
98
99
100 public static final String ITK_AUDIT_IDENTITY_URI = "itk:DistributionEnvelope/itk:header/itk:auditIdentity/itk:id/@uri";
101
102
103 public static final String ITK_AUDIT_IDENTITY_TYPE = "itk:DistributionEnvelope/itk:header/itk:auditIdentity/itk:id/@type";
104
105
106 public static final String ITK_FROM_ADDRESS = "itk:DistributionEnvelope/itk:header/itk:senderAddress/@uri";
107
108
109 public static final String ITK_FIRST_TO_ADDRESS = "itk:DistributionEnvelope/itk:header/itk:addresslist/itk:address[1]/@uri";
110
111
112 public static final String ITK_FIRST_MIMETYPE = "itk:DistributionEnvelope/itk:header/itk:manifest/itk:manifestitem[1]/@mimetype";
113
114
115 public static final String ITK_FIRST_BASE64 = "itk:DistributionEnvelope/itk:header/itk:manifest/itk:manifestitem[1]/@base64";
116
117
118
119
120
121
122
123 public static final String ITK_PROFILE_ID = "itk:DistributionEnvelope/itk:header/itk:manifest/itk:manifestitem[1]/@profileid";
124
125
126
127
128 public static final String ITK_TRACKING_ID = "itk:DistributionEnvelope/itk:header/@trackingid";
129
130
131
132
133
134 public static final String ITK_SERVICE = "itk:DistributionEnvelope/itk:header/@service";
135
136
137 public static final String ITK_BUSINESS_ACK_HANDLING_SPECIFICATIONS = "itk:DistributionEnvelope/itk:header/itk:handlingSpecification/itk:spec[@key='urn:nhs:itk:ns:201005:ackrequested']/@value";
138
139
140 public static final String ITK_FIRST_PAYLOAD = "itk:DistributionEnvelope/itk:payloads/itk:payload[1]/*[1]";
141
142
143 public static final String ITK_FIRST_PAYLOAD_TEXT = "itk:DistributionEnvelope/itk:payloads/itk:payload[1]/text()";
144
145
146 public static final String ITK_FIRST_PAYLOAD_ID = "itk:DistributionEnvelope/itk:payloads/itk:payload[1]/@id";
147
148
149 public static final String ITK_SIMPLE_MESSAGE_RESPONSE = "itk:SimpleMessageResponse";
150
151
152 public static final String ITK_SIMPLE_MESSAGE_RESPONSE_CONTENT = "itk:SimpleMessageResponse/* | itk:SimpleMessageResponse/text()";
153
154
155 public static final String SOAP_WRAPPED_ITK_FIRST_PAYLOAD = SOAP_BODY + "/" + ITK_FIRST_PAYLOAD;
156
157
158 public static final String SOAP_WRAPPED_ITK_SIMPLE_MESSAGE_RESPONSE = SOAP_BODY + "/" + ITK_SIMPLE_MESSAGE_RESPONSE;
159
160
161
162 public static final String WSA_SOAP_ERROR_DETAIL = SOAP_BODY + "/SOAP:Fault/SOAP:detail/*[1]";
163
164
165 public static final String WSA_SOAP_ERROR_DETAIL_ID = "itk:itkErrorInfo/itk:ErrorID";
166
167
168 public static final String WSA_SOAP_ERROR_DETAIL_CODE = "itk:itkErrorInfo/itk:ErrorCode";
169
170
171 public static final String WSA_SOAP_ERROR_DETAIL_TEXT = "itk:itkErrorInfo/itk:ErrorText";
172
173
174 public static final String WSA_SOAP_ERROR_DETAIL_DIAGNOSTIC = "itk:itkErrorInfo/itk:ErrorDiagnosticText";
175
176
177 public static final NamespaceContext NS_CONTEXT = new XMLNamespaceContext();
178
179
180
181 public static final XPathExpression ROOT_XPATH = compileXPath(ROOT);
182
183
184 public static final XPathExpression WSA_MSGID_XPATH = compileXPath(WSA_MSG_ID);
185
186
187 public static final XPathExpression WSA_REPLY_TO_XPATH = compileXPath(WSA_REPLY_TO);
188
189
190 public static final XPathExpression WSA_FAULT_TO_XPATH = compileXPath(WSA_FAULT_TO);
191
192
193 public static final XPathExpression WSA_ACTION_XPATH = compileXPath(WSA_ACTION);
194
195
196 public static final XPathExpression WSA_FROM_XPATH = compileXPath(WSA_FROM);
197
198
199 public static final XPathExpression WSA_TO_XPATH = compileXPath(WSA_TO);
200
201
202 public static final XPathExpression SOAP_HEADER_XPATH = compileXPath(SOAP_HEADER);
203
204
205 public static final XPathExpression SOAP_BODY_XPATH = compileXPath(SOAP_BODY);
206
207
208 public static final XPathExpression SOAP_BODY_CONTENT_XPATH = compileXPath(SOAP_BODY_CONTENT);
209
210
211 public static final XPathExpression SOAP_WRAPPED_ITK_FIRST_PAYLOAD_XPATH = compileXPath(SOAP_WRAPPED_ITK_FIRST_PAYLOAD);
212
213
214 public static final XPathExpression SOAP_WRAPPED_ITK_SIMPLE_MESSAGE_RESPONSE_XPATH = compileXPath(SOAP_WRAPPED_ITK_SIMPLE_MESSAGE_RESPONSE);
215
216
217 public static final XPathExpression ITK_SIMPLE_MESSAGE_RESPONSE_CONTENT_XPATH = compileXPath(ITK_SIMPLE_MESSAGE_RESPONSE_CONTENT);
218
219
220
221 public static final XPathExpression WSA_SECURITY_TIMESTAMP_XPATH = compileXPath(WSA_SECURITY_TIMESTAMP);
222
223
224 public static final XPathExpression WSA_SECURITY_CREATED_XPATH = compileXPath(WSA_SECURITY_CREATED);
225
226
227 public static final XPathExpression WSA_SECURITY_EXPIRES_XPATH = compileXPath(WSA_SECURITY_EXPIRES);
228
229
230 public static final XPathExpression WSA_SECURITY_USERNAME_XPATH = compileXPath(WSA_SECURITY_USERNAME);
231
232
233
234 public static final XPathExpression ITK_PAYLOAD_COUNT_XPATH = compileXPath(ITK_PAYLOAD_COUNT);
235
236
237 public static final XPathExpression ITK_AUDIT_IDENTITY_URI_XPATH = compileXPath(ITK_AUDIT_IDENTITY_URI);
238
239
240 public static final XPathExpression ITK_AUDIT_IDENTITY_TYPE_XPATH = compileXPath(ITK_AUDIT_IDENTITY_TYPE);
241
242
243 public static final XPathExpression ITK_FROM_ADDRESS_XPATH = compileXPath(ITK_FROM_ADDRESS);
244
245
246 public static final XPathExpression ITK_FIRST_TO_ADDRESS_XPATH = compileXPath(ITK_FIRST_TO_ADDRESS);
247
248
249 public static final XPathExpression ITK_PROFILE_ID_XPATH = compileXPath(ITK_PROFILE_ID);
250
251
252 public static final XPathExpression ITK_SERVICE_XPATH = compileXPath(ITK_SERVICE);
253
254
255 public static final XPathExpression ITK_BUSINESS_ACK_HANDLING_SPECIFICATIONS_XPATH = compileXPath(ITK_BUSINESS_ACK_HANDLING_SPECIFICATIONS);
256
257
258 public static final XPathExpression ITK_TRACKING_ID_XPATH = compileXPath(ITK_TRACKING_ID);
259
260
261 public static final XPathExpression ITK_FIRST_PAYLOAD_XPATH = compileXPath(ITK_FIRST_PAYLOAD);
262
263
264 public static final XPathExpression ITK_FIRST_PAYLOAD_ID_XPATH = compileXPath(ITK_FIRST_PAYLOAD_ID);
265
266
267 public static final XPathExpression ITK_FIRST_PAYLOAD_TEXT_XPATH = compileXPath(ITK_FIRST_PAYLOAD_TEXT);
268
269
270 public static final XPathExpression ITK_FIRST_MIMETYPE_XPATH = compileXPath(ITK_FIRST_MIMETYPE);
271
272
273 public static final XPathExpression ITK_FIRST_BASE64_XPATH = compileXPath(ITK_FIRST_BASE64);
274
275
276
277
278 public static final XPathExpression WSA_SOAP_ERROR_DETAIL_XPATH = compileXPath(WSA_SOAP_ERROR_DETAIL);
279
280
281 public static final XPathExpression WSA_SOAP_ERROR_DETAIL_ID_XPATH = compileXPath(WSA_SOAP_ERROR_DETAIL_ID);
282
283
284 public static final XPathExpression WSA_SOAP_ERROR_DETAIL_CODE_XPATH = compileXPath(WSA_SOAP_ERROR_DETAIL_CODE);
285
286
287 public static final XPathExpression WSA_SOAP_ERROR_DETAIL_TEXT_XPATH = compileXPath(WSA_SOAP_ERROR_DETAIL_TEXT);
288
289
290 public static final XPathExpression WSA_SOAP_ERROR_DETAIL_DIAGNOSTIC_XPATH = compileXPath(WSA_SOAP_ERROR_DETAIL_DIAGNOSTIC);
291
292 public static XPathExpression getXPathExpression(String xPath) {
293 XPath xp = xpathFactory.newXPath();
294 xp.setNamespaceContext(NS_CONTEXT);
295 try {
296 return xp.compile(xPath);
297 } catch (XPathExpressionException e) {
298 Logger.error("Error compiling XPath \"" + xPath + "\"", e);
299 }
300 return null;
301 }
302
303
304
305
306
307
308
309
310 private static XPathExpression compileXPath(String xPath) {
311 XPath xp = xpathFactory.newXPath();
312 xp.setNamespaceContext(NS_CONTEXT);
313 try {
314 return xp.compile(xPath);
315 } catch (XPathExpressionException e) {
316 Logger.error("Error compiling XPath \"" + xPath + "\"", e);
317 }
318 return null;
319 }
320
321
322
323 }