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: AbstractResponse.java,v 1.2 2008/06/25 05:47:36 qcheng Exp $
026 *
027 */
028
029 
030package com.sun.identity.saml.protocol;
031
032import com.sun.identity.saml.common.SAMLConstants;
033import com.sun.identity.saml.common.SAMLException;
034
035import java.util.Collections;
036import java.util.Date;
037import java.util.List;
038
039import org.w3c.dom.Element;
040
041/**
042 * This <code>AbstractResponse</code> class is an abstract base class for all
043 * SAML Response in <code>samlp</code> namespace. It corresponds to
044 * <code>ResponseAbstractType</code> in SAML protocol schema.
045 *
046 * @supported.all.api
047 */
048public abstract class AbstractResponse {
049
050    protected String    responseID      = null;
051    protected String    inResponseTo    = null;
052    protected int       majorVersion    = SAMLConstants.PROTOCOL_MAJOR_VERSION;
053    protected int       minorVersion    = SAMLConstants.PROTOCOL_MINOR_VERSION;
054    protected Element   signature       = null;
055    protected Date      issueInstant    = null;
056    protected String    recipient       = null;
057    protected boolean   signed          = false;
058    protected boolean   valid           = true;
059    protected boolean validationDone    = false;
060
061    /**
062     * Default constructor.
063     */
064    protected AbstractResponse() {
065    }
066
067    /**
068     * Return whether the object is signed or not.
069     * @return true if the object is signed; false otherwise.
070     */
071    public boolean isSigned() {
072        return signed;
073    }
074
075    /**
076     * Returns whether the signature on the object is valid or not.
077     *
078     * @return boolean true if the signature is valid; false otherwise.
079     */
080    public boolean isSignatureValid() {
081        return valid;
082    }
083
084    /**
085     * An abstract method that signs the object.
086     *
087     * @exception SAMLException if could not sign the object.
088     */
089    public abstract void signXML() throws SAMLException;
090
091    /**
092     * Gets the <code>ResponseID</code> of the Response.
093     *
094     * @return the <code>ResponseID</code> of the Response.
095     */
096    public String getResponseID() {
097        return responseID;
098    }
099
100    /**
101     * Set the <code>ResponseID</code> of the Response.
102     *
103     * @param responseID A String that is the <code>ResponseID</code> attribute
104     *        of the Response.
105     * @return true if the operation is successful.
106     */
107    public boolean setResponseID(String responseID) {
108        if (signed) {
109            return false;
110        }
111        if ((responseID == null) || (responseID.length() == 0)) {
112            return false;
113        } 
114        this.responseID = responseID;
115        return true;
116    }
117
118    /**
119     * Gets the <code>InResponseTo</code> of the Response.
120     * @return the <code>InResponseTo</code> of the Response.
121     */
122    public String getInResponseTo() {
123        return inResponseTo;
124    }
125
126    /**
127     * Set the <code>InResponseTo</code> of the Response.
128     * @param inResponseTo The <code>InResponseTo</code> attribute of the
129     *        Response.
130     * @return true if the operation is successful.
131     */
132    public boolean setInResponseTo(String inResponseTo) {
133        if (signed) {
134            return false;
135        }
136        if ((inResponseTo == null) || (inResponseTo.length() == 0)) {
137            return false;
138        }
139        this.inResponseTo = inResponseTo;
140        return true;
141    }
142
143    /**
144     * Gets the <code>MajorVersion</code> of the Response.
145     * @return The <code>MajorVersion</code> of the Response.
146     */
147    public int getMajorVersion() {
148        return majorVersion;
149    }
150
151    /**
152     * Gets the <code>MinorVersion</code> of the Response.
153     * @return The <code>MinorVersion</code> of the SAML response.
154     */
155    public int getMinorVersion() {
156        return minorVersion;
157    }
158    
159    /**
160     * Sets the <code>MajorVersion</code> of the Response.
161     * @param majorVersion the intended major version of SAML response.
162     */
163    public void setMajorVersion(int majorVersion) {
164        this.majorVersion = majorVersion;
165    }
166
167    /**
168     * Sets the <code>MinorVersion</code> of the Response.
169     * @param minorVersion the intended minor version of SAML response.
170     */
171    public void setMinorVersion(int minorVersion) {
172        this.minorVersion = minorVersion;
173    }
174
175    /**
176     * Gets the signature of the Response.
177     * @return The signature element of the Response.
178     *          null is returned if the Response has no ds:Signature.
179     */
180    public Element getSignature() {
181        return signature;
182    }
183
184    /**
185     * Set the signature for the Response.
186     *
187     * @param elem <code>ds:Signature</code> element
188     * @return true if the operation succeeds.
189     */
190    public boolean setSignature(Element elem) {
191        if (signed) {
192            return false;
193        }
194        if (elem == null) {
195            return false;
196        } else {
197            signature = elem;
198            signed = true;
199            return true;
200        }
201    }
202
203    /**
204     * Returns the <code>IssueInstant</code> of the Response.
205     *
206     * @return the <code>IssueInstant</code> of the Response.
207     */
208    public Date getIssueInstant() {
209        return issueInstant;
210    }
211
212    /**
213     * Set the <code>IssueInstant</code> of the Response.
214     *
215     * @param issueInstant a Date object representing the time when the Response
216     *          is issued.
217     * @return true if the operation succeeds.
218     */
219    public boolean setIssueInstant(Date issueInstant) {
220        if (signed) {
221            return false;
222        }
223        if (issueInstant == null) {
224            return false;
225        }
226        this.issueInstant = issueInstant;
227        return true;
228    }
229
230    /**
231     * Gets the recipient of the Response.
232     *
233     * @return The Recipient.
234     */
235    public String getRecipient() {
236        return recipient;
237    }
238
239    /**
240     * Set the Recipient attribute of the Response.
241     *
242     * @param recipient A String representing the Recipient attribute of the
243     *        Response.
244     * @return true if the operation is successful;
245     */
246    public boolean setRecipient(String recipient) {
247        if (signed) {
248            return false;
249        }
250        if ((recipient == null) || (recipient.length() == 0)) {
251             return false;
252        }
253        this.recipient = recipient;
254        return true;
255    }
256}