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: IDPProvidedNameIdentifier.java,v 1.2 2008/06/25 05:46:47 qcheng Exp $
026 *
027 */
028
029package com.sun.identity.federation.message.common;
030
031import com.sun.identity.federation.common.FSUtils;
032import com.sun.identity.federation.common.IFSConstants;
033import com.sun.identity.saml.assertion.NameIdentifier;
034import com.sun.identity.saml.common.SAMLException;
035import com.sun.identity.shared.xml.XMLUtils;
036import org.w3c.dom.Element;
037
038/**
039 * This class has methods to create the <code>NameIdentifier</code>
040 * object provided by the Identity Provider.
041 *
042 * @supported.all.api
043 */
044public class IDPProvidedNameIdentifier extends NameIdentifier {
045
046    protected int minorVersion = IFSConstants.FF_11_PROTOCOL_MINOR_VERSION;
047
048    /**
049     * Constructor creates <code>IDPProvidedNameIdentifier</code> object.
050     *
051     * @param name the Identity Provider name.
052     * @param nameQualifier 
053     * @param format
054     * @throws SAMLException on error.
055     */
056    public IDPProvidedNameIdentifier(String name,String nameQualifier,
057            String format) throws SAMLException {                
058                super(name, nameQualifier, format);                
059        }
060    
061    /**
062     * Constructor creates <code>IDPProvidedNameIdentifier</code> object
063     * from Document Element.
064     *
065     * @param idpProvidedNameIdentifierElement the Document Element.
066     * @throws FSMsgException on error.
067     */
068    public IDPProvidedNameIdentifier(Element idpProvidedNameIdentifierElement)
069                                     throws FSMsgException {
070        FSUtils.debug.message("IDPProvidedNameIdentifier(Element):  Called");
071        Element elt = (Element) idpProvidedNameIdentifierElement;
072        String eltName = elt.getLocalName();
073        if (eltName == null)  {
074            if (FSUtils.debug.messageEnabled()) {
075                FSUtils.debug.message("IDPProvidedNameIdentifier(Element): "
076                        + "local name missing");
077            }
078            throw new FSMsgException("nullInput",null) ;
079        }
080        if (!(eltName.equals("IDPProvidedNameIdentifier")))  {
081            if (FSUtils.debug.messageEnabled()) {
082                FSUtils.debug.message("IDPProvidedNameIdentifier(Element: "
083                        + "invalid root element");
084            }
085            throw new FSMsgException("invalidElement",null) ;
086        }
087        String read = elt.getAttribute("NameQualifier");
088        if (read != null) {
089            setNameQualifier(read);
090        }
091        read = elt.getAttribute("Format");
092        if (read != null) {
093            setFormat(read);
094        }
095        read = XMLUtils.getElementValue(elt);
096        if ((read == null) || (read.length() == 0)) {
097            if (FSUtils.debug.messageEnabled()) {
098                FSUtils.debug.message("IDPProvidedNameIdentifier(Element: "
099                        + "null input specified");
100            }
101            throw new FSMsgException("nullInput",null) ;
102        } else {
103            setName(read);
104        }
105    }
106    
107    /**
108     * Constructor creates <code> IDPProvidedNameIdentifier</code> object.
109     *
110     * @param securityDomain
111     * @param name
112     * @throws FSMsgException on error.
113     */
114    public IDPProvidedNameIdentifier(
115            String securityDomain, String name
116            ) throws FSMsgException {
117        if (name== null || name.length() == 0 )  {
118            if (FSUtils.debug.messageEnabled()) {
119                FSUtils.debug.message("IDPProvidedNameIdentifier: "
120                        + "null input specified");
121            }
122            throw new FSMsgException("nullInput",null) ;
123        }
124        setName(name);
125        if(securityDomain==null)
126            setNameQualifier("");
127        else
128            setNameQualifier(securityDomain);
129    }
130       
131    /**
132     * Sets the <code>MinorVersion</code> attribute.
133     *
134     * @param version the <code>MinorVersion</code> attribute.
135     * @see #getMinorVersion()
136     */
137    public void setMinorVersion(int version) {
138        minorVersion = version;
139    }
140        
141    /**
142     * Returns the <code>MinorVersion</code> attribute.
143     *
144     * @return the <code>MinorVersion</code> attribute.
145     * @see #setMinorVersion(int)
146     */
147    public int getMinorVersion() {
148        return minorVersion;
149    }
150    
151    /**
152     * Returns the string representation of this object.
153     *
154     * @return a string containing the valid <code>XML</code> for this element
155     * @throws FSMsgException if there is an error creating
156     *         <code>XML</code> string from this object.
157     */
158    public String toXMLString() throws FSMsgException {
159        String xml = this.toXMLString(true, false);
160        return xml;
161    }
162    /**
163     * Returns the string representation of this object.
164     *
165     * @param includeNS 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 containing the valid <code>XML</code> for this element
170     * @throws FSMsgException if there is an error creating
171     *         <code>XML</code> string from this object.
172     */
173    public String  toXMLString(
174            boolean includeNS, boolean declareNS
175            ) throws FSMsgException {
176        StringBuffer xml = new StringBuffer(3000);
177        String NS="";
178        String appendNS="";
179        if (declareNS) {
180            if(minorVersion == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) {
181                NS = IFSConstants.LIB_12_NAMESPACE_STRING;
182            } else {
183                NS=IFSConstants.LIB_NAMESPACE_STRING;
184            }
185        }
186        if (includeNS) appendNS=IFSConstants.LIB_PREFIX;
187        xml.append("<").append(appendNS).append("IDPProvidedNameIdentifier").
188                append(" ").append(NS).append(" ");
189        if ((getNameQualifier() != null) && 
190                     (!(getNameQualifier().length() == 0))) {
191            xml.append("NameQualifier").append("=\"")
192               .append(getNameQualifier())
193               .append("\"").append(" ");
194        }
195        if ((getFormat() != null) && (!(getFormat().length() == 0))) {
196            xml.append("Format").append("=\"").append(getFormat())
197               .append("\"").append(" ");
198        }
199        if ((getName() != null) && (!(getName().length() == 0))) {
200            xml.append(">").append(getName());
201            xml.append("</").append(appendNS)
202               .append("IDPProvidedNameIdentifier").append(">\n");
203        }
204           return xml.toString();
205    }    
206}