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: QueryResponse.java,v 1.2 2008/06/25 05:47:10 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.disco;
031
032import java.util.Iterator;
033import java.util.List;
034import java.util.ArrayList;
035
036import org.w3c.dom.*;
037
038import com.sun.identity.liberty.ws.common.Status;
039import com.sun.identity.liberty.ws.disco.common.DiscoConstants;
040import com.sun.identity.liberty.ws.disco.common.DiscoUtils;
041import com.sun.identity.liberty.ws.security.SecurityAssertion;
042import com.sun.identity.saml.common.SAMLException;
043
044/**
045 * The class <code>QueryResponse</code> represents a response for a discovery
046 * query request.
047 * The following schema fragment specifies the expected content within the
048 * <code>QueryResponse</code> object.
049 * <pre>
050 * &lt;complexType name="QueryResponseType">
051 *   &lt;complexContent>
052 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
053 *       &lt;sequence>
054 *         &lt;element ref="{urn:liberty:disco:2003-08}Status"/>
055 *         &lt;element ref="{urn:liberty:disco:2003-08}ResourceOffering" maxOccurs="unbounded" minOccurs="0"/>
056 *         &lt;element name="Credentials" minOccurs="0">
057 *           &lt;complexType>
058 *             &lt;complexContent>
059 *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
060 *                 &lt;sequence>
061 *                   &lt;any/>
062 *                 &lt;/sequence>
063 *               &lt;/restriction>
064 *             &lt;/complexContent>
065 *           &lt;/complexType>
066 *         &lt;/element>
067 *       &lt;/sequence>
068 *       &lt;attribute name="id" type="{http://www.w3.org/2001/XMLSchema}ID" />
069 *     &lt;/restriction>
070 *   &lt;/complexContent>
071 * &lt;/complexType>
072 * </pre>
073 * 
074 * @supported.all.api
075 */
076public class QueryResponse {
077
078    private String id = null;
079    private Status status = null;
080    private List offerings = null;
081    private List creds = null;
082
083    /**
084     * Constructor.
085     * @param root <code>QueryResponse</code> DOM element.
086     * @exception DiscoveryException if error occurs.
087     */
088    public QueryResponse(Element root) throws DiscoveryException {
089        if (root == null) {
090            DiscoUtils.debug.message("QueryResponse(Element): null input.");
091            throw new DiscoveryException(
092                DiscoUtils.bundle.getString("nullInput"));
093        }
094        String nodeName;
095        String nameSpaceURI;
096        if (((nodeName = root.getLocalName()) == null) ||
097            (!nodeName.equals("QueryResponse")) ||
098            ((nameSpaceURI = root.getNamespaceURI()) == null) ||
099            (!nameSpaceURI.equals(DiscoConstants.DISCO_NS)))
100        {
101            DiscoUtils.debug.message("QueryResponse(Element): wrong input");
102            throw new DiscoveryException(
103                DiscoUtils.bundle.getString("wrongInput"));
104        }
105
106        id = root.getAttribute("id");
107
108        NodeList contentnl = root.getChildNodes();
109        Node child;
110        boolean foundCreds = false;
111        for (int i = 0, length = contentnl.getLength(); i < length; i++) {
112            child = contentnl.item(i);
113            if ((nodeName = child.getLocalName()) != null) {
114                nameSpaceURI = ((Element) child).getNamespaceURI();
115                if ((nameSpaceURI == null) ||
116                    (!nameSpaceURI.equals(DiscoConstants.DISCO_NS)))
117                {
118                    if (DiscoUtils.debug.messageEnabled()) {
119                        DiscoUtils.debug.message("QueryResponse(Element): "
120                            + "invalid namespace for node " + nodeName);
121                    }
122                    throw new DiscoveryException(
123                        DiscoUtils.bundle.getString("wrongInput"));
124                }
125                if (nodeName.equals("Status")) {
126                    if (status != null) {
127                        if (DiscoUtils.debug.messageEnabled()) {
128                            DiscoUtils.debug.message("QueryResponse(Element): "
129                                + "included more than one Status.");
130                        }
131                        throw new DiscoveryException(
132                            DiscoUtils.bundle.getString("moreElement"));
133                    }
134                    status = DiscoUtils.parseStatus((Element) child);
135                } else if (nodeName.equals("ResourceOffering")) {
136                    if (offerings == null) {
137                        offerings = new ArrayList();
138                    }
139                    offerings.add(new ResourceOffering((Element) child));
140                } else if (nodeName.equals("Credentials")) {
141                    if (foundCreds) {
142                        if (DiscoUtils.debug.messageEnabled()) {
143                            DiscoUtils.debug.message("QueryResponse(Element): "
144                                + "included more than one Credentials.");
145                        }
146                        throw new DiscoveryException(
147                            DiscoUtils.bundle.getString("moreElement"));
148                    }
149                    foundCreds = true;
150                    parseCreds((Element) child);
151                } else {
152                    if (DiscoUtils.debug.messageEnabled()) {
153                        DiscoUtils.debug.message("QueryResponse(Element): "
154                            + "invalid node" + nodeName);
155                    }
156                    throw new DiscoveryException(
157                        DiscoUtils.bundle.getString("wrongInput"));
158                }
159            }
160        }
161
162        if (status == null) {
163            if (DiscoUtils.debug.messageEnabled()) {
164                DiscoUtils.debug.message("QueryResponse(Element): missing "
165                    + "Status.");
166            }
167            throw new DiscoveryException(
168                DiscoUtils.bundle.getString("missingStatus"));
169        }
170    }
171
172    private void parseCreds(Element elem) throws DiscoveryException {
173        NodeList contentnl = elem.getChildNodes();
174        Node child;
175        String nodeName;
176        SecurityAssertion assertion;
177        for (int i = 0, length = contentnl.getLength(); i < length; i++) {
178            child = contentnl.item(i);
179            if ((nodeName = child.getLocalName()) != null) {
180                try {
181                    assertion = new SecurityAssertion((Element) child);
182                } catch (SAMLException se) {
183                    if (DiscoUtils.debug.messageEnabled()) {
184                        DiscoUtils.debug.message("QueryResponse(Element): "
185                            + "Exception thrown when parsing Credentials:", se);
186                    }
187                    throw new DiscoveryException(
188                        DiscoUtils.bundle.getString("wrongCredential"));
189                }
190                if (creds == null) {
191                    creds = new ArrayList();
192                }
193                creds.add(assertion);
194            }
195        }
196    }
197
198    /**
199     * Default constructor.
200     */
201    public QueryResponse () {}
202
203    /**
204     * Constructor.
205     *
206     * @param status Status of the response.
207     */
208    public QueryResponse(Status status) {
209        this.status = status;
210    }
211
212    /**
213     * Gets status of the query response.
214     *
215     * @return status of the query response.
216     * @see #setStatus(com.sun.identity.liberty.ws.common.Status)
217     */
218    public com.sun.identity.liberty.ws.common.Status getStatus() {
219        return status;
220    }
221
222    /**
223     * Sets the Status of the query response.
224     *
225     * @param status the Status of the query response.
226     * @see #getStatus()
227     */
228    public void setStatus(com.sun.identity.liberty.ws.common.Status status) {
229        this.status = status;
230    }
231
232    /**
233     * Gets the returned <code>ResourceOffering</code>. 
234     * 
235     * @return List of <code>ResourceOffering</code> objects
236     * @see #setResourceOffering(List)
237     */
238    public java.util.List getResourceOffering() {
239        return offerings;
240    }
241
242    /**
243     * Sets <code>ResourceOffering</code> to return.
244     * 
245     * @param offerings List of <code>ResourceOffering</code> objects
246     * @see #getResourceOffering()
247     */
248    public void setResourceOffering(List offerings) {
249        this.offerings = offerings;
250    }
251
252    /**
253     * Gets id attribute.
254     *
255     * @return id attribute.
256     * @see #setId(String)
257     */
258    public java.lang.String getId() {
259        return id;
260    }
261
262    /**
263     * Sets id attribute.
264     *
265     * @param id id attribute.
266     * @see #getId()
267     */
268    public void setId(String id) {
269        this.id = id;
270    }
271
272    /**
273     * Gets credentials. 
274     * @return List of
275     *  <code>com.sun.identity.liberty.ws.security.SecurityAssertion</code>
276     *  objects.
277     * @see #setCredentials(List)
278     */
279    public List getCredentials() {
280        return creds;
281    }
282
283    /**
284     * Sets credentials. 
285     * @param credentials List of
286     *  <code>com.sun.identity.liberty.ws.security.SecurityAssertion</code>
287     *  objects.
288     * @see #getCredentials()
289     */
290    public void setCredentials(List credentials) {
291        creds = credentials;
292    }
293
294
295    /**
296     * Returns formatted string of the <code>QueryResponse</code>.
297     *
298     * @return formatted string of the <code>QueryResponse</code>.
299     */ 
300    public java.lang.String toString() {
301        StringBuffer sb = new StringBuffer(2000);
302        sb.append("<QueryResponse xmlns=\"").append(DiscoConstants.DISCO_NS).
303            append("\"");
304        if ((id != null) && id.length() != 0) {
305            sb.append(" id=\"").append(id).append("\"");
306        }
307        sb.append(">");
308        if (status != null) {
309            sb.append(status.toString());
310        }
311        if (offerings != null) {
312            Iterator iter = offerings.iterator();
313            while (iter.hasNext()) {
314                sb.append(((ResourceOffering) iter.next()).toString());
315            }
316        }
317        if (creds != null) {
318            sb.append("<Credentials xmlns=\"").append(DiscoConstants.DISCO_NS).
319                append("\">");
320            Iterator iter2 = creds.iterator();
321            while (iter2.hasNext()) {
322                sb.append(iter2.next().toString());
323            }
324            sb.append("</Credentials>");
325        }
326        sb.append("</QueryResponse>");
327        return sb.toString();
328    }
329}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.