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: AudienceRestrictionCondition.java,v 1.2 2008/06/25 05:47:31 qcheng Exp $
026 *
027 */
028
029  
030
031package com.sun.identity.saml.assertion;
032
033import com.sun.identity.saml.common.SAMLUtilsCommon;
034import com.sun.identity.saml.common.SAMLConstants;
035import com.sun.identity.saml.common.SAMLException;
036import com.sun.identity.saml.common.SAMLRequesterException;
037import com.sun.identity.shared.xml.XMLUtils;
038import org.w3c.dom.Element;
039import org.w3c.dom.Node;
040import org.w3c.dom.NodeList;
041import java.util.List;
042import java.util.Vector;
043import java.util.Iterator;
044
045/**
046 *This is an implementation of the abstract <code>Condition</code> class, which
047 *specifes that the assertion this AuthenticationCondition is part of, is 
048 *addressed to one or more specific audience.
049 *@supported.all.api
050 */
051
052public class AudienceRestrictionCondition extends Condition {
053    private SAMLConstants sc;
054    /**
055    _audience is a list of all the audience this condition is addressed to.
056    Its implemented as a list of <code>String</code> objects each of them 
057    containing an audience ( String). This list needs to have at least one 
058    audience specified.  An audience is actually a URI. The URI MAY identify a 
059    document that describes the terms and conditions of audience membership
060    */
061    private java.util.Vector _audience = new Vector();
062
063
064    /**
065     * Constructs an <code>AudienceRestrictionCondition</code> element from an
066     * existing XML block.
067     *
068     * @param audienceRestrictionConditionElement A
069     *        <code>org.w3c.dom.Element</code> representing DOM tree for
070     *        <code>AudienceRestrictionCondition</code> object.
071     * @exception SAMLException if it could not process the 
072     *            <code>org.w3c.dom.Element</code> properly, implying that there
073     *            is an error in the sender or in the element definition.
074     */      
075    public AudienceRestrictionCondition(
076        org.w3c.dom.Element audienceRestrictionConditionElement)  
077            throws SAMLException
078    {
079        Element elt = (Element)audienceRestrictionConditionElement;
080        String eltName = elt.getLocalName();
081        if (eltName == null)  {
082            if (SAMLUtilsCommon.debug.messageEnabled())  { 
083                SAMLUtilsCommon.debug.message("AudienceRestrictionCondition: "
084                    + "null condition ");
085            }
086            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
087                "nullInput"));
088        }
089        if (!(eltName.equals("AudienceRestrictionCondition"))) {
090            if (!(eltName.equals("Condition"))) {
091                if (SAMLUtilsCommon.debug.messageEnabled())  { 
092                    SAMLUtilsCommon.debug.message(
093                        "AudienceRestrictionCondition: "
094                        + "unsupported condition ");
095                }
096                throw new SAMLRequesterException(
097                    SAMLUtilsCommon.bundle.getString(
098                    "unsupportedCondition"));
099            }
100        }
101        if (eltName.equals("Condition")) { // seems like extension type
102            String type = elt.getAttribute("xsi:type");
103            if (!(type.equals("AudienceRestrictionCondition"))) {
104                if (SAMLUtilsCommon.debug.messageEnabled()) {
105                    SAMLUtilsCommon.debug.message(
106                        "AudienceRestrictionCondition: invalid condition");
107                }
108                throw new SAMLRequesterException(
109                    SAMLUtilsCommon.bundle.getString(
110                    "invalidElement"));
111            }
112        }
113        NodeList nl = elt.getChildNodes();
114        if (nl.getLength() <= 0 ) {
115            if (SAMLUtilsCommon.debug.messageEnabled())  {
116                SAMLUtilsCommon.debug.message("AudienceRestrictionCondition: "
117                        + "no Audience in this Element");
118            }
119            throw new SAMLRequesterException(
120                SAMLUtilsCommon.bundle.getString("noElement")) ;   
121        }
122        int length = nl.getLength();
123        for (int n=0; n<length; n++) {
124            Node child = (Node)nl.item(n);
125            if (child.getNodeType() != Node.ELEMENT_NODE)  {
126                continue;
127            }
128            String childName = child.getLocalName();
129            if (childName.equals("Audience"))  {
130                _audience.add(XMLUtils.getElementValue((Element)child));
131            } else { 
132                if (SAMLUtilsCommon.debug.messageEnabled())  {
133                    SAMLUtilsCommon.debug.message(
134                        "AudienceRestrictionCondition:"
135                        +"  invalid element found");
136                }
137                throw new SAMLRequesterException(
138                    SAMLUtilsCommon.bundle.getString("invalidElement")) ;   
139            }
140        }
141    }       
142
143    /**  
144     *Constructs <code>AudienceRestrictionCondition</code> with a
145     *<code>List</code> of audience for this condition, each of them 
146     *being a String.
147     *@param audience A List of audience to be included within this condition
148     *@exception SAMLException if the <code>List</code> is empty or if there is 
149     *some error in processing the contents of the <code>List</code>
150     */
151    public AudienceRestrictionCondition(List audience)  throws SAMLException {
152        if (audience.isEmpty()) {
153            if (SAMLUtilsCommon.debug.messageEnabled()) {
154                SAMLUtilsCommon.debug.message("AudienceRestrictionCondition:  "
155                    + "null input specified");
156            }
157            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
158                "nullInput")) ;   
159        }
160        _audience.addAll(audience);
161    }
162    
163    /**  
164     *Adds an audience to this Condition element
165     *@param audience audience to be added
166     *@return boolean indicating success or failure of operation
167     */
168    public  boolean  addAudience(java.lang.String audience) {
169        if ((audience != null) && !(audience.length() == 0)) {
170            _audience.add(audience);
171            return true; 
172        } else {
173            return false;
174        }
175    }                      
176                           
177    /** 
178     *Adds a <code>List</code> of audience held within this Condition element
179     *@param audience A <code>List</code> of audience to be included within 
180     *this condition
181     *@return  boolean indicating success or failure of operation.
182     */             
183    public boolean setAudience(List audience ) {
184        if (audience.isEmpty()) return false;
185        _audience.addAll(audience);
186        return true;
187    }
188                                      
189    /** 
190     *Returns list of Audience held within this Condition element
191     *@return An the <code>List</code> of Audience within this Condition element
192     */             
193    public java.util.List getAudience() {
194        return _audience;
195    }
196                                      
197    /** 
198     * Returns true if a particular audience string is contained within this
199     * <code>AudienceRestrictionCondition</code> object
200     *
201     * @param audience audience to be checked
202     * @return true if the audience exists.
203     */             
204    public boolean containsAudience(String audience) {
205        if ((audience != null) && !(audience.length() == 0)) {
206            if (_audience.contains((String)audience)) {
207                return true;
208            } else {
209                return false;
210            }
211        } else {
212            return false;
213        }
214    }
215
216    /**
217     * Removes an audience from the <code>List</code> within this Condition
218     * element
219     * @param audience A string representing the value of the Audience 
220     * @return boolean true/false representing success or failure of 
221     *         the operation 
222     */
223    public boolean  removeAudience(java.lang.String audience) {
224        if ((audience != null) && !(audience.length() == 0)) {
225            _audience.remove(audience);
226            return true; 
227        } else return false;
228    }
229                           
230    /**
231     * Returns a String representation of the element.
232     *
233     * @return A string containing the valid XML for this element
234     * By default name space name is prepended to the element name 
235     * example <code>&lt;saml:AudienceRestrictionCondition&gt;</code>.
236    */
237    public java.lang.String toString() {
238        // call toString() with includeNS true by default and declareNS false
239        String xml = this.toString(true, false);
240        return xml;
241    }
242
243    /**
244     * Returns a String representation of the
245     * <code>&lt;AudienceRestrictionCondition&gt;</code> element.
246     *
247     * @param includeNS Determines whether or not the namespace qualifier is 
248     *        prepended to the Element when converted
249     * @param declareNS Determines whether or not the namespace is declared
250     *        within the Element.
251     * @return A string containing the valid XML for this element
252    */                       
253    public java.lang.String toString(boolean includeNS, boolean declareNS) {
254        StringBuffer xml = new StringBuffer(3000);
255        String o = SAMLUtilsCommon.makeStartElementTagXML(
256                        "AudienceRestrictionCondition", includeNS, declareNS);
257        xml.append(o).append(sc.NL);
258        Iterator it = _audience.iterator();
259        while ( it.hasNext()) {
260            o = SAMLUtilsCommon.makeStartElementTagXML(
261                "Audience",includeNS, false);
262            xml.append(o).append((String)it.next());
263            o = SAMLUtilsCommon.makeEndElementTagXML("Audience",includeNS);
264            xml.append(o);
265        }
266        o = SAMLUtilsCommon.makeEndElementTagXML(
267                "AudienceRestrictionCondition",includeNS);
268        xml.append(o);
269        return xml.toString();
270    }                                                       
271
272    /** 
273     * Evaluates this condition 
274     * This method can be overridden by a plug-in which provides
275     * means of evaluating this condition
276     *
277     * @return evaluation status.
278     */
279    public int  evaluate() {
280        return Condition.INDETERMINATE;
281    }
282}
283




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.