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: UsageDirectiveHeader.java,v 1.2 2008/06/25 05:47:23 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.soapbinding; 
031
032import java.lang.Object;
033
034import java.util.ArrayList;
035import java.util.Iterator;
036import java.util.List;
037
038import org.w3c.dom.Document;
039import org.w3c.dom.Element;
040import org.w3c.dom.Node;
041import org.w3c.dom.NodeList;
042
043import com.sun.identity.saml.common.SAMLUtils;
044import com.sun.identity.shared.xml.XMLUtils;
045
046/**
047 * The <code>UsageDirectiveHeader</code> class represents 'UsageDirective'
048 * element defined in SOAP binding schema.
049 *
050 * @supported.all.api
051 */
052public class UsageDirectiveHeader {   
053
054    private String ref = null;
055    private String id = null;
056    private Boolean mustUnderstand = null;
057    private String actor = null;
058    private List elements = null;
059
060    /**
061     * Constructor.
062     *
063     * @param ref the value of <code>ref</code> attribute.
064     * @throws SOAPBindingException if the value of <code>ref</code> attribute
065     *                              is null.
066     */
067    public UsageDirectiveHeader(String ref) throws SOAPBindingException {
068        if (ref == null) {
069            String msg = Utils.bundle.getString("refAttributeNull");
070            Utils.debug.error("UsageDirectiveHeader: " + msg);
071            throw new SOAPBindingException(msg);
072        }
073        this.ref = ref;
074    }
075
076    /**
077     * This constructor takes a <code>org.w3c.dom.Element</code>.
078     *
079     * @param usageDirectiveElement a UsageDirective element.
080     * @throws SOAPBindingException if an error occurs while parsing
081     *                                 the UsageDirective element.
082     */
083    UsageDirectiveHeader(Element usageDirectiveElement) 
084                         throws SOAPBindingException {
085        ref = XMLUtils.getNodeAttributeValue(
086            usageDirectiveElement, SOAPBindingConstants.ATTR_REF);
087        id = XMLUtils.getNodeAttributeValue(
088            usageDirectiveElement, SOAPBindingConstants.ATTR_id);
089        String str = XMLUtils.getNodeAttributeValueNS(
090            usageDirectiveElement, SOAPBindingConstants.NS_SOAP,
091            SOAPBindingConstants.ATTR_MUSTUNDERSTAND);
092        if (str != null && str.length() > 0) {
093            try {
094                mustUnderstand = Utils.StringToBoolean(str);
095            } catch (Exception pe) {
096                String msg = Utils.bundle.getString("invalidMustUnderstand");
097                Utils.debug.error("UsageDirectiveHeader: " + msg, pe);
098                throw new SOAPBindingException(msg);
099            }
100        }
101        actor = XMLUtils.getNodeAttributeValueNS(usageDirectiveElement,
102            SOAPBindingConstants.NS_SOAP, SOAPBindingConstants.ATTR_ACTOR);
103        NodeList nl = usageDirectiveElement.getChildNodes();
104        int length = nl.getLength();
105        for(int i = 0; i < length; i++) {
106            Node child = nl.item(i);
107            if (child.getNodeType() == Node.ELEMENT_NODE) {
108                if (elements == null) {
109                    elements = new ArrayList();
110                }
111                elements.add(child);
112            }
113        }
114    }
115
116    /**
117     * Returns value of <code>ref</code> attribute.
118     *
119     * @return value of <code>ref</code> attribute.
120     */
121    public String getRef() {
122        return ref;
123    }
124
125    /**
126     * Returns value of <code>id</code> attribute.
127     *
128     * @return value of <code>id</code> attribute.
129     */
130    public String getId() {
131        return id;
132    }
133
134    /**
135     * Returns value of <code>mustUnderstand</code> attribute.
136     *
137     * @return value of <code>mustUnderstand</code> attribute.
138     */
139    public Boolean getMustUnderstand() {
140        return mustUnderstand;
141    }
142
143    /**
144     * Returns value of <code>actor</code> attribute.
145     *
146     * @return value of <code>actor</code> attribute.
147     */
148    public String getActor() {
149        return actor;
150    }
151
152    /**
153     * Returns a list of child elements. 
154     * Each entry will be a <code>org.w3c.dom.Element</code>.
155     *
156     * @return a list of child elements
157     */
158    public List getElements() {
159        return elements;
160    }
161
162    /**
163     * Sets value of <code>ref</code> attribute.
164     *
165     * @param ref value of <code>ref</code> attribute
166     */
167    public void setRef(String ref) {
168        if (ref != null) {
169            this.ref = ref;
170        }
171    }
172
173    /**
174     * Sets value of <code>mustUnderstand</code> attribute.
175     *
176     * @param mustUnderstand value of <code>mustUnderstand</code> attribute.
177     */
178    public void setMustUnderstand(Boolean mustUnderstand) {
179        this.mustUnderstand = mustUnderstand;
180    }
181
182    /**
183     * Sets value of <code>actor</code> attribute.
184     *
185     * @param actor value of <code>actor</code> attribute.
186     */
187    public void setActor(String actor) {
188        this.actor = actor;
189    }
190
191    /**
192     * Sets a list of child elements.
193     * Each entry will be a <code>org.w3c.dom.Element</code>.
194     *
195     * @param elements a list of child elements
196     */
197    public void setElements(List elements) {
198        this.elements = elements;
199    }
200
201    /**
202     * Sets the sign flag. The header will be signed if 
203     * the value is true.
204     *
205     * @param signFlag the sign flag
206     */
207    public void setSignFlag(boolean signFlag) {
208        if (signFlag) {
209            id = SAMLUtils.generateID();
210        } else {
211            id = null;
212        }
213    }
214
215    /**
216     * Converts this header to <code>org.w3c.dom.Element</code> and add to
217     * parent Header Element.
218     *
219     * @param headerE parent Header Element
220     */
221    void addToParent(Element headerE) {
222        Document doc = headerE.getOwnerDocument();
223        Element usageDirectiveHeaderE = doc.createElementNS(
224                SOAPBindingConstants.NS_SOAP_BINDING,
225                SOAPBindingConstants.PTAG_USAGE_DIRECTIVE);
226        headerE.appendChild(usageDirectiveHeaderE);
227        
228        usageDirectiveHeaderE.setAttributeNS(null,
229                SOAPBindingConstants.ATTR_REF,
230                ref);
231        if (id != null) {
232            usageDirectiveHeaderE.setAttributeNS(null,
233                    SOAPBindingConstants.ATTR_id,
234                    id);
235        }
236        if (mustUnderstand != null) {
237            usageDirectiveHeaderE.setAttributeNS(SOAPBindingConstants.NS_SOAP,
238                    SOAPBindingConstants.PATTR_MUSTUNDERSTAND,
239                    Utils.BooleanToString(mustUnderstand));
240        }
241        if (actor != null) {
242            usageDirectiveHeaderE.setAttributeNS(SOAPBindingConstants.NS_SOAP,
243                    SOAPBindingConstants.PATTR_ACTOR, actor);
244        }
245        if (elements != null && !elements.isEmpty()) {
246            Iterator iter = elements.iterator();
247            while(iter.hasNext()) {
248                Element childE = (Element)iter.next();
249                usageDirectiveHeaderE.appendChild(doc.importNode(childE,true));
250            }
251        }
252    }
253}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.