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 /**
  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 import java.util.logging.Level;
  34 
  35 import javax.xml.namespace.QName;
  36 import javax.xml.soap.*;
  37 
  38 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
  39 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
  40 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
  41 import com.sun.xml.internal.messaging.saaj.soap.impl.HeaderImpl;
  42 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
  43 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
  44 import org.w3c.dom.Element;
  45 
  46 
  47 public class Header1_2Impl extends HeaderImpl {
  48 
  49     protected static final Logger log =
  50         Logger.getLogger(
  51             LogDomainConstants.SOAP_VER1_2_DOMAIN,
  52             "com.sun.xml.internal.messaging.saaj.soap.ver1_2.LocalStrings");
  53 
  54     public Header1_2Impl(SOAPDocumentImpl ownerDocument, String prefix) {
  55         super(ownerDocument, NameImpl.createHeader1_2Name(prefix));
  56     }
  57 
  58     public Header1_2Impl(SOAPDocumentImpl ownerDoc, Element domElement) {
  59         super(ownerDoc, domElement);
  60     }
  61 
  62     protected NameImpl getNotUnderstoodName() {
  63         return NameImpl.createNotUnderstood1_2Name(null);
  64     }
  65 
  66     protected NameImpl getUpgradeName() {
  67         return NameImpl.createUpgrade1_2Name(null);
  68     }
  69 
  70     protected NameImpl getSupportedEnvelopeName() {
  71         return NameImpl.createSupportedEnvelope1_2Name(null);
  72     }
  73 
  74     public SOAPHeaderElement addNotUnderstoodHeaderElement(final QName sourceName)
  75         throws SOAPException {
  76 
  77         if (sourceName == null) {
  78             log.severe("SAAJ0410.ver1_2.no.null.to.addNotUnderstoodHeader");
  79             throw new SOAPException("Cannot pass NULL to addNotUnderstoodHeaderElement");
  80         }
  81         if ("".equals(sourceName.getNamespaceURI())) {
  82             log.severe("SAAJ0417.ver1_2.qname.not.ns.qualified");
  83             throw new SOAPException("The qname passed to addNotUnderstoodHeaderElement must be namespace-qualified");
  84         }
  85         String prefix = sourceName.getPrefix();
  86         if ("".equals(prefix)) {
  87             prefix = "ns1";
  88         }
  89         Name notunderstoodName = getNotUnderstoodName();
  90         SOAPHeaderElement notunderstoodHeaderElement =
  91             (SOAPHeaderElement) addChildElement(notunderstoodName);
  92         notunderstoodHeaderElement.addAttribute(
  93             NameImpl.createFromUnqualifiedName("qname"),
  94             getQualifiedName(
  95                 new QName(
  96                     sourceName.getNamespaceURI(),
  97                     sourceName.getLocalPart(),
  98                     prefix)));
  99         notunderstoodHeaderElement.addNamespaceDeclaration(
 100             prefix,
 101             sourceName.getNamespaceURI());
 102         return notunderstoodHeaderElement;
 103     }
 104 
 105     public SOAPElement addTextNode(String text) throws SOAPException {
 106         log.log(
 107             Level.SEVERE,
 108             "SAAJ0416.ver1_2.adding.text.not.legal",
 109             getElementQName());
 110         throw new SOAPExceptionImpl("Adding text to SOAP 1.2 Header is not legal");
 111     }
 112 
 113     protected SOAPHeaderElement createHeaderElement(Name name)
 114         throws SOAPException {
 115         String uri = name.getURI();
 116         if (uri == null || uri.equals("")) {
 117             log.severe("SAAJ0413.ver1_2.header.elems.must.be.ns.qualified");
 118             throw new SOAPExceptionImpl("SOAP 1.2 header elements must be namespace qualified");
 119         }
 120         return new HeaderElement1_2Impl(
 121             ((SOAPDocument) getOwnerDocument()).getDocument(),
 122             name);
 123     }
 124 
 125     protected SOAPHeaderElement createHeaderElement(QName name)
 126         throws SOAPException {
 127         String uri = name.getNamespaceURI();
 128         if (uri == null || uri.equals("")) {
 129             log.severe("SAAJ0413.ver1_2.header.elems.must.be.ns.qualified");
 130             throw new SOAPExceptionImpl("SOAP 1.2 header elements must be namespace qualified");
 131         }
 132         return new HeaderElement1_2Impl(
 133             ((SOAPDocument) getOwnerDocument()).getDocument(),
 134             name);
 135     }
 136 
 137     public void setEncodingStyle(String encodingStyle) throws SOAPException {
 138         log.severe("SAAJ0409.ver1_2.no.encodingstyle.in.header");
 139         throw new SOAPExceptionImpl("encodingStyle attribute cannot appear on Header");
 140     }
 141 
 142     public SOAPElement addAttribute(Name name, String value)
 143         throws SOAPException {
 144         if (name.getLocalName().equals("encodingStyle")
 145             && name.getURI().equals(NameImpl.SOAP12_NAMESPACE)) {
 146 
 147             setEncodingStyle(value);
 148         }
 149         return super.addAttribute(name, value);
 150     }
 151 
 152     public SOAPElement addAttribute(QName name, String value)
 153         throws SOAPException {
 154         if (name.getLocalPart().equals("encodingStyle")
 155             && name.getNamespaceURI().equals(NameImpl.SOAP12_NAMESPACE)) {
 156 
 157             setEncodingStyle(value);
 158         }
 159         return super.addAttribute(name, value);
 160     }
 161 }