001: /*
002: * $Id: HeaderImpl.java,v 1.33 2006/01/27 12:49:35 vj135062 Exp $
003: * $Revision: 1.33 $
004: * $Date: 2006/01/27 12:49:35 $
005: */
006:
007: /*
008: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
009: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
010: *
011: * This code is free software; you can redistribute it and/or modify it
012: * under the terms of the GNU General Public License version 2 only, as
013: * published by the Free Software Foundation. Sun designates this
014: * particular file as subject to the "Classpath" exception as provided
015: * by Sun in the LICENSE file that accompanied this code.
016: *
017: * This code is distributed in the hope that it will be useful, but WITHOUT
018: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
019: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
020: * version 2 for more details (a copy is included in the LICENSE file that
021: * accompanied this code).
022: *
023: * You should have received a copy of the GNU General Public License version
024: * 2 along with this work; if not, write to the Free Software Foundation,
025: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
026: *
027: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
028: * CA 95054 USA or visit www.sun.com if you need additional information or
029: * have any questions.
030: */
031: package com.sun.xml.internal.messaging.saaj.soap.impl;
032:
033: import java.util.*;
034: import java.util.logging.Level;
035:
036: import javax.xml.namespace.QName;
037: import javax.xml.soap.*;
038:
039: import org.w3c.dom.Element;
040:
041: import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
042: import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
043: import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
044: import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
045:
046: public abstract class HeaderImpl extends ElementImpl implements
047: SOAPHeader {
048: protected static final boolean MUST_UNDERSTAND_ONLY = false;
049:
050: protected HeaderImpl(SOAPDocumentImpl ownerDoc, NameImpl name) {
051: super (ownerDoc, name);
052: }
053:
054: protected abstract SOAPHeaderElement createHeaderElement(Name name)
055: throws SOAPException;
056:
057: protected abstract SOAPHeaderElement createHeaderElement(QName name)
058: throws SOAPException;
059:
060: protected abstract NameImpl getNotUnderstoodName();
061:
062: protected abstract NameImpl getUpgradeName();
063:
064: protected abstract NameImpl getSupportedEnvelopeName();
065:
066: public SOAPHeaderElement addHeaderElement(Name name)
067: throws SOAPException {
068: SOAPElement newHeaderElement = ElementFactory
069: .createNamedElement(((SOAPDocument) getOwnerDocument())
070: .getDocument(), name.getLocalName(), name
071: .getPrefix(), name.getURI());
072: if (newHeaderElement == null
073: || !(newHeaderElement instanceof SOAPHeaderElement)) {
074: newHeaderElement = createHeaderElement(name);
075: }
076:
077: // header elements must be namespace qualified
078: // check that URI is not empty, ensuring that the element is NS qualified.
079: String uri = newHeaderElement.getElementQName()
080: .getNamespaceURI();
081: if ((uri == null) || ("").equals(uri)) {
082: log.severe("SAAJ0131.impl.header.elems.ns.qualified");
083: throw new SOAPExceptionImpl(
084: "HeaderElements must be namespace qualified");
085: }
086: addNode(newHeaderElement);
087: return (SOAPHeaderElement) newHeaderElement;
088: }
089:
090: public SOAPHeaderElement addHeaderElement(QName name)
091: throws SOAPException {
092: SOAPElement newHeaderElement = ElementFactory
093: .createNamedElement(((SOAPDocument) getOwnerDocument())
094: .getDocument(), name.getLocalPart(), name
095: .getPrefix(), name.getNamespaceURI());
096: if (newHeaderElement == null
097: || !(newHeaderElement instanceof SOAPHeaderElement)) {
098: newHeaderElement = createHeaderElement(name);
099: }
100:
101: // header elements must be namespace qualified
102: // check that URI is not empty, ensuring that the element is NS qualified.
103: String uri = newHeaderElement.getElementQName()
104: .getNamespaceURI();
105: if ((uri == null) || ("").equals(uri)) {
106: log.severe("SAAJ0131.impl.header.elems.ns.qualified");
107: throw new SOAPExceptionImpl(
108: "HeaderElements must be namespace qualified");
109: }
110: addNode(newHeaderElement);
111: return (SOAPHeaderElement) newHeaderElement;
112: }
113:
114: protected SOAPElement addElement(Name name) throws SOAPException {
115: return addHeaderElement(name);
116: }
117:
118: protected SOAPElement addElement(QName name) throws SOAPException {
119: return addHeaderElement(name);
120: }
121:
122: public Iterator examineHeaderElements(String actor) {
123: return getHeaderElementsForActor(actor, false, false);
124: }
125:
126: public Iterator extractHeaderElements(String actor) {
127: return getHeaderElementsForActor(actor, true, false);
128: }
129:
130: protected Iterator getHeaderElementsForActor(String actor,
131: boolean detach, boolean mustUnderstand) {
132: if (actor == null || actor.equals("")) {
133: log.severe("SAAJ0132.impl.invalid.value.for.actor.or.role");
134: throw new IllegalArgumentException(
135: "Invalid value for actor or role");
136: }
137: return getHeaderElements(actor, detach, mustUnderstand);
138: }
139:
140: protected Iterator getHeaderElements(String actor, boolean detach,
141: boolean mustUnderstand) {
142: List elementList = new ArrayList();
143:
144: Iterator eachChild = getChildElements();
145:
146: Object currentChild = iterate(eachChild);
147: while (currentChild != null) {
148: if (!(currentChild instanceof SOAPHeaderElement)) {
149: currentChild = iterate(eachChild);
150: } else {
151: HeaderElementImpl currentElement = (HeaderElementImpl) currentChild;
152: currentChild = iterate(eachChild);
153:
154: boolean isMustUnderstandMatching = (!mustUnderstand || currentElement
155: .getMustUnderstand());
156: boolean doAdd = false;
157: if (actor == null && isMustUnderstandMatching) {
158: doAdd = true;
159: } else {
160: String currentActor = currentElement
161: .getActorOrRole();
162: if (currentActor == null) {
163: currentActor = "";
164: }
165:
166: if (currentActor.equalsIgnoreCase(actor)
167: && isMustUnderstandMatching) {
168: doAdd = true;
169: }
170: }
171:
172: if (doAdd) {
173: elementList.add(currentElement);
174: if (detach) {
175: currentElement.detachNode();
176: }
177: }
178: }
179: }
180:
181: return elementList.listIterator();
182: }
183:
184: private Object iterate(Iterator each) {
185: return each.hasNext() ? each.next() : null;
186: }
187:
188: public void setParentElement(SOAPElement element)
189: throws SOAPException {
190: if (!(element instanceof SOAPEnvelope)) {
191: log.severe("SAAJ0133.impl.header.parent.mustbe.envelope");
192: throw new SOAPException(
193: "Parent of SOAPHeader has to be a SOAPEnvelope");
194: }
195: super .setParentElement(element);
196: }
197:
198: // overriding ElementImpl's method to ensure that HeaderElements are
199: // namespace qualified. Holds for both SOAP versions.
200: // TODO - This check needs to be made for other addChildElement() methods
201: // as well.
202: public SOAPElement addChildElement(String localName)
203: throws SOAPException {
204:
205: SOAPElement element = super .addChildElement(localName);
206: // check that URI is not empty, ensuring that the element is NS qualified.
207: String uri = element.getElementName().getURI();
208: if ((uri == null) || ("").equals(uri)) {
209: log.severe("SAAJ0134.impl.header.elems.ns.qualified");
210: throw new SOAPExceptionImpl(
211: "HeaderElements must be namespace qualified");
212: }
213: return element;
214: }
215:
216: public Iterator examineAllHeaderElements() {
217: return getHeaderElements(null, false, MUST_UNDERSTAND_ONLY);
218: }
219:
220: public Iterator examineMustUnderstandHeaderElements(String actor) {
221: return getHeaderElements(actor, false, true);
222:
223: }
224:
225: public Iterator extractAllHeaderElements() {
226: return getHeaderElements(null, true, false);
227: }
228:
229: public SOAPHeaderElement addUpgradeHeaderElement(
230: Iterator supportedSoapUris) throws SOAPException {
231: if (supportedSoapUris == null) {
232: log.severe("SAAJ0411.ver1_2.no.null.supportedURIs");
233: throw new SOAPException(
234: "Argument cannot be null; iterator of supportedURIs cannot be null");
235: }
236: if (!supportedSoapUris.hasNext()) {
237: log
238: .severe("SAAJ0412.ver1_2.no.empty.list.of.supportedURIs");
239: throw new SOAPException(
240: "List of supported URIs cannot be empty");
241: }
242: Name upgradeName = getUpgradeName();
243: SOAPHeaderElement upgradeHeaderElement = (SOAPHeaderElement) addChildElement(upgradeName);
244: Name supportedEnvelopeName = getSupportedEnvelopeName();
245: int i = 0;
246: while (supportedSoapUris.hasNext()) {
247: SOAPElement subElement = upgradeHeaderElement
248: .addChildElement(supportedEnvelopeName);
249: String ns = "ns" + Integer.toString(i);
250: subElement.addAttribute(NameImpl
251: .createFromUnqualifiedName("qname"), ns
252: + ":Envelope");
253: subElement.addNamespaceDeclaration(ns,
254: (String) supportedSoapUris.next());
255: i++;
256: }
257: return upgradeHeaderElement;
258: }
259:
260: public SOAPHeaderElement addUpgradeHeaderElement(
261: String supportedSoapUri) throws SOAPException {
262: return addUpgradeHeaderElement(new String[] { supportedSoapUri });
263: }
264:
265: public SOAPHeaderElement addUpgradeHeaderElement(
266: String[] supportedSoapUris) throws SOAPException {
267:
268: if (supportedSoapUris == null) {
269: log.severe("SAAJ0411.ver1_2.no.null.supportedURIs");
270: throw new SOAPException(
271: "Argument cannot be null; array of supportedURIs cannot be null");
272: }
273: if (supportedSoapUris.length == 0) {
274: log
275: .severe("SAAJ0412.ver1_2.no.empty.list.of.supportedURIs");
276: throw new SOAPException(
277: "List of supported URIs cannot be empty");
278: }
279: Name upgradeName = getUpgradeName();
280: SOAPHeaderElement upgradeHeaderElement = (SOAPHeaderElement) addChildElement(upgradeName);
281: Name supportedEnvelopeName = getSupportedEnvelopeName();
282: for (int i = 0; i < supportedSoapUris.length; i++) {
283: SOAPElement subElement = upgradeHeaderElement
284: .addChildElement(supportedEnvelopeName);
285: String ns = "ns" + Integer.toString(i);
286: subElement.addAttribute(NameImpl
287: .createFromUnqualifiedName("qname"), ns
288: + ":Envelope");
289: subElement
290: .addNamespaceDeclaration(ns, supportedSoapUris[i]);
291: }
292: return upgradeHeaderElement;
293: }
294:
295: protected SOAPElement convertToSoapElement(Element element) {
296: if (element instanceof SOAPHeaderElement) {
297: return (SOAPElement) element;
298: } else {
299: SOAPHeaderElement headerElement;
300: try {
301: headerElement = createHeaderElement(NameImpl
302: .copyElementName(element));
303: } catch (SOAPException e) {
304: throw new ClassCastException(
305: "Could not convert Element to SOAPHeaderElement: "
306: + e.getMessage());
307: }
308: return replaceElementWithSOAPElement(element,
309: (ElementImpl) headerElement);
310: }
311: }
312:
313: public SOAPElement setElementQName(QName newName)
314: throws SOAPException {
315: log.log(Level.SEVERE,
316: "SAAJ0146.impl.invalid.name.change.requested",
317: new Object[] { elementQName.getLocalPart(),
318: newName.getLocalPart() });
319: throw new SOAPException("Cannot change name for "
320: + elementQName.getLocalPart() + " to "
321: + newName.getLocalPart());
322: }
323:
324: }
|