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: AssertionIDReference.java,v 1.2 2008/06/25 05:47:31 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.saml.assertion;
031
032import org.w3c.dom.*; 
033import com.sun.identity.saml.common.*; 
034import com.sun.identity.shared.xml.XMLUtils;
035
036/**
037 * <code>AssertionIDReference</code> element makes reference to a SAML
038 * assertion.
039 *
040 * @supported.all.api
041 */
042public class AssertionIDReference {     
043    protected String assertionID = null;
044    
045    /**
046     * Constructs an <code>AssertionIDReference</code> element from an existing 
047     * XML block.
048     *
049     * @param element representing a DOM tree element.
050     * @exception SAMLException if there is an error in the sender or in the
051     *            element definition.
052     */
053    public AssertionIDReference(Element element) throws SAMLException{
054        // make sure that the input xml block is not null
055        if (element == null) {
056            SAMLUtilsCommon.debug.message(
057            "AssertionIDReference: Input is null.");
058            throw new SAMLRequesterException(
059                      SAMLUtilsCommon.bundle.getString("nullInput"));
060        }
061        // Make sure this is as AssertionIDReference.
062        String tag = null;
063        tag = element.getLocalName(); 
064        if ((tag == null) || (!tag.equals("AssertionIDReference"))) {
065            SAMLUtilsCommon.debug.message("AssertionIDReference: wrong input");
066            throw new SAMLRequesterException(
067                      SAMLUtilsCommon.bundle.getString("wrongInput"));
068        }
069        assertionID = XMLUtils.getElementValue(element); 
070        // check if the AssertionIDReference is null.
071        if (assertionID == null || assertionID.length() == 0) {
072            if (SAMLUtilsCommon.debug.messageEnabled()) {
073                SAMLUtilsCommon.debug.message("AssertionIDReference is null.");
074            }
075            throw new SAMLRequesterException(
076                      SAMLUtilsCommon.bundle.getString("missingElementValue"));
077        }
078    }
079    
080    /**
081     *Default constructor 
082     */
083    public AssertionIDReference() {
084        assertionID = SAMLUtils.generateAssertionID(); 
085    }
086    
087    /**
088     *Constructs of <code>AssertionIDReference</code>.
089     *@param id A String representing an existing assertion id.  
090     */
091    public AssertionIDReference(String id) {
092        if (id == null || id.length() == 0) {
093            assertionID = SAMLUtils.generateAssertionID(); 
094        } else 
095            assertionID = id; 
096    }
097    
098    /**
099     *Returns a String representing the Assertion id.  
100     *@return A string representation of the assertion id.    
101     */           
102    public String getAssertionIDReference() {
103        return assertionID; 
104    }
105    
106    /**
107     * Translates the <code>AssertionID</code> to an XML String,
108     * @return A String representing the <code>AssertionID</code>
109     */
110    public String toString() {
111        return (toString(true, false)); 
112    }
113   
114    /**
115     * Returns a String representation of the <code>AssertionIDReference</code> 
116     * element.
117     *
118     * @param includeNS Determines whether or not the namespace qualifier
119     *        is prepended to the Element when converted
120     * @param declareNS Determines whether or not the namespace is declared
121     *        within the Element.
122     * @return A string representation of the
123     *         <code>&lt;saml:AssertionIDReference&gt;</code> element.
124     */
125    public String toString(boolean includeNS, boolean declareNS) {
126        StringBuffer result = new StringBuffer(100);
127        String prefix = "";
128        String uri = "";
129    
130        if (includeNS) {
131            prefix = SAMLConstants.ASSERTION_PREFIX;
132        }
133        if (declareNS) {
134            uri = SAMLConstants.assertionDeclareStr;
135        }
136        result.append("<").append(prefix).append("AssertionIDReference").
137               append(uri).append(">").append(assertionID).append("</").
138               append(prefix).append("AssertionIDReference>\n");
139        return (result.toString());
140    }
141}
142