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: AssertionBase.java,v 1.2 2008/06/25 05:47:31 qcheng Exp $
026 *
027 */
028
029  
030package com.sun.identity.saml.assertion;
031
032import com.sun.identity.shared.DateUtils;
033import com.sun.identity.saml.common.SAMLUtilsCommon;
034import com.sun.identity.saml.common.SAMLConstants;
035import com.sun.identity.saml.common.SAMLException;
036import com.sun.identity.saml.common.SAMLRequesterException;
037import com.sun.identity.saml.common.SAMLVersionMismatchException;
038import com.sun.identity.shared.xml.XMLUtils;
039import java.util.Set;
040import java.util.List;
041import java.util.Collections;
042import java.util.Iterator;
043import java.util.Date;
044import java.util.HashSet;
045import java.text.ParseException;
046import org.w3c.dom.Node;
047import org.w3c.dom.NodeList;
048import org.w3c.dom.Element;
049
050/**
051 *This object stands for <code>Assertion</code> element.An Assertion is a 
052 *package of information that supplies one or more <code>Statement</code> made 
053 *by an issuer. There are three kinds of assertionsL Authentication,  
054 *AuthorizationDecision and Attribute assertion.
055 *
056 *This class is an abstract base class for all Assertion implementations and
057 *encapsulates common functionality.
058 *
059 *@supported.all.api
060 */
061public abstract class AssertionBase {           
062    protected static SAMLConstants sc;
063
064    /**
065    The statements variable is a HashSet of all the stataments in this assertion
066    in the defined sequence
067    */
068    protected Set _statements = Collections.synchronizedSet(new HashSet());
069 
070    /**
071    This value specifies the SAML major version. Each assertion MUST specify 
072    the SAML major version identifier.The identifier for this version of SAML 
073    is 1. 
074    */  
075    protected  int _majorVersion = sc.ASSERTION_MAJOR_VERSION;
076   
077    /**
078    This value specifies the SAML minor version. Each assertion MUST specify 
079    the SAML minor version identifier. The identifier for this version of SAML 
080    is 0.
081    */
082    protected  int _minorVersion = sc.ASSERTION_MINOR_VERSION;
083   
084    /**
085    The _assertionID attribute specifies the assertion identifier.
086    */
087    protected AssertionIDReference _assertionID = null; 
088       
089    /**
090    The Issuer attribute specifies the issuer of the assertion by means of a 
091    string.
092    */
093    protected java.lang.String _issuer = null;
094    
095    /**
096    The IssueInstant attribute specifies the time instant of issue in Universal 
097    Coordinated Time.
098    */
099    protected java.util.Date _issueInstant;
100   
101    /**
102    The <code>Conditions</code> element specifies conditions that affect the 
103    validity of the asserted statement. 
104    */
105    protected Conditions _conditions;
106   
107    /**
108    The <code>Advice</code> element specifies additional information related 
109    to the assertion that may assist processing in certain situations but which 
110    can be ignored by applications that do not support its use. 
111    */
112    protected AdviceBase _advice;
113
114    protected String    xmlString       = null;
115    protected String    signatureString = null;
116    protected Element   signature       = null;
117    protected boolean   signed          = false;
118    protected boolean   valid           = true;
119    protected boolean   validationDone  = true; 
120
121    protected static final String ASSERTION_ID_ATTRIBUTE = "AssertionID";
122
123    /**
124     * Returns whether the object is signed or not.
125     * @return true if the object is signed; false otherwise.
126     */
127    public boolean isSigned() {
128        return signed;
129    }
130
131    /**
132     * Returns whether the signature on the object is valid or not.
133     * @return true if the signature on the object is valid; false otherwise.
134     */
135    public boolean isSignatureValid() {
136        return false;
137    }
138
139    /**
140     * Signs the Assertion.
141     * @exception SAMLException If it could not sign the Assertion.
142     */
143    public void signXML() throws SAMLException {
144        throw new UnsupportedOperationException();
145    }
146
147    /**
148     * Signs the Assertion.
149     *
150     * @param certAlias certification Alias used to sign Assertion.
151     * @exception SAMLException if it could not sign the Assertion.
152     */
153    public void signXML(String certAlias) throws SAMLException {
154        throw new UnsupportedOperationException();
155    }
156
157    /**
158     * Gets the Signature element of the Assertion.
159     * @return Element the Signature of the Assertion in DOM element.
160     */
161    public Element getSignature() {
162        return signature;
163    }
164    
165    /**
166     * Sets the signature for the Request 
167     * @param elem ds:Signature element
168     * @return A boolean value: true if the operation succeeds; false otherwise.
169     */
170    public boolean setSignature(Element elem) {
171        if (signed) {
172            return false;
173        }
174        if (elem == null) {
175            return false;
176        } else {
177            signature = elem;
178            signed = true;
179            signatureString = XMLUtils.print(elem); 
180            return true;
181        }
182    }
183
184    /**
185     * Creates appropriate Advice instance
186     * @param adviceElement the Advice Element
187     * @return the Advice instance
188     */
189    protected abstract AdviceBase createAdvice(Element adviceElement)
190        throws SAMLException;
191    
192    /**
193     * Create appropriate AuthorizationDecisionStatement instance
194     * @param authDecisionElement
195     *     the AuthorizationDecisionStatement Element
196     * @return AuthorizationDecisionStatement instance
197     */
198    protected abstract AuthorizationDecisionStatementBase
199        createAuthorizationDecisionStatement(Element authDecisionElement)
200        throws SAMLException;
201     
202    /**
203     * Creates appropriate AuthenticationStatement instance
204     * @param authenticationElement
205     *     the AuthenticationStatement Element
206     * @return AuthenticationStatement instance
207     */
208    protected abstract AuthenticationStatement
209        createAuthenticationStatement(Element authenticationElement)
210        throws SAMLException;
211     
212    /**
213     * Creates appropriate AttributeStatement instance
214     * @param attributeElement
215     *    the AttributeStatement Element
216     * @return AttributeStatement instance
217     */
218    protected abstract AttributeStatement
219        createAttributeStatement(Element attributeElement) throws SAMLException;
220
221    /**
222     * Creates appropriate AssertionIDReference instance
223     * @param assertionIDRefElement
224     *     the AssertionIDReference Element
225     * @return AssertionIDReference instance
226     */
227    protected abstract AssertionIDReference
228        createAssertionIDReference(Element assertionIDRefElement)
229        throws SAMLException;
230     
231    /**
232     * Creates appropriate AssertionIDReference instance
233     * @param assertionID
234     *     the AssertionID String
235     * @return AssertionIDReference instance
236     */
237    protected abstract AssertionIDReference
238        createAssertionIDReference(String assertionID) throws SAMLException;
239     
240    /**
241     * Creates appropriate Conditions instance
242     * @param conditionsElement
243     *     the Conditions Element
244     * @return Conditions instance
245     */
246    protected abstract Conditions
247        createConditions(Element conditionsElement) throws SAMLException;
248
249
250    /** 
251     *Default constructor, declaring protected to enable extensibility
252     */
253    protected AssertionBase() {}
254   
255    /**
256     * Contructor
257     * This constructor is used to build <code>Assertion</code> object from a
258     * block of existing XML that has already been built into a DOM.
259     *
260     * @param assertionElement A <code>org.w3c.dom.Element</code> representing 
261     *        DOM tree for <code>Assertion</code> object
262     * @exception SAMLException if it could not process the Element properly, 
263     *            implying that there is an error in the sender or in the
264     *            element definition.
265     */
266    public AssertionBase(org.w3c.dom.Element assertionElement) 
267        throws SAMLException 
268    {
269        Element elt = (Element) assertionElement;
270        String eltName = elt.getLocalName();
271        if (eltName == null)  {
272            if (SAMLUtilsCommon.debug.messageEnabled()) {
273                SAMLUtilsCommon.debug.message("Assertion: local name missing");
274            }
275            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString
276                                        ("nullInput")) ;
277        }
278        if (!(eltName.equals("Assertion")))  {
279            if (SAMLUtilsCommon.debug.messageEnabled()) {
280                SAMLUtilsCommon.debug.message("Assertion: " +
281                                              "invalid root element");
282            }
283            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString
284                ("invalidElement")+ ":"+eltName) ;   
285        }
286
287        String read = elt.getAttribute("Issuer");
288        if ((read == null) || (read.length() == 0)) {
289            if (SAMLUtilsCommon.debug.messageEnabled()) {
290                SAMLUtilsCommon.debug.message("Assertion: Issuer missing");
291            }
292            throw new SAMLRequesterException(
293                SAMLUtilsCommon.bundle.getString("missingAttribute") +
294                                                 ":Issuer");
295        } else  {
296            _issuer = read;
297        }
298
299        List signs = XMLUtils.getElementsByTagNameNS1(assertionElement,
300                                        SAMLConstants.XMLSIG_NAMESPACE_URI,
301                                        SAMLConstants.XMLSIG_ELEMENT_NAME);
302        int signsSize = signs.size();
303        if (signsSize == 1) {
304            // delay the signature validation till user call isSignatureValid()
305            xmlString = XMLUtils.print(assertionElement);
306            signed = true;
307            validationDone = false;
308        } else if (signsSize != 0) {
309            if (SAMLUtilsCommon.debug.messageEnabled()) {
310                SAMLUtilsCommon.debug.message("Assertion(Element): " +
311                     "included more than one Signature element.");
312            }
313            throw new SAMLRequesterException(
314                SAMLUtilsCommon.bundle.getString("moreElement"));
315        }
316
317        read = elt.getAttribute("MajorVersion");
318        if ((read == null) || (read.length() == 0)) {
319            if (SAMLUtilsCommon.debug.messageEnabled())  {
320                SAMLUtilsCommon.debug.message("Assertion: " +   
321                                              "MajorVersion missing");
322            }
323            throw new SAMLRequesterException(
324                    SAMLUtilsCommon.bundle.getString("missingAttribute")+":"+
325                        "MajorVersion");
326        }
327        else  {
328            int ver = 0;
329            try {
330                ver = Integer.parseInt(read);
331            } catch ( NumberFormatException ne ) {
332                SAMLUtilsCommon.debug.error(
333                        "Assertion: invalid integer in MajorVersion", ne);
334                throw new SAMLRequesterException(
335                        SAMLUtilsCommon.bundle.getString("invalidNumber")+":"+
336                        "MajorVersion");
337            }
338            if (ver != sc.ASSERTION_MAJOR_VERSION) {
339                if (ver < sc.ASSERTION_MAJOR_VERSION) {
340                    if (SAMLUtilsCommon.debug.messageEnabled())  {
341                        SAMLUtilsCommon.debug.message(
342                            "Assertion: MajorVersion too low");
343                    }
344                    throw new SAMLVersionMismatchException(
345                        SAMLUtilsCommon.bundle.getString(
346                        "assertionVersionTooLow")
347                        + ":"+"MajorVersion");
348                } else if (ver > sc.ASSERTION_MAJOR_VERSION) {
349                    if (SAMLUtilsCommon.debug.messageEnabled())  {
350                        SAMLUtilsCommon.debug.message(
351                            "Assertion: MajorVersion too high");
352                    }
353                    throw new SAMLVersionMismatchException(
354                        SAMLUtilsCommon.bundle.getString(
355                            "assertionVersionTooHigh")
356                            +":"+"MajorVersion");
357                } else {
358                    _minorVersion=Integer.parseInt(read);
359                }
360            }
361        }
362        read = elt.getAttribute("MinorVersion");
363        if ((read == null) || (read.length() == 0)) {
364            if (SAMLUtilsCommon.debug.messageEnabled()) 
365                SAMLUtilsCommon.debug.message(
366                    "Assertion: MinorVersion missing");
367            throw new SAMLRequesterException(
368                  SAMLUtilsCommon.bundle.getString("missingAttribute")
369                  +":"+"MinorVersion");
370        }
371        else  {
372            int ver = 0;
373            try {
374                ver = Integer.parseInt(read);
375            } catch ( NumberFormatException ne ) {
376                SAMLUtilsCommon.debug.error(
377                        "Assertion: invalid integer in MinorVersion", ne);
378                throw new SAMLRequesterException(
379                        SAMLUtilsCommon.bundle.getString("invalidNumber")
380                                    +":"+"MinorVersion");
381            }
382
383            if (ver < sc.ASSERTION_MINOR_VERSION_ZERO) {
384                if (SAMLUtilsCommon.debug.messageEnabled())  {
385                    SAMLUtilsCommon.debug.message(
386                        "Assertion: MinorVersion too low");
387                }
388                throw new SAMLVersionMismatchException(
389                        SAMLUtilsCommon.bundle.getString(
390                        "assertionVersionTooLow"));
391            } else if (ver > sc.ASSERTION_MINOR_VERSION_ONE) {
392                if (SAMLUtilsCommon.debug.messageEnabled())  {
393                    SAMLUtilsCommon.debug.message(
394                    "Assertion: MinorVersion too high");
395                }
396                throw new SAMLVersionMismatchException(
397                     SAMLUtilsCommon.bundle.getString("assertionVersionTooHigh")
398                     +":"+"MinorVersion");
399            } else {
400                _minorVersion=ver;
401            }
402        }
403        read = elt.getAttribute("AssertionID");
404        if ((read == null) || (read.length() == 0)) {
405            if (SAMLUtilsCommon.debug.messageEnabled()) 
406                SAMLUtilsCommon.debug.message("Assertion: AssertionID missing");
407            throw new SAMLRequesterException(
408                SAMLUtilsCommon.bundle.getString("missingAttribute")
409                +":"+"AssertionID");
410        }
411        else  {
412            _assertionID = createAssertionIDReference(read);
413        }
414
415        read = elt.getAttribute("IssueInstant");
416        if ((read == null) || (read.length() == 0)) {
417            if (SAMLUtilsCommon.debug.messageEnabled())  {
418                SAMLUtilsCommon.debug.message(
419                "Assertion: IssueInstant missing");
420            }
421            throw new SAMLRequesterException(
422                SAMLUtilsCommon.bundle.getString("missingAttribute")
423                +":"+"IssueInstant");
424        }
425        else  {
426            try {
427                _issueInstant = DateUtils.stringToDate(read);
428            } catch (ParseException pe) {
429                if (SAMLUtilsCommon.debug.messageEnabled()) 
430                    SAMLUtilsCommon.debug.message(
431                    "Assertion: could not parse IssueInstant", pe);
432               throw new SAMLRequesterException(
433                    SAMLUtilsCommon.bundle.getString(
434                    "wrongInput") + " " + pe.getMessage());
435            }
436        }
437        boolean statementFound = false;
438        NodeList nl = assertionElement.getChildNodes();
439        int length = nl.getLength();
440        for (int n=0; n<length; n++) {
441            Node child = (Node)nl.item(n);
442            if (child.getNodeType() != Node.ELEMENT_NODE) continue;
443            String childName = child.getLocalName();
444            if (childName.equals("Conditions"))
445                _conditions = createConditions((Element)child);
446            else if (childName.equals("Advice"))
447                _advice = createAdvice((Element)child);
448            else if (childName.equals("AuthenticationStatement")) {
449                _statements.add(createAuthenticationStatement((Element)child));
450                statementFound=true;
451            }
452            else if (childName.equals("AuthorizationDecisionStatement")) {
453                _statements.add(createAuthorizationDecisionStatement(
454                        (Element)child));
455                statementFound=true;
456            }
457            else if (childName.equals("AttributeStatement")) {
458                _statements.add(createAttributeStatement((Element)child));
459                statementFound=true;
460            }
461            else if (childName.equals("Signature")) {
462                signature = (Element) child;
463            }
464            else {
465                if (SAMLUtilsCommon.debug.messageEnabled()) {
466                    SAMLUtilsCommon.debug.message(
467                        "Assertion: invalid element in Assertion");
468                }
469                throw new SAMLRequesterException("invalidElement");
470            }
471        }
472        if (!statementFound) {
473            if (SAMLUtilsCommon.debug.messageEnabled()) {
474                SAMLUtilsCommon.debug.message(
475                    "Assertion: mandatory statement missing");
476            }
477            throw new SAMLRequesterException("missingStatement");
478        }
479            
480    } 
481   
482    /**
483     * Gets the validity of the assertion evaluating its conditions if
484     * specified.
485     *
486     * @return false if conditions is invalid based on it lying between
487     *         <code>NotBefore</code> (current time inclusive) and
488     *         <code>NotOnOrAfter</code> (current time exclusive) values 
489     *         and true otherwise or if no conditions specified.
490     */
491    public boolean isTimeValid() {
492        if (_conditions == null)  {
493            return true;
494        }
495        else  {
496            
497            return _conditions.checkDateValidity(System.currentTimeMillis());
498        }
499    }
500 
501    /**
502     *Contructor
503     *This constructor is used to populate the data members:
504     * <code>assertionID</code>, the issuer, time when assertion issued and a
505     * set of <code>Statement</code>(s) in the assertion.
506     *
507     * @param assertionID <code>assertionID</code> attribute contained within
508     *        this <code>Assertion</code> if null, an <code>assertionID</code>
509     *        is generated internally.
510     * @param issuer The issuer of this assertion.
511     * @param issueInstant time instant of the issue. It has type
512     *        <code>dateTime</code> which is built in to the W3C XML Schema
513     *        Types specification.if null, current time is used.
514     * @param statements set of <code>Statement</code> objects within this 
515     *        <code>Assertion</code>. It could be of type
516     *        <code>AuthenticationStatement</code>, 
517     *        <code>AuthorizationDecisionStatement</code> and
518     *        <code>AttributeStatement</code>. Each Assertion can have multiple
519     *        type of statements in it.
520     * @exception SAMLException if there is an error in processing input.
521     */
522    public AssertionBase(String assertionID,java.lang.String issuer, 
523        Date issueInstant,  Set statements) throws SAMLException
524    {
525        if ((issuer == null) || (issuer.length() == 0)) {
526            if (SAMLUtilsCommon.debug.messageEnabled())  {
527                SAMLUtilsCommon.debug.message(
528                "Assertion:  null input specified");
529            }
530            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
531                "nullInput")) ;   
532        }
533        if (statements.size() == 0 ) {
534            if (SAMLUtilsCommon.debug.messageEnabled())  {
535                SAMLUtilsCommon.debug.message("Assertion:mandatory statement"
536                    + " missing");
537            }
538            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
539                "missingStatement")) ;   
540        } else {
541            _statements.addAll(statements);
542        }
543        _assertionID = createAssertionIDReference(assertionID);
544        if (issuer != null) {
545            _issuer = issuer;
546        }
547        if (issueInstant != null)  {
548            _issueInstant = issueInstant;
549        }
550        else {
551            _issueInstant = new Date();
552        }
553    }
554
555    /**
556     * This constructor is used to populate the data members: the
557     * <code>assertionID</code>, the issuer, time when assertion issued, the 
558     * conditions when creating a new assertion and a set of
559     * <code>Statement</code>(s) in the assertion.
560     *
561     * @param assertionID <code>AssertionID</code> contained within this 
562     *        <code>Assertion</code> if null its generated internally.
563     * @param issuer The issuer of this assertion.
564     * @param issueInstant time instant of the issue. It has type
565     *        <code>dateTime</code> which is built in to the W3C XML Schema
566     *        Types specification. if null, current time is used.
567     * @param conditions <code>Conditions</code> under which the this 
568     *        <code>Assertion</code> is valid.
569     * @param statements Set of <code>Statement</code> objects within this 
570     *        <code>Assertion</code>. It could be of type
571     *        <code>AuthenticationStatement</code>,
572     *        <code>AuthorizationDecisionStatement</code> and 
573     *        <code>AttributeStatement</code>. Each Assertion can have multiple
574     *        type of statements in it.
575     * @exception SAMLException if there is an error in processing input.
576     */
577    public AssertionBase(String assertionID,java.lang.String issuer, 
578        Date issueInstant,  Conditions conditions, Set statements) 
579        throws SAMLException
580    {
581        if ((issuer == null) || (issuer.length() == 0) || (conditions == null))
582        {
583            if (SAMLUtilsCommon.debug.messageEnabled()) {
584                SAMLUtilsCommon.debug.message(
585                "Assertion:  null input specified");
586            }
587            throw new SAMLRequesterException(
588                SAMLUtilsCommon.bundle.getString("nullInput")) ;   
589        }
590        if (statements.size() == 0 ) {
591            if (SAMLUtilsCommon.debug.messageEnabled())  {
592                SAMLUtilsCommon.debug.message("Assertion:mandatory statement"
593                    + " missing");
594            }
595            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
596                "missingStatement")) ;   
597        } else {
598            _statements.addAll(statements);
599        }
600        _assertionID = createAssertionIDReference(assertionID);
601        if (issuer != null)  {
602            _issuer = issuer;
603        }
604        if (issueInstant != null)  {
605            _issueInstant = issueInstant;
606        } else  {
607            _issueInstant = new Date();
608        }
609        if (conditions != null)  {
610            _conditions = conditions;
611        }
612    }
613   
614    /**
615     * This constructor is used to populate the data members: the 
616     * <code>ssertionID</code>, the issuer, time when assertion issued,
617     * the conditions when creating a new assertion , <code>Advice</code>
618     * applicable to this <code>Assertion</code> and a set of
619     * <code>Statement</code>(s) in the assertion.
620     *
621     * @param assertionID <code>AssertionID</code> object contained within this
622     *        <code>Assertion</code> if null its generated internally.
623     * @param issuer The issuer of this assertion.
624     * @param issueInstant Time instant of the issue. It has type
625     *        <code>dateTime</code> which is built in to the W3C XML Schema
626     *        Types specification. if null, current time is used.
627     * @param conditions <code>Conditions</code> under which the this 
628     *        <code>Assertion</code> is valid.
629     * @param advice <code>Advice</code> applicable for this
630     *        <code>Assertion</code>.
631     * @param statements Set of <code>Statement</code> objects within this 
632     *         <code>Assertion</code>. It could be of type
633     *         <code>AuthenticationStatement</code>,
634     *         <code>AuthorizationDecisionStatement</code> and 
635     *         <code>AttributeStatement</code>. Each Assertion can have
636     *         multiple type of statements in it.
637     * @exception SAMLException if there is an error in processing input.
638     */
639    public AssertionBase(String assertionID,java.lang.String issuer, 
640        Date issueInstant,  Conditions conditions, AdviceBase advice, 
641        Set statements) throws SAMLException
642    {
643        if ((issuer == null) || (issuer.length() == 0) || (conditions == null))
644        {
645            if (SAMLUtilsCommon.debug.messageEnabled()) {
646                SAMLUtilsCommon.debug.message(
647                "Assertion:  null input specified");
648            }
649            throw new SAMLRequesterException(
650                SAMLUtilsCommon.bundle.getString("nullInput")) ;   
651        }
652        if (statements.size() == 0 ) {
653            if (SAMLUtilsCommon.debug.messageEnabled())  {
654                SAMLUtilsCommon.debug.message("Assertion:mandatory statement"
655                    + " missing");
656            }
657            throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString(
658                "missingStatement")) ;   
659        } else {
660            _statements.addAll(statements);
661        }
662        _assertionID = createAssertionIDReference(assertionID);
663        if (issuer != null)  {
664            _issuer = issuer;
665        }
666        if (issueInstant != null)  {
667            _issueInstant = issueInstant;
668        } else  {
669            _issueInstant = new Date();
670        }
671        if (conditions != null)  {
672            _conditions = conditions;
673        }
674        if (advice != null)  {
675            _advice = advice;
676        }
677    }
678
679    /**
680     *Adds a statement to this <code>Assertion</code>
681     *@param statement <code>Statement</code> to be added
682     *@return boolean indicating success or failure of operation.
683     */
684    public boolean addStatement(Statement statement) {
685        if (signed) {
686            return false;
687        }
688        if (statement == null) {
689           return false;
690        }
691        _statements.add(statement);
692        return true;
693    }
694   
695    /**
696     *Set the time when the assertion was issued
697     *@param issueInstant : <code>java.util.Date</code> representing the time
698     *                      of the assertion 
699     *@return A boolean indicating the success of the operation.
700     */
701    protected boolean setIssueInstant(java.util.Date issueInstant) {
702        if (signed) {
703            return false;
704        }
705        if (issueInstant == null) {
706            return false;
707        }
708        _issueInstant = issueInstant;
709        return true; 
710    }
711      
712    /**
713     *Set the <code>AssertionID</code> for this assertion
714     *@param assertionID : a String representing id of this 
715     *                     assertion.
716     *@return A boolean indicating the success of the operation.
717     */
718    protected boolean setAssertionID(String assertionID) {
719        if (signed) {
720            return false;
721        }
722        if(assertionID == null) {
723            return false;
724        }
725        try {
726            _assertionID = createAssertionIDReference(assertionID);
727        } catch (Exception e ) {
728            if (SAMLUtilsCommon.debug.messageEnabled()) {
729                SAMLUtilsCommon.debug.message("Assertion: Exception in setting"
730                    + " assertion id: "+e.getMessage());
731            }
732            return false;
733        }
734        return true; 
735    }
736      
737    /**
738     *Sets the issuer for an assertion
739     *@param issuer : a string representing the issuer of the assertion 
740     *@return A boolean indicating the success of the operation.
741     */
742    protected boolean setIssuer(java.lang.String issuer) {
743        if (signed) {
744            return false;
745        }
746        if ((issuer == null) || (issuer.length() == 0)) {
747            return false;
748        }
749        _issuer = issuer;
750        return true; 
751    }
752      
753    /**
754     *Sets the advice for an assertion 
755     *@param advice : a linked list representing the advice information 
756     *@return A boolean indicating the success of the operation.
757     */
758    public boolean setAdvice(AdviceBase advice) {
759        if (signed) {
760            return false;
761        }
762        if (advice == null)  {
763            return false;
764        }
765        _advice = advice;
766        return true; 
767    }
768       
769    /**
770     *Sets the Conditions information for an assertion 
771     *@param conditions a linked list representing the conditions information 
772     *@return A boolean indicating the success of the operation.
773     */                  
774    public boolean setConditions(Conditions  conditions) {
775        if (signed) {
776            return false;
777        }
778        if ( conditions == null)  {
779            return false;
780        }
781        _conditions = conditions;
782        return true; 
783    }                                                                    
784   
785    /**
786     * Returns the minor version number of an assertion.
787     *
788     * @return The minor version number of an assertion.
789     */
790    public int getMinorVersion() {
791        return _minorVersion; 
792    }
793    
794    /**
795     * Sets the minor version number of an assertion.
796     *
797     * @param minorVersion minor version.
798     */
799    public void setMinorVersion(int minorVersion) {
800        this._minorVersion = minorVersion; 
801    }
802    
803    /**
804     * Returns the major version number of an assertion.
805     *
806     * @return The major version number of an assertion. 
807     */
808    public int getMajorVersion() {
809        return _majorVersion; 
810    }
811   
812    /**
813     * Sets the major version number of an assertion.
814     *
815     * @param majorVersion major version.
816     */
817    public void setMajorVersion(int majorVersion) {
818        this._majorVersion = majorVersion; 
819    }
820    
821    /**                 
822     * Returns the time when the assertion was issued.
823     *
824     * @return The time in <code>java.util.Date</code> format.
825     */
826    public Date getIssueInstant() {
827        return _issueInstant; 
828    }
829   
830    /**
831     * Returns the issuer of an assertion.
832     *
833     * @return The issuer of an assertion. 
834    */
835    public java.lang.String getIssuer() {
836        return _issuer; 
837    }
838
839    /**
840     * Returns the assertion ID.
841     *
842     * @return Assertion ID of the assertion.
843     */
844    public String getAssertionID() {
845        return _assertionID.getAssertionIDReference();
846    }
847      
848    /**
849     * Returns the conditions of an assertion.
850     *
851     * @return <code>Conditions</code> object containing conditions for an
852     *          assertion being valid.
853    */
854    public Conditions getConditions() {
855        return _conditions; 
856    }
857
858   
859    /**
860     * Returns a set of <code>Statement</code> contained within this assertion.
861     *
862     * @return a set of <code>Statement</code> contained within this assertion. 
863     */
864    public Set getStatement() {
865        return _statements;
866    }
867
868    /**
869     * Returns a String representation of the element.
870     *
871     * @return A string containing the valid XML for this element
872     *         By default name space name is prepended to the element name
873     *         example <code>&lt;saml:Assertion&gt;</code>.
874     */
875    public java.lang.String toString() {
876        // call toString() with includeNS true by default and declareNS false
877        String xml = this.toString(true, false);
878        return xml;
879    }
880
881    /**
882     * Returns a String representation of the <code>&lt;Assertion&gt;</code>
883     * element.
884     *
885     * @param includeNS if true prepends all elements by their Namespace 
886     *        name example <code>&lt;saml:Assertion&gt;</code>
887     * @param declareNS if true includes the namespace within the generated
888     *        XML.
889     * @return The valid XML for this element
890     */
891    public java.lang.String toString(boolean includeNS, boolean declareNS) {
892        if (signed && (xmlString != null)) {
893            return xmlString;
894        }
895
896        StringBuffer xml = new StringBuffer(3000);
897        String NS="";
898        String appendNS="";
899        if (declareNS) {
900            NS=SAMLConstants.assertionDeclareStr;
901        }
902        if (includeNS) {
903            appendNS="saml:";
904        }
905        String dateStr = null;
906        if (_issueInstant != null)  {
907            dateStr = DateUtils.toUTCDateFormat(_issueInstant);         
908        }
909
910        xml.append("<").append(appendNS).append("Assertion").append(" ").
911            append(NS).append(" ").append("MajorVersion").append("=\"").
912            append(_majorVersion).append("\"").append(" ").
913            append("MinorVersion").append("=\"").append(_minorVersion).
914            append("\"").append(" ").append("AssertionID=\"").
915            append(_assertionID.getAssertionIDReference()).append("\"").
916            append(" ").append("Issuer").append("=\"").append(_issuer).
917            append("\"").append(" ").append("IssueInstant").append("=\"").
918            append(dateStr).append("\"").
919            append(" ").append(">").append(sc.NL);
920        if (_conditions != null) {
921            xml.append(_conditions.toString(includeNS, false));
922        }
923        if (_advice != null) {
924            xml.append(_advice.toString(includeNS, false));
925        }
926        Iterator i = getStatement().iterator();
927        while (i.hasNext()) {
928           Statement st = (Statement)i.next();
929           xml.append(st.toString(includeNS, false));
930        }
931        if (signed && (signatureString != null)) {
932            xml.append(signatureString);
933        }
934        String o = SAMLUtilsCommon.makeEndElementTagXML("Assertion", includeNS);
935        xml.append(o);
936        return xml.toString();
937    }
938
939}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.