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: FSSubject.java,v 1.2 2008/06/25 05:46:45 qcheng Exp $
026 * Portions Copyrighted 2014 ForgeRock AS
027 */
028
029
030package com.sun.identity.federation.message;
031
032import com.sun.identity.federation.common.FSUtils;
033import com.sun.identity.federation.common.IFSConstants;
034import com.sun.identity.federation.message.common.FSMsgException;
035import com.sun.identity.federation.message.common.IDPProvidedNameIdentifier;
036import com.sun.identity.saml.assertion.NameIdentifier;
037import com.sun.identity.saml.assertion.Subject;
038import com.sun.identity.saml.assertion.SubjectConfirmation;
039import com.sun.identity.saml.common.SAMLConstants;
040import com.sun.identity.saml.common.SAMLException;
041import org.w3c.dom.Element;
042import org.w3c.dom.Node;
043import org.w3c.dom.NodeList;
044
045/**
046 * This class has methods to create <code>Subject</code> object.
047 *
048 * @supported.all.api
049 * @deprecated since 12.0.0
050 */
051@Deprecated
052public class FSSubject extends Subject {
053    protected IDPProvidedNameIdentifier _idpNameIdentifier;
054    
055    /**
056     * Default Constructor.
057     */
058    protected FSSubject() {}
059    
060    /**
061     * Constructor creates <code>FSSubject</code> object.
062     *
063     * @param nameIdentifier the <code>NameIdentifier</code> of
064     *        the subject.
065     * @param subjectConfirmation the <code>SubjectConfirmation</code>
066     *        object.
067     * @param idpNameIdentifier the <code>IDPProvidedNameIdentifier</code>
068     *         object.
069     * @throws FSMsgException if there is an error creating this object.
070     * @throws SAMLException if there is an error creating this object.
071     */
072    public FSSubject(NameIdentifier nameIdentifier,
073            SubjectConfirmation subjectConfirmation,
074            IDPProvidedNameIdentifier idpNameIdentifier)
075            throws FSMsgException, SAMLException {
076        super(nameIdentifier, subjectConfirmation);
077        _idpNameIdentifier = idpNameIdentifier;
078    }
079    
080    
081    /**
082     * Constructor creates <code>FSSubject</code> object.
083     *
084     * @param nameIdentifier the <code>NameIdentifier</code> of
085     *        the subject.
086     * @param idpNameIdentifier the <code>IDPProvidedNameIdentifier</code>
087     *         object.
088     * @throws FSMsgException if there is an error creating this object.
089     * @throws SAMLException if there is an error creating this object.
090     */
091    public FSSubject(NameIdentifier nameIdentifier,
092            IDPProvidedNameIdentifier idpNameIdentifier)
093            throws FSMsgException, SAMLException {
094        super(nameIdentifier);
095        _idpNameIdentifier = idpNameIdentifier;
096    }
097    
098    /**
099     * Constructor creates <code>FSSubject</code> object from
100     * the Document Element.
101     *
102     * @param subjectElement the Document Element
103     * @throws FSMsgException if there is an error creating this object.
104     * @throws SAMLException if there is an error creating this object.
105     */
106    public FSSubject(Element subjectElement)
107    throws FSMsgException, SAMLException {
108        FSUtils.debug.message("FSSubject(Element): Called");
109        int elementCount=0;
110        Element elt = (Element)subjectElement;
111        String rootTagName = elt.getLocalName();
112        if (rootTagName == null) {
113            if (FSUtils.debug.messageEnabled()) {
114                FSUtils.debug.message("FSSubject: local name missing");
115            }
116            throw new FSMsgException("nullInput",null) ;
117        }
118        if (!(rootTagName.equals("Subject")))  {
119            if (FSUtils.debug.messageEnabled()) {
120                FSUtils.debug.message("FSSubject: invalid root element");
121            }
122            throw new FSMsgException("invalidElement",null) ;
123        }
124        NodeList nl = subjectElement.getChildNodes();
125        int length = nl.getLength();
126        if (length == 0 ) {
127            if (FSUtils.debug.messageEnabled()) {
128                FSUtils.debug.message("FSSubject: No sub elements found");
129            }
130            throw new FSMsgException("emptyElement",null) ;
131        }
132        for (int n=0; n < length; n++) {
133            Node child = (Node)nl.item(n);
134            if (child.getNodeType() != Node.ELEMENT_NODE) {
135                continue;
136            }
137            String childName = child.getLocalName();
138            if (childName.equals("NameIdentifier"))  {
139                setNameIdentifier(new NameIdentifier((Element)child));
140                elementCount++;
141            } else if (childName.equals("SubjectConfirmation"))  {
142                setSubjectConfirmation(new SubjectConfirmation((Element)child));
143                elementCount++;
144            }else if (childName.equals("IDPProvidedNameIdentifier"))  {
145                _idpNameIdentifier =
146                        new IDPProvidedNameIdentifier((Element)child);
147                elementCount++;
148            } else {
149                if (FSUtils.debug.messageEnabled()) {
150                    FSUtils.debug.message("FSSubject: Invalid element "
151                            + "encountered.");
152                }
153                throw new FSMsgException("invalidElement",null) ;
154            }
155        }
156        if (elementCount > 3 ) {
157            if (FSUtils.debug.messageEnabled()) {
158                FSUtils.debug.message("FSSubject: more than allowed elements "
159                        + "passed");
160            }
161            throw new FSMsgException("moreElement",null) ;
162        }
163        FSUtils.debug.message("FSSubject(Element): leaving");
164    }
165    
166    /**
167     * Constructor creates <code>FSSubject</code> object.
168     *
169     * @param subjectConfirmation the <code>SubjectConfirmation</code> object.
170     * @throws SAMLException if there is an error creating this object.
171     */
172    public FSSubject(SubjectConfirmation subjectConfirmation)
173    throws SAMLException {
174        super(subjectConfirmation);
175    }
176    
177    /**
178     * Sets the Identity Provider's <code>NameIdentifier</code>.
179     *
180     * @param idpNameIdentifier the Identity Provider's
181     *        <code>NameIdentifier</code>.
182     */
183    public boolean setIDPProvidedNameIdentifier(
184            IDPProvidedNameIdentifier idpNameIdentifier) {
185        if (idpNameIdentifier == null)  {
186            if (FSUtils.debug.messageEnabled())  {
187                FSUtils.debug.message("FSSubject:null IDPProvidedNameIdentifier"
188                        + "specified");
189            }
190            return false;
191        }
192        _idpNameIdentifier = idpNameIdentifier;
193        return true;
194    }
195    
196    /**
197     * Returns the Identity Provider's <code>NameIdentifier</code>.
198     *
199     * @return the Identity Provider's <code>NameIdentifier</code>.
200     */
201    public IDPProvidedNameIdentifier getIDPProvidedNameIdentifier() {
202        return _idpNameIdentifier;
203    }
204    
205    /**
206     * Returns a String representation of this object.
207     *
208     * @return a string containing the valid XML for this element
209     * @throws FSMsgException if there is an error converting
210     *         this object ot a string.
211     */
212    public String toXMLString() throws FSMsgException {
213        String xml = this.toXMLString(true, false);
214        return xml;
215    }
216    
217    /**
218     * Returns a String representation of the Logout Response.
219     *
220     * @param includeNS : Determines whether or not the namespace qualifier
221     *        is prepended to the Element when converted
222     * @param declareNS : Determines whether or not the namespace is declared
223     *        within the Element.
224     * @return a string containing the valid XML for this element
225     * @throws FSMsgException if there is an error converting
226     *         this object ot a string.
227     */
228    public String toXMLString(boolean includeNS, boolean declareNS)
229    throws FSMsgException {
230        StringBuffer xml = new StringBuffer(3000);
231        String prefix = "";
232        String libprefix = "";
233        String uri = "";
234        String liburi = "";
235        if (includeNS) {
236            prefix = SAMLConstants.ASSERTION_PREFIX;
237            libprefix = IFSConstants.LIB_PREFIX;
238            
239        }
240        if (declareNS) {
241            uri = SAMLConstants.assertionDeclareStr;
242            liburi = IFSConstants.LIB_NAMESPACE_STRING;
243        }
244        
245        xml.append("<").append(prefix).append("Subject").append(" ").
246                append(uri).append(" ").append(liburi).append(" ").
247                append("xsi:type").
248                append("=\"").append(libprefix).append("SubjectType").
249                append("\"").
250                append(">");
251        
252        if (getNameIdentifier() != null ) {
253            xml.append(getNameIdentifier().toString(includeNS, false));
254        }
255        if (getSubjectConfirmation() != null)  {
256            xml.append(getSubjectConfirmation().toString(includeNS, false));
257        }
258        if (_idpNameIdentifier != null ) {
259            xml.append(_idpNameIdentifier.toXMLString(includeNS, false));
260        }
261        xml.append("</").append(prefix).append("Subject").append(">");
262        return xml.toString();
263    }
264}