001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.xml.saaj;
031:
032: import java.util.*;
033:
034: import javax.xml.XMLConstants;
035: import javax.xml.namespace.*;
036: import javax.xml.soap.*;
037:
038: public class SOAP11HeaderImpl extends SOAPElementImpl implements
039: SOAPHeader {
040: private static final NameImpl QNAME_NAME = new NameImpl("qname");
041:
042: protected static final NameImpl SOAP_1_1_SUPPORTED_ENVELOPE_NAME = new NameImpl(
043: SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE,
044: "SupportedEnvelope", SOAPConstants.SOAP_ENV_PREFIX);
045:
046: protected static final NameImpl SOAP_1_2_SUPPORTED_ENVELOPE_NAME = new NameImpl(
047: SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE,
048: "SupportedEnvelope", SOAPConstants.SOAP_ENV_PREFIX);
049:
050: protected NameImpl _supportedEnvelopeName;
051: protected NameImpl _upgradeName;
052:
053: SOAP11HeaderImpl(SOAPFactory factory, NameImpl name)
054: throws SOAPException {
055: super (factory, name);
056:
057: _supportedEnvelopeName = SOAP_1_1_SUPPORTED_ENVELOPE_NAME;
058: _upgradeName = SOAP11HeaderElementImpl.SOAP_1_1_UPGRADE_NAME;
059: }
060:
061: public SOAPElement addChildElement(Name name) throws SOAPException {
062: return addHeaderElement(name);
063: }
064:
065: public SOAPElement addChildElement(SOAPElement element)
066: throws SOAPException {
067: SOAPElement child = element;
068:
069: if (!(element instanceof SOAPHeaderElement))
070: element = new SOAP11HeaderElementImpl(_factory, element);
071:
072: return super .addChildElement(element);
073: }
074:
075: public SOAPHeaderElement addHeaderElement(Name name)
076: throws SOAPException {
077: SOAPHeaderElement child = new SOAP11HeaderElementImpl(_factory,
078: NameImpl.fromName(name));
079:
080: appendChild(child);
081:
082: return child;
083: }
084:
085: public SOAPHeaderElement addHeaderElement(QName qname)
086: throws SOAPException {
087: return addHeaderElement((Name) NameImpl.fromQName(qname));
088: }
089:
090: public SOAPHeaderElement addNotUnderstoodHeaderElement(QName name)
091: throws SOAPException {
092: throw new UnsupportedOperationException(
093: "NotUnderstood Header element not supported by SOAP 1.1");
094: }
095:
096: public SOAPHeaderElement addUpgradeHeaderElement(
097: Iterator supportedSoapUris) throws SOAPException {
098: // E.G.
099: // <env:Upgrade>
100: // <env:SupportedEnvelope qname="ns1:Envelope"
101: // xmlns:ns1="http://www.w3.org/2003/05/soap-envelope"/>
102: // <env:SupportedEnvelope qname="ns2:Envelope"
103: // xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/"/>
104: // </env:Upgrade>
105:
106: SOAPHeaderElement element = createHeaderElement(_upgradeName);
107:
108: int i = 1;
109:
110: while (supportedSoapUris.hasNext()) {
111: String uri = supportedSoapUris.next().toString();
112: element.addChildElement(createSupportedElement(uri, i++));
113: }
114:
115: addChildElement(element);
116:
117: return element;
118: }
119:
120: public SOAPHeaderElement addUpgradeHeaderElement(
121: String supportedSoapUri) throws SOAPException {
122: SOAPHeaderElement element = createHeaderElement(_upgradeName);
123:
124: element.addChildElement(createSupportedElement(
125: supportedSoapUri, 1));
126:
127: addChildElement(element);
128:
129: return element;
130: }
131:
132: public SOAPHeaderElement addUpgradeHeaderElement(
133: String[] supportedSoapUris) throws SOAPException {
134: SOAPHeaderElement element = createHeaderElement(_upgradeName);
135:
136: for (int i = 0; i < supportedSoapUris.length; i++) {
137: String uri = supportedSoapUris[i];
138: element.addChildElement(createSupportedElement(uri, i++));
139: }
140:
141: addChildElement(element);
142:
143: return element;
144: }
145:
146: private SOAPElement createSupportedElement(String uri, int i)
147: throws SOAPException {
148: SOAPElement supported = createHeaderElement(_supportedEnvelopeName);
149:
150: supported.addAttribute((Name) QNAME_NAME, "ns" + i
151: + ":Envelope");
152: supported.addNamespaceDeclaration("ns" + i, uri);
153:
154: return supported;
155: }
156:
157: protected SOAPHeaderElement createHeaderElement(NameImpl name)
158: throws SOAPException {
159: return new SOAP11HeaderElementImpl(_factory, name);
160: }
161:
162: public Iterator examineAllHeaderElements() {
163: return getChildElements();
164: }
165:
166: public Iterator examineHeaderElements(String actor) {
167: return new HeaderElementIterator(actor);
168: }
169:
170: public Iterator examineMustUnderstandHeaderElements(String actor) {
171: return new HeaderElementIterator(actor, true);
172: }
173:
174: public Iterator extractAllHeaderElements() {
175: ArrayList<SOAPHeaderElement> elements = new ArrayList<SOAPHeaderElement>();
176: Iterator i = examineAllHeaderElements();
177:
178: while (i.hasNext()) {
179: SOAPHeaderElement element = (SOAPHeaderElement) i.next();
180:
181: element.detachNode();
182: elements.add(element);
183: }
184:
185: return elements.iterator();
186: }
187:
188: public Iterator extractHeaderElements(String actor) {
189: ArrayList<SOAPHeaderElement> elements = new ArrayList<SOAPHeaderElement>();
190: Iterator i = examineAllHeaderElements();
191:
192: while (i.hasNext()) {
193: SOAPHeaderElement element = (SOAPHeaderElement) i.next();
194:
195: if (actor.equals(element.getActor())) {
196: element.detachNode();
197: elements.add(element);
198: }
199: }
200:
201: return elements.iterator();
202: }
203:
204: public SOAPElement setElementQName(QName newName)
205: throws SOAPException {
206: throw new SOAPException("Cannot set name of SOAP Header");
207: }
208:
209: public void detachNode() {
210: if (getParentNode() instanceof SOAPEnvelopeImpl) {
211: SOAPEnvelopeImpl parent = (SOAPEnvelopeImpl) getParentNode();
212: parent._header = null;
213: }
214:
215: super .detachNode();
216: }
217:
218: protected class HeaderElementIterator implements Iterator {
219: private String _actor;
220: private boolean _mustUnderstand;
221: private SOAPHeaderElement _headerElement;
222:
223: public HeaderElementIterator(String actor) {
224: this (actor, false);
225: }
226:
227: public HeaderElementIterator(String actor,
228: boolean mustUnderstand) {
229: _actor = actor;
230: _mustUnderstand = mustUnderstand;
231:
232: advanceHeaderElement((SOAPHeaderElement) _firstChild);
233: }
234:
235: public boolean hasNext() {
236: return _headerElement != null;
237: }
238:
239: public Object next() {
240: if (_headerElement == null)
241: throw new NoSuchElementException();
242:
243: SOAPHeaderElement next = _headerElement;
244:
245: advanceHeaderElement((SOAPHeaderElement) _headerElement
246: .getNextSibling());
247:
248: return next;
249: }
250:
251: public void remove() {
252: throw new UnsupportedOperationException();
253: }
254:
255: private void advanceHeaderElement(SOAPHeaderElement next) {
256: for (_headerElement = next; _headerElement != null; _headerElement = (SOAPHeaderElement) _headerElement
257: .getNextSibling()) {
258: if (_actor.equals(_headerElement.getActor())) {
259: if (_mustUnderstand) {
260: if (_headerElement.getMustUnderstand())
261: break;
262: } else
263: break;
264: }
265: }
266: }
267: }
268: }
|