1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.messaging.saaj.soap.impl;
27
28 import java.util.*;
29 import java.util.logging.Level;
30
31 import javax.xml.namespace.QName;
32 import javax.xml.soap.*;
33
34 import org.w3c.dom.Element;
35 import org.w3c.dom.Node;
36
37 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
38 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
39 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
40 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
41
42 public abstract class HeaderImpl extends ElementImpl implements SOAPHeader {
43 protected static final boolean MUST_UNDERSTAND_ONLY = false;
44
45 protected HeaderImpl(SOAPDocumentImpl ownerDoc, NameImpl name) {
46 super(ownerDoc, name);
47 }
48
49 protected abstract SOAPHeaderElement createHeaderElement(Name name)
50 throws SOAPException;
51 protected abstract SOAPHeaderElement createHeaderElement(QName name)
52 throws SOAPException;
53 protected abstract NameImpl getNotUnderstoodName();
54 protected abstract NameImpl getUpgradeName();
55 protected abstract NameImpl getSupportedEnvelopeName();
99 addNode(newHeaderElement);
100 return (SOAPHeaderElement) newHeaderElement;
101 }
102
103 protected SOAPElement addElement(Name name) throws SOAPException {
104 return addHeaderElement(name);
105 }
106
107 protected SOAPElement addElement(QName name) throws SOAPException {
108 return addHeaderElement(name);
109 }
110
111 public Iterator examineHeaderElements(String actor) {
112 return getHeaderElementsForActor(actor, false, false);
113 }
114
115 public Iterator extractHeaderElements(String actor) {
116 return getHeaderElementsForActor(actor, true, false);
117 }
118
119 protected Iterator getHeaderElementsForActor(
120 String actor,
121 boolean detach,
122 boolean mustUnderstand) {
123 if (actor == null || actor.equals("")) {
124 log.severe("SAAJ0132.impl.invalid.value.for.actor.or.role");
125 throw new IllegalArgumentException("Invalid value for actor or role");
126 }
127 return getHeaderElements(actor, detach, mustUnderstand);
128 }
129
130 protected Iterator getHeaderElements(
131 String actor,
132 boolean detach,
133 boolean mustUnderstand) {
134 List elementList = new ArrayList();
135
136 Iterator eachChild = getChildElements();
137
138 Object currentChild = iterate(eachChild);
139 while (currentChild != null) {
140 if (!(currentChild instanceof SOAPHeaderElement)) {
141 currentChild = iterate(eachChild);
142 } else {
143 HeaderElementImpl currentElement =
144 (HeaderElementImpl) currentChild;
145 currentChild = iterate(eachChild);
146
147 boolean isMustUnderstandMatching =
148 (!mustUnderstand || currentElement.getMustUnderstand());
149 boolean doAdd = false;
150 if (actor == null && isMustUnderstandMatching) {
151 doAdd = true;
152 } else {
153 String currentActor = currentElement.getActorOrRole();
154 if (currentActor == null) {
155 currentActor = "";
156 }
157
158 if (currentActor.equalsIgnoreCase(actor)
159 && isMustUnderstandMatching) {
160 doAdd = true;
161 }
162 }
163
164 if (doAdd) {
165 elementList.add(currentElement);
166 if (detach) {
167 currentElement.detachNode();
168 }
169 }
170 }
171 }
172
173 return elementList.listIterator();
174 }
175
176 private Object iterate(Iterator each) {
177 return each.hasNext() ? each.next() : null;
178 }
179
180 public void setParentElement(SOAPElement element) throws SOAPException {
181 if (!(element instanceof SOAPEnvelope)) {
182 log.severe("SAAJ0133.impl.header.parent.mustbe.envelope");
183 throw new SOAPException("Parent of SOAPHeader has to be a SOAPEnvelope");
184 }
185 super.setParentElement(element);
186 }
187
188 // overriding ElementImpl's method to ensure that HeaderElements are
189 // namespace qualified. Holds for both SOAP versions.
190 // TODO - This check needs to be made for other addChildElement() methods
191 // as well.
192 public SOAPElement addChildElement(String localName) throws SOAPException {
193
194 SOAPElement element = super.addChildElement(localName);
195 // check that URI is not empty, ensuring that the element is NS qualified.
196 String uri = element.getElementName().getURI();
|
1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.messaging.saaj.soap.impl;
27
28 import java.util.*;
29 import java.util.logging.Level;
30
31 import javax.xml.namespace.QName;
32 import javax.xml.soap.*;
33
34 import org.w3c.dom.Element;
35
36 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
37 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
38 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
39 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
40
41 public abstract class HeaderImpl extends ElementImpl implements SOAPHeader {
42 protected static final boolean MUST_UNDERSTAND_ONLY = false;
43
44 protected HeaderImpl(SOAPDocumentImpl ownerDoc, NameImpl name) {
45 super(ownerDoc, name);
46 }
47
48 protected abstract SOAPHeaderElement createHeaderElement(Name name)
49 throws SOAPException;
50 protected abstract SOAPHeaderElement createHeaderElement(QName name)
51 throws SOAPException;
52 protected abstract NameImpl getNotUnderstoodName();
53 protected abstract NameImpl getUpgradeName();
54 protected abstract NameImpl getSupportedEnvelopeName();
98 addNode(newHeaderElement);
99 return (SOAPHeaderElement) newHeaderElement;
100 }
101
102 protected SOAPElement addElement(Name name) throws SOAPException {
103 return addHeaderElement(name);
104 }
105
106 protected SOAPElement addElement(QName name) throws SOAPException {
107 return addHeaderElement(name);
108 }
109
110 public Iterator examineHeaderElements(String actor) {
111 return getHeaderElementsForActor(actor, false, false);
112 }
113
114 public Iterator extractHeaderElements(String actor) {
115 return getHeaderElementsForActor(actor, true, false);
116 }
117
118 protected Iterator<SOAPHeaderElement> getHeaderElementsForActor(
119 String actor,
120 boolean detach,
121 boolean mustUnderstand) {
122 if (actor == null || actor.equals("")) {
123 log.severe("SAAJ0132.impl.invalid.value.for.actor.or.role");
124 throw new IllegalArgumentException("Invalid value for actor or role");
125 }
126 return getHeaderElements(actor, detach, mustUnderstand);
127 }
128
129 protected Iterator<SOAPHeaderElement> getHeaderElements(
130 String actor,
131 boolean detach,
132 boolean mustUnderstand) {
133 List<SOAPHeaderElement> elementList = new ArrayList<SOAPHeaderElement>();
134
135 Iterator<org.w3c.dom.Node> eachChild = getChildElements();
136
137 org.w3c.dom.Node currentChild = iterate(eachChild);
138 while (currentChild != null) {
139 if (!(currentChild instanceof SOAPHeaderElement)) {
140 currentChild = iterate(eachChild);
141 } else {
142 HeaderElementImpl currentElement =
143 (HeaderElementImpl) currentChild;
144 currentChild = iterate(eachChild);
145
146 boolean isMustUnderstandMatching =
147 (!mustUnderstand || currentElement.getMustUnderstand());
148 boolean doAdd = false;
149 if (actor == null && isMustUnderstandMatching) {
150 doAdd = true;
151 } else {
152 String currentActor = currentElement.getActorOrRole();
153 if (currentActor == null) {
154 currentActor = "";
155 }
156
157 if (currentActor.equalsIgnoreCase(actor)
158 && isMustUnderstandMatching) {
159 doAdd = true;
160 }
161 }
162
163 if (doAdd) {
164 elementList.add(currentElement);
165 if (detach) {
166 currentElement.detachNode();
167 }
168 }
169 }
170 }
171
172 return elementList.listIterator();
173 }
174
175 private <T> T iterate(Iterator<T> each) {
176 return each.hasNext() ? each.next() : null;
177 }
178
179 public void setParentElement(SOAPElement element) throws SOAPException {
180 if (!(element instanceof SOAPEnvelope)) {
181 log.severe("SAAJ0133.impl.header.parent.mustbe.envelope");
182 throw new SOAPException("Parent of SOAPHeader has to be a SOAPEnvelope");
183 }
184 super.setParentElement(element);
185 }
186
187 // overriding ElementImpl's method to ensure that HeaderElements are
188 // namespace qualified. Holds for both SOAP versions.
189 // TODO - This check needs to be made for other addChildElement() methods
190 // as well.
191 public SOAPElement addChildElement(String localName) throws SOAPException {
192
193 SOAPElement element = super.addChildElement(localName);
194 // check that URI is not empty, ensuring that the element is NS qualified.
195 String uri = element.getElementName().getURI();
|