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: Status.java,v 1.2 2008/06/25 05:47:37 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.saml.protocol;
031
032import com.sun.identity.shared.xml.XMLUtils; 
033
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.SAMLUtils;
038
039import java.util.ArrayList;
040import java.util.Collections;
041import java.util.Iterator;
042import java.util.List;
043
044import org.w3c.dom.Element;
045import org.w3c.dom.Node;
046import org.w3c.dom.NodeList;
047
048/**
049 * This class represents the Status element. It corresponds to 
050 * <code>&lt;samlp:StatusType&gt;</code> in SAML protocol schema.
051 *
052 * @supported.all.api
053 */
054public class Status {
055
056    private StatusCode statusCode       = null;
057    private String statusMessage        = null;
058    private Element statusDetail        = null;
059         
060    /**
061     * This is the default constructor of Status.
062     */
063    Status() {
064    }
065  
066    /**
067     * This constructor is used to construct a Status from a DOM element.      
068     * @param status An DOM Element that's rooted by &lt;Status&gt;.
069     * @exception SAMLException when an error occurs.
070     */
071    public Status(Element status) throws SAMLException {
072        String tag = null;
073        if (status == null) {
074            SAMLUtils.debug.message("Status: null input.");
075            throw new SAMLRequesterException(
076                        SAMLUtils.bundle.getString("nullInput"));
077        }
078        if (((tag = status.getLocalName()) == null) ||
079            (!tag.equals("Status"))) {
080            SAMLUtils.debug.message("Status: wrong input.");
081            throw new SAMLRequesterException(
082                        SAMLUtils.bundle.getString("wrongInput"));
083        }
084
085        NodeList nl = status.getChildNodes();
086        Node child;
087        String childName;
088        String message;
089        int length = nl.getLength();
090        for (int k = 0; k < length; k++) {
091            child = nl.item(k);
092            if ((childName = child.getLocalName()) != null) {
093                if (childName.equals("StatusCode")) {
094                    if (statusCode != null) {
095                        if (SAMLUtils.debug.messageEnabled()) {
096                            SAMLUtils.debug.message("Status: contained more"
097                                + " than one <StatusCode>");
098                        }
099                        throw new SAMLRequesterException(
100                            SAMLUtils.bundle.getString("moreElement"));
101                    }
102                    statusCode = new StatusCode((Element) child);
103                } else if (childName.equals("StatusMessage")) {
104                    message = XMLUtils.getElementValue((Element) child);
105                    if ((message == null) ||
106                        (message.length() == 0)) {
107                        SAMLUtils.debug.message("Status: Empty StatusMessage.");
108                        throw new SAMLRequesterException(
109                            SAMLUtils.bundle.getString("emptyElement"));
110                    }
111                    if (statusMessage != null) {
112                        if (SAMLUtils.debug.messageEnabled()) {
113                            SAMLUtils.debug.message("Status: included more "
114                                + "than one <StatusMessage>");
115                        }
116                        throw new SAMLRequesterException(
117                            SAMLUtils.bundle.getString("moreElement"));
118                    }
119                    statusMessage = message;
120                } else if (childName.equals("StatusDetail")) {
121                    if (statusDetail != null) {
122                        if (SAMLUtils.debug.messageEnabled()) {
123                            SAMLUtils.debug.message("Status: included more "
124                                + "than one <StatusDetail>");
125                        }
126                        throw new SAMLRequesterException(
127                            SAMLUtils.bundle.getString("moreElement"));
128                    }
129                    // set statusDetail
130                    statusDetail = (Element) child;
131                } else {
132                    if (SAMLUtils.debug.messageEnabled()) {
133                        SAMLUtils.debug.message("Status: contained wrong"
134                            + " element:" + childName);
135                    }
136                    throw new SAMLRequesterException(
137                        SAMLUtils.bundle.getString("wrongInput"));
138                }
139            } // if childName != null
140        } // end for loop
141
142        if (statusCode == null) {
143            SAMLUtils.debug.message("Status: missing <StatusCode>.");
144            throw new SAMLRequesterException(
145                SAMLUtils.bundle.getString("missingElement"));
146        }
147    }
148
149    /**
150     * Constructor.
151     * @param code <code>StatusCode</code>.
152     * @param message A String that is the <code>StatusMessage</code> of the
153     *        response. It could be null when there is no
154     *        <code>StatusMessage</code>.
155     * @param detail A DOM tree element that is the <code>StatusDetail</code>
156     *        of the response. It could be null when there is no 
157     *        <code>StatusDetail</code>.
158     * @throws SAMLException
159     */
160    public Status(StatusCode code, String message, Element detail) 
161                                                throws SAMLException {
162        if (code == null) {
163            SAMLUtils.debug.message("Status: null input.");
164            throw new SAMLRequesterException(
165                        SAMLUtils.bundle.getString("nullInput"));
166        }
167        statusCode = code;
168
169        if ((message != null) && (message.length() == 0)) {
170            SAMLUtils.debug.message("Status: Empty StatusMessage.");
171            throw new SAMLRequesterException(
172                            SAMLUtils.bundle.getString("emptyElement"));
173        }
174        statusMessage = message;
175
176        statusDetail = detail;
177    }
178
179    /**
180     * Constructs a Status object from a <code>StatusCode</code>.
181     * @param code <code>StatusCode</code>.
182     * @throws SAMLException
183     */
184    public Status(StatusCode code) throws SAMLException {
185        if (code == null) {
186            SAMLUtils.debug.message("Status: null input.");
187            throw new SAMLRequesterException(
188                        SAMLUtils.bundle.getString("nullInput"));
189        }
190        statusCode = code;
191    }
192
193    /**
194     * Gets the <code>StatusCode</code> of the Response.
195     * @return <code>StatusCode</code> of the response. 
196     */
197    public StatusCode getStatusCode() {
198        return statusCode;
199    }
200
201    /**
202     * Returns the <code>StatusMessage</code> of the Response.
203     * @return A String that represents the <code>StatusMessage</code> of the
204     *         response. null is returned when there is no
205     *         <code>StatusMessage</code> in the response.
206     */
207    public String getStatusMessage() {
208        return statusMessage;
209    }
210
211    /**
212     * Gets the <code>StatusDetail</code> of the Response.
213     * @return A DOM tree element that represents the <code>StatusDetail</code>
214     *         of the response. Null is returned if no <code>StatusDetail</code>
215     *         in the response.
216     */
217    public Element getStatusDetail() {
218        return statusDetail;
219    }
220
221    /**
222     * This method translates the <code>AssertionArtifact</code> to an XML
223     * document String based on the SAML schema.
224     *
225     * @return An XML String representing the <code>AssertionArtifact</code>.
226     */
227    public String toString() {
228        return toString(true, false);
229    }
230
231    /**
232     * Creates a String representation of the <code>&lt;samlp:Status&gt;</code>
233     * element.
234     * @param includeNS Determines whether or not the namespace qualifier
235     *        is prepended to the Element when converted
236     * @param declareNS Determines whether or not the namespace is declared
237     *        within the Element.
238     * @return A string containing the valid XML for this element
239     */   
240    public String toString(boolean includeNS, boolean declareNS) {
241        StringBuffer xml = new StringBuffer(200);
242        String prefix = "";
243        String uri = "";
244        if (includeNS) {
245            prefix = SAMLConstants.PROTOCOL_PREFIX;
246        }
247        if (declareNS) {
248            uri = SAMLConstants.PROTOCOL_NAMESPACE_STRING;
249        }
250        xml.append("<").append(prefix).append("Status").append(uri).
251            append(">\n").append(statusCode.toString(includeNS, false));
252        if (statusMessage != null) {
253            xml.append("<").append(prefix).append("StatusMessage>").
254                    append(statusMessage).append("</").append(prefix).
255                    append("StatusMessage>\n");
256        }
257        if (statusDetail != null) {
258            xml.append("<").append(prefix).append("StatusDetail>\n");
259            NodeList nl = statusDetail.getChildNodes();
260            int len = nl.getLength();
261            for (int i = 0; i < len; i++) {
262                xml.append(XMLUtils.print(nl.item(i)));
263            }
264            xml.append("</").append(prefix).append("StatusDetail>\n");
265        }
266        xml.append("</").append(prefix).append("Status>\n");
267        return xml.toString();
268    }
269}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.