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: DSTQuery.java,v 1.3 2008/06/25 05:47:13 qcheng Exp $ 026 * 027 */ 028 029 030package com.sun.identity.liberty.ws.dst; 031 032import com.sun.identity.liberty.ws.disco.EncryptedResourceID; 033import java.util.List; 034import java.util.ArrayList; 035import java.util.Iterator; 036import org.w3c.dom.Node; 037import org.w3c.dom.Element; 038import org.w3c.dom.NodeList; 039import com.sun.identity.shared.xml.XMLUtils; 040 041/** 042 * The <code>DSTQuery</code> class represents a <code>DST</code> query request. 043 * <p>The following schema fragment specifies the expected content within the 044 * <code>DSTQuery</code> object. 045 * <p> 046 * <pre> 047 * <complexType name="QueryType"> 048 * <complexContent> 049 * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 050 * <sequence> 051 * <group ref="{urn:liberty:idpp:2003-08}ResourceIDGroup"/> 052 * <element name="QueryItem" maxOccurs="unbounded"> 053 * <complexType> 054 * <complexContent> 055 * <restriction base="{http://www.w3.org/2001/XMLSchema} 056 * anyType"> 057 * <sequence> 058 * <element name="Select" 059 * type="{urn:liberty:idpp:2003-08}SelectType"/> 060 * </sequence> 061 * <attribute name="id" 062 * type="{http://www.w3.org/2001/XMLSchema}ID" /> 063 * <attribute name="changedSince" 064 * type="{http://www.w3.org/2001/XMLSchema}dateTime" /> 065 * <attribute name="itemID" 066 * type="{urn:liberty:idpp:2003-08}IDType" /> 067 * <attribute name="includeCommonAttributes" 068 * type="{http://www.w3.org/2001/XMLSchema}boolean" /> 069 * </restriction> 070 * </complexContent> 071 * </complexType> 072 * </element> 073 * <element ref="{urn:liberty:idpp:2003-08}Extension" 074 * maxOccurs="unbounded" minOccurs="0"/> 075 * </sequence> 076 * <attribute name="itemID" type="{urn:liberty:idpp:2003-08}IDType" /> 077 * <attribute name="id" type="{http://www.w3.org/2001/XMLSchema}ID" /> 078 * </restriction> 079 * </complexContent> 080 * </complexType> 081 * </pre> 082 * 083 * @supported.all.api 084 */ 085 086public class DSTQuery { 087 088 private String resourceID = null; 089 private EncryptedResourceID encryptedResourceID = null; 090 private String id = null; 091 private String itemID = null; 092 private List dstQueryItems = new ArrayList(); 093 private List extensions = new ArrayList(); 094 private String nameSpaceURI = null; 095 private String prefix = null; 096 097 /** 098 * Constructor 099 */ 100 public DSTQuery() {} 101 102 /** 103 * Constructor 104 * @param resourceID id for the resource to be queried. 105 * @param items List of <code>DSTQueryItem</code> objects. 106 * @param serviceNS service name space. 107 */ 108 public DSTQuery(String resourceID, java.util.List items, String serviceNS) { 109 this.resourceID = resourceID; 110 if(items != null && items.size() != 0) { 111 dstQueryItems.addAll(items); 112 DSTQueryItem item = (DSTQueryItem)items.get(0); 113 if(serviceNS == null) { 114 nameSpaceURI = item.getNameSpaceURI(); 115 } else { 116 nameSpaceURI = serviceNS; 117 } 118 prefix = item.getNameSpacePrefix(); 119 } 120 } 121 122 /** 123 * Constructor 124 * @param encResourceID id for the encrypted resource to be queried. 125 * @param items list of <code>DSTQueryItem</code> objects. 126 * @param serviceNS service name space. 127 */ 128 public DSTQuery(com.sun.identity.liberty.ws.disco.EncryptedResourceID 129 encResourceID, java.util.List items, String serviceNS) { 130 this.encryptedResourceID = encResourceID; 131 if(items != null && items.size() != 0) { 132 dstQueryItems.addAll(items); 133 DSTQueryItem item = (DSTQueryItem)items.get(0); 134 if(serviceNS == null) { 135 nameSpaceURI = item.getNameSpaceURI(); 136 } else { 137 nameSpaceURI = serviceNS; 138 } 139 prefix = item.getNameSpacePrefix(); 140 } 141 } 142 143 /** 144 * Constructor 145 * 146 * @param element <code>DOM</code> Element. 147 * @throws DSTException 148 */ 149 public DSTQuery(org.w3c.dom.Element element) throws DSTException{ 150 if(element == null) { 151 DSTUtils.debug.error("DSTQuery(element):null input"); 152 throw new DSTException(DSTUtils.bundle.getString("nullInputParams")); 153 } 154 String elementName = element.getLocalName(); 155 if(elementName == null || !elementName.equals("Query")) { 156 DSTUtils.debug.error("DSTQuery(element):Invalid element name"); 157 throw new DSTException(DSTUtils.bundle.getString("invalidElement")); 158 } 159 nameSpaceURI = element.getNamespaceURI(); 160 if(nameSpaceURI == null) { 161 DSTUtils.debug.error("DSTModify(element): NameSpace is not defined"); 162 throw new DSTException(DSTUtils.bundle.getString("noNameSpace")); 163 } 164 prefix = element.getPrefix(); 165 id = element.getAttribute("id"); 166 itemID = element.getAttribute("itemID"); 167 NodeList list = element.getElementsByTagNameNS( 168 nameSpaceURI, "ResourceID"); 169 170 if((list.getLength() == 0) || (list.getLength() > 1)) { 171 DSTUtils.debug.error("DSTQuery(element): ResourceIDNode is null" + 172 " or more than one resource id is found."); 173 throw new DSTException( 174 DSTUtils.bundle.getString("invalidResourceID")); 175 } 176 resourceID = XMLUtils.getElementValue((Element)list.item(0)); 177 if(resourceID == null) { 178 DSTUtils.debug.error("DSTQuery(element): ResourceID is null" ); 179 throw new DSTException( 180 DSTUtils.bundle.getString("invalidResourceID")); 181 } 182 183 NodeList queryItemNodes = element.getElementsByTagNameNS( 184 nameSpaceURI, "QueryItem"); 185 if(queryItemNodes == null || queryItemNodes.getLength() == 0) { 186 DSTUtils.debug.error("DSTQuery(element): QueryItems are null" ); 187 throw new DSTException( 188 DSTUtils.bundle.getString("nullQueryItems")); 189 } 190 int size = queryItemNodes.getLength(); 191 for(int i=0; i < size; i++) { 192 Node node = queryItemNodes.item(0); 193 DSTQueryItem dstQueryItem = 194 new DSTQueryItem((Element)node); 195 dstQueryItems.add(dstQueryItem); 196 } 197 } 198 199 /** 200 * Gets the value of the <code>QueryItem</code> property. 201 * 202 * @return List of <code>DSTQueryItem</code> objects 203 */ 204 public java.util.List getQueryItems() { 205 return dstQueryItems; 206 } 207 208 /** 209 * Sets the value of the <code>QueryItem</code> property. 210 * 211 * @param items List of <code>DSTQueryItem</code> objects 212 */ 213 public void setQueryItem(java.util.List items) { 214 if(items != null && items.size() != 0) { 215 dstQueryItems.addAll(items); 216 } 217 } 218 219 /** 220 * Gets id attribute. 221 * 222 * @return id attribute. 223 */ 224 public java.lang.String getId() { 225 return id; 226 } 227 228 /** 229 * Sets id attribute 230 * @param id value of id to be set 231 */ 232 public void setId(java.lang.String id) { 233 this.id = id; 234 } 235 236 /** 237 * Gets encrypted resource ID. 238 * @return Encrypted resource ID. 239 */ 240 public com.sun.identity.liberty.ws.disco.EncryptedResourceID 241 getEncryptedResourceID() { 242 return encryptedResourceID; 243 } 244 245 /** 246 * Sets encrypted resource ID for the <code>DST</code> query. 247 * 248 * @param encResourceID encrypted resource ID. 249 */ 250 public void setEncryptedResourceID( 251 com.sun.identity.liberty.ws.disco.EncryptedResourceID encResourceID) { 252 this.encryptedResourceID = encResourceID; 253 } 254 255 /** 256 * Gets resource ID. 257 * 258 * @return resource ID. 259 */ 260 public java.lang.String getResourceID() { 261 return resourceID; 262 } 263 264 /** 265 * Sets resource ID for the <code>DST</code> query. 266 * @param resourceID resource ID to be set 267 */ 268 public void setResourceID(java.lang.String resourceID) { 269 this.resourceID = resourceID; 270 } 271 272 /** 273 * Gets item ID attribute 274 * @return String 275 */ 276 public java.lang.String getItemID() { 277 return itemID; 278 } 279 280 /** 281 * Sets item ID attribute 282 * @param value item ID to be set 283 */ 284 public void setItemID(java.lang.String value) { 285 this.itemID = value; 286 } 287 288 /** 289 * Gets the extension property. 290 * 291 * @return List of any Object 292 * 293 */ 294 public java.util.List getExtension() { 295 return extensions; 296 } 297 298 /** 299 * Gets the name space. 300 * @return name space. 301 */ 302 public java.lang.String getNameSpaceURI() { 303 return nameSpaceURI; 304 } 305 306 /** 307 * Sets the name space. 308 * 309 * @param nameSpace Name space URI. 310 */ 311 public void setNameSpaceURI(String nameSpace) { 312 this.nameSpaceURI = nameSpace; 313 } 314 315 /** 316 * Sets the name space prefix. 317 * @param prefix Name space prefix. 318 */ 319 public void setNameSpacePrefix(String prefix) { 320 this.prefix = prefix; 321 } 322 323 /** 324 * Gets the name space prefix. 325 * @return Name space prefix. 326 */ 327 public java.lang.String getNameSpacePrefix() { 328 return prefix; 329 } 330 331 /** 332 * Creates a String representation of this object. 333 * By default name space name is prepended to the element name 334 * @return String A string containing the valid XML for this element 335 */ 336 public java.lang.String toString() { 337 return toString(true, false); 338 } 339 340 /** 341 * Creates a String representation of this object. 342 * @param includeNS if true prepends all elements by their name space prefix 343 * @param declareNS if true includes the name space within the 344 * generated. 345 * @return String A string containing the valid XML for this element 346 */ 347 public java.lang.String toString(boolean includeNS, boolean declareNS) { 348 349 if((encryptedResourceID == null && resourceID == null) || 350 dstQueryItems.isEmpty()) { 351 DSTUtils.debug.error("DSTQuery.toString: ResourceID or QueryItems" + 352 " are null."); 353 return ""; 354 } 355 356 String tempPrefix = ""; 357 if(includeNS) { 358 if(prefix == null) { 359 prefix = DSTConstants.DEFAULT_NS_PREFIX; 360 } 361 tempPrefix = prefix + ":"; 362 } 363 364 if (declareNS && nameSpaceURI == null) { 365 DSTUtils.debug.error("DSTQuery.toString: Name Space is " + 366 "not defined"); 367 return ""; 368 } 369 370 StringBuffer sb = new StringBuffer(3000); 371 sb.append("<").append(tempPrefix).append("Query"); 372 if(id != null && id.length() != 0) { 373 sb.append(" id=\"").append(id).append("\""); 374 } 375 if(itemID != null && itemID.length() != 0) { 376 sb.append(" itemID=\"").append(itemID).append("\""); 377 } 378 if(declareNS) { 379 sb.append(" xmlns:").append(prefix).append("=\"") 380 .append(nameSpaceURI).append("\""); 381 } 382 sb.append(">"); 383 if(encryptedResourceID == null) { 384 sb.append("<").append(tempPrefix).append("ResourceID").append(">") 385 .append(resourceID).append("</").append(tempPrefix) 386 .append("ResourceID").append(">"); 387 } else { 388 sb.append(encryptedResourceID.toString(nameSpaceURI)); 389 } 390 391 Iterator iter = dstQueryItems.iterator(); 392 while(iter.hasNext()) { 393 DSTQueryItem item = (DSTQueryItem)iter.next(); 394 sb.append(item.toString(true, false)); 395 } 396 sb.append("</").append(tempPrefix).append("Query").append(">"); 397 398 if(DSTUtils.debug.messageEnabled()) { 399 DSTUtils.debug.message("DSTQuery.toString: Query: " + sb.toString()); 400 } 401 402 return sb.toString(); 403 } 404 405}
Copyright © 2010-2017, ForgeRock All Rights Reserved.