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: DecisionImpl.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.shared.xml.XMLUtils; 032 033import com.sun.identity.xacml.common.XACMLConstants; 034import com.sun.identity.xacml.common.XACMLException; 035import com.sun.identity.xacml.common.XACMLSDKUtils; 036import com.sun.identity.xacml.context.Decision; 037 038import org.w3c.dom.Document; 039import org.w3c.dom.Element; 040import org.w3c.dom.Node; 041 042/** 043 * The <code>Decision</code> element is a container of 044 * one or more <code>Decision</code>s issued by policy decision point 045 * @supported.all.api 046 * <p/> 047 * Schema: 048 * <pre> 049 * <xs:simpleType name="DecisionType"> 050 * <xs:restriction base="xs:string"> 051 * <xs:enumeration value="Permit"/> 052 * <xs:enumeration value="Deny"/> 053 * <xs:enumeration value="Indeterminate"/> 054 * <xs:enumeration value="NotApplicable"/> 055 * </xs:restriction> 056 * </xs:simpleType> 057 * </pre> 058 * 059 */ 060public class DecisionImpl implements Decision { 061 062 private String value = null; 063 private boolean mutable = true; 064 065 /** 066 * Constructs a <code>Decision</code> object 067 */ 068 public DecisionImpl() throws XACMLException { 069 } 070 071 /** 072 * Constructs a <code>Decision</code> object from an XML string 073 * 074 * @param xml string representing a <code>Decision</code> object 075 * @throws XACMLException if the XML string could not be processed 076 */ 077 public DecisionImpl(String xml) throws XACMLException { 078 Document document = XMLUtils.toDOMDocument(xml, XACMLSDKUtils.debug); 079 if (document != null) { 080 Element rootElement = document.getDocumentElement(); 081 processElement(rootElement); 082 makeImmutable(); 083 } else { 084 XACMLSDKUtils.debug.error( 085 "DecisionImpl.processElement(): invalid XML input"); 086 throw new XACMLException( 087 XACMLSDKUtils.xacmlResourceBundle.getString( 088 "errorObtainingElement")); 089 } 090 } 091 092 /** 093 * Constructs a <code>Decision</code> object from an XML DOM element 094 * 095 * @param element XML DOM element representing a <code>Decision</code> 096 * object 097 * 098 * @throws XACMLException if the DOM element could not be processed 099 */ 100 public DecisionImpl(Element element) throws XACMLException { 101 processElement(element); 102 makeImmutable(); 103 } 104 105 /** 106 * Returns the <code>value</code>s of this object 107 * 108 * @return the <code>value</code>s of this object 109 */ 110 public String getValue() { 111 return value; 112 } 113 114 /** 115 * Sets the <code>value</code>s of this object 116 * 117 * @exception XACMLException if the object is immutable 118 */ 119 public void setValue(String value) throws XACMLException { 120 if (!mutable) { 121 throw new XACMLException( 122 XACMLSDKUtils.xacmlResourceBundle.getString("objectImmutable")); 123 } 124 125 if (value == null) { 126 throw new XACMLException( 127 XACMLSDKUtils.xacmlResourceBundle.getString("null_not_valid")); 128 } 129 130 if (!XACMLSDKUtils.isValidDecision(value)) { 131 throw new XACMLException( 132 XACMLSDKUtils.xacmlResourceBundle.getString("invalid_value")); 133 } 134 this.value = value; 135 } 136 137 138 /** 139 * Returns a string representation 140 * 141 * @return a string representation 142 * @exception XACMLException if conversion fails for any reason 143 */ 144 public String toXMLString() throws XACMLException { 145 return toXMLString(true, false); 146 } 147 148 /** 149 * Returns a string representation 150 * @param includeNSPrefix Determines whether or not the namespace qualifier 151 * is prepended to the Element when converted 152 * @param declareNS Determines whether or not the namespace is declared 153 * within the Element. 154 * @return a string representation 155 * @exception XACMLException if conversion fails for any reason 156 */ 157 public String toXMLString(boolean includeNSPrefix, boolean declareNS) 158 throws XACMLException { 159 StringBuffer sb = new StringBuffer(2000); 160 String nsPrefix = ""; 161 String nsDeclaration = ""; 162 if (declareNS) { 163 nsDeclaration = XACMLConstants.CONTEXT_NS_DECLARATION; 164 } 165 if (includeNSPrefix) { 166 nsPrefix = XACMLConstants.CONTEXT_NS_PREFIX + ":"; 167 } 168 sb.append("<").append(nsPrefix).append(XACMLConstants.DECISION) 169 .append(nsDeclaration).append(">"); 170 if (value != null) { 171 sb.append(value); 172 } 173 sb.append("</").append(nsPrefix).append(XACMLConstants.DECISION) 174 .append(">\n"); 175 return sb.toString(); 176 } 177 178 /** 179 * Checks if the object is mutable 180 * 181 * @return <code>true</code> if the object is mutable, 182 * <code>false</code> otherwise 183 */ 184 public boolean isMutable() { 185 return mutable; 186 } 187 188 /** 189 * Makes the object immutable 190 */ 191 public void makeImmutable() { 192 mutable = false; 193 } 194 195 private void processElement(Element element) throws XACMLException { 196 if (element == null) { 197 XACMLSDKUtils.debug.error( 198 "DecisionImpl.processElement(): invalid root element"); 199 throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString( 200 "invalid_element")); 201 } 202 String elemName = element.getLocalName(); 203 if (elemName == null) { 204 XACMLSDKUtils.debug.error( 205 "DecisionImpl.processElement(): local name missing"); 206 throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString( 207 "missing_local_name")); 208 } 209 210 if (!elemName.equals(XACMLConstants.DECISION)) { 211 XACMLSDKUtils.debug.error( 212 "DecisionImpl.processElement(): invalid local name " 213 + elemName); 214 throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString( 215 "invalid_local_name")); 216 } 217 String elementValue = element.getTextContent(); 218 if (elementValue == null) { 219 throw new XACMLException( 220 XACMLSDKUtils.xacmlResourceBundle.getString("null_not_valid")); 221 } 222 if (!XACMLSDKUtils.isValidDecision(elementValue.trim())) { 223 throw new XACMLException( 224 XACMLSDKUtils.xacmlResourceBundle.getString("invalid_value")); 225 } else { 226 this.value = elementValue; 227 } 228 } 229 230}