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: AttributeStatement.java,v 1.2 2008/06/25 05:47:31 qcheng Exp $ 026 * 027 */ 028 029 030package com.sun.identity.saml.assertion; 031 032import java.util.*; 033import org.w3c.dom.*; 034import com.sun.identity.saml.common.SAMLUtilsCommon; 035import com.sun.identity.saml.common.SAMLConstants; 036import com.sun.identity.saml.common.SAMLException; 037import com.sun.identity.saml.common.SAMLRequesterException; 038 039/** 040 *The <code>AttributeStatement</code> element supplies a statement by the issuer 041 *that the specified subject is associated with the specified attributes. 042 *@supported.all.api 043 */ 044public class AttributeStatement extends SubjectStatement { 045 private List _attributes = null; 046 047 /** 048 *Dafault constructor 049 */ 050 protected AttributeStatement() { 051 } 052 053 /** 054 * Constructs an <code>AttributStatement</code> element from an existing 055 * XML block 056 * @param element representing a DOM tree element 057 * @exception SAMLException if there is an error in the sender or in the 058 * element definition. 059 */ 060 public AttributeStatement(Element element)throws SAMLException { 061 // make sure input is not null 062 if (element == null) { 063 SAMLUtilsCommon.debug.message("AttributeStatement: null input."); 064 throw new SAMLRequesterException( 065 SAMLUtilsCommon.bundle.getString("nullInput")); 066 } 067 068 // check if it's an AttributeStatement 069 boolean valid = SAMLUtilsCommon.checkStatement(element, 070 "AttributeStatement"); 071 if (!valid) { 072 SAMLUtilsCommon.debug.message("AttributeStatement: Wrong input."); 073 throw new SAMLRequesterException( 074 SAMLUtilsCommon.bundle.getString("wrongInput")); 075 } 076 077 //Handle the children elements of AttributeStatement 078 NodeList nodes = element.getChildNodes(); 079 int nodeCount = nodes.getLength(); 080 if (nodeCount > 0) { 081 for (int i = 0; i < nodeCount; i++) { 082 Node currentNode = nodes.item(i); 083 if (currentNode.getNodeType() == Node.ELEMENT_NODE) { 084 String tagName = currentNode.getLocalName(); 085 String tagNS = currentNode.getNamespaceURI(); 086 if ((tagName == null) || tagName.length() == 0 || 087 tagNS == null || tagNS.length() == 0) { 088 if (SAMLUtilsCommon.debug.messageEnabled()) { 089 SAMLUtilsCommon.debug.message( 090 "AttributeStatement: " + 091 " The tag name or tag namespace of child" + 092 " element is either null or empty."); 093 } 094 throw new SAMLRequesterException( 095 SAMLUtilsCommon.bundle.getString("nullInput")); 096 } 097 if (tagName.equals("Subject") && 098 tagNS.equals(SAMLConstants.assertionSAMLNameSpaceURI)) { 099 if (this._subject != null) { 100 if (SAMLUtilsCommon.debug.messageEnabled()) { 101 SAMLUtilsCommon.debug.message( 102 "AttributeStatement: "+ 103 "should not contain more than one subject."); 104 } 105 throw new SAMLRequesterException( 106 SAMLUtilsCommon.bundle.getString( 107 "oneElement")); 108 109 } else { 110 this._subject = 111 createSubject((Element)currentNode); 112 } 113 } else if (tagName.equals("Attribute") && 114 tagNS.equals(SAMLConstants.assertionSAMLNameSpaceURI)) { 115 if (_attributes == null) { 116 _attributes = new ArrayList(); 117 } 118 if (!_attributes.add(createAttribute( 119 (Element)currentNode))) { 120 if (SAMLUtilsCommon.debug.messageEnabled()) { 121 SAMLUtilsCommon.debug.message( 122 "AttributeStatement:"+ 123 " failed to add to the Attribute list."); 124 } 125 throw new SAMLRequesterException( 126 SAMLUtilsCommon.bundle.getString( 127 "addListError")); 128 } 129 } else { 130 if (SAMLUtilsCommon.debug.messageEnabled()) { 131 SAMLUtilsCommon.debug.message( 132 "AttributeStatement:" + 133 "wrong element:" + tagName); 134 } 135 throw new SAMLRequesterException( 136 SAMLUtilsCommon.bundle.getString("wrongInput")); 137 } 138 } //end of if (currentNode.getNodeType() == Node.ELEMENT_NODE) 139 } // end of for loop 140 } // end of if (nodeCount > 0) 141 142 // check if the subject is null 143 if (this._subject == null) { 144 if (SAMLUtilsCommon.debug.messageEnabled()) { 145 SAMLUtilsCommon.debug.message( 146 "AttributeStatement: missing Subject"); 147 } 148 throw new SAMLRequesterException( 149 SAMLUtilsCommon.bundle.getString("missingElement")); 150 } 151 //check if the attribute is null 152 if (_attributes == null || _attributes.isEmpty()) { 153 if (SAMLUtilsCommon.debug.messageEnabled()) { 154 SAMLUtilsCommon.debug.message("AttributeStatement: " + 155 "should at least contain one Attribute element."); 156 } 157 throw new SAMLRequesterException( 158 SAMLUtilsCommon.bundle.getString("missingElement")); 159 } 160 } 161 162 /** 163 * Constructs an instance of <code>AttributeStatement</code>. 164 * 165 * @param subject (required) A Subject object. 166 * @param attribute (one or more) A List of Attribute objects. 167 * @exception SAMLException if there is an error in the sender. 168 */ 169 public AttributeStatement(Subject subject, List attribute) 170 throws SAMLException { 171 // check if the subject is null 172 if (subject == null) { 173 if (SAMLUtilsCommon.debug.messageEnabled()) { 174 SAMLUtilsCommon.debug.message("AttributeStatement: " + 175 "missing subject."); 176 } 177 throw new SAMLRequesterException( 178 SAMLUtilsCommon.bundle.getString("missingElement")); 179 } else { 180 this._subject = subject; 181 } 182 // check if containing any Attribute 183 if (attribute == null || attribute.isEmpty()) { 184 if (SAMLUtilsCommon.debug.messageEnabled()) { 185 SAMLUtilsCommon.debug.message( 186 "AttributeStatement: Attribute is required."); 187 } 188 throw new SAMLRequesterException( 189 SAMLUtilsCommon.bundle.getString("missingElement")); 190 } 191 if (_attributes == null) { 192 _attributes = new ArrayList(); 193 } 194 _attributes = attribute; 195 } 196 197 /** 198 *Gets attribute from Attribute statement 199 *@return A list of Attributes contained in this statement 200 */ 201 public List getAttribute() { 202 return _attributes; 203 } 204 205 206 /** 207 *Gets the type of statement. 208 *@return an Integer which is Statement.ATTRIBUTE_STATEMENT. 209 */ 210 public int getStatementType() { 211 return Statement.ATTRIBUTE_STATEMENT; 212 } 213 214 /** 215 *Creates a String representation of the attribute statement 216 *@return A string representation of the <code>AttributeStatement</code> 217 * element 218 */ 219 public String toString() { 220 return toString(true, false); 221 } 222 223 /** 224 * Returns a String representation of the Attribute statement. 225 * 226 * @param includeNS Determines whether or not the namespace qualifier 227 * is prepended to the Element when converted 228 * @param declareNS Determines whether or not the namespace is declared 229 * within the Element. 230 *@return A string representation of the 231 * <code><saml:AttributeStatement></code> element. 232 */ 233 public String toString(boolean includeNS, boolean declareNS) { 234 StringBuffer result = new StringBuffer(3000); 235 String prefix = ""; 236 String uri = ""; 237 if (includeNS) { 238 prefix = SAMLConstants.ASSERTION_PREFIX; 239 } 240 if (declareNS) { 241 uri = SAMLConstants.assertionDeclareStr; 242 } 243 result.append("<").append(prefix). 244 append("AttributeStatement ").append(uri).append(">\n"); 245 246 result.append(this._subject.toString(includeNS, false)); 247 Iterator iter = _attributes.iterator(); 248 while (iter.hasNext()) { 249 Attribute att = (Attribute)iter.next(); 250 result.append(att.toString(includeNS, false)); 251 } 252 result.append("</").append(prefix).append("AttributeStatement>\n"); 253 return(result.toString()); 254 } 255 256 protected Subject createSubject(Element subjectElement) 257 throws SAMLException { 258 return new Subject(subjectElement); 259 } 260 261 protected Attribute createAttribute(Element attributeElement) 262 throws SAMLException { 263 return new Attribute(attributeElement); 264 } 265}