1 /*
   2  * Copyright (c) 1997, 2012, 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 /**
  27 *
  28 * @author SAAJ RI Development Team
  29 */
  30 package com.sun.xml.internal.messaging.saaj.soap.ver1_2;
  31 
  32 import java.util.logging.Logger;
  33 
  34 import javax.xml.namespace.QName;
  35 import javax.xml.soap.*;
  36 
  37 import org.w3c.dom.Node;
  38 
  39 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
  40 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
  41 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
  42 import com.sun.xml.internal.messaging.saaj.soap.impl.BodyImpl;
  43 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
  44 
  45 public class Body1_2Impl extends BodyImpl {
  46 
  47     protected static final Logger log =
  48         Logger.getLogger(Body1_2Impl.class.getName(),
  49                          "com.sun.xml.internal.messaging.saaj.soap.ver1_2.LocalStrings");
  50 
  51     public Body1_2Impl(SOAPDocumentImpl ownerDocument, String prefix) {
  52             super(ownerDocument, NameImpl.createBody1_2Name(prefix));
  53     }
  54 
  55     protected NameImpl getFaultName(String name) {
  56         return NameImpl.createFault1_2Name(name, null);
  57     }
  58 
  59     protected SOAPBodyElement createBodyElement(Name name) {
  60         return new BodyElement1_2Impl(
  61             ((SOAPDocument) getOwnerDocument()).getDocument(),
  62             name);
  63     }
  64     protected SOAPBodyElement createBodyElement(QName name) {
  65         return new BodyElement1_2Impl(
  66             ((SOAPDocument) getOwnerDocument()).getDocument(),
  67             name);
  68     }
  69 
  70     protected QName getDefaultFaultCode() {
  71         return SOAPConstants.SOAP_RECEIVER_FAULT;
  72     }
  73 
  74     public SOAPFault addFault() throws SOAPException {
  75         if (hasAnyChildElement()) {
  76             log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
  77             throw new SOAPExceptionImpl(
  78                 "No other element except Fault allowed in SOAPBody");
  79         }
  80         return super.addFault();
  81     }
  82 
  83     /*
  84      * Override setEncodingStyle of ElementImpl to restrict adding encodingStyle
  85      * attribute to SOAP Body (SOAP 1.2 spec, part 1, section 5.1.1)
  86      */
  87     public void setEncodingStyle(String encodingStyle) throws SOAPException {
  88         log.severe("SAAJ0401.ver1_2.no.encodingstyle.in.body");
  89         throw new SOAPExceptionImpl("encodingStyle attribute cannot appear on Body");
  90     }
  91 
  92     /*
  93      * Override addAttribute of ElementImpl to restrict adding encodingStyle
  94      * attribute to SOAP Body (SOAP 1.2 spec, part 1, section 5.1.1)
  95      */
  96     public SOAPElement addAttribute(Name name, String value)
  97         throws SOAPException {
  98         if (name.getLocalName().equals("encodingStyle")
  99             && name.getURI().equals(NameImpl.SOAP12_NAMESPACE)) {
 100 
 101             setEncodingStyle(value);
 102         }
 103         return super.addAttribute(name, value);
 104     }
 105 
 106     public SOAPElement addAttribute(QName name, String value)
 107         throws SOAPException {
 108         if (name.getLocalPart().equals("encodingStyle")
 109             && name.getNamespaceURI().equals(NameImpl.SOAP12_NAMESPACE)) {
 110 
 111             setEncodingStyle(value);
 112         }
 113         return super.addAttribute(name, value);
 114     }
 115 
 116     protected boolean isFault(SOAPElement child) {
 117         return (child.getElementName().getURI().equals(
 118                     SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE) &&
 119                 child.getElementName().getLocalName().equals(
 120                     "Fault"));
 121     }
 122 
 123     protected SOAPFault createFaultElement() {
 124         return new Fault1_2Impl(
 125             ((SOAPDocument) getOwnerDocument()).getDocument(), getPrefix());
 126     }
 127 
 128     /*
 129      * section 5.4 of SOAP1.2 candidate recommendation says that a
 130      * SOAP message MUST contain a single Fault element as the only
 131      * child element of the SOAP Body.
 132      */
 133     public SOAPBodyElement addBodyElement(Name name) throws SOAPException {
 134         if (hasFault()) {
 135             log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
 136             throw new SOAPExceptionImpl(
 137                 "No other element except Fault allowed in SOAPBody");
 138         }
 139         return super.addBodyElement(name);
 140     }
 141 
 142     public SOAPBodyElement addBodyElement(QName name) throws SOAPException {
 143         if (hasFault()) {
 144             log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
 145             throw new SOAPExceptionImpl(
 146                 "No other element except Fault allowed in SOAPBody");
 147         }
 148         return super.addBodyElement(name);
 149     }
 150 
 151     protected SOAPElement addElement(Name name) throws SOAPException {
 152         if (hasFault()) {
 153             log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
 154             throw new SOAPExceptionImpl(
 155                 "No other element except Fault allowed in SOAPBody");
 156         }
 157         return super.addElement(name);
 158     }
 159 
 160     protected SOAPElement addElement(QName name) throws SOAPException {
 161         if (hasFault()) {
 162             log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
 163             throw new SOAPExceptionImpl(
 164                 "No other element except Fault allowed in SOAPBody");
 165         }
 166         return super.addElement(name);
 167     }
 168 
 169     public SOAPElement addChildElement(Name name) 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.addChildElement(name);
 176     }
 177 
 178     public SOAPElement addChildElement(QName name) throws SOAPException {
 179         if (hasFault()) {
 180             log.severe("SAAJ0402.ver1_2.only.fault.allowed.in.body");
 181             throw new SOAPExceptionImpl(
 182                 "No other element except Fault allowed in SOAPBody");
 183         }
 184         return super.addChildElement(name);
 185     }
 186 
 187     private boolean hasAnyChildElement() {
 188         Node currentNode = getFirstChild();
 189         while (currentNode != null) {
 190             if (currentNode.getNodeType() == Node.ELEMENT_NODE)
 191                 return true;
 192             currentNode = currentNode.getNextSibling();
 193         }
 194         return false;
 195     }
 196 }