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: EncryptableNameIdentifier.java,v 1.4 2008/06/25 05:46:46 qcheng Exp $
026 *
027 * Portions Copyrighted 2014-2016 ForgeRock AS.
028 */
029
030package com.sun.identity.federation.message.common;
031
032import static org.forgerock.openam.utils.Time.*;
033
034import org.w3c.dom.Element;
035import java.util.Date;
036
037import com.sun.identity.federation.common.FSException;
038import com.sun.identity.federation.common.IFSConstants;
039import com.sun.identity.federation.common.FSUtils;
040
041import com.sun.identity.saml.assertion.NameIdentifier;
042import com.sun.identity.shared.DateUtils;
043import com.sun.identity.shared.xml.XMLUtils;
044
045/**
046 * This class contains methods for encrypting the  <code>NameIdentifier</code> 
047 * object.
048 *
049 * @supported.all.api
050 * @deprecated since 12.0.0
051 */
052@Deprecated
053public class EncryptableNameIdentifier {
054
055    private java.lang.String _nameQualifier = "";
056    private java.lang.String _name = "";
057    private java.lang.String _nonce = "";
058    private java.lang.String _format = "";
059    private java.util.Date _issueInstant = null;
060
061
062    /**
063     * Default Constructor.
064     */
065    protected EncryptableNameIdentifier() {}
066
067    /**
068     * Constructor creates <code>EncryptableNameIdentifier</code> object.
069     *
070     * @param ni the <code>NameIdentifier</code> object to be encrypted.
071     * @throws FSException if there is an error.
072     */
073    public EncryptableNameIdentifier(NameIdentifier ni) throws FSException {
074        if(ni == null) {
075           throw new FSException("nullInput", null) ;   
076        }
077        _nameQualifier = ni.getNameQualifier();
078        _name = ni.getName();
079        if(_nameQualifier == null || _name == null) {
080           throw new FSException("nullInput", null) ;   
081        }
082        _format = ni.getFormat();
083        if(_format == null) {
084           throw new FSException("notValidFormat", null) ;   
085        }
086        _nonce = FSUtils.generateID();
087        _issueInstant = newDate();
088    }
089
090    /**
091     * Consturctor creates <code>EncryptableNameIdentifier</code> object.
092     *
093     * @param name 
094     * @param nameQualifier
095     * @param format
096     * @param issueInstant the Issue Instant
097     * @param nonce
098     * @throws FSException if there is an error.
099     */
100    public EncryptableNameIdentifier(String name,String nameQualifier,
101                                     String format,Date issueInstant,
102                                     String nonce ) throws FSException {
103
104        if(name == null || nameQualifier == null || issueInstant == null ||
105            format == null || nonce == null) {
106           throw new FSException("nullInput", null) ;   
107        }
108        _name = name;
109        _nameQualifier = nameQualifier;
110        _format = format;
111        _nonce = nonce;
112        _issueInstant = issueInstant;
113    }
114
115
116   
117    /**
118     * Constructs a <code>EncryptedNameIdentifer</code> element from 
119     * the Document Element.
120     *
121     * @param nameIdentifier a <code>org.w3c.dom.Element</code> 
122     *        representing DOM tree for <code>EncryptableNameIdentifier</code>
123     *        object
124     * @throws FSException if it could not process the 
125     *            <code>org.w3c.dom.Element</code> properly, implying that there
126     *            is an error in the sender or in the element definition.
127     */
128    public EncryptableNameIdentifier(org.w3c.dom.Element nameIdentifier)  
129        throws FSException {
130        Element elt = (Element) nameIdentifier;
131        String eltName = elt.getLocalName();
132        if (eltName == null)  {
133            if (FSUtils.debug.messageEnabled()) {
134                FSUtils.debug.message("EncryptableNameIdentifier: local" +
135                " name missing");
136            }
137            throw new FSException("nullInput", null) ;   
138        }
139        if (!(eltName.equals("EncryptableNameIdentifier")))  {
140            if (FSUtils.debug.messageEnabled()) {
141                FSUtils.debug.message("EncryptableNameIdentifier: invalid"+
142                " root element");
143            }
144            throw new FSException("invalidElement", null) ;   
145        }
146        String read = elt.getAttribute("NameQualifier");
147        if (read != null) {
148            _nameQualifier = read;
149        }
150        read = elt.getAttribute("Format");
151        if (read != null) {
152            _format = read;
153        }
154
155        read = elt.getAttribute("Nonce");
156        if (read != null) {
157            _nonce = read;
158        }
159
160        read = elt.getAttribute("IssueInstant");
161        if(read != null) {
162           try {
163               _issueInstant = DateUtils.stringToDate(read);
164           } catch (java.text.ParseException pe) {
165               if (FSUtils.debug.messageEnabled()) {
166                   FSUtils.debug.message("EncryptableNameIdentifier: "+
167                   "Could not parse issue instant", pe);
168               }
169               throw new FSException("wrongInput", null) ;   
170           }
171        }
172        read = XMLUtils.getElementValue(elt);
173        if ((read == null) || (read.length() == 0)) {
174            if (FSUtils.debug.messageEnabled()) {
175                FSUtils.debug.message("EncryptableNameIdentifier: null"+
176                " input specified");
177            }
178            throw new FSException("nullInput", null) ;   
179        } else {
180           _name = read;
181        }
182    }   
183
184    /**
185     * Returns value of the <code>Format</code> attribute.
186     * 
187     * @return value of the <code>Format</code> attribute.
188     */
189    public java.lang.String getFormat() {
190        return _format;
191    }
192
193   
194    /**
195     * Sets the <code>Format</code> attribute.
196     *
197     * @param format the value of the <code>Format</code> attribute.
198     * @return true if the operation succeeds.
199     */
200    public boolean setFormat(java.lang.String  format ) {
201        // TODO do I need to restrict the format to those defined 
202        // by SAML specification ?
203        if ((format == null) || (format.length() == 0))  {
204            return false;
205        }
206        _format = format;
207        return true;
208    }
209   
210    /**
211     * Returns the <code>NameQualifier</code> attribute.
212     *
213     * @return the <code>nameQualifier</code>. 
214     */
215    public java.lang.String  getNameQualifier() {
216        return _nameQualifier;
217    }
218
219   
220    /**
221     * Sets <code>nameQualifier</code> attribute.
222     *
223     * @param nameQualifier the  <code>nameQualifier</code> attribute.
224     * @return true if operation succeeds.
225     */
226    public boolean setNameQualifier(java.lang.String  nameQualifier ) {
227        if ((nameQualifier == null) || (nameQualifier.length() == 0))  {
228            return false;
229        }
230        _nameQualifier=nameQualifier;
231        return true;
232    }
233
234    /**
235     * Sets the name attribute.
236     *
237     * @param name name of the <code>nameQualifier</code>.
238     * @return true if operation succeeds.
239     */
240    protected boolean setName(java.lang.String  name ) {
241        if ((name == null) || (name.length() == 0))  {
242            return false;
243        }
244        _name = name;
245        return true;
246    }
247
248    /**
249     * Returns the name from <code>NameQualifier</code>.
250     *
251     * @return the name from <code>NameQualifier</code>.
252     */
253    public java.lang.String getName() {
254        return _name;
255    }
256
257    /**
258     * Retunrs the nounce.
259     *
260     * @return the nounce.
261     */
262    public java.lang.String getNonce() {
263        return _nonce;
264    }
265
266    /**
267     * Returns the Issue Instant.
268     *
269     * @return the Issue Instant.
270     */
271    public java.util.Date getIssueInstant() {
272        return _issueInstant;
273    }
274   
275    /**
276     * Returns a String representation of the element.
277     *
278     * @return A string containing the valid XML for this element
279     *         By default name space name is prepended to the element name 
280     *         example <code>&lt;saml:EncryptableNameIdentifier&gt;</code>.
281     */
282    public java.lang.String toString() {
283        // call toString() with includeNS true by default and declareNS false
284         String xml = this.toString(true, false);
285        return xml;
286    }
287
288    /**
289     * Returns String representation of the 
290     * <code>&lt;EncryptableNameIdentifier&gt;</code> element.
291     *
292     * @param includeNS Determines whether or not the namespace qualifier is 
293     *        prepended to the Element when converted.
294     * @param declareNS Determines whether or not the namespace is declared 
295     * within the Element.
296     * @return A string containing the valid XML for this element
297     */
298    public java.lang.String  toString(boolean includeNS, boolean declareNS) {
299        StringBuffer xml = new StringBuffer(3000);
300        String NS="";
301        String appendNS="";
302        if (declareNS) {
303            NS=IFSConstants.LIB_12_NAMESPACE_STRING;
304        }
305        if (includeNS) {
306            appendNS=IFSConstants.LIB_PREFIX;
307        }
308
309        String dateStr = null;
310        if(_issueInstant != null) {
311           dateStr = DateUtils.toUTCDateFormat(_issueInstant);
312        }
313
314        xml.append("<").append(appendNS).append("EncryptableNameIdentifier").
315             append(NS);
316        if ((_nameQualifier != null) && (!(_nameQualifier.length() == 0))) {
317            xml.append(" ").append("NameQualifier").append("=\"").
318                append(_nameQualifier).append("\"");
319        }
320        if ((_format != null) && (!(_format.length() == 0))) {
321            xml.append(" ").append("Format").append("=\"").append(_format).
322            append("\"");
323        }
324        if ((_nonce != null) && (!(_nonce.length() == 0))) {
325            xml.append(" ").append("Nonce").append("=\"").append(_nonce).
326            append("\"");
327        }
328        if ((_issueInstant != null) && (dateStr.length() != 0)) {
329            xml.append(" ").append("IssueInstant").append("=\"").
330            append(dateStr).append("\"");
331        }
332        xml.append(">").append(_name);
333        xml.append("</").append(appendNS).append("EncryptableNameIdentifier").
334        append(">");
335           return xml.toString();
336    }                        
337}