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 * Portions Copyrighted 2014 ForgeRock AS
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 * @deprecated since 12.0.0
044 */
045@Deprecated
046
047public class SPProvidedNameIdentifier extends NameIdentifier {
048    
049    protected int minorVersion = IFSConstants.FF_11_PROTOCOL_MINOR_VERSION;
050
051     
052    /**
053     * Constructor creates <code>SPProvidedNameIdentifier</code> object.
054     *
055     * @param name
056     * @param nameQualifier
057     * @param format
058     * @throws SAMLException on errors.
059     */
060    public SPProvidedNameIdentifier(String name, String nameQualifier,
061            String format) throws SAMLException {
062        super(name, nameQualifier, format);
063    }
064
065    /**
066     * Constructor creates <code>SPProvidedNameIdentifier</code> object
067     * from Document Element.
068     *
069     * @param spProvidedNameIdentifierElement the Document Element.
070     * @throws FSMsgException on errors.
071     */
072    public SPProvidedNameIdentifier(Element spProvidedNameIdentifierElement)
073                                            throws FSMsgException {
074        Element elt = (Element) spProvidedNameIdentifierElement;
075        String eltName = elt.getLocalName();
076        if (eltName == null)  {
077            if (FSUtils.debug.messageEnabled()) {
078                FSUtils.debug.message("SPProvidedNameIdentifier(Element): "
079                        + "local name missing");
080            }
081            throw new FSMsgException("nullInput",null) ;
082        }
083        if (!(eltName.equals("SPProvidedNameIdentifier")))  {
084            if (FSUtils.debug.messageEnabled()) {
085                FSUtils.debug.message("SPProvidedNameIdentifier(Element: "
086                        + "invalid root element");
087            }
088            throw new FSMsgException("invalidElement",null) ;
089        }
090        String read = elt.getAttribute("NameQualifier");
091        if (read != null) {
092            setNameQualifier(read);
093        }
094        read = elt.getAttribute("Format");
095        if (read != null) {
096            setFormat(read);
097        }
098        read = XMLUtils.getElementValue(elt);
099        if ((read == null) || (read.length() == 0)) {
100            if (FSUtils.debug.messageEnabled()) {
101                FSUtils.debug.message("SPProvidedNameIdentifier(Element:  "
102                        + "null input specified");
103            }
104            throw new FSMsgException("nullInput",null) ;
105        } else {
106            setName(read);
107        }
108    }
109
110    /**
111     * Constructor creates <code>SPProvidedNameIdentifier</code> object.
112     *
113     * @param securityDomain the Security Domain
114     * @param name the identifier
115     * @throws FSMsgException if there is an error .
116     */
117    public SPProvidedNameIdentifier(String securityDomain, String name)
118    throws FSMsgException {
119        if (name== null || name.length() == 0 )  {
120            if (FSUtils.debug.messageEnabled()) {
121                FSUtils.debug.message("SPProvidedNameIdentifier: "
122                        + "null input specified");
123            }
124            throw new FSMsgException("nullInput",null) ;
125        }
126        setName(name);
127        if(securityDomain==null) {
128            setNameQualifier("");
129        }else {
130            setNameQualifier(securityDomain);
131        }
132    }
133    
134    /**
135     * Sets the <code>MinorVersion</code> attribute.
136     *
137     * @param version the <code>MinorVersion</code> attribute.
138     * @see #getMinorVersion()
139     */
140    public void setMinorVersion(int version) {
141        minorVersion = version;
142    }
143    
144    /**
145     * Returns the <code>MinorVersion</code> attribute.
146     *
147     * @return the <code>MinorVersion</code> attribute.
148     * @see #setMinorVersion(int)
149     */
150    public int getMinorVersion() {
151        return minorVersion;
152    }
153    
154    /**
155     * Returns the string representation of this object.
156     *
157     * @return a string containing the valid <code>XML</code> for this element
158     * @throws FSMsgException if there is an error creating
159     *         <code>XML</code> string from this object.
160     */
161    public String toXMLString() throws FSMsgException {
162        String xml = this.toXMLString(true, false);
163        return xml;
164    }
165    
166    /**
167     * Returns the string representation of this object.
168     *
169     * @param includeNS determines whether or not the namespace qualifier
170     *        is prepended to the Element when converted
171     * @param declareNS : Determines whether or not the namespace is declared
172     *        within the Element.
173     * @return a string containing the valid <code>XML</code> for this element
174     * @throws FSMsgException if there is an error creating
175     *         <code>XML</code> string from this object.
176     */
177    public String  toXMLString(boolean includeNS, boolean declareNS)
178                   throws FSMsgException  {
179        StringBuffer xml = new StringBuffer(3000);
180        String NS="";
181        String appendNS="";
182        if (declareNS) {
183            if (minorVersion == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) {
184                NS = IFSConstants.LIB_12_NAMESPACE_STRING;
185            } else {
186                NS = IFSConstants.LIB_NAMESPACE_STRING;
187            }
188        }
189        if (includeNS) appendNS=IFSConstants.LIB_PREFIX;
190        xml.append("<").append(appendNS).append("SPProvidedNameIdentifier").
191                append(" ").append(NS).append(" ");
192        if ((getNameQualifier() != null) &&
193                (!(getNameQualifier().length() == 0))) {
194            xml.append("NameQualifier").append("=\"").
195                    append(getNameQualifier()).
196                    append("\"").append(" ");
197        }
198        if ((getFormat() != null) && (!(getFormat().length() == 0))) {
199            xml.append("Format").append("=\"").append(getFormat()).
200                    append("\"").append(" ");
201        }
202        if ((getName() != null) && (!(getName().length() == 0))) {
203            xml.append(">").append(getName());
204            xml.append("</").append(appendNS).
205                    append("SPProvidedNameIdentifier").append(">\n");
206        }
207        return xml.toString();
208    }
209}