001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2007 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: ActionImpl.java,v 1.3 2008/06/25 05:48:12 qcheng Exp $ 026 * 027 */ 028 029package com.sun.identity.xacml.context.impl; 030 031import com.sun.identity.xacml.common.XACMLException; 032import com.sun.identity.xacml.common.XACMLConstants; 033import com.sun.identity.xacml.common.XACMLSDKUtils; 034import com.sun.identity.xacml.context.Action; 035import com.sun.identity.xacml.context.Attribute; 036import com.sun.identity.xacml.context.ContextFactory; 037import com.sun.identity.shared.xml.XMLUtils; 038 039import java.util.List; 040import java.net.URI; 041import java.util.ArrayList; 042import org.w3c.dom.Document; 043import org.w3c.dom.Element; 044import org.w3c.dom.Node; 045import org.w3c.dom.NodeList; 046 047/** 048 * The <code>Action</code> element specifies information about the 049 * action requested in the <code>Request</code> context by listing a 050 * sequence of <code>Attribute</code> elements associated with the 051 * action. 052 * <p> 053 * <pre> 054 * <xs:element name="Action" type="xacml-context:ActionType"/> 055 * <xs:complexType name="ActionType"> 056 * <xs:sequence> 057 * <xs:element ref="xacml-context:Attribute" minOccurs="0" 058 * maxOccurs="unbounded"/> 059 * <xs:sequence> 060 * <xs:complexType> 061 * </pre> 062 *@supported.all.api 063 */ 064public class ActionImpl implements Action { 065 066 private List attributes ; 067 private boolean mutable = true; 068 069 070 /** Creates a new instance of ActionImpl */ 071 public ActionImpl() { 072 } 073 074 /** 075 * This constructor is used to build <code>Action</code> object from a 076 * XML string. 077 * 078 * @param xml A <code>java.lang.String</code> representing 079 * a <code>Action</code> object 080 * @exception XACMLException if it could not process the XML string 081 */ 082 083 public ActionImpl(String xml) throws XACMLException { 084 Document document = XMLUtils.toDOMDocument(xml, XACMLSDKUtils.debug); 085 if (document != null) { 086 Element rootElement = document.getDocumentElement(); 087 processElement(rootElement); 088 makeImmutable(); 089 } else { 090 XACMLSDKUtils.debug.error( 091 "ActionImpl.processElement(): invalid XML input"); 092 throw new XACMLException( 093 XACMLSDKUtils.xacmlResourceBundle.getString( 094 "errorObtainingElement")); 095 } 096 } 097 098 /** 099 * This constructor is used to build <code>Action</code> object from a 100 * block of existing XML that has already been built into a DOM. 101 * 102 * @param element A <code>org.w3c.dom.Element</code> representing 103 * DOM tree for <code>Action</code> object 104 * @exception XACMLException if it could not process the Element 105 */ 106 public ActionImpl(Element element) throws XACMLException { 107 processElement(element); 108 makeImmutable(); 109 } 110 111 private void processElement(Element element) throws XACMLException { 112 if (element == null) { 113 XACMLSDKUtils.debug.error( 114 "ActionImpl.processElement(): invalid root element"); 115 throw new XACMLException( 116 XACMLSDKUtils.xacmlResourceBundle.getString( 117 "invalid_element")); 118 } 119 String elemName = element.getLocalName(); 120 if (elemName == null) { 121 XACMLSDKUtils.debug.error( 122 "ActionImpl.processElement(): local name missing"); 123 throw new XACMLException( 124 XACMLSDKUtils.xacmlResourceBundle.getString( 125 "missing_local_name")); 126 } 127 128 if (!elemName.equals(XACMLConstants.ACTION)) { 129 XACMLSDKUtils.debug.error( 130 "ActionImpl.processElement(): invalid local name " + 131 elemName); 132 throw new XACMLException( 133 XACMLSDKUtils.xacmlResourceBundle.getString( 134 "invalid_local_name")); 135 } 136 // starts processing subelements 137 NodeList nodes = element.getChildNodes(); 138 int numOfNodes = nodes.getLength(); 139 if (numOfNodes >= 1) { 140 ContextFactory factory = ContextFactory.getInstance(); 141 for (int nextElem = 0; nextElem < numOfNodes; nextElem++) { 142 Node child = (Node)nodes.item(nextElem); 143 if (child.getNodeType() == Node.ELEMENT_NODE) { 144 // The child nodes should be <Attribute> 145 String attrChildName = child.getLocalName(); 146 if (attrChildName.equals(XACMLConstants.ATTRIBUTE)) { 147 if (this.attributes == null) { 148 this.attributes = new ArrayList(); 149 } 150 Attribute attribute = factory.getInstance(). 151 createAttribute((Element)child); 152 attributes.add(attribute); 153 } else { 154 XACMLSDKUtils.debug.error("ActionImpl." 155 +"processElement(): Invalid element :" 156 +attrChildName); 157 throw new XACMLException( 158 XACMLSDKUtils.xacmlResourceBundle.getString( 159 "invalid_element")); 160 } 161 } 162 } 163 } 164 } 165 166 public java.util.List getAttributes() { 167 return attributes; 168 } 169 170 /** 171 * Sets the <code>Attribute</code> elements of this object 172 * 173 * @param attributes <code>Attribute</code> elements of this object 174 * attributes could be an empty <code>List</code>, if no attributes 175 * are present. 176 * 177 * @exception XACMLException if the object is immutable 178 * An object is considered <code>immutable</code> if <code> 179 * makeImmutable()</code> has been invoked on it. It can 180 * be determined by calling <code>mutable</code> on the object. 181 */ 182 public void setAttributes(java.util.List attributes) throws 183 XACMLException { 184 if (!mutable) { 185 throw new XACMLException( 186 XACMLSDKUtils.xacmlResourceBundle.getString( 187 "objectImmutable")); 188 } 189 if (attributes != null && !attributes.isEmpty()) { 190 if (this.attributes == null) { 191 this.attributes = new ArrayList(); 192 } 193 this.attributes.addAll(attributes); 194 } 195 } 196 197 /** 198 * Returns a <code>String</code> representation of this object 199 * @param includeNSPrefix Determines whether or not the namespace qualifier 200 * is prepended to the Element when converted 201 * @param declareNS Determines whether or not the namespace is declared 202 * within the Element. 203 * @return a string representation of this object 204 * @exception XACMLException if conversion fails for any reason 205 */ 206 public String toXMLString(boolean includeNSPrefix, boolean declareNS) 207 throws XACMLException { 208 StringBuffer sb = new StringBuffer(2000); 209 StringBuffer namespaceBuffer = new StringBuffer(100); 210 String nsDeclaration = ""; 211 if (declareNS) { 212 namespaceBuffer.append(XACMLConstants.CONTEXT_NS_DECLARATION). 213 append(XACMLConstants.SPACE); 214 namespaceBuffer.append(XACMLConstants.XSI_NS_URI). 215 append(XACMLConstants.SPACE).append(XACMLConstants. 216 CONTEXT_SCHEMA_LOCATION); 217 } 218 if (includeNSPrefix) { 219 nsDeclaration = XACMLConstants.CONTEXT_NS_PREFIX + ":"; 220 } 221 sb.append("<").append(nsDeclaration).append(XACMLConstants.ACTION). 222 append(namespaceBuffer); 223 sb.append(">"); 224 int length = 0; 225 if (attributes != null) { 226 sb.append("\n"); 227 length = attributes.size(); 228 for (int i = 0; i < length; i++) { 229 Attribute attr = (Attribute)attributes.get(i); 230 sb.append(attr.toXMLString(includeNSPrefix, false)); 231 } 232 } 233 sb.append("</").append(nsDeclaration).append(XACMLConstants.ACTION); 234 sb.append(">\n"); 235 return sb.toString(); 236 } 237 238 /** 239 * Returns a string representation of this object 240 * 241 * @return a string representation of this object 242 * @exception XACMLException if conversion fails for any reason 243 */ 244 public String toXMLString() throws XACMLException { 245 return toXMLString(true, false); 246 } 247 248 /** 249 * Makes the object immutable 250 */ 251 public void makeImmutable() { 252 mutable = false; 253 } 254 255 /** 256 * Checks if the object is mutable 257 * 258 * @return <code>true</code> if the object is mutable, 259 * <code>false</code> otherwise 260 */ 261 public boolean isMutable() { 262 return mutable; 263 } 264 265}
Copyright © 2010-2017, ForgeRock All Rights Reserved.