001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: HeaderImpl.java,v 1.2 2007/07/16 16:41:23 ofung Exp $
022: * $Revision: 1.2 $
023: * $Date: 2007/07/16 16:41:23 $
024: */
025:
026: /*
027: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028: *
029: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030: *
031: * The contents of this file are subject to the terms of either the GNU
032: * General Public License Version 2 only ("GPL") or the Common Development
033: * and Distribution License("CDDL") (collectively, the "License"). You
034: * may not use this file except in compliance with the License. You can obtain
035: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
037: * language governing permissions and limitations under the License.
038: *
039: * When distributing the software, include this License Header Notice in each
040: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041: * Sun designates this particular file as subject to the "Classpath" exception
042: * as provided by Sun in the GPL Version 2 section of the License file that
043: * accompanied this code. If applicable, add the following below the License
044: * Header, with the fields enclosed by brackets [] replaced by your own
045: * identifying information: "Portions Copyrighted [year]
046: * [name of copyright owner]"
047: *
048: * Contributor(s):
049: *
050: * If you wish your version of this file to be governed by only the CDDL or
051: * only the GPL Version 2, indicate your decision by adding "[Contributor]
052: * elects to include this software in this distribution under the [CDDL or GPL
053: * Version 2] license." If you don't indicate a single choice of license, a
054: * recipient has the option to distribute your version of this file under
055: * either the CDDL, the GPL Version 2 or to extend the choice of license to
056: * its licensees as provided above. However, if you add GPL Version 2 code
057: * and therefore, elected the GPL Version 2 license, then the option applies
058: * only if the new code is made subject to such option by the copyright
059: * holder.
060: */
061: package com.sun.xml.messaging.saaj.soap.impl;
062:
063: import java.util.*;
064: import java.util.logging.Level;
065:
066: import javax.xml.namespace.QName;
067: import javax.xml.soap.*;
068:
069: import org.w3c.dom.Element;
070:
071: import com.sun.xml.messaging.saaj.SOAPExceptionImpl;
072: import com.sun.xml.messaging.saaj.soap.SOAPDocument;
073: import com.sun.xml.messaging.saaj.soap.SOAPDocumentImpl;
074: import com.sun.xml.messaging.saaj.soap.name.NameImpl;
075:
076: public abstract class HeaderImpl extends ElementImpl implements
077: SOAPHeader {
078: protected static final boolean MUST_UNDERSTAND_ONLY = false;
079:
080: protected HeaderImpl(SOAPDocumentImpl ownerDoc, NameImpl name) {
081: super (ownerDoc, name);
082: }
083:
084: protected abstract SOAPHeaderElement createHeaderElement(Name name)
085: throws SOAPException;
086:
087: protected abstract SOAPHeaderElement createHeaderElement(QName name)
088: throws SOAPException;
089:
090: protected abstract NameImpl getNotUnderstoodName();
091:
092: protected abstract NameImpl getUpgradeName();
093:
094: protected abstract NameImpl getSupportedEnvelopeName();
095:
096: public SOAPHeaderElement addHeaderElement(Name name)
097: throws SOAPException {
098: SOAPElement newHeaderElement = ElementFactory
099: .createNamedElement(((SOAPDocument) getOwnerDocument())
100: .getDocument(), name.getLocalName(), name
101: .getPrefix(), name.getURI());
102: if (newHeaderElement == null
103: || !(newHeaderElement instanceof SOAPHeaderElement)) {
104: newHeaderElement = createHeaderElement(name);
105: }
106:
107: // header elements must be namespace qualified
108: // check that URI is not empty, ensuring that the element is NS qualified.
109: String uri = newHeaderElement.getElementQName()
110: .getNamespaceURI();
111: if ((uri == null) || ("").equals(uri)) {
112: log.severe("SAAJ0131.impl.header.elems.ns.qualified");
113: throw new SOAPExceptionImpl(
114: "HeaderElements must be namespace qualified");
115: }
116: addNode(newHeaderElement);
117: return (SOAPHeaderElement) newHeaderElement;
118: }
119:
120: public SOAPHeaderElement addHeaderElement(QName name)
121: throws SOAPException {
122: SOAPElement newHeaderElement = ElementFactory
123: .createNamedElement(((SOAPDocument) getOwnerDocument())
124: .getDocument(), name.getLocalPart(), name
125: .getPrefix(), name.getNamespaceURI());
126: if (newHeaderElement == null
127: || !(newHeaderElement instanceof SOAPHeaderElement)) {
128: newHeaderElement = createHeaderElement(name);
129: }
130:
131: // header elements must be namespace qualified
132: // check that URI is not empty, ensuring that the element is NS qualified.
133: String uri = newHeaderElement.getElementQName()
134: .getNamespaceURI();
135: if ((uri == null) || ("").equals(uri)) {
136: log.severe("SAAJ0131.impl.header.elems.ns.qualified");
137: throw new SOAPExceptionImpl(
138: "HeaderElements must be namespace qualified");
139: }
140: addNode(newHeaderElement);
141: return (SOAPHeaderElement) newHeaderElement;
142: }
143:
144: protected SOAPElement addElement(Name name) throws SOAPException {
145: return addHeaderElement(name);
146: }
147:
148: protected SOAPElement addElement(QName name) throws SOAPException {
149: return addHeaderElement(name);
150: }
151:
152: public Iterator examineHeaderElements(String actor) {
153: return getHeaderElementsForActor(actor, false, false);
154: }
155:
156: public Iterator extractHeaderElements(String actor) {
157: return getHeaderElementsForActor(actor, true, false);
158: }
159:
160: protected Iterator getHeaderElementsForActor(String actor,
161: boolean detach, boolean mustUnderstand) {
162: if (actor == null || actor.equals("")) {
163: log.severe("SAAJ0132.impl.invalid.value.for.actor.or.role");
164: throw new IllegalArgumentException(
165: "Invalid value for actor or role");
166: }
167: return getHeaderElements(actor, detach, mustUnderstand);
168: }
169:
170: protected Iterator getHeaderElements(String actor, boolean detach,
171: boolean mustUnderstand) {
172: List elementList = new ArrayList();
173:
174: Iterator eachChild = getChildElements();
175:
176: Object currentChild = iterate(eachChild);
177: while (currentChild != null) {
178: if (!(currentChild instanceof SOAPHeaderElement)) {
179: currentChild = iterate(eachChild);
180: } else {
181: HeaderElementImpl currentElement = (HeaderElementImpl) currentChild;
182: currentChild = iterate(eachChild);
183:
184: boolean isMustUnderstandMatching = (!mustUnderstand || currentElement
185: .getMustUnderstand());
186: boolean doAdd = false;
187: if (actor == null && isMustUnderstandMatching) {
188: doAdd = true;
189: } else {
190: String currentActor = currentElement
191: .getActorOrRole();
192: if (currentActor == null) {
193: currentActor = "";
194: }
195:
196: if (currentActor.equalsIgnoreCase(actor)
197: && isMustUnderstandMatching) {
198: doAdd = true;
199: }
200: }
201:
202: if (doAdd) {
203: elementList.add(currentElement);
204: if (detach) {
205: currentElement.detachNode();
206: }
207: }
208: }
209: }
210:
211: return elementList.listIterator();
212: }
213:
214: private Object iterate(Iterator each) {
215: return each.hasNext() ? each.next() : null;
216: }
217:
218: public void setParentElement(SOAPElement element)
219: throws SOAPException {
220: if (!(element instanceof SOAPEnvelope)) {
221: log.severe("SAAJ0133.impl.header.parent.mustbe.envelope");
222: throw new SOAPException(
223: "Parent of SOAPHeader has to be a SOAPEnvelope");
224: }
225: super .setParentElement(element);
226: }
227:
228: // overriding ElementImpl's method to ensure that HeaderElements are
229: // namespace qualified. Holds for both SOAP versions.
230: // TODO - This check needs to be made for other addChildElement() methods
231: // as well.
232: public SOAPElement addChildElement(String localName)
233: throws SOAPException {
234:
235: SOAPElement element = super .addChildElement(localName);
236: // check that URI is not empty, ensuring that the element is NS qualified.
237: String uri = element.getElementName().getURI();
238: if ((uri == null) || ("").equals(uri)) {
239: log.severe("SAAJ0134.impl.header.elems.ns.qualified");
240: throw new SOAPExceptionImpl(
241: "HeaderElements must be namespace qualified");
242: }
243: return element;
244: }
245:
246: public Iterator examineAllHeaderElements() {
247: return getHeaderElements(null, false, MUST_UNDERSTAND_ONLY);
248: }
249:
250: public Iterator examineMustUnderstandHeaderElements(String actor) {
251: return getHeaderElements(actor, false, true);
252:
253: }
254:
255: public Iterator extractAllHeaderElements() {
256: return getHeaderElements(null, true, false);
257: }
258:
259: public SOAPHeaderElement addUpgradeHeaderElement(
260: Iterator supportedSoapUris) throws SOAPException {
261: if (supportedSoapUris == null) {
262: log.severe("SAAJ0411.ver1_2.no.null.supportedURIs");
263: throw new SOAPException(
264: "Argument cannot be null; iterator of supportedURIs cannot be null");
265: }
266: if (!supportedSoapUris.hasNext()) {
267: log
268: .severe("SAAJ0412.ver1_2.no.empty.list.of.supportedURIs");
269: throw new SOAPException(
270: "List of supported URIs cannot be empty");
271: }
272: Name upgradeName = getUpgradeName();
273: SOAPHeaderElement upgradeHeaderElement = (SOAPHeaderElement) addChildElement(upgradeName);
274: Name supportedEnvelopeName = getSupportedEnvelopeName();
275: int i = 0;
276: while (supportedSoapUris.hasNext()) {
277: SOAPElement subElement = upgradeHeaderElement
278: .addChildElement(supportedEnvelopeName);
279: String ns = "ns" + Integer.toString(i);
280: subElement.addAttribute(NameImpl
281: .createFromUnqualifiedName("qname"), ns
282: + ":Envelope");
283: subElement.addNamespaceDeclaration(ns,
284: (String) supportedSoapUris.next());
285: i++;
286: }
287: return upgradeHeaderElement;
288: }
289:
290: public SOAPHeaderElement addUpgradeHeaderElement(
291: String supportedSoapUri) throws SOAPException {
292: return addUpgradeHeaderElement(new String[] { supportedSoapUri });
293: }
294:
295: public SOAPHeaderElement addUpgradeHeaderElement(
296: String[] supportedSoapUris) throws SOAPException {
297:
298: if (supportedSoapUris == null) {
299: log.severe("SAAJ0411.ver1_2.no.null.supportedURIs");
300: throw new SOAPException(
301: "Argument cannot be null; array of supportedURIs cannot be null");
302: }
303: if (supportedSoapUris.length == 0) {
304: log
305: .severe("SAAJ0412.ver1_2.no.empty.list.of.supportedURIs");
306: throw new SOAPException(
307: "List of supported URIs cannot be empty");
308: }
309: Name upgradeName = getUpgradeName();
310: SOAPHeaderElement upgradeHeaderElement = (SOAPHeaderElement) addChildElement(upgradeName);
311: Name supportedEnvelopeName = getSupportedEnvelopeName();
312: for (int i = 0; i < supportedSoapUris.length; i++) {
313: SOAPElement subElement = upgradeHeaderElement
314: .addChildElement(supportedEnvelopeName);
315: String ns = "ns" + Integer.toString(i);
316: subElement.addAttribute(NameImpl
317: .createFromUnqualifiedName("qname"), ns
318: + ":Envelope");
319: subElement
320: .addNamespaceDeclaration(ns, supportedSoapUris[i]);
321: }
322: return upgradeHeaderElement;
323: }
324:
325: protected SOAPElement convertToSoapElement(Element element) {
326: if (element instanceof SOAPHeaderElement) {
327: return (SOAPElement) element;
328: } else {
329: SOAPHeaderElement headerElement;
330: try {
331: headerElement = createHeaderElement(NameImpl
332: .copyElementName(element));
333: } catch (SOAPException e) {
334: throw new ClassCastException(
335: "Could not convert Element to SOAPHeaderElement: "
336: + e.getMessage());
337: }
338: return replaceElementWithSOAPElement(element,
339: (ElementImpl) headerElement);
340: }
341: }
342:
343: public SOAPElement setElementQName(QName newName)
344: throws SOAPException {
345: log.log(Level.SEVERE,
346: "SAAJ0146.impl.invalid.name.change.requested",
347: new Object[] { elementQName.getLocalPart(),
348: newName.getLocalPart() });
349: throw new SOAPException("Cannot change name for "
350: + elementQName.getLocalPart() + " to "
351: + newName.getLocalPart());
352: }
353:
354: }
|