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: EvidenceBase.java,v 1.2 2008/06/25 05:47:32 qcheng Exp $ 026 * 027 */ 028 029package com.sun.identity.saml.assertion; 030 031import com.sun.identity.saml.common.SAMLUtilsCommon; 032import com.sun.identity.saml.common.SAMLConstants; 033import com.sun.identity.saml.common.SAMLException; 034import com.sun.identity.saml.common.SAMLRequesterException; 035import com.sun.identity.shared.xml.XMLUtils; 036import org.w3c.dom.Element; 037import org.w3c.dom.Node; 038import org.w3c.dom.NodeList; 039import java.util.Set; 040import java.util.HashSet; 041import java.util.Iterator; 042 043/** 044 *The <code>Evidence</code> element specifies an assertion either by 045 *reference or by value. An assertion is specified by reference to the value of 046 *the assertion's <code>AssertionIDReference</code> element. 047 *An assertion is specified by value by including the entire 048 *<code>Assertion</code> object 049 * 050 * This class is an abstract base class for all Evidence implementations and 051 * encapsulates common functionality. 052 *@supported.all.api 053 */ 054public abstract class EvidenceBase { 055 static SAMLConstants sc; 056 private Set _assertionIDRef = new HashSet(); 057 private Set _assertion = new HashSet(); 058 059 /** 060 * Constructs an <code>Evidence</code> object from a block of existing XML 061 * that has already been built into a DOM. 062 * 063 * @param assertionSpecifierElement A <code>org.w3c.dom.Element</code> 064 * representing DOM tree for <code>Evidence</code> object. 065 * @exception SAMLException if it could not process the Element properly, 066 * implying that there is an error in the sender or in the 067 * element definition. 068 */ 069 public EvidenceBase(org.w3c.dom.Element assertionSpecifierElement) 070 throws SAMLException 071 { 072 String elementName = assertionSpecifierElement.getLocalName(); 073 if (elementName == null) { 074 if (SAMLUtilsCommon.debug.messageEnabled()) { 075 SAMLUtilsCommon.debug.message("Evidence:local name " 076 + "missing"); 077 } 078 throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString 079 ("nullInput")) ; 080 } 081 if (!(elementName.equals("Evidence"))) { 082 if (SAMLUtilsCommon.debug.messageEnabled()) { 083 SAMLUtilsCommon.debug.message( 084 "Evidence: invalid root element"); 085 } 086 throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString( 087 "invalidElement")+":"+elementName) ; 088 } 089 NodeList nl = assertionSpecifierElement.getChildNodes(); 090 int length = nl.getLength(); 091 if (length <= 0 ) { 092 if (SAMLUtilsCommon.debug.messageEnabled()) { 093 SAMLUtilsCommon.debug.message(elementName+":" 094 +"no sub elements found in this Element"); 095 } 096 throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString( 097 "noElement")) ; 098 } 099 for (int n=0; n < length; n++) { 100 Node child = (Node)nl.item(n); 101 if (child.getNodeType() != Node.ELEMENT_NODE) { 102 continue; 103 } 104 String childName = child.getLocalName(); 105 if (childName.equals("Assertion")) { 106 _assertion.add(createAssertion((Element)child)); 107 } else if (childName.equals("AssertionIDReference")) { 108 _assertionIDRef.add(createAssertionIDReference( 109 XMLUtils.getElementValue((Element) child))); 110 } else { 111 if (SAMLUtilsCommon.debug.messageEnabled()) { 112 SAMLUtilsCommon.debug.message("Evidence: " 113 + "invalid sub element"); 114 } 115 throw new SAMLRequesterException( 116 SAMLUtilsCommon.bundle.getString("invalidElement")) ; 117 } 118 } 119 } 120 121 /** 122 * Constructs a new <code>Evidence></code> element containing a 123 * set of <code>Assertion</code> objects. 124 * 125 * @param evidenceContent A set of <code>Assertion</code> and 126 * <code>AssertionIDReference</code> objects to be put within the 127 * <code>Evidence</code> element. The same Set contains both type 128 * of elements. 129 * @exception SAMLException if the Set is empty or has invalid object. 130 */ 131 public EvidenceBase(Set evidenceContent) throws SAMLException { 132 if (evidenceContent.isEmpty()) { 133 if (SAMLUtilsCommon.debug.messageEnabled()) { 134 SAMLUtilsCommon.debug.message("Evidence: null input " 135 + "specified"); 136 } 137 throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString( 138 "nullInput")) ; 139 } 140 Iterator it = evidenceContent.iterator(); 141 while (it.hasNext()) { 142 Object obj = it.next(); 143 if (obj instanceof AssertionBase) { 144 _assertion.add((AssertionBase)obj); 145 } else if (obj instanceof AssertionIDReference) { 146 _assertionIDRef.add((AssertionIDReference)obj); 147 } else { 148 SAMLUtilsCommon.debug.message( 149 "Evidence: Invalid input Element"); 150 throw new SAMLRequesterException( 151 SAMLUtilsCommon.bundle.getString("invalidElement")) ; 152 } 153 } 154 } 155 156 /** 157 * Constructs an Evidence from a Set of <code>Assertion</code> and 158 * <code>AssertionIDReference</code> objects. 159 * 160 * @param assertionIDRef Set of <code>AssertionIDReference</code> objects. 161 * @param assertion Set of <code>Assertion</code> objects. 162 * @exception SAMLException if either Set is empty or has invalid object. 163 */ 164 public EvidenceBase(Set assertionIDRef, Set assertion) 165 throws SAMLException { 166 if (assertionIDRef.isEmpty() && assertion.isEmpty()) { 167 if (SAMLUtilsCommon.debug.messageEnabled()) { 168 SAMLUtilsCommon.debug.message("Evidence: null input " 169 + "specified"); 170 } 171 throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString( 172 "nullInput")) ; 173 } 174 Iterator it = assertionIDRef.iterator(); 175 while (it.hasNext()) { 176 Object assID = it.next(); 177 if (assID instanceof AssertionIDReference) { 178 _assertionIDRef.add((AssertionIDReference)assID); 179 } 180 else { 181 SAMLUtilsCommon.debug.message( 182 "Evidence: Invalid input Element"); 183 throw new SAMLRequesterException( 184 SAMLUtilsCommon.bundle.getString("invalidElement")) ; 185 } 186 } 187 it = assertion.iterator(); 188 while (it.hasNext()) { 189 Object ass = it.next(); 190 if (ass instanceof AssertionBase) { 191 _assertion.add((AssertionBase)ass); 192 } 193 else { 194 SAMLUtilsCommon.debug.message( 195 "Evidence: Invalid input Element"); 196 throw new SAMLRequesterException( 197 SAMLUtilsCommon.bundle.getString("invalidElement")) ; 198 } 199 } 200 } 201 202 /** 203 * Adds an <code>Assertion</code> object into the Evidence object. 204 * @param assertion <code>Assertion</code> to be added 205 * @return true if operation succeeds. 206 */ 207 public boolean addAssertion(AssertionBase assertion) { 208 if (assertion == null) { 209 if (SAMLUtilsCommon.debug.messageEnabled()) { 210 SAMLUtilsCommon.debug.message("Evidence: null input " 211 + "specified"); 212 } 213 return false; 214 } else { 215 _assertion.add(assertion); 216 return true; 217 } 218 } 219 220 221 /** 222 * Adds an <code>AssertionIDReference</code> object into the 223 * <code>Evidence</code> object. 224 * 225 * @param assertionIDRef <code>AssertionIDReference</code> to be added. 226 * @return true if operation succeeds. 227 */ 228 public boolean addAssertionIDReference(AssertionIDReference assertionIDRef) 229 { 230 if (assertionIDRef == null) { 231 if (SAMLUtilsCommon.debug.messageEnabled()) { 232 SAMLUtilsCommon.debug.message("Evidence: null input " 233 + "specified"); 234 } 235 return false; 236 } else { 237 _assertionIDRef.add(assertionIDRef); 238 return true; 239 } 240 } 241 242 /** 243 * Removes an <code>Assertion</code> object from the <code>Evidence</code> 244 * object. 245 * 246 * @param assertion <code>Assertion</code> to be removed. 247 * @return true if the operation succeeds, Returns failure of the 248 * <code>Assertion</code> is the only element inside the 249 * <code>Evidence</code>. 250 */ 251 public boolean removeAssertion(AssertionBase assertion) 252 { 253 if (assertion != null) { 254 if (_assertionIDRef.size() + _assertion.size() > 1 ) { 255 _assertion.remove(assertion); 256 return true; 257 } 258 } else { 259 if (SAMLUtilsCommon.debug.messageEnabled()) { 260 SAMLUtilsCommon.debug.message("Evidence: null input " 261 + "specified"); 262 } 263 } 264 return false; 265 } 266 267 /** 268 * Removes an <code>AssertionIDReference</code> object from the 269 * <code>Evidence</code> object. 270 * 271 * @param assertionIDRef <code>AssertionIDReference</code> to be removed 272 * @return true if the operation succeeds, Returns false if the 273 * <code>AssertionIDReference</code> is the only element 274 * inside the <code>Evidence</code>. 275 */ 276 public boolean removeAssertionIDReference(AssertionIDReference 277 assertionIDRef) 278 { 279 if (assertionIDRef != null) { 280 if (_assertionIDRef.size() + _assertion.size() > 1 ) { 281 _assertionIDRef.remove(assertionIDRef); 282 return true; 283 } 284 } else { 285 if (SAMLUtilsCommon.debug.messageEnabled()) { 286 SAMLUtilsCommon.debug.message("Evidence: null input " 287 + "specified"); 288 } 289 } 290 return false; 291 } 292 293 /** 294 *Get <code>java.util.Set</code> of <code>AssertionIDReference</code> 295 *objects in the <code>Evidence</code> 296 *@return <code>java.util.Set</code> of <code>AssertionIDReference</code> 297 *objects within this Evidence. 298 */ 299 public Set getAssertionIDReference() { 300 return _assertionIDRef; 301 } 302 303 /** 304 *Get <code>java.util.Set</code> of <code>Assertion</code> 305 *objects in the <code>Evidence</code> 306 *@return <code>java.util.Set</code> of <code>Assertion</code> 307 *objects within this Evidence. 308 */ 309 public Set getAssertion() { 310 return _assertion; 311 } 312 313 /** 314 * Returns a String representation of the element. 315 * 316 * @return A string containing the valid XML for this element 317 * By default name space name is prepended to the element name 318 * example <code><saml:Evidence></code>. 319 */ 320 public java.lang.String toString() { 321 // call toString() with includeNS true by default and declareNS false 322 String xml = this.toString(true, false); 323 return xml; 324 } 325 326 /** 327 * Returns a String representation of the <code><Evidence></code> 328 * element (or of the <code><Evidence></code> element). 329 * 330 * @param includeNS Determines whether or not the namespace qualifier is 331 * prepended to the <code>Element</code> when converted. 332 * @param declareNS Determines whether or not the namespace is declared 333 * within the Element. 334 * @return The string containing the valid XML for this element .The top 335 * level element is <code>Evidence</code>. 336 */ 337 public java.lang.String toString(boolean includeNS, boolean declareNS) { 338 StringBuffer xml = new StringBuffer(3000); 339 String o=null; 340 o = SAMLUtilsCommon.makeStartElementTagXML("Evidence", 341 includeNS, declareNS); 342 xml.append(o).append(sc.NL); 343 Iterator it = _assertion.iterator(); 344 while (it.hasNext()) { 345 AssertionBase assertion = (AssertionBase)it.next(); 346 xml.append(assertion.toString(includeNS, false)); 347 } 348 it = _assertionIDRef.iterator(); 349 while (it.hasNext()) { 350 AssertionIDReference aidRef = (AssertionIDReference)it.next(); 351 xml.append(aidRef.toString(includeNS, false)); 352 } 353 o = SAMLUtilsCommon.makeEndElementTagXML("Evidence",includeNS); 354 xml.append(o); 355 return xml.toString(); 356 } 357 358 /** 359 * Creates appropriate Assertion Instance 360 * @param assertionElement the assertion Element 361 * @return the assertion instance 362 */ 363 protected abstract AssertionBase 364 createAssertion(Element assertionElement) 365 throws SAMLException; 366 367 /** 368 * Creates appropriate AssertionIDReference Instance 369 * @param assertionID the assertion ID String 370 * @return the AssertionIDReference instance 371 */ 372 protected abstract AssertionIDReference 373 createAssertionIDReference(String assertionID) 374 throws SAMLException; 375}