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: Query.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;
035import org.w3c.dom.*;
036import com.sun.identity.liberty.ws.disco.common.DiscoConstants;
037import com.sun.identity.liberty.ws.disco.common.DiscoUtils;
038
039/**
040 * The class <code>Query</code> represents a discovery Query object.
041 * The following schema fragment specifies the expected content within the
042 * <code>Query</code> object.
043 * <pre>
044 * &lt;xs:element name="Query" type="Query"/> 
045 * &lt;complexType name="Query">
046 *   &lt;complexContent>
047 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
048 *       &lt;sequence>
049 *         &lt;group ref="{urn:liberty:disco:2003-08}ResourceIDGroup"/>
050 *         &lt;element name="RequestedServiceType" maxOccurs="unbounded" minOccurs="0">
051 *           &lt;complexType>
052 *             &lt;complexContent>
053 *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
054 *                 &lt;sequence>
055 *                   &lt;element ref="{urn:liberty:disco:2003-08}ServiceType"/>
056 *                   &lt;element ref="{urn:liberty:disco:2003-08}Options" minOccurs="0"/>
057 *                 &lt;/sequence>
058 *               &lt;/restriction>
059 *             &lt;/complexContent>
060 *           &lt;/complexType>
061 *         &lt;/element>
062 *       &lt;/sequence>
063 *       &lt;attribute name="id" type="{http://www.w3.org/2001/XMLSchema}ID" />
064 *     &lt;/restriction>
065 *   &lt;/complexContent>
066 * &lt;/complexType>
067 * </pre>
068 * 
069 * @supported.all.api
070 */
071public class Query {
072
073    private String id = null;
074    private ResourceID resourceID = null;
075    private EncryptedResourceID encryptResID = null;
076    private List requestedService = null;
077
078    /**
079     * Constructor.
080     * @param resourceID resource ID of the discovery resource to be queried.
081     * @param RequestedService List of <code>RequestService</code> object.
082     */
083    public Query (ResourceID resourceID, java.util.List RequestedService) {
084        this.resourceID = resourceID;
085        requestedService = RequestedService;
086    }
087
088    /**
089     * Constructor.
090     * @param resourceID encrypted resource ID of the discovery resource 
091     *                to be queried.
092     * @param RequestedService List of <code>RequestService</code> object.
093     */
094    public Query (EncryptedResourceID resourceID, 
095                  java.util.List RequestedService) 
096    {
097        encryptResID = resourceID;
098        requestedService = RequestedService;
099    }
100
101    /**
102     * Constructor.
103     * @param root Query in DOM Element
104     * @exception DiscoveryException if error occurs
105     */
106    public Query (Element root) throws DiscoveryException {
107        if (root == null) {
108            DiscoUtils.debug.message("Query(Element): null input.");
109            throw new DiscoveryException(
110                DiscoUtils.bundle.getString("nullInput"));
111        }
112        String nodeName;
113        String nameSpaceURI;
114        if (((nodeName = root.getLocalName()) == null) ||
115            (!nodeName.equals("Query")) ||
116            ((nameSpaceURI = root.getNamespaceURI()) == null) ||
117            (!nameSpaceURI.equals(DiscoConstants.DISCO_NS)))
118        {
119            DiscoUtils.debug.message("Query(Element): wrong input");
120            throw new DiscoveryException(
121                DiscoUtils.bundle.getString("wrongInput"));
122        }
123
124        // attribute id
125        id = root.getAttribute("id");
126
127        // loop to get ResourceID or EncryptedResourceID
128        // 0 or more RequestedService
129        NodeList contentnl = root.getChildNodes();
130        Node child;
131        for (int i = 0, length = contentnl.getLength(); i < length; i++) {
132            child = contentnl.item(i);
133            if ((nodeName = child.getLocalName()) != null) {
134                nameSpaceURI = ((Element) child).getNamespaceURI();
135                if ((nameSpaceURI == null) ||
136                    (!nameSpaceURI.equals(DiscoConstants.DISCO_NS)))
137                {
138                    if (DiscoUtils.debug.messageEnabled()) {
139                        DiscoUtils.debug.message("Query(Element): "
140                            + "invalid namespace for node " + nodeName);
141                    }
142                    throw new DiscoveryException(
143                        DiscoUtils.bundle.getString("wrongInput"));
144                }
145                if (nodeName.equals("ResourceID")) {
146                    if ((resourceID != null) || (encryptResID != null)) {
147                        if (DiscoUtils.debug.messageEnabled()) {
148                            DiscoUtils.debug.message("Query(Element): Included"
149                                + " more than one ResourceIDGroup element.");
150                        }
151                        throw new DiscoveryException(
152                            DiscoUtils.bundle.getString("moreResourceIDGroup"));
153                    }
154                    resourceID = new ResourceID((Element) child);
155                } else if (nodeName.equals("EncryptedResourceID")) {
156                    if ((resourceID != null) || (encryptResID != null)) {
157                        if (DiscoUtils.debug.messageEnabled()) {
158                            DiscoUtils.debug.message("Query(Element): Included"
159                                + " more than one ResourceIDGroup element.");
160                        }
161                        throw new DiscoveryException(
162                            DiscoUtils.bundle.getString("moreResourceIDGroup"));
163                    }
164                    encryptResID = new EncryptedResourceID((Element) child);
165                } else if (nodeName.equals("RequestedServiceType")) {
166                    if (requestedService == null) {
167                        requestedService = new ArrayList();
168                    }
169                    requestedService.add(new RequestedService((Element) child));
170                } else {
171                    if (DiscoUtils.debug.messageEnabled()) {
172                        DiscoUtils.debug.message("Query(Element): invalid"
173                                + " node" + nodeName);
174                    }
175                    throw new DiscoveryException(
176                        DiscoUtils.bundle.getString("wrongInput"));
177                }
178            } // if nodeName != null
179        } // done for the nl loop
180
181        // make sure there is a ResourceID or EncryptedResourceID
182        if ((resourceID == null) && (encryptResID == null)) {
183            if (DiscoUtils.debug.messageEnabled()) {
184                DiscoUtils.debug.message("Query(Element): missing ResourceID "
185                    + "or EncryptedResourceID element.");
186            }
187            throw new DiscoveryException(
188                DiscoUtils.bundle.getString("missingResourceIDGroup"));
189        }
190    }
191
192    /**
193     * Gets id attribute.
194     *
195     * @return id attribute.
196     * @see #setId(String)
197     */
198    public java.lang.String getId() {
199        return id;
200    }
201
202    /**
203     * Sets id attribute.
204     *
205     * @param id id attribute.
206     * @see #getId()
207     */
208    public void setId(String id) {
209        this.id = id;
210    }
211
212    /**
213     * Gets the encrypted resource ID of the discovery resource to be queried.
214     *
215     * @return the encrypted resource ID of the discovery resource to be
216     *         queried.
217     * @see #setEncryptedResourceID(EncryptedResourceID)
218     */
219    public EncryptedResourceID getEncryptedResourceID() {
220        return encryptResID;
221    }
222
223    /**
224     * Sets the encrypted resource ID of the discovery resource to be queried.
225     *
226     * @param value the encrypted resource ID.
227     * @see #getEncryptedResourceID()
228     */
229    public void setEncryptedResourceID(EncryptedResourceID value) {
230        encryptResID = value;
231    }
232
233    /**
234     * Gets the resource ID of the discovery resource to be queried.
235     *
236     * @return the resource ID of the discovery resource to be queried.
237     * @see #setResourceID(ResourceID)
238     */
239    public ResourceID getResourceID() {
240        return resourceID;
241    }
242
243    /**
244     * Sets the resource ID of the discovery resource to be queried
245     *
246     * @param resourceID the resource ID of the discovery resource to be
247     *        queried.
248     * @see #getResourceID()
249     */
250    public void setResourceID(ResourceID resourceID) {
251        this.resourceID = resourceID;
252    }
253
254    /**
255     * Gets the list of the requested service types.
256     *
257     * @return the list of the requested service types.
258     * @see #setRequestedServiceType(List)
259     */
260    public java.util.List getRequestedServiceType() {
261        return requestedService;
262    }
263
264    /**
265     * Sets the list of the requested service types.
266     *
267     * @param requestedService the list of the requested service types to be
268     *        set.
269     * @see #getRequestedServiceType()
270     */
271    public void setRequestedServiceType(List requestedService) {
272        this.requestedService = requestedService;
273    }
274
275    /**
276     * Returns formatted string of the <code>Query</code> object.
277     *
278     * @return formatted string of the <code>Query</code> object.
279     */ 
280    public java.lang.String toString() {
281        StringBuffer sb = new StringBuffer(1000);
282        sb.append("<Query xmlns=\"").append(DiscoConstants.DISCO_NS).
283                append("\"");
284        if ((id != null) && id.length() != 0) {
285            sb.append(" id=\"").append(id).append("\"");
286        }
287        sb.append(">");
288        if (resourceID != null) {
289            sb.append(resourceID.toString());
290        } else if (encryptResID != null) {
291            sb.append(encryptResID.toString());
292        }
293        if ((requestedService != null) && !requestedService.isEmpty()) {
294            Iterator iter = requestedService.iterator();
295            while (iter.hasNext()) {
296                sb.append(((RequestedService) iter.next()).toString());
297            }
298        }
299        sb.append("</Query>");
300        return sb.toString();
301    }
302}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.