001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: ProviderHeader.java,v 1.3 2008/06/25 05:47:23 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.soapbinding; 
031
032import org.w3c.dom.Document;
033import org.w3c.dom.Element;
034import com.sun.identity.saml.common.SAMLUtils;
035import com.sun.identity.shared.xml.XMLUtils;
036
037/**
038 * The <code>ProviderHeader</code> class represents 'Provider' element defined
039 * in SOAP binding schema.
040 *
041 * @supported.all.api
042 */
043
044public class ProviderHeader {
045
046    private String providerID = null;
047    private String affiliationID = null;
048    private String id = null;
049    private Boolean mustUnderstand = null;
050    private String actor = null;
051
052    /**
053     * This constructor takes value of 'providerID' attribute which is
054     * required.
055     *
056     * @param providerID value of 'providerID' attribute
057     * @exception SOAPBindingException if the providerID is null
058     */
059    public ProviderHeader(String providerID) throws SOAPBindingException {
060        if (providerID == null) {
061            String msg = Utils.bundle.getString("missingProviderID");
062            Utils.debug.error("ProviderHeader.setProviderID: " + msg);
063            throw new SOAPBindingException(msg);
064        }
065        this.providerID = providerID;
066        if(id == null) {
067           id = SAMLUtils.generateID();
068        }
069        actor = SOAPBindingConstants.DEFAULT_SOAP_ACTOR;
070        mustUnderstand = Boolean.TRUE;
071    }
072
073    /**
074     * This constructor takes a <code>org.w3c.dom.Element</code>.
075     *
076     * @param providerElement a Provider element
077     * @exception SOAPBindingException if an error occurs while parsing
078     *                                 the Consent element
079     */
080    ProviderHeader(Element providerElement) throws SOAPBindingException {
081        providerID = XMLUtils.getNodeAttributeValue(
082                providerElement,
083                SOAPBindingConstants.ATTR_PROVIDER_ID);
084        if (providerID == null) {
085            String msg = Utils.bundle.getString("missingProviderID");
086            Utils.debug.error("ProviderHeader.setProviderID: " + msg);
087            throw new SOAPBindingException(msg);
088        }
089        affiliationID = XMLUtils.getNodeAttributeValue(
090                providerElement,
091                SOAPBindingConstants.ATTR_AFFILIATION_ID);
092        id = XMLUtils.getNodeAttributeValue(
093                providerElement,
094                SOAPBindingConstants.ATTR_id);
095        String str = XMLUtils.getNodeAttributeValueNS(
096                providerElement,
097                SOAPBindingConstants.NS_SOAP,
098                SOAPBindingConstants.ATTR_MUSTUNDERSTAND);
099        if (str != null && str.length() > 0) {
100            try {
101                mustUnderstand = Utils.StringToBoolean(str);
102            } catch (Exception pe) {
103                String msg = Utils.bundle.getString("invalidMustUnderstand");
104                Utils.debug.error("ProviderHeader: " + msg, pe);
105                throw new SOAPBindingException(msg);
106            }
107        }
108        
109        actor = XMLUtils.getNodeAttributeValueNS(
110                providerElement,
111                SOAPBindingConstants.NS_SOAP,
112                SOAPBindingConstants.ATTR_ACTOR);
113    }
114
115    /**
116     * Returns value of <code>providerID</code> attribute.
117     *
118     * @return value of <code>providerID</code> attribute
119     */
120    public String getProviderID() {
121        return providerID;
122    }
123
124    /**
125     * Returns value of <code>affiliationID</code> attribute.
126     *
127     * @return value of <code>affiliationID</code> attribute.
128     */
129    public String getAffiliationID() {
130        return affiliationID;
131    }
132
133    /**
134     * Returns value of <code>id</code> attribute.
135     *
136     * @return value of <code>id</code> attribute
137     */
138    public String getId() {
139        return id;
140    }
141
142    /**
143     * Returns value of <code>mustUnderstand</code> attribute.
144     *
145     * @return value of <code>mustUnderstand</code> attribute
146     */
147    public Boolean getMustUnderstand() {
148        return mustUnderstand;
149    }
150
151    /**
152     * Returns value of <code>actor</code> attribute.
153     *
154     * @return value of <code>actor</code> attribute
155     */
156    public String getActor() {
157        return actor;
158    }
159
160    /**
161     * Sets value of <code>providerID</code> attribute if the value is not null.
162     *
163     * @param providerID value of <code>providerID</code> attribute
164     */
165    public void setProviderID(String providerID) {
166        if (providerID != null) {
167            this.providerID = providerID;
168        }
169    }
170
171    /**
172     * Sets value of <code>affiliationID</code> attribute.
173     *
174     * @param affiliationID value of <code>affiliationID</code> attribute.
175     */
176    public void setAffiliationID(String affiliationID) {
177        this.affiliationID = affiliationID;
178    }
179
180    /**
181     * Sets value of <code>mustUnderstand</code> attribute.
182     *
183     * @param mustUnderstand value of <code>mustUnderstand</code> attribute
184     */
185    public void setMustUnderstand(Boolean mustUnderstand) {
186        this.mustUnderstand = mustUnderstand;
187    }
188
189    /**
190     * Sets value of <code>actor</code> attribute.
191     *
192     * @param actor value of <code>actor</code> attribute.
193     */
194    public void setActor(String actor) {
195        this.actor = actor;
196    }
197
198    /**
199     * Sets the sign flag. The header will be signed if the
200     * value is true.
201     *
202     * @param signFlag the boolean value of the sign flag.
203     */
204    public void setSignFlag(boolean signFlag) {
205        if (signFlag) {
206            id = SAMLUtils.generateID();
207        } else {
208            id = null;
209        }
210    }
211
212    /**
213     * Converts this header to <code>org.w3c.dom.Element</code> and add to
214     * parent Header Element.
215     *
216     * @param headerE parent Header Element
217     */
218    void addToParent(Element headerE) {
219        Document doc = headerE.getOwnerDocument();
220        Element providerHeaderE = doc.createElementNS(
221                SOAPBindingConstants.NS_SOAP_BINDING,
222                SOAPBindingConstants.PTAG_PROVIDER);
223        headerE.appendChild(providerHeaderE);
224        
225        providerHeaderE.setAttributeNS(null,
226                SOAPBindingConstants.ATTR_PROVIDER_ID,
227                providerID);
228        if (affiliationID != null) {
229            providerHeaderE.setAttributeNS(null,
230                    SOAPBindingConstants.ATTR_AFFILIATION_ID,
231                    affiliationID);
232        }
233        if (id != null) {
234            providerHeaderE.setAttributeNS(null, SOAPBindingConstants.ATTR_id,
235                    id);
236        }
237        if (mustUnderstand != null) {
238            providerHeaderE.setAttributeNS(SOAPBindingConstants.NS_SOAP,
239                    SOAPBindingConstants.PATTR_MUSTUNDERSTAND,
240                    Utils.BooleanToString(mustUnderstand));
241        }
242        if (actor != null) {
243            providerHeaderE.setAttributeNS(SOAPBindingConstants.NS_SOAP,
244                    SOAPBindingConstants.PATTR_ACTOR,
245                    actor);
246        }
247    }
248}