1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.axis.message.addressing.util;
17
18 import java.io.StringReader;
19 import java.io.StringWriter;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 import javax.wsdl.Input;
29 import javax.wsdl.Operation;
30 import javax.wsdl.Output;
31 import javax.wsdl.extensions.AttributeExtensible;
32 import javax.xml.namespace.QName;
33 import javax.xml.rpc.handler.MessageContext;
34 import javax.xml.soap.Name;
35 import javax.xml.soap.SOAPElement;
36 import javax.xml.soap.SOAPException;
37 import javax.xml.soap.SOAPHeader;
38 import javax.xml.soap.SOAPHeaderElement;
39
40 import org.apache.axis.AxisEngine;
41 import org.apache.axis.message.addressing.AddressingHeaders;
42 import org.apache.axis.message.addressing.AddressingVersion;
43 import org.apache.axis.message.addressing.Constants;
44 import org.apache.axis.message.addressing.EndpointReference;
45 import org.apache.axis.utils.DOM2Writer;
46 import org.apache.axis.utils.XMLUtils;
47 import org.w3c.dom.Document;
48 import org.w3c.dom.Element;
49 import org.xml.sax.InputSource;
50
51
52
53
54
55
56
57
58 public abstract class AddressingUtils implements Constants {
59
60
61
62
63
64 private static final Set<String> ADDRESSING_NS_URI_SET = new HashSet<String>();
65
66
67
68
69
70 private static final Set<String> W3C_ADDRESSING_NS_URI_SET = new HashSet<String>();
71
72
73
74
75 private static final Map<String, AddressingVersion> VERSIONS
76 = new HashMap<String, AddressingVersion>();
77
78
79
80
81
82 private static final String DEFAULT_ADDRESSING_NS;
83
84 static {
85 addVersion(NS_URI_ADDRESSING_2003_03, null, RESPONSE, false, false, true, false);
86 addVersion(NS_URI_ADDRESSING_2004_03, null, REPLY, false, false, true, false);
87 addVersion(NS_URI_ADDRESSING_2004_08, null, REPLY, false, true, true, false);
88
89 addVersion(NS_URI_ADDRESSING_2005_08, URI_ANONYMOUS_W3C_CR, REPLY,
90 true, true, true, true);
91
92
93 DEFAULT_ADDRESSING_NS = System.getProperty(
94 Constants.ENV_ADDRESSING_NAMESPACE_URI,
95 Constants.NS_URI_ADDRESSING_DEFAULT);
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109 private static void addVersion(String ns, String anonURI, String response,
110 boolean w3c, boolean params, boolean props, boolean mdata) {
111 if (w3c) {
112 W3C_ADDRESSING_NS_URI_SET.add(ns);
113 } else {
114 ADDRESSING_NS_URI_SET.add(ns);
115 }
116
117 if (anonURI == null) {
118 anonURI = ns + "/role/anonymous";
119 }
120 VERSIONS.put(ns, new WsaVersion(ns, anonURI, new QName(ns, response),
121 w3c, params, props, mdata));
122 }
123
124
125
126
127 protected AddressingUtils() {
128 }
129
130
131
132
133
134
135
136
137
138 public static void removeHeader(SOAPHeader soapHeader, String actorURI,
139 String headerName) {
140 if (soapHeader == null) {
141 return;
142 }
143 Iterator<?> headers = soapHeader.examineHeaderElements(actorURI);
144 List<SOAPHeaderElement> existingElements = new ArrayList<SOAPHeaderElement>();
145 while (headers.hasNext()) {
146 SOAPHeaderElement hElement = (SOAPHeaderElement) headers.next();
147 Name hName = hElement.getElementName();
148 if (isAddressingNamespaceURI(hName.getURI())
149 && hName.getLocalName().equals(headerName)) {
150 existingElements.add(hElement);
151 }
152 }
153 for (int i = 0; i < existingElements.size(); i++) {
154 SOAPHeaderElement el = (SOAPHeaderElement) existingElements.get(i);
155 el.detachNode();
156 }
157 }
158
159
160
161
162
163
164
165
166 public static AddressingVersion getAddressingVersion(MessageContext ctx) {
167 return VERSIONS.get(getAddressingNamespaceURI(ctx));
168 }
169
170
171
172
173
174
175
176
177 public static AddressingVersion getAddressingVersion(String ns) {
178 return VERSIONS.get(ns);
179 }
180
181
182
183
184
185
186
187 public static AddressingVersion getAddressingVersion() {
188 String ns = getAddressingNamespaceURI(AxisEngine.getCurrentMessageContext());
189 return VERSIONS.get(ns);
190 }
191
192
193
194
195
196
197
198
199
200 public static boolean isAddressingNamespaceURI(String uri) {
201 return ADDRESSING_NS_URI_SET.contains(uri);
202 }
203
204
205
206
207
208
209
210
211
212
213 public static boolean isW3CAddressingNamespaceURI(String uri) {
214 return W3C_ADDRESSING_NS_URI_SET.contains(uri);
215 }
216
217
218
219
220
221
222
223 public static void removeHeaders(SOAPHeader soapHeader, String actorURI) {
224 if (soapHeader == null) {
225 return;
226 }
227 Iterator<?> headers = soapHeader.examineHeaderElements(actorURI);
228 List<SOAPHeaderElement> existingElements = new ArrayList<SOAPHeaderElement>();
229 while (headers.hasNext()) {
230 SOAPHeaderElement hElement = (SOAPHeaderElement) headers.next();
231 Name hName = hElement.getElementName();
232 AddressingVersion version = getAddressingVersion(hName.getURI());
233 if (version != null) {
234 existingElements.add(hElement);
235 }
236 }
237 for (int i = 0; i < existingElements.size(); i++) {
238 SOAPHeaderElement el = (SOAPHeaderElement) existingElements.get(i);
239 el.detachNode();
240 }
241 }
242
243
244
245
246
247
248
249
250
251
252 public static AddressingHeaders getResponseHeaders(MessageContext msgCtx) {
253 return getHeaders(msgCtx, Constants.ENV_ADDRESSING_RESPONSE_HEADERS);
254 }
255
256
257
258
259
260
261
262
263
264
265 public static AddressingHeaders getRequestHeaders(MessageContext msgCtx) {
266 return getHeaders(msgCtx, Constants.ENV_ADDRESSING_REQUEST_HEADERS);
267 }
268
269
270
271
272
273
274
275
276 public static String getInputAction(QName portTypeQName, Operation operation) {
277 if (portTypeQName == null || operation == null) {
278 throw new IllegalArgumentException("Null parameters are not allowed.");
279 }
280 Input input = operation.getInput();
281 if (input == null) {
282 return null;
283 }
284 return getAction(input, input.getName(), portTypeQName, operation, "Request");
285 }
286
287
288
289
290
291
292
293
294 public static String getOutputAction(QName portTypeQName, Operation operation) {
295 if (portTypeQName == null || operation == null) {
296 throw new IllegalArgumentException("Null parameters are not allowed.");
297 }
298 Output output = operation.getOutput();
299 if (output == null) {
300 return null;
301 }
302 return getAction(output, output.getName(), portTypeQName, operation, "Response");
303 }
304
305
306
307
308
309
310
311
312
313
314 public static String getNamespacePrefix(SOAPElement element, String namespace)
315 throws SOAPException {
316 if (element == null || namespace == null) {
317 throw new IllegalArgumentException("Null parameters are not allowed.");
318 }
319 Iterator<?> iter = element.getVisibleNamespacePrefixes();
320 String prefix;
321 String ns;
322 while (iter.hasNext()) {
323 prefix = (String) iter.next();
324 ns = element.getNamespaceURI(prefix);
325 if (namespace.equals(ns)) {
326 return prefix;
327 }
328 }
329
330 int i = 0;
331 prefix = "ns" + i;
332 while (element.getNamespaceURI(prefix) != null) {
333 prefix = "ns" + i++;
334 }
335
336 element.addNamespaceDeclaration(prefix, namespace);
337
338 return prefix;
339 }
340
341
342
343
344
345
346
347
348 public static String getAddressingNamespaceURI() {
349
350 MessageContext ctx = AxisEngine.getCurrentMessageContext();
351 return getAddressingNamespaceURI(ctx);
352 }
353
354
355
356
357
358
359
360
361 public static String getAddressingNamespaceURI(MessageContext ctx) {
362 String nsURI = null;
363 if (ctx != null) {
364 nsURI = (String) ctx.getProperty(Constants.ENV_ADDRESSING_NAMESPACE_URI);
365 }
366 if (nsURI == null) {
367 nsURI = DEFAULT_ADDRESSING_NS;
368 }
369 return nsURI;
370 }
371
372
373
374
375
376
377
378
379 public static String getFaultActionURI() {
380 return getAddressingVersion().getFaultActionURI();
381 }
382
383
384
385
386
387
388
389
390 public static String getAnonymousRoleURI() {
391 AddressingVersion version = getAddressingVersion();
392 return version.getAnonymousRoleURI();
393 }
394
395
396
397
398
399
400
401
402 public static String getAnonymousRoleURI(MessageContext ctx) {
403 return getAddressingVersion(ctx).getAnonymousRoleURI();
404 }
405
406
407
408
409
410
411
412 public static QName getResponseRelationshipType() {
413 return getAddressingVersion().getResponseRelationshipType();
414 }
415
416
417
418
419
420
421
422
423 public static QName getResponseRelationshipType(MessageContext ctx) {
424 return getAddressingVersion(ctx).getResponseRelationshipType();
425 }
426
427
428
429
430
431
432
433
434
435
436
437
438 public static EndpointReference parseReference(String epr) throws Exception {
439 Document doc = XMLUtils.newDocument(new InputSource(new StringReader(epr)));
440 Element elem = doc.getDocumentElement();
441 return new EndpointReference(elem);
442 }
443
444
445
446
447
448
449
450
451
452 public static String toString(EndpointReference epr, boolean prettyPrint)
453 throws Exception {
454
455 if (epr == null) {
456 return null;
457 } else {
458 Document doc = XMLUtils.newDocument();
459 Element elem = epr.toDOM(doc, "wsa:EndpointReference");
460
461 StringWriter writer = new StringWriter();
462 DOM2Writer.serializeAsXML(elem, writer, true, prettyPrint);
463 return writer.toString();
464 }
465 }
466
467
468
469
470
471
472
473
474 private static AddressingHeaders getHeaders(MessageContext ctx, String type) {
475 AddressingHeaders headers = (AddressingHeaders) ctx.getProperty(type);
476
477 if (headers == null) {
478 headers = new AddressingHeaders();
479 headers.isW3CVersion = getAddressingVersion(ctx).isW3C();
480
481
482 ctx.setProperty(type, headers);
483 }
484
485 return headers;
486 }
487
488
489
490
491
492
493
494
495
496
497
498 private static String getAction(AttributeExtensible inputOutput,
499 String inputOutputName, QName portTypeQName, Operation operation,
500 String defaultName) {
501
502 Object value = inputOutput.getExtensionAttribute(new QName(
503 getAddressingNamespaceURI(), Constants.WSDL_ATTRIB_ACTION));
504 if (value != null) {
505
506 if (value instanceof QName) {
507 return ((QName) value).getLocalPart();
508 } else {
509 return value.toString();
510 }
511 }
512 String name = inputOutputName;
513 if (name == null) {
514 name = operation.getName() + defaultName;
515 }
516 StringBuffer buf = new StringBuffer();
517 buf.append(portTypeQName.getNamespaceURI());
518 if (!portTypeQName.getNamespaceURI().endsWith("/")) {
519 buf.append("/");
520 }
521 buf.append(portTypeQName.getLocalPart()).append("/").append(name);
522 return buf.toString();
523 }
524
525 }