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: ObligationsImpl.java,v 1.3 2008/11/10 22:57:06 veiming Exp $
026 *
027 */
028
029package com.sun.identity.xacml.policy.impl;
030
031import com.sun.identity.shared.xml.XMLUtils;
032
033import com.sun.identity.xacml.common.XACMLConstants;
034import com.sun.identity.xacml.common.XACMLException;
035import com.sun.identity.xacml.common.XACMLSDKUtils;
036import com.sun.identity.xacml.context.Response;
037import com.sun.identity.xacml.context.Result;
038import com.sun.identity.xacml.policy.PolicyFactory;
039import com.sun.identity.xacml.policy.Obligation;
040import com.sun.identity.xacml.policy.Obligations;
041
042import java.util.ArrayList;
043import java.util.Collections;
044import java.util.Iterator;
045import java.util.List;
046
047import org.w3c.dom.Document;
048import org.w3c.dom.Element;
049import org.w3c.dom.Node;
050import org.w3c.dom.NodeList;
051
052/**
053 * The <code>Obligations</code> element is a container of 
054 * one or more <code>Obligation</code>s issuded by 
055 * authorization authority.
056 * @supported.all.api
057 */
058public class ObligationsImpl implements Obligations {
059
060    /* schema
061        <xs:element name="Obligations" type="xacml:ObligationsType"/>
062        <xs:complexType name="ObligationsType">
063            <xs:sequence>
064                <xs:element ref="xacml:Obligation" maxOccurs="unbounded"/>
065            </xs:sequence>
066        </xs:complexType>
067    */
068    private List obligations = new ArrayList(); //Obligation+ 
069    private boolean mutable = true;
070
071    /**
072     * Default constructor
073     */
074    public ObligationsImpl() {
075    }
076
077    /** 
078     * Constructs an <code>ObligationsImpl</code> object from an XML string
079     *
080     * @param xml string representing an <code>ObligationsImpl</code> object
081     * @throws XACMLException if the XML string could not be processed
082     */
083    public ObligationsImpl(String xml) throws XACMLException {
084        Document document = XMLUtils.toDOMDocument(xml, XACMLSDKUtils.debug);
085        if (document != null) {
086            Element rootElement = document.getDocumentElement();
087            processElement(rootElement);
088        } else {
089            XACMLSDKUtils.debug.error(
090                "ResponseImpl.processElement(): invalid XML input");
091            throw new XACMLException(
092                XACMLSDKUtils.xacmlResourceBundle.getString(
093                "errorObtainingElement"));
094        }
095    }
096    /** 
097     * Constructs an <code>ObligationsImpl</code> object from an XML DOM element
098     *
099     * @param element XML DOM element representing a
100     *        <code>ObligationsImpl</code>  object.
101     *
102     * @throws SAMLException if the DOM element could not be processed
103     */
104    public ObligationsImpl(Element element) throws XACMLException {
105        processElement(element);
106        makeImmutable();
107    }
108
109
110    /**
111     * Returns the <code>Obligation</code> objects set in this
112     * <code>Obligations</code>
113     * @return the <code>Obligation</code> objects set in this
114     * <code>Obligations</code>
115     */
116    public List getObligations() {
117        return obligations;
118    }
119
120    /**
121     * Sets the <code>Obligation</code> objects of this
122     * <code>Obligations</code>
123     *
124     * @param obligations the <code>Obligation</code> objects to set in this
125     * <code>Obligations</code>
126     * @throws XACMLException if the object is immutable.
127     */
128    public void setObligations(List obligations) throws XACMLException {
129        if (!mutable) {
130            throw new XACMLException(
131                XACMLSDKUtils.xacmlResourceBundle.getString("objectImmutable"));
132        }
133        if (obligations != null) {
134            Iterator iter = obligations.iterator();
135            this.obligations = new ArrayList();
136            while (iter.hasNext()) {
137                Obligation obligation = (Obligation) iter.next();
138                this.obligations.add(obligation);
139            }
140        } else {
141            obligations = null;
142        }
143    }
144
145    /**
146     * Adds an <code>Obligation</code> to this object.
147     *
148     * @param obligation the <code>Obligation</code> to add.
149     * @throws XACMLException if the object is immutable.
150     */
151    public void addObligation(Obligation obligation) throws XACMLException {
152        if (!mutable) {
153            throw new XACMLException(
154                XACMLSDKUtils.xacmlResourceBundle.getString("objectImmutable"));
155        }
156        if (obligations == null) {
157            obligations = new ArrayList();
158        }
159        obligations.add(obligation);
160    }
161
162
163   /**
164    * Returns a string representation of this object
165    * @param includeNSPrefix Determines whether or not the namespace qualifier
166    *        is prepended to the Element when converted
167    * @param declareNS Determines whether or not the namespace is declared
168    *        within the Element.
169    * @return a string representation
170    * @exception XACMLException if conversion fails for any reason
171     */
172    public String toXMLString(boolean includeNSPrefix, boolean declareNS)
173            throws XACMLException {
174        StringBuffer sb = new StringBuffer(2000);
175        String nsPrefix = "";
176        String nsDeclaration = "";
177        if (includeNSPrefix) {
178            nsPrefix = XACMLConstants.XACML_NS_PREFIX + ":";
179        }
180        if (declareNS) {
181            nsDeclaration = XACMLConstants.XACML_NS_DECLARATION;
182        }
183        sb.append("<").append(nsPrefix).append(XACMLConstants.OBLIGATIONS)
184            .append(" ").append(nsDeclaration).append(">\n");
185        int length = 0;
186        if (obligations != null) {
187            length = obligations.size();
188            for (int i = 0; i < length; i++) {
189                Obligation obligation = (Obligation)obligations.get(i);
190                sb.append(obligation.toXMLString(includeNSPrefix, false));
191            }
192        }
193        sb.append("</").append(nsPrefix)
194                .append(XACMLConstants.OBLIGATIONS).append(">\n");
195        return sb.toString();
196                
197    }
198
199   /**
200    * Returns a string representation of this object
201    *
202    * @return a string representation
203    * @exception XACMLException if conversion fails for any reason
204    */
205    public String toXMLString() throws XACMLException {
206        return toXMLString(true, false);
207    }
208
209   /**
210    * Makes this object immutable
211    */
212    public void makeImmutable() {
213        mutable = false;
214    }
215
216   /**
217    * Checks if this object is mutable
218    *
219    * @return <code>true</code> if the object is mutable,
220    *         <code>false</code> otherwise
221    */
222    public boolean isMutable() {
223        return mutable;
224    }
225    
226    /** 
227     * Initializes a <code>ObligationsImpl</code> object from an XML DOM element
228     *
229     * @param element XML DOM element representing a
230     *        <code>ObligationsImpl</code> object
231     *
232     * @throws XACMLException if the DOM element could not be processed
233     */
234    private void processElement(Element element) throws XACMLException {
235        if (element == null) {
236            XACMLSDKUtils.debug.error(
237                "ResponseImpl.processElement(): invalid root element");
238            throw new XACMLException(
239                XACMLSDKUtils.xacmlResourceBundle.getString("invalid_element"));
240        }
241        String elemName = element.getLocalName();
242        if (elemName == null) {
243            XACMLSDKUtils.debug.error(
244                "ResponseImpl.processElement(): local name missing");
245            throw new XACMLException(
246                XACMLSDKUtils.xacmlResourceBundle.getString(
247                "missing_local_name"));
248        }
249
250        if (!elemName.equals(XACMLConstants.OBLIGATIONS)) {
251            XACMLSDKUtils.debug.error(
252                "ResponseImpl.processElement: invalid local name " + elemName);
253            throw new XACMLException(
254                XACMLSDKUtils.xacmlResourceBundle.getString(
255                "invalid_local_name"));
256        }
257
258        // starts processing subelements
259        NodeList nodes = element.getChildNodes();
260        int numOfNodes = nodes.getLength();
261        int nextElem = 0;
262
263        while (nextElem < numOfNodes) { 
264            Node child = (Node) nodes.item(nextElem);
265            if (child.getNodeType() == Node.ELEMENT_NODE) {
266                String childName = child.getLocalName();
267                if (childName != null) {
268                    if (childName.equals(XACMLConstants.OBLIGATION)) {
269                        obligations.add(
270                            PolicyFactory.getInstance().createObligation(
271                                    (Element)child));
272                    } else {
273                        XACMLSDKUtils.debug.error(
274                            "ObligationsImpl.processElement(): "
275                            + " invalid child element: " + elemName);
276                        throw new XACMLException(
277                            XACMLSDKUtils.xacmlResourceBundle.getString(
278                            "invalid_child_name")); 
279                    }
280                }
281            }
282            nextElem++;
283        }
284    }
285
286    
287}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.