Source code: com/sun/xacml/cond/EqualFunction.java
1
2 /*
3 * @(#)EqualFunction.java
4 *
5 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistribution of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistribution in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
18 * be used to endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * This software is provided "AS IS," without a warranty of any kind. ALL
22 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
24 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
25 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
26 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
27 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
28 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
29 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
30 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use in
34 * the design, construction, operation or maintenance of any nuclear facility.
35 */
36
37 package com.sun.xacml.cond;
38
39 import com.sun.xacml.EvaluationCtx;
40
41 import com.sun.xacml.attr.AnyURIAttribute;
42 import com.sun.xacml.attr.AttributeValue;
43 import com.sun.xacml.attr.Base64BinaryAttribute;
44 import com.sun.xacml.attr.BooleanAttribute;
45 import com.sun.xacml.attr.DateAttribute;
46 import com.sun.xacml.attr.DateTimeAttribute;
47 import com.sun.xacml.attr.DayTimeDurationAttribute;
48 import com.sun.xacml.attr.DoubleAttribute;
49 import com.sun.xacml.attr.HexBinaryAttribute;
50 import com.sun.xacml.attr.IntegerAttribute;
51 import com.sun.xacml.attr.RFC822NameAttribute;
52 import com.sun.xacml.attr.StringAttribute;
53 import com.sun.xacml.attr.TimeAttribute;
54 import com.sun.xacml.attr.YearMonthDurationAttribute;
55 import com.sun.xacml.attr.X500NameAttribute;
56
57 import java.util.Collections;
58 import java.util.HashMap;
59 import java.util.HashSet;
60 import java.util.List;
61 import java.util.Set;
62
63
64 /**
65 * A class that implements all the *-equal functions. It takes two operands
66 * of the appropriate type and returns a <code>BooleanAttribute</code>
67 * indicating whether both of the operands are equal. If either of the
68 * operands is indeterminate, an indeterminate result is returned.
69 *
70 * @since 1.0
71 * @author Steve Hanna
72 * @author Seth Proctor
73 */
74 public class EqualFunction extends FunctionBase
75 {
76
77 /**
78 * Standard identifier for the string-equal function.
79 */
80 public static final String NAME_STRING_EQUAL =
81 FUNCTION_NS + "string-equal";
82
83 /**
84 * Standard identifier for the boolean-equal function.
85 */
86 public static final String NAME_BOOLEAN_EQUAL =
87 FUNCTION_NS + "boolean-equal";
88
89 /**
90 * Standard identifier for the integer-equal function.
91 */
92 public static final String NAME_INTEGER_EQUAL =
93 FUNCTION_NS + "integer-equal";
94
95 /**
96 * Standard identifier for the double-equal function.
97 */
98 public static final String NAME_DOUBLE_EQUAL =
99 FUNCTION_NS + "double-equal";
100
101 /**
102 * Standard identifier for the date-equal function.
103 */
104 public static final String NAME_DATE_EQUAL =
105 FUNCTION_NS + "date-equal";
106
107 /**
108 * Standard identifier for the time-equal function.
109 */
110 public static final String NAME_TIME_EQUAL =
111 FUNCTION_NS + "time-equal";
112
113 /**
114 * Standard identifier for the dateTime-equal function.
115 */
116 public static final String NAME_DATETIME_EQUAL =
117 FUNCTION_NS + "dateTime-equal";
118
119 /**
120 * Standard identifier for the dayTimeDuration-equal function.
121 */
122 public static final String NAME_DAYTIME_DURATION_EQUAL =
123 FUNCTION_NS + "dayTimeDuration-equal";
124
125 /**
126 * Standard identifier for the yearMonthDuration-equal function.
127 */
128 public static final String NAME_YEARMONTH_DURATION_EQUAL =
129 FUNCTION_NS + "yearMonthDuration-equal";
130
131 /**
132 * Standard identifier for the anyURI-equal function.
133 */
134 public static final String NAME_ANYURI_EQUAL =
135 FUNCTION_NS + "anyURI-equal";
136
137 /**
138 * Standard identifier for the x500Name-equal function.
139 */
140 public static final String NAME_X500NAME_EQUAL =
141 FUNCTION_NS + "x500Name-equal";
142
143 /**
144 * Standard identifier for the rfc822Name-equal function.
145 */
146 public static final String NAME_RFC822NAME_EQUAL =
147 FUNCTION_NS + "rfc822Name-equal";
148
149 /**
150 * Standard identifier for the hexBinary-equal function.
151 */
152 public static final String NAME_HEXBINARY_EQUAL =
153 FUNCTION_NS + "hexBinary-equal";
154
155 /**
156 * Standard identifier for the base64Binary-equal function.
157 */
158 public static final String NAME_BASE64BINARY_EQUAL =
159 FUNCTION_NS + "base64Binary-equal";
160
161 // private mapping of standard functions to their argument types
162 private static HashMap typeMap;
163
164 /**
165 * Static initializer sets up a map of standard function names to their
166 * associated datatypes
167 */
168 static {
169 typeMap = new HashMap();
170
171 typeMap.put(NAME_STRING_EQUAL, StringAttribute.identifier);
172 typeMap.put(NAME_BOOLEAN_EQUAL, BooleanAttribute.identifier);
173 typeMap.put(NAME_INTEGER_EQUAL, IntegerAttribute.identifier);
174 typeMap.put(NAME_DOUBLE_EQUAL, DoubleAttribute.identifier);
175 typeMap.put(NAME_DATE_EQUAL, DateAttribute.identifier);
176 typeMap.put(NAME_TIME_EQUAL, TimeAttribute.identifier);
177 typeMap.put(NAME_DATETIME_EQUAL, DateTimeAttribute.identifier);
178 typeMap.put(NAME_DAYTIME_DURATION_EQUAL,
179 DayTimeDurationAttribute.identifier);
180 typeMap.put(NAME_YEARMONTH_DURATION_EQUAL,
181 YearMonthDurationAttribute.identifier);
182 typeMap.put(NAME_ANYURI_EQUAL, AnyURIAttribute.identifier);
183 typeMap.put(NAME_X500NAME_EQUAL, X500NameAttribute.identifier);
184 typeMap.put(NAME_RFC822NAME_EQUAL, RFC822NameAttribute.identifier);
185 typeMap.put(NAME_HEXBINARY_EQUAL, HexBinaryAttribute.identifier);
186 typeMap.put(NAME_BASE64BINARY_EQUAL, Base64BinaryAttribute.identifier);
187 }
188
189 /**
190 * Returns an <code>EqualFunction</code> that provides the type-equal
191 * functionality over the given attribute type. This should be used to
192 * create new function instances for any new attribute types, and the
193 * resulting object should be put into the <code>FunctionFactory</code>
194 * (instances for the standard types are pre-installed in the standard
195 * factory).
196 * <p>
197 * Note that this method has the same affect as invoking the constructor
198 * with the same parameters. This method is provided as a convenience,
199 * and for symmetry with the bag and set functions.
200 *
201 * @param functionName the name to use for the function
202 * @param argumentType the type to operate on
203 *
204 * @return a new <code>EqualFunction</code>
205 */
206 public static EqualFunction getEqualInstance(String functionName,
207 String argumentType) {
208 return new EqualFunction(functionName, argumentType);
209 }
210
211 /**
212 * Creates a new <code>EqualFunction</code> object that supports one
213 * of the standard type-equal functions. If you need to create an
214 * instance for a custom type, use the <code>getEqualInstance</code>
215 * method or the alternate constructor.
216 *
217 * @param functionName the standard XACML name of the function to be
218 * handled by this object, including the full namespace
219 *
220 * @throws IllegalArgumentException if the function isn't standard
221 */
222 public EqualFunction(String functionName) {
223 this(functionName, getArgumentType(functionName));
224 }
225
226 /**
227 * Creates a new <code>EqualFunction</code> object.
228 *
229 * @param functionName the standard XACML name of the function to be
230 * handled by this object, including the full namespace
231 * @param argumentType the standard XACML name for the type of
232 * the arguments, inlcuding the full namespace
233 */
234 public EqualFunction(String functionName, String argumentType) {
235 super(functionName, 0, argumentType, false, 2,
236 BooleanAttribute.identifier, false);
237 }
238
239 /**
240 * Private helper that returns the type used for the given standard
241 * type-equal function.
242 */
243 private static String getArgumentType(String functionName) {
244 String datatype = (String)(typeMap.get(functionName));
245
246 if (datatype == null)
247 throw new IllegalArgumentException("not a standard function: " +
248 functionName);
249
250 return datatype;
251 }
252
253 /**
254 * Returns a <code>Set</code> containing all the function identifiers
255 * supported by this class.
256 *
257 * @return a <code>Set</code> of <code>String</code>s
258 */
259 public static Set getSupportedIdentifiers() {
260 return Collections.unmodifiableSet(typeMap.keySet());
261 }
262
263 /**
264 * Evaluate the function, using the specified parameters.
265 *
266 * @param inputs a <code>List</code> of <code>Evaluatable</code>
267 * objects representing the arguments passed to the function
268 * @param context an <code>EvaluationCtx</code> so that the
269 * <code>Evaluatable</code> objects can be evaluated
270 * @return an <code>EvaluationResult</code> representing the
271 * function's result
272 */
273 public EvaluationResult evaluate(List inputs, EvaluationCtx context) {
274
275 // Evaluate the arguments
276 AttributeValue [] argValues = new AttributeValue[inputs.size()];
277 EvaluationResult result = evalArgs(inputs, context, argValues);
278 if (result != null)
279 return result;
280
281 // Now that we have real values, perform the equals operation
282 return EvaluationResult.getInstance(argValues[0].equals(argValues[1]));
283 }
284
285 }