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 com.caucho.util.*;
033: import com.caucho.xml.*;
034: import javax.xml.soap.*;
035: import javax.xml.transform.*;
036: import javax.xml.transform.dom.*;
037: import javax.xml.transform.stream.*;
038: import org.w3c.dom.*;
039: import org.xml.sax.*;
040: import java.util.*;
041:
042: public class SOAPPartImpl extends SOAPPart {
043: private static final L10N L = new L10N(SOAPPartImpl.class);
044:
045: private SOAPFactory _factory;
046: private SOAPEnvelopeImpl _envelope;
047: private String _protocol;
048: private MimeHeaders _headers = new MimeHeaders();
049: private Transformer _transformer;
050: private String _xmlEncoding = "utf-8";
051: private String _xmlVersion = "1.0";
052: private boolean _xmlStandalone = true;
053:
054: SOAPPartImpl(SOAPFactory factory, String protocol)
055: throws SOAPException {
056: _factory = factory;
057: _protocol = protocol;
058:
059: // prepare the envelope with an empty body and header
060:
061: Name envelopeName = null;
062:
063: if (SOAPConstants.SOAP_1_1_PROTOCOL.equals(_protocol))
064: envelopeName = SOAPEnvelopeImpl.SOAP_1_1_ENVELOPE_NAME;
065: else if (SOAPConstants.SOAP_1_2_PROTOCOL.equals(_protocol))
066: envelopeName = SOAPEnvelopeImpl.SOAP_1_2_ENVELOPE_NAME;
067: else if (SOAPConstants.DYNAMIC_SOAP_PROTOCOL.equals(_protocol))
068: // this constructor does not get its structure from an
069: // external input -> cannot do dynamic protocol detection
070: throw new UnsupportedOperationException();
071: else
072: throw new SOAPException("Unknown SOAP protocol: "
073: + _protocol);
074:
075: _envelope = (SOAPEnvelopeImpl) _factory
076: .createElement(envelopeName);
077: _envelope.setOwner((Document) this );
078: _envelope.addHeader();
079: _envelope.addBody();
080: }
081:
082: SOAPPartImpl(SOAPFactory factory, String protocol, Document document)
083: throws SOAPException {
084: _factory = factory;
085: _protocol = protocol;
086:
087: Name envelopeName = null;
088:
089: if (SOAPConstants.SOAP_1_1_PROTOCOL.equals(_protocol))
090: envelopeName = SOAPEnvelopeImpl.SOAP_1_1_ENVELOPE_NAME;
091: else if (SOAPConstants.SOAP_1_2_PROTOCOL.equals(_protocol))
092: envelopeName = SOAPEnvelopeImpl.SOAP_1_2_ENVELOPE_NAME;
093: else if (SOAPConstants.DYNAMIC_SOAP_PROTOCOL.equals(_protocol))
094: // dynamic protocol is a special SAAJ "protocol" that means
095: // get the version from the input.
096: envelopeName = NameImpl.fromElement(document
097: .getDocumentElement());
098: else
099: throw new SOAPException("Unknown SOAP protocol: "
100: + _protocol);
101:
102: _xmlEncoding = document.getXmlEncoding();
103: _xmlVersion = document.getXmlVersion();
104: _xmlStandalone = document.getXmlStandalone();
105:
106: _envelope = (SOAPEnvelopeImpl) _factory
107: .createElement(envelopeName);
108: _envelope.setOwner((Document) this );
109:
110: _envelope.deepCopy(document.getDocumentElement());
111: }
112:
113: private Transformer getTransformer() throws TransformerException {
114: if (_transformer == null) {
115: TransformerFactory transformerFactory = TransformerFactory
116: .newInstance();
117: _transformer = transformerFactory.newTransformer();
118: }
119:
120: return _transformer;
121: }
122:
123: public void addMimeHeader(String name, String value) {
124: _headers.addHeader(name, value);
125: }
126:
127: public Iterator getAllMimeHeaders() {
128: return _headers.getAllHeaders();
129: }
130:
131: public Source getContent() throws SOAPException {
132: return new DOMSource(this );
133: }
134:
135: public SOAPEnvelope getEnvelope() throws SOAPException {
136: return _envelope;
137: }
138:
139: public Iterator getMatchingMimeHeaders(String[] names) {
140: return _headers.getMatchingHeaders(names);
141: }
142:
143: public String[] getMimeHeader(String name) {
144: return _headers.getHeader(name);
145: }
146:
147: public Iterator getNonMatchingMimeHeaders(String[] names) {
148: return _headers.getNonMatchingHeaders(names);
149: }
150:
151: public void removeAllMimeHeaders() {
152: _headers.removeAllHeaders();
153: }
154:
155: public void removeMimeHeader(String header) {
156: _headers.removeHeader(header);
157: }
158:
159: public void setContent(Source source) throws SOAPException {
160: org.w3c.dom.Node node = null;
161: Element element = null;
162:
163: if (source instanceof DOMSource) {
164: node = ((DOMSource) source).getNode();
165: } else {
166: try {
167: DOMResult result = new DOMResult();
168: getTransformer().transform(source, result);
169: node = result.getNode();
170: } catch (TransformerException e) {
171: throw new SOAPException(e);
172: }
173: }
174:
175: if (node.getNodeType() == DOCUMENT_NODE)
176: element = ((Document) node).getDocumentElement();
177: else if (node.getNodeType() == ELEMENT_NODE)
178: element = (Element) node;
179: else
180: throw new SOAPException(
181: L
182: .l(
183: "Source (or transformed DOM) does not have a Document or Element node: {0}",
184: source));
185:
186: Name envelopeName = null;
187:
188: if (SOAPConstants.SOAP_1_1_PROTOCOL.equals(_protocol))
189: envelopeName = SOAPEnvelopeImpl.SOAP_1_1_ENVELOPE_NAME;
190: else if (SOAPConstants.SOAP_1_2_PROTOCOL.equals(_protocol))
191: envelopeName = SOAPEnvelopeImpl.SOAP_1_2_ENVELOPE_NAME;
192: else if (SOAPConstants.DYNAMIC_SOAP_PROTOCOL.equals(_protocol))
193: // dynamic protocol is a special SAAJ "protocol" that means
194: // get the version from the input.
195: envelopeName = NameImpl.fromElement(element);
196: else
197: throw new SOAPException("Unknown SOAP protocol: "
198: + _protocol);
199:
200: _envelope = (SOAPEnvelopeImpl) _factory
201: .createElement(envelopeName);
202: _envelope.setOwner((Document) this );
203: _envelope.deepCopy(element);
204: }
205:
206: public void setMimeHeader(String name, String value) {
207: _headers.setHeader(name, value);
208: }
209:
210: // javax.xml.soap.Node
211:
212: public void detachNode() {
213: throw new UnsupportedOperationException();
214: }
215:
216: public SOAPElement getParentElement() {
217: throw new UnsupportedOperationException();
218: }
219:
220: public String getValue() {
221: throw new UnsupportedOperationException();
222: }
223:
224: public void recycleNode() {
225: throw new UnsupportedOperationException();
226: }
227:
228: public void setParentElement(SOAPElement parent)
229: throws SOAPException {
230: throw new UnsupportedOperationException();
231: }
232:
233: public void setValue(String value) {
234: throw new UnsupportedOperationException();
235: }
236:
237: // org.w3c.dom.Document
238:
239: public org.w3c.dom.Node adoptNode(org.w3c.dom.Node source) {
240: throw new UnsupportedOperationException();
241: }
242:
243: public Attr createAttribute(String name) {
244: throw new UnsupportedOperationException();
245: }
246:
247: public Attr createAttributeNS(String namespaceURI,
248: String qualifiedName) {
249: throw new UnsupportedOperationException();
250: }
251:
252: public CDATASection createCDATASection(String data) {
253: throw new UnsupportedOperationException();
254: }
255:
256: public Comment createComment(String data) {
257: throw new UnsupportedOperationException();
258: }
259:
260: public DocumentFragment createDocumentFragment() {
261: throw new UnsupportedOperationException();
262: }
263:
264: public Element createElement(String tagName) {
265: throw new UnsupportedOperationException();
266: }
267:
268: public Element createElementNS(String namespaceURI,
269: String qualifiedName) {
270: throw new UnsupportedOperationException();
271: }
272:
273: public EntityReference createEntityReference(String name) {
274: throw new UnsupportedOperationException();
275: }
276:
277: public ProcessingInstruction createProcessingInstruction(
278: String target, String data) {
279: throw new UnsupportedOperationException();
280: }
281:
282: public org.w3c.dom.Text createTextNode(String data) {
283: throw new UnsupportedOperationException();
284: }
285:
286: public DocumentType getDoctype() {
287: throw new UnsupportedOperationException();
288: }
289:
290: public Element getDocumentElement() {
291: return _envelope;
292: }
293:
294: public String getDocumentURI() {
295: throw new UnsupportedOperationException();
296: }
297:
298: public DOMConfiguration getDomConfig() {
299: throw new UnsupportedOperationException();
300: }
301:
302: public Element getElementById(String elementId) {
303: throw new UnsupportedOperationException();
304: }
305:
306: public NodeList getElementsByTagName(String tagname) {
307: throw new UnsupportedOperationException();
308: }
309:
310: public NodeList getElementsByTagNameNS(String namespaceURI,
311: String localName) {
312: throw new UnsupportedOperationException();
313: }
314:
315: public DOMImplementation getImplementation() {
316: throw new UnsupportedOperationException();
317: }
318:
319: public String getInputEncoding() {
320: throw new UnsupportedOperationException();
321: }
322:
323: public boolean getStrictErrorChecking() {
324: throw new UnsupportedOperationException();
325: }
326:
327: public String getXmlEncoding() {
328: return _xmlEncoding;
329: }
330:
331: public boolean getXmlStandalone() {
332: return _xmlStandalone;
333: }
334:
335: public String getXmlVersion() {
336: return _xmlVersion;
337: }
338:
339: public org.w3c.dom.Node importNode(org.w3c.dom.Node importedNode,
340: boolean deep) {
341: throw new UnsupportedOperationException();
342: }
343:
344: public void normalizeDocument() {
345: throw new UnsupportedOperationException();
346: }
347:
348: public org.w3c.dom.Node renameNode(org.w3c.dom.Node n,
349: String namespaceURI, String qualifiedName)
350: throws DOMException {
351: throw new UnsupportedOperationException();
352: }
353:
354: public void setDocumentURI(String documentURI) {
355: throw new UnsupportedOperationException();
356: }
357:
358: public void setStrictErrorChecking(boolean strictErrorChecking) {
359: throw new UnsupportedOperationException();
360: }
361:
362: public void setXmlStandalone(boolean xmlStandalone) {
363: _xmlStandalone = xmlStandalone;
364: }
365:
366: public void setXmlVersion(String xmlVersion) {
367: _xmlVersion = xmlVersion;
368: }
369:
370: // org.w3c.dom.Node
371:
372: public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild) {
373: throw new UnsupportedOperationException();
374: }
375:
376: public org.w3c.dom.Node cloneNode(boolean deep) {
377: throw new UnsupportedOperationException();
378: }
379:
380: public short compareDocumentPosition(org.w3c.dom.Node other) {
381: throw new UnsupportedOperationException();
382: }
383:
384: public NamedNodeMap getAttributes() {
385: throw new UnsupportedOperationException();
386: }
387:
388: public String getBaseURI() {
389: throw new UnsupportedOperationException();
390: }
391:
392: public NodeList getChildNodes() {
393: return new NodeListImpl();
394: }
395:
396: public Object getFeature(String feature, String version) {
397: throw new UnsupportedOperationException();
398: }
399:
400: public org.w3c.dom.Node getFirstChild() {
401: return _envelope;
402: }
403:
404: public org.w3c.dom.Node getLastChild() {
405: return _envelope;
406: }
407:
408: public String getLocalName() {
409: throw new UnsupportedOperationException();
410: }
411:
412: public String getNamespaceURI() {
413: throw new UnsupportedOperationException();
414: }
415:
416: public org.w3c.dom.Node getNextSibling() {
417: throw new UnsupportedOperationException();
418: }
419:
420: public String getNodeName() {
421: return "#document";
422: }
423:
424: public short getNodeType() {
425: return DOCUMENT_NODE;
426: }
427:
428: public String getNodeValue() {
429: throw new UnsupportedOperationException();
430: }
431:
432: public Document getOwnerDocument() {
433: throw new UnsupportedOperationException();
434: }
435:
436: public org.w3c.dom.Node getParentNode() {
437: throw new UnsupportedOperationException();
438: }
439:
440: public String getPrefix() {
441: throw new UnsupportedOperationException();
442: }
443:
444: public org.w3c.dom.Node getPreviousSibling() {
445: throw new UnsupportedOperationException();
446: }
447:
448: public String getTextContent() {
449: throw new UnsupportedOperationException();
450: }
451:
452: public Object getUserData(String key) {
453: throw new UnsupportedOperationException();
454: }
455:
456: public boolean hasAttributes() {
457: throw new UnsupportedOperationException();
458: }
459:
460: public boolean hasChildNodes() {
461: throw new UnsupportedOperationException();
462: }
463:
464: public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild,
465: org.w3c.dom.Node refChild) {
466: throw new UnsupportedOperationException();
467: }
468:
469: public boolean isDefaultNamespace(String namespaceURI) {
470: throw new UnsupportedOperationException();
471: }
472:
473: public boolean isEqualNode(org.w3c.dom.Node arg) {
474: throw new UnsupportedOperationException();
475: }
476:
477: public boolean isSameNode(org.w3c.dom.Node other) {
478: throw new UnsupportedOperationException();
479: }
480:
481: public boolean isSupported(String feature, String version) {
482: throw new UnsupportedOperationException();
483: }
484:
485: public String lookupNamespaceURI(String prefix) {
486: throw new UnsupportedOperationException();
487: }
488:
489: public String lookupPrefix(String namespaceURI) {
490: throw new UnsupportedOperationException();
491: }
492:
493: public void normalize() {
494: throw new UnsupportedOperationException();
495: }
496:
497: public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild) {
498: throw new UnsupportedOperationException();
499: }
500:
501: public org.w3c.dom.Node replaceChild(org.w3c.dom.Node newChild,
502: org.w3c.dom.Node oldChild) {
503: throw new UnsupportedOperationException();
504: }
505:
506: public void setNodeValue(String nodeValue) {
507: throw new UnsupportedOperationException();
508: }
509:
510: public void setPrefix(String prefix) {
511: throw new UnsupportedOperationException();
512: }
513:
514: public void setTextContent(String textContent) {
515: throw new UnsupportedOperationException();
516: }
517:
518: public Object setUserData(String key, Object data,
519: UserDataHandler handler) {
520: throw new UnsupportedOperationException();
521: }
522:
523: protected class NodeListImpl implements NodeList {
524: public int getLength() {
525: return 1;
526: }
527:
528: public org.w3c.dom.Node item(int i) {
529: if (i != 0)
530: return null;
531:
532: return _envelope;
533: }
534: }
535: }
|