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.Iterator;
29 import java.util.Locale;
30 import java.util.logging.Level;
31
32 import javax.xml.namespace.QName;
33 import javax.xml.soap.*;
34 import javax.xml.stream.XMLStreamException;
35 import javax.xml.stream.XMLStreamReader;
36 import javax.xml.parsers.DocumentBuilder;
37 import javax.xml.parsers.DocumentBuilderFactory;
38
39 import org.w3c.dom.*;
40 import org.w3c.dom.Node;
41
42 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
43 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
44 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
45 import com.sun.xml.internal.messaging.saaj.soap.StaxBridge;
46 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
47
48 /**
49 * The implementation of SOAP-ENV:BODY or the SOAPBody abstraction.
50 *
51 * @author Anil Vijendran (anil@sun.com)
52 */
53 public abstract class BodyImpl extends ElementImpl implements SOAPBody {
54 private SOAPFault fault;
55 // private XMLStreamReaderToXMLStreamWriter staxBridge;
56 private StaxBridge staxBridge;
57 private boolean payloadStreamRead = false;
58
59 protected BodyImpl(SOAPDocumentImpl ownerDoc, NameImpl bodyName) {
60 super(ownerDoc, bodyName);
61 }
62
63 protected abstract NameImpl getFaultName(String name);
64 protected abstract boolean isFault(SOAPElement child);
65 protected abstract SOAPBodyElement createBodyElement(Name name);
66 protected abstract SOAPBodyElement createBodyElement(QName name);
67 protected abstract SOAPFault createFaultElement();
68 protected abstract QName getDefaultFaultCode();
69
70 public SOAPFault addFault() throws SOAPException {
71 if (hasFault()) {
72 log.severe("SAAJ0110.impl.fault.already.exists");
73 throw new SOAPExceptionImpl("Error: Fault already exists");
74 }
75
76 fault = createFaultElement();
77
78 addNode(fault);
79
80 fault.setFaultCode(getDefaultFaultCode());
81 fault.setFaultString("Fault string, and possibly fault code, not set");
82
138 return child;
139 }
140 }
141
142 return null;
143 }
144
145 public boolean hasFault() {
146 QName payloadQName = getPayloadQName();
147 return getFaultQName().equals(payloadQName);
148 }
149
150 private Object getFaultQName() {
151 return new QName(getNamespaceURI(), "Fault");
152 }
153
154 public SOAPFault getFault() {
155 if (hasFault()) {
156 if (fault == null) {
157 //initialize fault member
158 fault = (SOAPFault) getFirstChildElement();
159 }
160 return fault;
161 }
162 return null;
163 }
164
165 public SOAPBodyElement addBodyElement(Name name) throws SOAPException {
166 SOAPBodyElement newBodyElement =
167 (SOAPBodyElement) ElementFactory.createNamedElement(
168 ((SOAPDocument) getOwnerDocument()).getDocument(),
169 name.getLocalName(),
170 name.getPrefix(),
171 name.getURI());
172 if (newBodyElement == null) {
173 newBodyElement = createBodyElement(name);
174 }
175 addNode(newBodyElement);
176 return newBodyElement;
177 }
178
242 docFrag.appendChild(rootElement);
243
244 Document ownerDoc = getOwnerDocument();
245 // This copies the whole tree which could be very big so it's slow.
246 // However, it does have the advantage of actually working.
247 org.w3c.dom.Node replacingNode = ownerDoc.importNode(docFrag, true);
248 // Adding replacingNode at the last of the children list of body
249 addNode(replacingNode);
250 Iterator<Node> i =
251 getChildElements(NameImpl.copyElementName(rootElement));
252 // Return the child element with the required name which is at the
253 // end of the list
254 while(i.hasNext())
255 newBodyElement = (SOAPBodyElement) i.next();
256 }
257 return newBodyElement;
258 //*/
259 }
260
261 protected SOAPElement convertToSoapElement(Element element) {
262 if ((element instanceof SOAPBodyElement) &&
263 //this check is required because ElementImpl currently
264 // implements SOAPBodyElement
265 !(element.getClass().equals(ElementImpl.class))) {
266 return (SOAPElement) element;
267 } else {
268 return replaceElementWithSOAPElement(
269 element,
270 (ElementImpl) createBodyElement(NameImpl
271 .copyElementName(element)));
272 }
273 }
274
275 public SOAPElement setElementQName(QName newName) throws SOAPException {
276 log.log(Level.SEVERE,
277 "SAAJ0146.impl.invalid.name.change.requested",
278 new Object[] {elementQName.getLocalPart(),
279 newName.getLocalPart()});
280 throw new SOAPException("Cannot change name for "
281 + elementQName.getLocalPart() + " to "
282 + newName.getLocalPart());
283 }
284
285 public Document extractContentAsDocument() throws SOAPException {
286
297 else {
298 for (org.w3c.dom.Node node = firstBodyElement.getNextSibling();
299 node != null;
300 node = node.getNextSibling()) {
301
302 if (node instanceof Element) {
303 exactlyOneChildElement = false;
304 break;
305 }
306 }
307 }
308
309 if(!exactlyOneChildElement) {
310 log.log(Level.SEVERE,
311 "SAAJ0250.impl.body.should.have.exactly.one.child");
312 throw new SOAPException("Cannot extract Document from body");
313 }
314
315 Document document = null;
316 try {
317 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
318 factory.setNamespaceAware(true);
319 DocumentBuilder builder = factory.newDocumentBuilder();
320 document = builder.newDocument();
321
322 Element rootElement = (Element) document.importNode(
323 firstBodyElement,
324 true);
325
326 document.appendChild(rootElement);
327
328 } catch(Exception e) {
329 log.log(Level.SEVERE,
330 "SAAJ0251.impl.cannot.extract.document.from.body");
331 throw new SOAPExceptionImpl(
332 "Unable to extract Document from body", e);
333 }
334
335 firstBodyElement.detachNode();
336
337 return document;
423 Element elem = getFirstChildElement();
424 if (elem != null) {
425 String ns = elem.getNamespaceURI();
426 String pref = elem.getPrefix();
427 String local = elem.getLocalName();
428 if (pref != null) return new QName(ns, local, pref);
429 if (ns != null) return new QName(ns, local);
430 return new QName(local);
431 }
432 }
433 return null;
434 }
435
436 String getPayloadAttributeValue(String attName) {
437 if (staxBridge != null) {
438 return staxBridge.getPayloadAttributeValue(attName);
439 } else {
440 //not lazy -Just get first child element and return its attribute
441 Element elem = getFirstChildElement();
442 if (elem != null) {
443 return elem.getAttribute(localName);
444 }
445 }
446 return null;
447 }
448
449 String getPayloadAttributeValue(QName attNAme) {
450 if (staxBridge != null) {
451 return staxBridge.getPayloadAttributeValue(attNAme);
452 } else {
453 //not lazy -Just get first child element and return its attribute
454 Element elem = getFirstChildElement();
455 if (elem != null) {
456 return elem.getAttributeNS(attNAme.getNamespaceURI(), attNAme.getLocalPart());
457 }
458 }
459 return null;
460 }
461
462 public boolean isLazy() {
463 return (staxBridge != null && !payloadStreamRead);
|
1 /*
2 * Copyright (c) 1997, 2017, 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.Iterator;
29 import java.util.Locale;
30 import java.util.logging.Level;
31
32 import javax.xml.namespace.QName;
33 import javax.xml.soap.*;
34 import javax.xml.stream.XMLStreamException;
35 import javax.xml.stream.XMLStreamReader;
36 import javax.xml.parsers.DocumentBuilder;
37 import javax.xml.parsers.DocumentBuilderFactory;
38
39 import com.sun.xml.internal.messaging.saaj.util.SAAJUtil;
40 import org.w3c.dom.*;
41 import org.w3c.dom.Node;
42
43 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
44 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
45 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
46 import com.sun.xml.internal.messaging.saaj.soap.StaxBridge;
47 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
48
49 /**
50 * The implementation of SOAP-ENV:BODY or the SOAPBody abstraction.
51 *
52 * @author Anil Vijendran (anil@sun.com)
53 */
54 public abstract class BodyImpl extends ElementImpl implements SOAPBody {
55 private SOAPFault fault;
56 // private XMLStreamReaderToXMLStreamWriter staxBridge;
57 private StaxBridge staxBridge;
58 private boolean payloadStreamRead = false;
59
60 protected BodyImpl(SOAPDocumentImpl ownerDoc, NameImpl bodyName) {
61 super(ownerDoc, bodyName);
62 }
63
64 public BodyImpl(SOAPDocumentImpl ownerDoc, Element domElement) {
65 super(ownerDoc, domElement);
66 }
67
68 protected abstract NameImpl getFaultName(String name);
69 protected abstract boolean isFault(SOAPElement child);
70 protected abstract SOAPBodyElement createBodyElement(Name name);
71 protected abstract SOAPBodyElement createBodyElement(QName name);
72 protected abstract SOAPFault createFaultElement();
73 protected abstract QName getDefaultFaultCode();
74
75 public SOAPFault addFault() throws SOAPException {
76 if (hasFault()) {
77 log.severe("SAAJ0110.impl.fault.already.exists");
78 throw new SOAPExceptionImpl("Error: Fault already exists");
79 }
80
81 fault = createFaultElement();
82
83 addNode(fault);
84
85 fault.setFaultCode(getDefaultFaultCode());
86 fault.setFaultString("Fault string, and possibly fault code, not set");
87
143 return child;
144 }
145 }
146
147 return null;
148 }
149
150 public boolean hasFault() {
151 QName payloadQName = getPayloadQName();
152 return getFaultQName().equals(payloadQName);
153 }
154
155 private Object getFaultQName() {
156 return new QName(getNamespaceURI(), "Fault");
157 }
158
159 public SOAPFault getFault() {
160 if (hasFault()) {
161 if (fault == null) {
162 //initialize fault member
163 fault = (SOAPFault) getSoapDocument().find(getFirstChildElement());
164 }
165 return fault;
166 }
167 return null;
168 }
169
170 public SOAPBodyElement addBodyElement(Name name) throws SOAPException {
171 SOAPBodyElement newBodyElement =
172 (SOAPBodyElement) ElementFactory.createNamedElement(
173 ((SOAPDocument) getOwnerDocument()).getDocument(),
174 name.getLocalName(),
175 name.getPrefix(),
176 name.getURI());
177 if (newBodyElement == null) {
178 newBodyElement = createBodyElement(name);
179 }
180 addNode(newBodyElement);
181 return newBodyElement;
182 }
183
247 docFrag.appendChild(rootElement);
248
249 Document ownerDoc = getOwnerDocument();
250 // This copies the whole tree which could be very big so it's slow.
251 // However, it does have the advantage of actually working.
252 org.w3c.dom.Node replacingNode = ownerDoc.importNode(docFrag, true);
253 // Adding replacingNode at the last of the children list of body
254 addNode(replacingNode);
255 Iterator<Node> i =
256 getChildElements(NameImpl.copyElementName(rootElement));
257 // Return the child element with the required name which is at the
258 // end of the list
259 while(i.hasNext())
260 newBodyElement = (SOAPBodyElement) i.next();
261 }
262 return newBodyElement;
263 //*/
264 }
265
266 protected SOAPElement convertToSoapElement(Element element) {
267 final Node soapNode = getSoapDocument().findIfPresent(element);
268 if ((soapNode instanceof SOAPBodyElement) &&
269 //this check is required because ElementImpl currently
270 // implements SOAPBodyElement
271 !(soapNode.getClass().equals(ElementImpl.class))) {
272 return (SOAPElement) soapNode;
273 } else {
274 return replaceElementWithSOAPElement(
275 element,
276 (ElementImpl) createBodyElement(NameImpl
277 .copyElementName(element)));
278 }
279 }
280
281 public SOAPElement setElementQName(QName newName) throws SOAPException {
282 log.log(Level.SEVERE,
283 "SAAJ0146.impl.invalid.name.change.requested",
284 new Object[] {elementQName.getLocalPart(),
285 newName.getLocalPart()});
286 throw new SOAPException("Cannot change name for "
287 + elementQName.getLocalPart() + " to "
288 + newName.getLocalPart());
289 }
290
291 public Document extractContentAsDocument() throws SOAPException {
292
303 else {
304 for (org.w3c.dom.Node node = firstBodyElement.getNextSibling();
305 node != null;
306 node = node.getNextSibling()) {
307
308 if (node instanceof Element) {
309 exactlyOneChildElement = false;
310 break;
311 }
312 }
313 }
314
315 if(!exactlyOneChildElement) {
316 log.log(Level.SEVERE,
317 "SAAJ0250.impl.body.should.have.exactly.one.child");
318 throw new SOAPException("Cannot extract Document from body");
319 }
320
321 Document document = null;
322 try {
323 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", SAAJUtil.getSystemClassLoader());
324 factory.setNamespaceAware(true);
325 DocumentBuilder builder = factory.newDocumentBuilder();
326 document = builder.newDocument();
327
328 Element rootElement = (Element) document.importNode(
329 firstBodyElement,
330 true);
331
332 document.appendChild(rootElement);
333
334 } catch(Exception e) {
335 log.log(Level.SEVERE,
336 "SAAJ0251.impl.cannot.extract.document.from.body");
337 throw new SOAPExceptionImpl(
338 "Unable to extract Document from body", e);
339 }
340
341 firstBodyElement.detachNode();
342
343 return document;
429 Element elem = getFirstChildElement();
430 if (elem != null) {
431 String ns = elem.getNamespaceURI();
432 String pref = elem.getPrefix();
433 String local = elem.getLocalName();
434 if (pref != null) return new QName(ns, local, pref);
435 if (ns != null) return new QName(ns, local);
436 return new QName(local);
437 }
438 }
439 return null;
440 }
441
442 String getPayloadAttributeValue(String attName) {
443 if (staxBridge != null) {
444 return staxBridge.getPayloadAttributeValue(attName);
445 } else {
446 //not lazy -Just get first child element and return its attribute
447 Element elem = getFirstChildElement();
448 if (elem != null) {
449 return elem.getAttribute(getLocalName());
450 }
451 }
452 return null;
453 }
454
455 String getPayloadAttributeValue(QName attNAme) {
456 if (staxBridge != null) {
457 return staxBridge.getPayloadAttributeValue(attNAme);
458 } else {
459 //not lazy -Just get first child element and return its attribute
460 Element elem = getFirstChildElement();
461 if (elem != null) {
462 return elem.getAttributeNS(attNAme.getNamespaceURI(), attNAme.getLocalPart());
463 }
464 }
465 return null;
466 }
467
468 public boolean isLazy() {
469 return (staxBridge != null && !payloadStreamRead);
|