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: NameIdentifier.java,v 1.2 2008/06/25 05:47:32 qcheng Exp $
026 *
027 */
028
029  
030
031package com.sun.identity.saml.assertion;
032import org.w3c.dom.Element;
033import com.sun.identity.saml.common.SAMLException;
034import com.sun.identity.saml.common.SAMLUtilsCommon;
035import com.sun.identity.saml.common.SAMLRequesterException;
036import com.sun.identity.saml.common.SAMLConstants;
037import com.sun.identity.shared.xml.XMLUtils;
038
039
040/**
041 * The NameIdentifier element specifies a <code>Subject</code> by a   
042 * combination of a name and a security domain governing the name of the
043 * <code>Subject</code>.
044 * @supported.all.api
045 */
046public class NameIdentifier {
047    static SAMLConstants sc;
048    private java.lang.String _nameQualifier = "";
049    private java.lang.String _format = "";
050    private java.lang.String _name = "";
051
052    /** 
053     * Default contructor
054     *
055     */
056    protected NameIdentifier() {}
057   
058    /**
059     * Constructs a <code>NameIdentifer</code> element from an existing XML
060     * block.
061     *
062     * @param nameIdentifierElement A <code>org.w3c.dom.Element</code> 
063     *        representing DOM tree for <code>NameIdentifier</code> object
064     * @exception SAMLException if it could not process the 
065     *        <code>org.w3c.dom.Element</code> properly, implying that there
066     *        is an error in the sender or in the element definition.
067     */
068    public NameIdentifier(org.w3c.dom.Element nameIdentifierElement)  
069        throws SAMLException 
070    {
071        Element elt = (Element) nameIdentifierElement;
072        String eltName = elt.getLocalName();
073        if (eltName == null)  {
074            if (SAMLUtilsCommon.debug.messageEnabled()) {
075                SAMLUtilsCommon.debug.message(
076                    "NameIdentifier: local name missing");
077            }
078            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString
079                ("nullInput")) ;   
080        }
081        if (!(eltName.equals("NameIdentifier")))  {
082            if (SAMLUtilsCommon.debug.messageEnabled()) {
083                SAMLUtilsCommon.debug.message(
084                    "NameIdentifier: invalid root element");
085            }
086            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
087                "invalidElement")) ;   
088        }
089        String read = elt.getAttribute("NameQualifier");
090        if (read != null) {
091            _nameQualifier = read;
092        }  // by default if not specified then _nameQualifer is ""
093        read = elt.getAttribute("Format");
094        // TODO considering the null and "" same both mean no 
095        // format .
096        if (read != null) {
097            _format = read;
098        }
099        read = XMLUtils.getElementValue(elt);
100        if ((read == null) || (read.length() == 0)) {
101            if (SAMLUtilsCommon.debug.messageEnabled()) {
102                SAMLUtilsCommon.debug.message("NameIdentifier: null input "
103                    + "specified");
104            }
105            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
106                "nullInput")) ;   
107        } else {
108           _name = read;
109        }
110    }   
111
112    /**
113     * Constructor
114     *@param name - The string representing the name of the Subject
115     *@exception SAMLException if the input has an error.
116     */
117    public NameIdentifier(String name)  
118        throws SAMLException 
119    {
120        if ((name == null) || (name.length() == 0)) {
121            if (SAMLUtilsCommon.debug.messageEnabled()) {
122                SAMLUtilsCommon.debug.message(
123                    "NameIdentifier: null input specified");
124            }
125            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
126                "nullInput")) ;   
127        }
128        _name=name;
129    }
130   
131    /**
132     * Constructs a <code>NameQualifier</code> instance.
133     *
134     * @param name The string representing the name of the Subject
135     * @param nameQualifier The security or administrative domain that qualifies
136     *        the name of the <code>Subject</code>. This is optional, could be
137     *        null or "".
138     * @exception SAMLException if the input has an error.
139     */
140    public NameIdentifier(String name, String nameQualifier)  
141        throws SAMLException 
142    {
143        if ((name == null) || (name.length() == 0)) {
144            if (SAMLUtilsCommon.debug.messageEnabled()) {
145                SAMLUtilsCommon.debug.message(
146                    "NameIdentifier: null input specified");
147            }
148            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
149                "nullInput")) ;   
150        }
151        if (nameQualifier != null ) {
152            _nameQualifier = nameQualifier;
153        } // else by default its ""
154            
155        // TODO Treating null same as "" for nameQualifier 
156        // as it does not have any -ve impact(??) but its easy in equals()
157        // method
158        // TODO need I restrict format to the ones defined per the SAML
159        // specification. ?
160        _name=name;
161    }
162   
163    /**
164     * Constructs a <code>NameQualifier</code> instance.
165     *
166     * @param name The string representing the name of the Subject
167     * @param nameQualifier The security or administrative domain that qualifies
168     *        the name of the <code>Subject</code>. This is optional could be
169     *        null or "".
170     * @param format The syntax used to describe the name of the
171     *        <code>Subject</code>. This optional, could be null or "".
172     * @exception SAMLException if the input has an error.
173     */
174    public NameIdentifier(String name, String nameQualifier, String format)  
175        throws SAMLException 
176    {
177        if ((name == null) || (name.length() == 0)) {
178            if (SAMLUtilsCommon.debug.messageEnabled()) {
179                SAMLUtilsCommon.debug.message(
180                    "NameIdentifier: null input specified");
181            }
182            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
183                "nullInput")) ;   
184        }
185        if (nameQualifier == null ) {
186            _nameQualifier="";
187        } else {
188            _nameQualifier = nameQualifier;
189        }
190        // TODO Treating null same as "" for securityDomain 
191        // as it does not have any -ve impact(??) but its easy in equals()
192        // method
193        // TODO need I restrict format to the ones defined per the SAML
194        // specification. ?
195        if (format == null ) {
196            _format="";
197        } else {
198            _format = format;
199        }
200        _name=name;
201    }
202   
203    /**
204     * Returns format.
205     * 
206     * @return format element. Returns null if there is no format specified.
207     */
208    public java.lang.String  getFormat() {
209        return _format;
210    }
211
212   
213    /**
214     * Sets the format attribute.
215     *
216     * @param format A String representing the format.
217     * @return true if operation succeeds.
218     */
219    public boolean setFormat(java.lang.String  format ) {
220        // TODO do I need to restrict the format to those defined 
221        // by SAML specification ?
222        if ((format == null) || (format.length() == 0))  {
223            return false;
224        }
225        _format = format;
226        return true;
227    }
228   
229    /**
230     * Returns the name.
231     *
232     * @return A String representing the <code>nameQualifier</code>. 
233     * Returns null if there is no <code>nameQualifier</code>.
234     */
235    public java.lang.String  getNameQualifier() {
236        return _nameQualifier;
237    }
238
239   
240    /**
241     * Sets <code>nameQualifier</code> attribute.
242     *
243     * @param nameQualifier name qualifier.
244     * @return true if operation succeeds.
245     */
246    public boolean setNameQualifier(java.lang.String  nameQualifier ) {
247        if ((nameQualifier == null) || (nameQualifier.length() == 0))  {
248            return false;
249        }
250        _nameQualifier=nameQualifier;
251        return true;
252    }
253
254    /**
255     * Sets the name attribute.
256     *
257     * @param name name of the <code>nameQualifier</code>.
258     * @return true if operation succeeds.
259     */
260    protected boolean setName(java.lang.String  name ) {
261        if ((name == null) || (name.length() == 0))  {
262            return false;
263        }
264        _name = name;
265        return true;
266    }
267
268    /**
269     * Returns the name from <code>NameQualifier</code>.
270     *
271     * @return name
272     */
273    public java.lang.String getName() {
274        return _name;
275    }
276   
277    /**
278     * Returns a String representation of the element.
279     *
280     * @return A string containing the valid XML for this element
281     *         By default name space name is prepended to the element name 
282     *         example <code>&lt;saml:NameIdentifier&gt;</code>.
283     */
284    public java.lang.String toString() {
285        // call toString() with includeNS true by default and declareNS false
286        String xml = this.toString(true, false);
287        return xml;
288    }
289
290    /**
291     * Returns String representation of the <code>&lt;NameIdentifier&gt;</code>
292     * element.
293     *
294     * @param includeNS Determines whether or not the namespace qualifier is 
295     *        prepended to the Element when converted.
296     * @param declareNS Determines whether or not the namespace is declared 
297     *        within the Element.
298     * @return A string containing the valid XML for this element
299     */
300    public java.lang.String  toString(boolean includeNS, boolean declareNS) {
301        StringBuffer xml = new StringBuffer(3000);
302        String NS="";
303        String appendNS="";
304        if (declareNS) NS=sc.assertionDeclareStr;
305        if (includeNS) appendNS="saml:";
306        xml.append("<").append(appendNS).append("NameIdentifier").
307            append(NS);
308        if ((_nameQualifier != null) && (!(_nameQualifier.length() == 0))) {
309            xml.append(" ").append("NameQualifier").append("=\"").
310                append(_nameQualifier).append("\"");
311        }
312        if ((_format != null) && (!(_format.length() == 0))) {
313            xml.append(" ").append("Format").append("=\"").append(_format).
314            append("\"");
315        }
316        xml.append(sc.RIGHT_ANGLE).append(_name);
317        xml.append(SAMLUtilsCommon.makeEndElementTagXML(
318            "NameIdentifier",includeNS));
319        return xml.toString();
320    }                        
321
322    /**
323     * Checks for equality between this object and the
324     * <code>NameQualifier</code> passed down as parameter. Checks if Name is
325     * equal and if it has <code>NameQualifier</code> and Format defined
326     * checks for equality in those too.
327     *
328     * @param nid <code>NameIdentifier</code> to be checked
329     * @return true if the two <code>NameQualifier</code> are equal or not
330     */
331    public boolean equals(NameIdentifier nid) {
332        if (nid != null) {
333            String name = nid.getName();
334            String nameQualifier = nid.getNameQualifier();
335            String format = nid.getFormat();
336            // never null as null converted to ""
337            if ((name.length() == 0) || (!name.equalsIgnoreCase(_name))) {
338                return false;
339            }
340            if (!nameQualifier.equalsIgnoreCase(_nameQualifier)) {
341                return false;
342            }
343            // TODO checking format for exact match, is that correct ?
344            if (!format.equals(_format)) {
345                return false;
346            }
347        } else {
348            return false;
349        }
350        return true;
351    }
352}
353




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.