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