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: IDPEntry.java,v 1.2 2008/06/25 05:46:47 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.federation.message.common;
031
032import org.w3c.dom.Element;
033import org.w3c.dom.Node;
034import org.w3c.dom.NodeList;
035
036import com.sun.identity.shared.xml.XMLUtils;
037import com.sun.identity.saml.common.SAMLUtils;
038import com.sun.identity.federation.common.FSUtils;
039import com.sun.identity.federation.common.IFSConstants;
040
041/**
042 * This class defines methods to set/retrieve single identity provider
043 * information trusted by the request issuer to authenticate the presenter.
044 *
045 * @supported.all.api
046 */
047
048public class IDPEntry {
049    public static final int LIB_TYPE_IDP = 0;
050    public static final int LIB_TYPE_BROKER = 1;
051    
052    private String providerID = null;
053    private String providerName = null;
054    private String location = null;
055    
056    /**
057     * Default Constructor.
058     */
059    public IDPEntry() {
060    }
061    
062    /**
063     * Constructor creates <code>IDPEntry</code> object.
064     *
065     * @param providerID the Identity Provider identifier.
066     * @param providerName the Identity Provider name.
067     * @param location
068     */
069    public IDPEntry(String providerID,String providerName,String location) {
070        this.providerID = providerID;
071        this.providerName = providerName;
072        this.location = location;
073    }
074    
075    /**
076     * Returns the value of <code>ProviderID</code> attribute.
077     *
078     * @return the value of <code>ProviderID</code> attribute.
079     * @see #setProviderID(String)
080     */
081    
082    public String getProviderID() {
083        return providerID;
084    }
085    
086    /**
087     * Sets the value of <code>ProviderID</code> attribute.
088     *
089     * @param providerID the value of <code>ProviderID</code> attribute.
090     * @see #getProviderID
091     */
092    public void setProviderID(String providerID) {
093        this.providerID = providerID;
094    }
095    
096    /**
097     * Returns the Identity Provider Name.
098     *
099     * @return the Identity Provider Name.
100     * @see #setProviderName(String)
101     */
102    public String getProviderName() {
103        return providerName;
104    }
105    
106    /**
107     * Sets the Identity Provider Name.
108     *
109     * @param providerName the Identity Provider Name.
110     * @see #getProviderName
111     */
112    public void setProviderName(String providerName) {
113        this.providerName = providerName;
114    }
115    
116    /**
117     * Returns the location URI of the Identity Provider.
118     *
119     * @return the location URI of the Identity Provider.
120     * @see #setLocation(String)
121     */
122    public String getLocation() {
123        return location;
124    }
125    
126    /**
127     * Sets the location URI of the Identity Provider.
128     *
129     * @param location the location URI of the Identity Provider.
130     * @see #getLocation
131     */
132    public void setLocation(String location) {
133        this.location = location;
134    }
135    
136    /**
137     * Returns the string representation of this object.
138     * This method translates the response to an XML document string.
139     *
140     * @return An XML String representing the response. NOTE: this is a
141     *         complete SAML response xml string with ResponseID,
142     *         MajorVersion, etc.
143     */
144    
145    public String toXMLString() throws FSMsgException {
146        return toXMLString(true, true);
147    }
148    
149    /**
150     * Returns a String representation of this object.
151     *
152     * @param includeNS  Determines whether or not the namespace qualifier
153     *        is prepended to the Element when converted
154     * @param declareNS Determines whether or not the namespace is declared
155     *        within the Element.
156     * @return a string containing the valid XML for this element
157     * @throws FSMsgException if there is an error converting
158     *         this object ot a string.
159     */
160    
161    public String toXMLString(boolean includeNS,boolean declareNS)
162    throws FSMsgException {
163        return toXMLString(includeNS, declareNS, false);
164    }
165    /**
166     * Returns a String representation of this object.
167     *
168     * @param includeNS Determines whether or not the namespace qualifier
169     *        is prepended to the Element when converted
170     * @param declareNS Determines whether or not the namespace is declared
171     *        within the Element.
172     * @param includeHeader Determines whether the output include the xml
173     *        declaration header.
174     * @return a string containing the valid XML for this element
175     * @throws FSMsgException if there is an error converting
176     *         this object ot a string.
177     */
178    
179    public String toXMLString(boolean includeNS,boolean declareNS,
180            boolean includeHeader) throws FSMsgException {
181        StringBuffer xml = new StringBuffer(300);
182        if (includeHeader) {
183            xml.append("<?xml version=\"1.0\" encoding=\"").
184                    append(IFSConstants.DEFAULT_ENCODING).append("\" ?>\n");
185        }
186        String prefix = "";
187        String uri = "";
188        if (includeNS) {
189            prefix = IFSConstants.LIB_PREFIX;
190        }
191        if (declareNS) {
192            uri = IFSConstants.LIB_NAMESPACE_STRING;
193        }
194        
195        xml.append("<").append(prefix).append("IDPEntry").append(uri).
196                append(">\n");
197        
198        if(providerID != null){
199            xml.append("<").append(prefix).append("ProviderID").append(">")
200                    .append(providerID)
201                    .append("</").append(prefix).append("ProviderID")
202                    .append(">\n");
203        }
204        
205        if(providerName != null){
206            xml.append("<").append(prefix).append("ProviderName").append(">")
207                    .append(providerName)
208                    .append("</").append(prefix).append("ProviderName")
209                    .append(">\n");
210        }
211        
212        if(location != null){
213            xml.append("<").append(prefix).append("Loc").append(">").
214                    append(location).
215                    append("</").append(prefix).append("Loc").append(">\n");
216        }
217        
218        xml.append("</").append(prefix).append("IDPEntry").append(">\n");
219        
220        return xml.toString();
221    }
222    
223    /**
224     * Constructor creates <code>IDPEntry</code> Object from
225     * Document Element.
226     *
227     * @param root Document Element of <code>IDPEntry<code> object.
228     * @throws FSMsgException if <code>IDPEntry<code> cannot be created.
229     */
230    
231    public IDPEntry(Element root) throws FSMsgException {
232        if (root == null) {
233            SAMLUtils.debug.message("IDPEntry.parseXML: null input.");
234            throw new FSMsgException("nullInput",null);
235        }
236        String tag = null;
237        if (((tag = root.getLocalName()) == null) ||
238                (!tag.equals("IDPEntry"))) {
239            FSUtils.debug.message("IDPEntry.parseXML: wrong input.");
240            throw new FSMsgException("wrongInput",null);
241        }
242        NodeList nl = root.getChildNodes();
243        Node child;
244        String nodeName;
245        int length = nl.getLength();
246        for (int i = 0; i < length; i++) {
247            child = nl.item(i);
248            if ((nodeName = child.getLocalName()) != null) {
249                if (nodeName.equals("ProviderID")) {
250                    if (providerID != null) {
251                        if (FSUtils.debug.messageEnabled()) {
252                            FSUtils.debug.message("IDPEntry(Element): should"
253                                    + "contain only one ProviderID");
254                        }
255                        throw new FSMsgException("wrongInput",null);
256                    }
257                    providerID = XMLUtils.getElementValue((Element) child);
258                } else if (nodeName.equals("ProviderName")) {
259                    if (providerName != null) {
260                        if (FSUtils.debug.messageEnabled()) {
261                            FSUtils.debug.message("IDPEntry(Element): should"
262                                    + "contain only one ProviderName");
263                        }
264                        throw new FSMsgException("wrongInput",null);
265                    }
266                    providerName = XMLUtils.getElementValue((Element) child);
267                } else if (nodeName.equals("Loc")) {
268                    if (location != null) {
269                        if (FSUtils.debug.messageEnabled()) {
270                            FSUtils.debug.message("IDPEntry(Element): should"
271                                    + "contain only one Loc");
272                        }
273                        throw new FSMsgException("wrongInput",null);
274                    }
275                    location = XMLUtils.getElementValue((Element) child);
276                }
277            }
278        }
279    }
280}