1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.axis.message.addressing;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import javax.xml.namespace.QName;
23 import javax.xml.soap.Name;
24 import javax.xml.soap.SOAPEnvelope;
25 import javax.xml.soap.SOAPException;
26 import javax.xml.soap.SOAPHeader;
27 import javax.xml.soap.SOAPHeaderElement;
28
29 import org.apache.axis.AxisFault;
30 import org.apache.axis.MessageContext;
31 import org.apache.axis.message.MessageElement;
32 import org.apache.axis.message.addressing.util.AddressingUtils;
33 import org.apache.axis.message.addressing.util.TextExtractor;
34 import org.apache.axis.types.URI;
35 import org.apache.axis.types.URI.MalformedURIException;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.w3c.dom.Element;
39
40
41
42
43
44
45
46
47
48 public class AddressingHeaders {
49
50
51
52
53 private static final Log LOG = LogFactory.getLog(AddressingHeaders.class);
54
55
56
57
58 public boolean isW3CVersion;
59
60
61
62
63 private Action action;
64
65
66
67
68 private ReplyTo replyTo;
69
70
71
72
73 private To to;
74
75
76
77
78 private FaultTo faultTo;
79
80
81
82
83 private From from;
84
85
86
87
88 private Recipient recipient;
89
90
91
92
93 private MessageID messageID;
94
95
96
97
98 private List<RelatesTo> relatesTo;
99
100
101
102
103 private ReferencePropertiesType referenceProperties;
104
105
106
107
108 private ReferenceParametersType referenceParameters;
109
110
111
112
113 private boolean setMustUnderstand;
114
115
116
117
118 private AddressingHeaderFault addrHeaderFault;
119
120
121
122
123 public AddressingHeaders() {
124 this.referenceProperties = new ReferencePropertiesType();
125 this.referenceParameters = new ReferenceParametersType();
126 this.relatesTo = new ArrayList<RelatesTo>();
127 }
128
129
130
131
132
133
134 public AddressingHeaders(EndpointReference epr) {
135 this.to = new To(epr.getAddress());
136 this.referenceProperties = epr.getProperties();
137 this.referenceParameters = epr.getParameters();
138 }
139
140
141
142
143
144
145
146 public AddressingHeaders(SOAPEnvelope env) throws Exception {
147 this(env, null, true, false, true, new ArrayList<QName>(0));
148 }
149
150
151
152
153
154
155
156
157
158 public AddressingHeaders(SOAPEnvelope env, boolean process, boolean remove)
159 throws Exception {
160 this(env, null, process, remove, true, new ArrayList<QName>(0));
161 }
162
163
164
165
166
167
168
169
170
171
172 public AddressingHeaders(SOAPEnvelope env, String actorURI, boolean process,
173 boolean remove) throws Exception {
174 this(env, actorURI, process, remove, true, new ArrayList<QName>(0));
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public AddressingHeaders(SOAPEnvelope env, String actorURI, boolean process,
197 boolean remove, boolean setMustUnderstand, boolean allowNonSpecificActions,
198 List<QName> refPropsQNames) throws Exception {
199 this();
200
201 this.setMustUnderstand = setMustUnderstand;
202
203 SOAPHeader header = env.getHeader();
204 if (header == null) {
205 return;
206 }
207
208 Iterator<?> iter = header.examineHeaderElements(actorURI);
209
210 while (iter.hasNext()) {
211 SOAPHeaderElement elem = (SOAPHeaderElement) iter.next();
212 if (addHeader(elem, allowNonSpecificActions, refPropsQNames)) {
213
214
215
216
217
218
219
220
221
222 if (remove) {
223 elem.detachNode();
224 }
225
226
227 if (process && elem instanceof org.apache.axis.message.SOAPHeaderElement) {
228 ((org.apache.axis.message.SOAPHeaderElement) elem).setProcessed(true);
229 }
230
231 }
232 }
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 public AddressingHeaders(SOAPEnvelope env, String actorURI, boolean process,
253 boolean remove, boolean setMustUnderstand, List<QName> refPropsQNames)
254 throws Exception {
255 this(env, actorURI, process, remove, setMustUnderstand, false, refPropsQNames);
256 }
257
258
259
260
261
262
263
264
265 private void addAction(SOAPHeaderElement elem, boolean allowNonSpecificActions)
266 throws MalformedURIException {
267
268
269
270 MessageContext msgContext = MessageContext.getCurrentContext();
271 if (msgContext != null) {
272 msgContext.setProperty(Constants.ENV_ADDRESSING_NAMESPACE_URI,
273 elem.getElementName().getURI());
274 }
275
276 if (this.action != null) {
277 this.addrHeaderFault = new AddressingHeaderFault("duplicate Action header",
278 AddressingHeaderFault.INVALID_CARDINALITY, Constants.ACTION);
279 }
280
281 URI uri = new URI(TextExtractor.getText(elem), allowNonSpecificActions);
282 this.action = new Action(uri);
283 }
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298 private boolean addHeader(SOAPHeaderElement headerElement,
299 boolean allowNonSpecificActions, List<QName> refPropsQNames)
300 throws MalformedURIException {
301
302 Name name = headerElement.getElementName();
303
304
305
306
307 AddressingVersion version = AddressingUtils.getAddressingVersion(name.getURI());
308
309 if (version != null) {
310
311 this.isW3CVersion = version.isW3C();
312 String localName = name.getLocalName();
313
314 if (localName.equals(Constants.MESSAGE_ID)) {
315 addMessageID(headerElement);
316 } else if (localName.equals(Constants.TO)) {
317 addTo(headerElement);
318 } else if (localName.equals(Constants.ACTION)) {
319 addAction(headerElement, allowNonSpecificActions);
320 } else if (localName.equals(Constants.FROM)) {
321 addFrom(headerElement);
322 } else if (localName.equals(Constants.REPLY_TO)) {
323 addReplyTo(headerElement);
324 } else if (localName.equals(Constants.FAULT_TO)) {
325 addFaultTo(headerElement);
326 } else if (localName.equals(Constants.RECIPIENT)) {
327 this.recipient = new Recipient(headerElement);
328 } else if (localName.equals(Constants.RELATES_TO)) {
329 this.relatesTo.add(new RelatesTo(headerElement));
330 } else if (localName.equals(Constants.FAULT_DETAIL)) {
331
332 LOG.debug("Not done yet");
333 } else {
334 this.addrHeaderFault = new AddressingHeaderFault(
335 "Unsupported addressing header: " + localName, localName);
336 }
337 return true;
338 } else {
339 this.isW3CVersion = false;
340
341
342
343 return this.checkReferenceP(headerElement, refPropsQNames);
344 }
345 }
346
347
348
349
350
351
352
353 private void addFaultTo(SOAPHeaderElement elem) throws MalformedURIException {
354 if (this.faultTo == null) {
355 this.faultTo = new FaultTo(elem);
356 } else {
357
358 this.faultTo = null;
359 this.addrHeaderFault = new AddressingHeaderFault(
360 AddressingHeaderFault.INVALID_CARDINALITY,
361 Constants.FAULT_TO);
362 }
363 }
364
365
366
367
368
369
370
371 private void addFrom(SOAPHeaderElement elem) throws MalformedURIException {
372 if (this.from == null) {
373 this.from = new From(elem);
374 } else {
375 this.addrHeaderFault = new AddressingHeaderFault(
376 AddressingHeaderFault.INVALID_CARDINALITY,
377 Constants.FROM);
378 }
379 }
380
381
382
383
384
385
386
387 private void addMessageID(SOAPHeaderElement elem) throws MalformedURIException {
388 if (this.messageID == null) {
389 this.messageID = new MessageID(elem);
390 } else {
391 this.addrHeaderFault = new AddressingHeaderFault(
392 AddressingHeaderFault.INVALID_CARDINALITY,
393 Constants.MESSAGE_ID);
394 }
395 }
396
397
398
399
400
401
402 public void addReferenceParameter(Element param) {
403 if (this.referenceParameters == null) {
404 this.referenceParameters = new ReferenceParametersType();
405 }
406 this.referenceParameters.add(param);
407 }
408
409
410
411
412
413
414 public void addReferenceParameter(MessageElement param) {
415 if (this.referenceParameters == null) {
416 this.referenceParameters = new ReferenceParametersType();
417 }
418 this.referenceParameters.add(param);
419 }
420
421
422
423
424
425
426 public void addReferenceProperty(Element prop) {
427 if (this.referenceProperties == null) {
428 this.referenceProperties = new ReferencePropertiesType();
429 }
430 this.referenceProperties.add(prop);
431 }
432
433
434
435
436
437
438 public void addReferenceProperty(MessageElement prop) {
439 if (this.referenceProperties == null) {
440 this.referenceProperties = new ReferencePropertiesType();
441 }
442 this.referenceProperties.add(prop);
443 }
444
445
446
447
448
449
450
451
452 public void addRelatesTo(String uri, QName type) throws URI.MalformedURIException {
453 this.relatesTo.add(new RelatesTo(uri, type));
454 }
455
456
457
458
459
460
461
462
463 public void addRelatesTo(String uri, String type) throws URI.MalformedURIException {
464 this.relatesTo.add(new RelatesTo(uri, type));
465 }
466
467
468
469
470
471
472
473 private void addReplyTo(SOAPHeaderElement elem) throws MalformedURIException {
474 if (this.replyTo == null) {
475 this.replyTo = new ReplyTo(elem);
476 } else {
477 this.addrHeaderFault = new AddressingHeaderFault(
478 AddressingHeaderFault.INVALID_CARDINALITY,
479 Constants.REPLY_TO);
480 }
481 }
482
483
484
485
486
487
488
489 private void addTo(SOAPHeaderElement elem) throws MalformedURIException {
490 if (this.to == null) {
491 this.to = new To(elem);
492 } else {
493 this.addrHeaderFault = new AddressingHeaderFault(
494 AddressingHeaderFault.INVALID_CARDINALITY,
495 Constants.TO);
496 }
497 }
498
499
500
501
502
503
504
505
506
507 private boolean checkReferenceP(SOAPHeaderElement headerElement,
508 List<QName> refPropQNames) {
509 Name name = headerElement.getElementName();
510 QName elementName = new QName(name.getURI(), name.getLocalName());
511
512
513
514
515
516 if (refPropQNames == null || refPropQNames.contains(elementName)) {
517 this.referenceProperties.add(headerElement);
518 return true;
519 } else {
520 return false;
521 }
522 }
523
524
525
526
527
528
529 public Action getAction() {
530 return this.action;
531 }
532
533
534
535
536
537
538 public AxisFault getAddrHeaderFault() {
539 return this.addrHeaderFault;
540 }
541
542
543
544
545
546
547 public EndpointReference getFaultTo() {
548 return this.faultTo;
549 }
550
551
552
553
554
555
556 public EndpointReference getFrom() {
557 return this.from;
558 }
559
560
561
562
563
564
565 public MessageID getMessageID() {
566 return this.messageID;
567 }
568
569
570
571
572
573
574 public ReferenceParametersType getReferenceParameters() {
575 return this.referenceParameters;
576 }
577
578
579
580
581
582
583 public ReferencePropertiesType getReferenceProperties() {
584 return this.referenceProperties;
585 }
586
587
588
589
590
591
592 public List<RelatesTo> getRelatesTo() {
593 return this.relatesTo;
594 }
595
596
597
598
599
600
601 public EndpointReference getReplyTo() {
602 return this.replyTo;
603 }
604
605
606
607
608
609
610 public To getTo() {
611 return this.to;
612 }
613
614
615
616
617
618
619 public boolean isSetMustUnderstand() {
620 return this.setMustUnderstand;
621 }
622
623
624
625
626
627
628
629
630 private SOAPHeaderElement makeSOAPHeader(Object refP) throws SOAPException {
631 SOAPHeaderElement element;
632 if (refP instanceof SOAPHeaderElement) {
633
634 element = (SOAPHeaderElement) refP;
635 } else if (refP instanceof MessageElement) {
636
637 MessageElement msgElement = (MessageElement) refP;
638 try {
639 element = new org.apache.axis.message.SOAPHeaderElement(msgElement.getAsDOM());
640 } catch (SOAPException e) {
641 throw e;
642 } catch (Exception e) {
643 throw new SOAPException(e);
644 }
645 } else if (refP instanceof Element) {
646 Element refPropElement = (Element) refP;
647
648 element = new org.apache.axis.message.SOAPHeaderElement(refPropElement);
649 } else {
650 throw new SOAPException(refP.getClass().getName());
651 }
652 return element;
653 }
654
655
656
657
658
659
660
661
662
663 private void serializeReferencePs(SOAPEnvelope env, List<MessageElement> refPs,
664 String actorURI, boolean addAttribute) throws SOAPException {
665
666 if (refPs == null || refPs.size() == 0) {
667 return;
668 }
669
670 SOAPHeaderElement element;
671 SOAPHeader header = env.getHeader();
672 if (header == null) {
673 header = env.addHeader();
674 }
675
676
677 for (MessageElement refProp : refPs) {
678 element = this.makeSOAPHeader(refProp);
679 element.setActor(actorURI);
680
681
682 if (addAttribute) {
683 element.addAttribute(Constants.ATTR_REFP, "true");
684 }
685 header.addChildElement(element);
686 }
687 }
688
689
690
691
692
693
694 public void setAction(Action action) {
695 this.action = action;
696 }
697
698
699
700
701
702
703
704 public void setAction(String uri) throws Exception {
705 this.action = new Action(new URI(uri));
706 }
707
708
709
710
711
712
713 public void setAction(URI uri) {
714 this.action = (uri == null) ? null : new Action(uri);
715 }
716
717
718
719
720
721
722 public void setFaultTo(EndpointReference epr) {
723 this.faultTo = new FaultTo(epr);
724 }
725
726
727
728
729
730
731 public void setFrom(EndpointReference ref) {
732 this.from = new From(ref);
733 }
734
735
736
737
738
739
740 public void setMessageID(MessageID messageID) {
741 this.messageID = messageID;
742 }
743
744
745
746
747
748
749 public void setReferenceParameters(ReferenceParametersType params) {
750 this.referenceParameters = params;
751 }
752
753
754
755
756
757
758 public void setReferenceProperties(ReferencePropertiesType props) {
759 this.referenceProperties = props;
760 }
761
762
763
764
765
766
767
768 public void setRelatesTo(List<RelatesTo> v) {
769 this.relatesTo.clear();
770 this.relatesTo.addAll(v);
771 }
772
773
774
775
776
777
778 public void setReplyTo(EndpointReference ref) {
779 this.replyTo = new ReplyTo(ref);
780 }
781
782
783
784
785
786
787 public void setSetMustUnderstand(boolean setMustUnderstand) {
788 this.setMustUnderstand = setMustUnderstand;
789 }
790
791
792
793
794
795
796 public void setTo(AttributedURI uri) {
797 this.to = new To(uri);
798 }
799
800
801
802
803
804
805 public void setTo(To to) {
806 this.to = to;
807 }
808
809
810
811
812
813
814
815 public void toEnvelope(SOAPEnvelope env) throws Exception {
816 this.toEnvelope(env, null);
817 }
818
819
820
821
822
823
824
825
826 public void toEnvelope(SOAPEnvelope env, String actorURI) throws SOAPException {
827
828 AddressingVersion version = AddressingUtils.getAddressingVersion();
829
830 if (env.getNamespaceURI(Constants.NS_PREFIX_ADDRESSING) == null) {
831 env.addNamespaceDeclaration(Constants.NS_PREFIX_ADDRESSING,
832 version.getNamespace());
833 }
834
835 AddressingUtils.removeHeaders(env.getHeader(), actorURI);
836
837 addItem(version, env, actorURI, this.messageID);
838 addItem(version, env, actorURI, this.to);
839 addItem(version, env, actorURI, this.action);
840 addItem(version, env, actorURI, this.from);
841 addItem(version, env, actorURI, this.replyTo);
842 addItem(version, env, actorURI, this.faultTo);
843 addItem(version, env, actorURI, this.recipient);
844
845 if (this.relatesTo != null) {
846 for (int i = 0; i < this.relatesTo.size(); i++) {
847 AddressingHeaderItem item = this.relatesTo.get(i);
848 addItem(version, env, actorURI, item);
849 }
850 }
851
852 if (this.referenceProperties != null && this.referenceProperties.size() > 0) {
853 this.serializeReferencePs(env, this.referenceProperties, actorURI, false);
854 }
855
856 if (this.referenceParameters != null && this.referenceParameters.size() > 0) {
857 this.serializeReferencePs(env, this.referenceParameters, actorURI,
858 this.isW3CVersion);
859 }
860 }
861
862
863
864
865
866
867
868
869
870
871 private void addItem(AddressingVersion version, SOAPEnvelope env, String actorURI,
872 AddressingHeaderItem item) throws SOAPException {
873 if (item != null) {
874 SOAPHeaderElement elem = item.toSOAPHeaderElement(version, env, actorURI);
875 elem.setMustUnderstand(this.setMustUnderstand);
876 }
877 }
878 }