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 javax.xml.namespace.QName;
  29 import javax.xml.soap.*;
  30 
  31 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
  32 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
  33 import org.w3c.dom.Element;
  34 
  35 public abstract class HeaderElementImpl
  36     extends ElementImpl
  37     implements SOAPHeaderElement {
  38 
  39     protected static Name RELAY_ATTRIBUTE_LOCAL_NAME =
  40         NameImpl.createFromTagName("relay");
  41     protected static Name MUST_UNDERSTAND_ATTRIBUTE_LOCAL_NAME =
  42         NameImpl.createFromTagName("mustUnderstand");
  43 
  44     public HeaderElementImpl(SOAPDocumentImpl ownerDoc, Name qname) {
  45         super(ownerDoc, qname);
  46     }
  47     public HeaderElementImpl(SOAPDocumentImpl ownerDoc, QName qname) {
  48         super(ownerDoc, qname);
  49     }
  50 
  51     public HeaderElementImpl(SOAPDocumentImpl ownerDoc, Element domElement) {
  52         super(ownerDoc, domElement);
  53     }
  54 
  55     protected abstract NameImpl getActorAttributeName();
  56     protected abstract NameImpl getRoleAttributeName();
  57     protected abstract NameImpl getMustunderstandAttributeName();
  58     protected abstract boolean getMustunderstandAttributeValue(String str);
  59     protected abstract String getMustunderstandLiteralValue(boolean mu);
  60     protected abstract NameImpl getRelayAttributeName();
  61     protected abstract boolean getRelayAttributeValue(String str);
  62     protected abstract String getRelayLiteralValue(boolean mu);
  63     protected abstract String getActorOrRole();
  64 
  65 
  66     @Override
  67     public void setParentElement(SOAPElement element) throws SOAPException {
  68         if (!(element instanceof SOAPHeader)) {
  69             log.severe("SAAJ0130.impl.header.elem.parent.mustbe.header");
  70             throw new SOAPException("Parent of a SOAPHeaderElement has to be a SOAPHeader");
  71         }
  72 
  73         super.setParentElement(element);
  74     }
  75 
  76     @Override
  77     public void setActor(String actorUri) {
  78         try {
  79             removeAttribute(getActorAttributeName());
  80             addAttribute((Name) getActorAttributeName(), actorUri);
  81         } catch (SOAPException ex) {
  82         }
  83     }
  84 
  85     //SOAP 1.2 supports Role
  86     @Override
  87     public void setRole(String roleUri) throws SOAPException {
  88         // runtime exception thrown if called for SOAP 1.1
  89         removeAttribute(getRoleAttributeName());
  90         addAttribute((Name) getRoleAttributeName(), roleUri);
  91     }
  92 
  93 
  94     Name actorAttNameWithoutNS = NameImpl.createFromTagName("actor");
  95 
  96     @Override
  97     public String getActor() {
  98         String actor = getAttributeValue(getActorAttributeName());
  99         return actor;
 100     }
 101 
 102     Name roleAttNameWithoutNS = NameImpl.createFromTagName("role");
 103 
 104     @Override
 105     public String getRole() {
 106         // runtime exception thrown for 1.1
 107         String role = getAttributeValue(getRoleAttributeName());
 108         return role;
 109     }
 110 
 111     @Override
 112     public void setMustUnderstand(boolean mustUnderstand) {
 113         try {
 114             removeAttribute(getMustunderstandAttributeName());
 115             addAttribute(
 116                 (Name) getMustunderstandAttributeName(),
 117                 getMustunderstandLiteralValue(mustUnderstand));
 118         } catch (SOAPException ex) {
 119         }
 120     }
 121 
 122     @Override
 123     public boolean getMustUnderstand() {
 124         String mu = getAttributeValue(getMustunderstandAttributeName());
 125 
 126         if (mu != null)
 127             return getMustunderstandAttributeValue(mu);
 128 
 129         return false;
 130     }
 131 
 132     @Override
 133     public void setRelay(boolean relay) throws SOAPException {
 134         // runtime exception thrown for 1.1
 135         removeAttribute(getRelayAttributeName());
 136         addAttribute(
 137             (Name) getRelayAttributeName(),
 138             getRelayLiteralValue(relay));
 139     }
 140 
 141     @Override
 142     public boolean getRelay() {
 143         String mu = getAttributeValue(getRelayAttributeName());
 144         if (mu != null)
 145             return getRelayAttributeValue(mu);
 146 
 147         return false;
 148     }
 149 }