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: Body1_2Impl.java,v 1.2 2007/07/16 16:41:24 ofung Exp $
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: /**
061: *
062: * @author SAAJ RI Development Team
063: */package com.sun.xml.messaging.saaj.soap.ver1_2;
064:
065: import java.util.logging.Logger;
066: import java.util.Locale;
067:
068: import javax.xml.namespace.QName;
069: import javax.xml.soap.*;
070:
071: import org.w3c.dom.Node;
072:
073: import com.sun.xml.messaging.saaj.SOAPExceptionImpl;
074: import com.sun.xml.messaging.saaj.soap.SOAPDocument;
075: import com.sun.xml.messaging.saaj.soap.SOAPDocumentImpl;
076: import com.sun.xml.messaging.saaj.soap.impl.BodyImpl;
077: import com.sun.xml.messaging.saaj.soap.name.NameImpl;
078:
079: public class Body1_2Impl extends BodyImpl {
080:
081: protected static Logger log = Logger.getLogger(Body1_2Impl.class
082: .getName(),
083: "com.sun.xml.messaging.saaj.soap.ver1_2.LocalStrings");
084:
085: public Body1_2Impl(SOAPDocumentImpl ownerDocument, String prefix) {
086: super (ownerDocument, NameImpl.createBody1_2Name(prefix));
087: }
088:
089: protected NameImpl getFaultName(String name) {
090: return NameImpl.createFault1_2Name(name, null);
091: }
092:
093: protected SOAPBodyElement createBodyElement(Name name) {
094: return new BodyElement1_2Impl(
095: ((SOAPDocument) getOwnerDocument()).getDocument(), name);
096: }
097:
098: protected SOAPBodyElement createBodyElement(QName name) {
099: return new BodyElement1_2Impl(
100: ((SOAPDocument) getOwnerDocument()).getDocument(), name);
101: }
102:
103: protected QName getDefaultFaultCode() {
104: return SOAPConstants.SOAP_RECEIVER_FAULT;
105: }
106:
107: public SOAPFault addFault() throws SOAPException {
108: if (hasAnyChildElement()) {
109: log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
110: throw new SOAPExceptionImpl(
111: "No other element except Fault allowed in SOAPBody");
112: }
113: return super .addFault();
114: }
115:
116: /*
117: * Override setEncodingStyle of ElementImpl to restrict adding encodingStyle
118: * attribute to SOAP Body (SOAP 1.2 spec, part 1, section 5.1.1)
119: */
120: public void setEncodingStyle(String encodingStyle)
121: throws SOAPException {
122: log.severe("SAAJ0401.ver1_2.no.encodingstyle.in.body");
123: throw new SOAPExceptionImpl(
124: "encodingStyle attribute cannot appear on Body");
125: }
126:
127: /*
128: * Override addAttribute of ElementImpl to restrict adding encodingStyle
129: * attribute to SOAP Body (SOAP 1.2 spec, part 1, section 5.1.1)
130: */
131: public SOAPElement addAttribute(Name name, String value)
132: throws SOAPException {
133: if (name.getLocalName().equals("encodingStyle")
134: && name.getURI().equals(NameImpl.SOAP12_NAMESPACE)) {
135:
136: setEncodingStyle(value);
137: }
138: return super .addAttribute(name, value);
139: }
140:
141: public SOAPElement addAttribute(QName name, String value)
142: throws SOAPException {
143: if (name.getLocalPart().equals("encodingStyle")
144: && name.getNamespaceURI().equals(
145: NameImpl.SOAP12_NAMESPACE)) {
146:
147: setEncodingStyle(value);
148: }
149: return super .addAttribute(name, value);
150: }
151:
152: protected boolean isFault(SOAPElement child) {
153: return (child.getElementName().getURI().equals(
154: SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE) && child
155: .getElementName().getLocalName().equals("Fault"));
156: }
157:
158: protected SOAPFault createFaultElement() {
159: return new Fault1_2Impl(((SOAPDocument) getOwnerDocument())
160: .getDocument(), getPrefix());
161: }
162:
163: /*
164: * section 5.4 of SOAP1.2 candidate recommendation says that a
165: * SOAP message MUST contain a single Fault element as the only
166: * child element of the SOAP Body.
167: */
168: public SOAPBodyElement addBodyElement(Name name)
169: throws SOAPException {
170: if (hasFault()) {
171: log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
172: throw new SOAPExceptionImpl(
173: "No other element except Fault allowed in SOAPBody");
174: }
175: return super .addBodyElement(name);
176: }
177:
178: public SOAPBodyElement addBodyElement(QName name)
179: throws SOAPException {
180: if (hasFault()) {
181: log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
182: throw new SOAPExceptionImpl(
183: "No other element except Fault allowed in SOAPBody");
184: }
185: return super .addBodyElement(name);
186: }
187:
188: protected SOAPElement addElement(Name name) throws SOAPException {
189: if (hasFault()) {
190: log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
191: throw new SOAPExceptionImpl(
192: "No other element except Fault allowed in SOAPBody");
193: }
194: return super .addElement(name);
195: }
196:
197: protected SOAPElement addElement(QName name) throws SOAPException {
198: if (hasFault()) {
199: log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
200: throw new SOAPExceptionImpl(
201: "No other element except Fault allowed in SOAPBody");
202: }
203: return super .addElement(name);
204: }
205:
206: public SOAPElement addChildElement(Name name) throws SOAPException {
207: if (hasFault()) {
208: log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
209: throw new SOAPExceptionImpl(
210: "No other element except Fault allowed in SOAPBody");
211: }
212: return super .addChildElement(name);
213: }
214:
215: public SOAPElement addChildElement(QName name) throws SOAPException {
216: if (hasFault()) {
217: log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
218: throw new SOAPExceptionImpl(
219: "No other element except Fault allowed in SOAPBody");
220: }
221: return super .addChildElement(name);
222: }
223:
224: private boolean hasAnyChildElement() {
225: Node currentNode = getFirstChild();
226: while (currentNode != null) {
227: if (currentNode.getNodeType() == Node.ELEMENT_NODE)
228: return true;
229: currentNode = currentNode.getNextSibling();
230: }
231: return false;
232: }
233: }
|