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: FSIDPList.java,v 1.2 2008/06/25 05:46:44 qcheng Exp $
026 *
027 */
028
029package com.sun.identity.federation.message;
030
031import com.sun.identity.federation.common.IFSConstants;
032import com.sun.identity.federation.message.common.FSMsgException;
033import com.sun.identity.federation.message.common.IDPEntries;
034import com.sun.identity.federation.message.common.GetComplete;
035import com.sun.identity.federation.common.FSUtils;
036
037import java.util.List;
038import java.util.Collections;
039import java.util.Iterator;
040import java.util.ArrayList;
041
042import org.w3c.dom.Element;
043import org.w3c.dom.Node;
044import org.w3c.dom.NodeList;
045
046/**
047 * This class has methods to construct an object or
048 * message representing a list of trusted Identity Providers.
049 *
050 * @supported.all.api
051 */
052public class FSIDPList {
053    private List getCompleteList = null;
054    /**
055     * <code>IDPEntries</code> object.
056     */
057    public IDPEntries idpEntries = null;
058    private int minorVersion = IFSConstants.FF_11_PROTOCOL_MINOR_VERSION;
059    
060    /**
061     * Default Constructor.
062     */
063    public FSIDPList() {
064    }
065    
066    /**
067     * Constructor creates <code>FSIDPList</code> object from
068     * Document Element.
069     *
070     * @param root the Document Element object.
071     * @throws FSMsgException if there is an error creating
072     *         the object.
073     */
074    public FSIDPList(Element root) throws FSMsgException {
075        if (root == null) {
076            FSUtils.debug.message("FSIDPList.parseXML: null input.");
077            throw new FSMsgException("nullInput",null);
078        }
079        String ns = root.getNamespaceURI();
080        if (ns == null) {
081            FSUtils.debug.error("FSIDPList(Element):No namespace");
082            throw new FSMsgException("wrongInput",null);
083        }
084        
085        if (ns.equals(IFSConstants.FF_12_XML_NS)) {
086            minorVersion = IFSConstants.FF_12_PROTOCOL_MINOR_VERSION;
087        }
088        
089        String tag = null;
090        if (((tag = root.getLocalName()) == null) ||
091                (!tag.equals(IFSConstants.IDP_LIST))) {
092            FSUtils.debug.message("FSIDPList.parseXML: wrong input.");
093            throw new FSMsgException("wrongInput",null);
094        }
095        
096        NodeList nl = root.getChildNodes();
097        Node child;
098        String childName;
099        int length = nl.getLength();
100        for (int i = 0; i < length; i++) {
101            child = nl.item(i);
102            if ((childName = child.getLocalName()) != null) {
103                if (childName.equals(IFSConstants.GET_COMPLETE)) {
104                    if ((getCompleteList == null) ||
105                            (getCompleteList == Collections.EMPTY_LIST)) {
106                        getCompleteList = new ArrayList();
107                    }
108                    getCompleteList.add(new GetComplete((Element)child));
109                }else if (childName.equals(IFSConstants.IDP_ENTRIES)) {
110                    idpEntries = new IDPEntries((Element) child);
111                }
112            }
113        }
114    }
115    
116    /**
117     * Constructor creates <code>FSIDPList</code> from <code>IDPEntries</code>
118     * object and a list of <code>GetComplete</code> objects.
119     *
120     * @param idpEntries the <code>IDPEntries</code> object.
121     * @param getCompleteList list of <code>GetComplete</code> objects.
122     */
123    public FSIDPList(IDPEntries idpEntries, List getCompleteList) {
124        this.idpEntries = idpEntries;
125        this.getCompleteList = getCompleteList;
126    }
127    
128    /**
129     * Sets the value of <code>MinorVersion</code> attribute.
130     *
131     * @param minorVersion the value of <code>MinorVersion</code> attribute
132     *        in the assertion.
133     * @see #setMinorVersion(int)
134     */
135    public void setMinorVersion(int minorVersion) {
136        this.minorVersion = minorVersion;
137    }
138    
139    /**
140     * Returns the value of <code>MinorVersion</code> attribute.
141     *
142     * @return the value of <code>MinorVersion</code> attribute.
143     * @see #setMinorVersion(int)
144     */
145    public int getMinorVersion() {
146        return minorVersion;
147    }
148    
149    /**
150     * Returns <code>IDPEntries</code> object.
151     *
152     * @return the <code>IDPEntries</code> object.
153     * @see #setIDPEntries(IDPEntries)
154     */
155    public IDPEntries getIDPEntries() {
156        return idpEntries;
157    }
158    
159    /**
160     * Returns list of <code>GetComplete</code> objects.
161     *
162     * @return list of <code>GetComplete</code> objects.
163     * @see #setGetCompleteList(List)
164     */
165    public List getGetCompleteList() {
166        return getCompleteList;
167    }
168    
169    /**
170     * Sets <code>IDPEntries</code> object.
171     *
172     * @param idpEntries <code>IDPEntries</code> object.
173     * @see #getIDPEntries
174     */
175    public void setIDPEntries(IDPEntries idpEntries) {
176        this.idpEntries = idpEntries;
177    }
178    
179    /**
180     * Sets list of <code>GetComplete</code> objects.
181     *
182     * @param getCompleteList list of <code>GetComplete</code> objects.
183     * @see #setGetCompleteList(List)
184     */
185    public void setGetCompleteList(List getCompleteList) {
186        this.getCompleteList = getCompleteList;
187    }
188    
189    /**
190     * Returns a <code>XML</code> string representation of this object.
191     *
192     * @return XML String representing this object.
193     * @throws FSMsgException if there is an error creating
194     *         the XML string or if the required elements to create
195     *         the string do not conform to the schema.
196     */
197    
198    public String toXMLString() throws FSMsgException {
199        return toXMLString(true, true);
200    }
201    
202    /**
203     * Creates a String representation of this object.
204     *
205     * @param includeNS : Determines whether or not the namespace qualifier
206     *        is prepended to the Element when converted
207     * @param declareNS : Determines whether or not the namespace is declared
208     *        within the Element.
209     * @return string containing the valid XML for this element.
210     * @throws FSMsgException if there is an error.
211     */
212    public String toXMLString(boolean includeNS, boolean declareNS)
213    throws FSMsgException {
214        return toXMLString(includeNS, declareNS, false);
215    }
216    
217    /**
218     * Creates a String representation of this element.
219     *
220     * @param includeNS Determines whether or not the namespace qualifier
221     *        is prepended to the Element when converted
222     * @param declareNS Determines whether or not the namespace is declared
223     *        within the Element.
224     * @param includeHeader Determines whether the output include the xml
225     *        declaration header.
226     * @return A string containing the valid XML for this element.
227     * @throws FSMsgException if there is an error.
228     */
229    public String toXMLString(boolean includeNS,boolean declareNS,
230            boolean includeHeader) throws FSMsgException {
231        
232        StringBuffer xml = new StringBuffer(300);
233        if (includeHeader) {
234            xml.append(IFSConstants.XML_PREFIX)
235            .append(IFSConstants.DEFAULT_ENCODING)
236            .append(IFSConstants.QUOTE)
237            .append(IFSConstants.SPACE)
238            .append(IFSConstants.QUESTION_MARK)
239            .append(IFSConstants.RIGHT_ANGLE);
240        }
241        String prefix = "";
242        String uri = "";
243        if (includeNS) {
244            prefix = IFSConstants.LIB_PREFIX;
245        }
246        if (declareNS) {
247            if(minorVersion == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) {
248                uri = IFSConstants.LIB_12_NAMESPACE_STRING;
249            } else {
250                uri = IFSConstants.LIB_NAMESPACE_STRING;
251            }
252        }
253        xml.append(IFSConstants.LEFT_ANGLE)
254        .append(prefix)
255        .append(IFSConstants.IDP_LIST)
256        .append(uri)
257        .append(IFSConstants.RIGHT_ANGLE);
258        
259        if (idpEntries != null){
260            xml.append(idpEntries.toXMLString(true, false));
261        }
262        
263        if ((this.getCompleteList != null) &&
264                (getCompleteList != Collections.EMPTY_LIST)){
265            Iterator i = getCompleteList.iterator();
266            while (i.hasNext()) {
267                xml.append((String)i.next());
268            }
269        }
270        xml.append(IFSConstants.START_END_ELEMENT)
271        .append(prefix)
272        .append(IFSConstants.IDP_LIST)
273        .append(IFSConstants.RIGHT_ANGLE);
274        
275        return xml.toString();
276    }
277}