Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.policy; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.NamespaceVersion; |
aoqi@0 | 29 | import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; |
aoqi@0 | 31 | import java.util.Arrays; |
aoqi@0 | 32 | import java.util.Collection; |
aoqi@0 | 33 | import java.util.Collections; |
aoqi@0 | 34 | import java.util.Iterator; |
aoqi@0 | 35 | import java.util.LinkedList; |
aoqi@0 | 36 | import java.util.List; |
aoqi@0 | 37 | import java.util.Set; |
aoqi@0 | 38 | import java.util.TreeSet; |
aoqi@0 | 39 | import javax.xml.namespace.QName; |
aoqi@0 | 40 | |
aoqi@0 | 41 | /** |
aoqi@0 | 42 | * A policy represents normalized policy as a wrapper of available policy alternatives represented by |
aoqi@0 | 43 | * child {@link AssertionSet AssertionSets}. |
aoqi@0 | 44 | * |
aoqi@0 | 45 | * @author Fabian Ritzmann, Marek Potociar |
aoqi@0 | 46 | */ |
aoqi@0 | 47 | public class Policy implements Iterable<AssertionSet> { |
aoqi@0 | 48 | /** |
aoqi@0 | 49 | * A string constant used in package private constructor to customize the object's toString() method output. |
aoqi@0 | 50 | */ |
aoqi@0 | 51 | private static final String POLICY_TOSTRING_NAME = "policy"; |
aoqi@0 | 52 | |
aoqi@0 | 53 | /** |
aoqi@0 | 54 | * Constant represents empty list of assertion sets. This represents the content of a 'NULL' policy - a policy with |
aoqi@0 | 55 | * no alternatives. The constant supports memory effective creation of 'NULL' policy objects. |
aoqi@0 | 56 | */ |
aoqi@0 | 57 | private static final List<AssertionSet> NULL_POLICY_ASSERTION_SETS = Collections.unmodifiableList(new LinkedList<AssertionSet>()); |
aoqi@0 | 58 | |
aoqi@0 | 59 | /** |
aoqi@0 | 60 | * Constant represents list of assertion sets with single empty assertion set. This represents the content of |
aoqi@0 | 61 | * an 'EMPTY' policy - a policy with a single empty alternative. The constant supports memory effective creation |
aoqi@0 | 62 | * of 'EMPTY' policy objects. |
aoqi@0 | 63 | */ |
aoqi@0 | 64 | private static final List<AssertionSet> EMPTY_POLICY_ASSERTION_SETS = Collections.unmodifiableList(new LinkedList<AssertionSet>(Arrays.asList(new AssertionSet[] {AssertionSet.emptyAssertionSet()}))); |
aoqi@0 | 65 | |
aoqi@0 | 66 | /** |
aoqi@0 | 67 | * Constant represents empty vocabulary of a 'NULL' or 'EMPTY' policies. The constant supports memory effective |
aoqi@0 | 68 | * creation of 'NULL' and 'EMPTY' policy objects. |
aoqi@0 | 69 | */ |
aoqi@0 | 70 | private static final Set<QName> EMPTY_VOCABULARY = Collections.unmodifiableSet(new TreeSet<QName>(PolicyUtils.Comparison.QNAME_COMPARATOR)); |
aoqi@0 | 71 | |
aoqi@0 | 72 | /** |
aoqi@0 | 73 | * Constant representation of all 'NULL' policies returned by createNullPolicy() factory method. This is to optimize |
aoqi@0 | 74 | * the memory footprint. |
aoqi@0 | 75 | */ |
aoqi@0 | 76 | private static final Policy ANONYMOUS_NULL_POLICY = new Policy(null, null, NULL_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); |
aoqi@0 | 77 | |
aoqi@0 | 78 | /** |
aoqi@0 | 79 | * Constant representation of all 'EMPTY' policies returned by createEmptyPolicy() factory method. This constant is |
aoqi@0 | 80 | * to optimize the memory footprint. |
aoqi@0 | 81 | */ |
aoqi@0 | 82 | private static final Policy ANONYMOUS_EMPTY_POLICY = new Policy(null, null, EMPTY_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); |
aoqi@0 | 83 | |
aoqi@0 | 84 | /** |
aoqi@0 | 85 | * Policy ID holder |
aoqi@0 | 86 | */ |
aoqi@0 | 87 | private String policyId; |
aoqi@0 | 88 | |
aoqi@0 | 89 | /** |
aoqi@0 | 90 | * Policy name holder |
aoqi@0 | 91 | */ |
aoqi@0 | 92 | private String name; |
aoqi@0 | 93 | |
aoqi@0 | 94 | /** |
aoqi@0 | 95 | * Namespace version holder |
aoqi@0 | 96 | */ |
aoqi@0 | 97 | private NamespaceVersion nsVersion; |
aoqi@0 | 98 | |
aoqi@0 | 99 | /** |
aoqi@0 | 100 | * internal collection of policy alternatives |
aoqi@0 | 101 | */ |
aoqi@0 | 102 | private final List<AssertionSet> assertionSets; |
aoqi@0 | 103 | |
aoqi@0 | 104 | /** |
aoqi@0 | 105 | * internal collection of policy vocabulary entries (qualified names of all assertion types present in the policy expression) |
aoqi@0 | 106 | */ |
aoqi@0 | 107 | private final Set<QName> vocabulary; |
aoqi@0 | 108 | |
aoqi@0 | 109 | /** |
aoqi@0 | 110 | * immutable version of policy vocabulary that is made available to clients via getter method |
aoqi@0 | 111 | */ |
aoqi@0 | 112 | private final Collection<QName> immutableVocabulary; |
aoqi@0 | 113 | |
aoqi@0 | 114 | /** |
aoqi@0 | 115 | * policy object name used in a toString() method. This ensures that Policy class children can customize |
aoqi@0 | 116 | * (via package private Policy constructors) the toString() method without having to override it. |
aoqi@0 | 117 | */ |
aoqi@0 | 118 | private final String toStringName; |
aoqi@0 | 119 | |
aoqi@0 | 120 | /** |
aoqi@0 | 121 | * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'nothing allowed'</emph> |
aoqi@0 | 122 | * policy expression. The policy is created using the latest namespace version supported. |
aoqi@0 | 123 | * |
aoqi@0 | 124 | * @return policy instance which represents a <emph>'nothing allowed'</emph> (no policy alternatives). |
aoqi@0 | 125 | */ |
aoqi@0 | 126 | public static Policy createNullPolicy() { |
aoqi@0 | 127 | return ANONYMOUS_NULL_POLICY; |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | /** |
aoqi@0 | 131 | * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'anything allowed'</emph> |
aoqi@0 | 132 | * policy expression. The policy is created using the latest namespace version supported. |
aoqi@0 | 133 | * |
aoqi@0 | 134 | * @return policy instance which represents a <emph>'anything allowed'</emph> (empty policy alternative with no plicy |
aoqi@0 | 135 | * assertions prescribed). |
aoqi@0 | 136 | */ |
aoqi@0 | 137 | public static Policy createEmptyPolicy() { |
aoqi@0 | 138 | return ANONYMOUS_EMPTY_POLICY; |
aoqi@0 | 139 | } |
aoqi@0 | 140 | |
aoqi@0 | 141 | /** |
aoqi@0 | 142 | * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'nothing allowed'</emph> |
aoqi@0 | 143 | * policy expression. The policy is created using the latest namespace version supported. |
aoqi@0 | 144 | * |
aoqi@0 | 145 | * @param name global URI of the policy. May be {@code null}. |
aoqi@0 | 146 | * @param policyId local URI of the policy. May be {@code null}. |
aoqi@0 | 147 | * @return policy instance which represents a <emph>'nothing allowed'</emph> (no policy alternatives). |
aoqi@0 | 148 | */ |
aoqi@0 | 149 | public static Policy createNullPolicy(final String name, final String policyId) { |
aoqi@0 | 150 | if (name == null && policyId == null) { |
aoqi@0 | 151 | return ANONYMOUS_NULL_POLICY; |
aoqi@0 | 152 | } else { |
aoqi@0 | 153 | return new Policy(name, policyId, NULL_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); |
aoqi@0 | 154 | } |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | /** |
aoqi@0 | 158 | * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'nothing allowed'</emph> |
aoqi@0 | 159 | * policy expression. The policy is created using the latest namespace version supported. |
aoqi@0 | 160 | * |
aoqi@0 | 161 | * @param nsVersion Policy namespace version to be used when marshalling the policy expression |
aoqi@0 | 162 | * @param name global URI of the policy. May be {@code null}. |
aoqi@0 | 163 | * @param policyId local URI of the policy. May be {@code null}. |
aoqi@0 | 164 | * @return policy instance which represents a <emph>'nothing allowed'</emph> (no policy alternatives). |
aoqi@0 | 165 | */ |
aoqi@0 | 166 | public static Policy createNullPolicy(final NamespaceVersion nsVersion, final String name, final String policyId) { |
aoqi@0 | 167 | if ((nsVersion == null || nsVersion == NamespaceVersion.getLatestVersion()) && name == null && policyId == null) { |
aoqi@0 | 168 | return ANONYMOUS_NULL_POLICY; |
aoqi@0 | 169 | } else { |
aoqi@0 | 170 | return new Policy(nsVersion, name, policyId, NULL_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); |
aoqi@0 | 171 | } |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | /** |
aoqi@0 | 175 | * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'anything allowed'</emph> |
aoqi@0 | 176 | * policy expression. The policy is created using the latest namespace version supported. |
aoqi@0 | 177 | * |
aoqi@0 | 178 | * @param name global URI of the policy. May be {@code null}. |
aoqi@0 | 179 | * @param policyId local URI of the policy. May be {@code null}. |
aoqi@0 | 180 | * |
aoqi@0 | 181 | * @return policy instance which represents a <emph>'anything allowed'</emph> (empty policy alternative with no plicy |
aoqi@0 | 182 | * assertions prescribed). |
aoqi@0 | 183 | */ |
aoqi@0 | 184 | public static Policy createEmptyPolicy(final String name, final String policyId) { |
aoqi@0 | 185 | if (name == null && policyId == null) { |
aoqi@0 | 186 | return ANONYMOUS_EMPTY_POLICY; |
aoqi@0 | 187 | } else { |
aoqi@0 | 188 | return new Policy(name, policyId, EMPTY_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); |
aoqi@0 | 189 | } |
aoqi@0 | 190 | } |
aoqi@0 | 191 | |
aoqi@0 | 192 | /** |
aoqi@0 | 193 | * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'anything allowed'</emph> |
aoqi@0 | 194 | * policy expression. The policy is created using the latest namespace version supported. |
aoqi@0 | 195 | * |
aoqi@0 | 196 | * @param nsVersion Policy namespace version to be used when marshalling the policy expression |
aoqi@0 | 197 | * @param name global URI of the policy. May be {@code null}. |
aoqi@0 | 198 | * @param policyId local URI of the policy. May be {@code null}. |
aoqi@0 | 199 | * |
aoqi@0 | 200 | * @return policy instance which represents a <emph>'anything allowed'</emph> (empty policy alternative with no plicy |
aoqi@0 | 201 | * assertions prescribed). |
aoqi@0 | 202 | */ |
aoqi@0 | 203 | public static Policy createEmptyPolicy(final NamespaceVersion nsVersion, final String name, final String policyId) { |
aoqi@0 | 204 | if ((nsVersion == null || nsVersion == NamespaceVersion.getLatestVersion()) && name == null && policyId == null) { |
aoqi@0 | 205 | return ANONYMOUS_EMPTY_POLICY; |
aoqi@0 | 206 | } else { |
aoqi@0 | 207 | return new Policy(nsVersion, name, policyId, EMPTY_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); |
aoqi@0 | 208 | } |
aoqi@0 | 209 | } |
aoqi@0 | 210 | |
aoqi@0 | 211 | /** |
aoqi@0 | 212 | * The factory method creates an <b>immutable</b> policy instance which represents a policy expression with |
aoqi@0 | 213 | * alternatives specified by {@code sets} input parameter. If the collection of policy alternatives is null or empty |
aoqi@0 | 214 | * an object representing a 'NULL' policy expression is returned. However, in such case it is better to use |
aoqi@0 | 215 | * {@link #createNullPolicy()} factory method directly. The policy is created using the latest namespace version supported. |
aoqi@0 | 216 | * |
aoqi@0 | 217 | * @param sets represents the collection of policy alternatives of the policy object created. During the creation of |
aoqi@0 | 218 | * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, |
aoqi@0 | 219 | * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. |
aoqi@0 | 220 | * |
aoqi@0 | 221 | * @return policy instance which represents the policy with given alternatives. |
aoqi@0 | 222 | */ |
aoqi@0 | 223 | public static Policy createPolicy(final Collection<AssertionSet> sets) { |
aoqi@0 | 224 | if (sets == null || sets.isEmpty()) { |
aoqi@0 | 225 | return createNullPolicy(); |
aoqi@0 | 226 | } else { |
aoqi@0 | 227 | return new Policy(POLICY_TOSTRING_NAME, sets); |
aoqi@0 | 228 | } |
aoqi@0 | 229 | } |
aoqi@0 | 230 | |
aoqi@0 | 231 | /** |
aoqi@0 | 232 | * The factory method creates an <b>immutable</b> policy instance which represents a policy expression with |
aoqi@0 | 233 | * alternatives specified by {@code sets} input parameter. If the collection of policy alternatives is null or empty |
aoqi@0 | 234 | * an object representing a 'NULL' policy expression is returned. However, in such case it is better to use |
aoqi@0 | 235 | * {@link #createNullPolicy(String, String)} factory method directly. The policy is created using the latest namespace version supported. |
aoqi@0 | 236 | * |
aoqi@0 | 237 | * @param name global URI of the policy. May be {@code null}. |
aoqi@0 | 238 | * @param policyId local URI of the policy. May be {@code null}. |
aoqi@0 | 239 | * @param sets represents the collection of policy alternatives of the policy object created. During the creation of |
aoqi@0 | 240 | * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, |
aoqi@0 | 241 | * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. |
aoqi@0 | 242 | * |
aoqi@0 | 243 | * @return policy instance which represents the policy with given alternatives. |
aoqi@0 | 244 | */ |
aoqi@0 | 245 | public static Policy createPolicy(final String name, final String policyId, final Collection<AssertionSet> sets) { |
aoqi@0 | 246 | if (sets == null || sets.isEmpty()) { |
aoqi@0 | 247 | return createNullPolicy(name, policyId); |
aoqi@0 | 248 | } else { |
aoqi@0 | 249 | return new Policy(POLICY_TOSTRING_NAME, name, policyId, sets); |
aoqi@0 | 250 | } |
aoqi@0 | 251 | } |
aoqi@0 | 252 | |
aoqi@0 | 253 | /** |
aoqi@0 | 254 | * The factory method creates an <b>immutable</b> policy instance which represents a policy expression with |
aoqi@0 | 255 | * alternatives specified by {@code sets} input parameter. If the collection of policy alternatives is null or empty |
aoqi@0 | 256 | * an object representing a 'NULL' policy expression is returned. However, in such case it is better to use |
aoqi@0 | 257 | * {@link #createNullPolicy(String, String)} factory method directly. The policy is created using the latest namespace version supported. |
aoqi@0 | 258 | * |
aoqi@0 | 259 | * @param nsVersion Policy namespace version to be used when marshalling the policy expression |
aoqi@0 | 260 | * @param name global URI of the policy. May be {@code null}. |
aoqi@0 | 261 | * @param policyId local URI of the policy. May be {@code null}. |
aoqi@0 | 262 | * @param sets represents the collection of policy alternatives of the policy object created. During the creation of |
aoqi@0 | 263 | * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, |
aoqi@0 | 264 | * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. |
aoqi@0 | 265 | * |
aoqi@0 | 266 | * @return policy instance which represents the policy with given alternatives. |
aoqi@0 | 267 | */ |
aoqi@0 | 268 | public static Policy createPolicy(NamespaceVersion nsVersion, final String name, final String policyId, final Collection<AssertionSet> sets) { |
aoqi@0 | 269 | if (sets == null || sets.isEmpty()) { |
aoqi@0 | 270 | return createNullPolicy(nsVersion, name, policyId); |
aoqi@0 | 271 | } else { |
aoqi@0 | 272 | return new Policy(nsVersion, POLICY_TOSTRING_NAME, name, policyId, sets); |
aoqi@0 | 273 | } |
aoqi@0 | 274 | } |
aoqi@0 | 275 | |
aoqi@0 | 276 | /** |
aoqi@0 | 277 | * A most flexible policy object constructor that allows private creation of policy objects and direct setting |
aoqi@0 | 278 | * of all its attributes. |
aoqi@0 | 279 | * |
aoqi@0 | 280 | * @param name global URI of the policy. May be {@code null}. |
aoqi@0 | 281 | * @param policyId local URI of the policy. May be {@code null}. |
aoqi@0 | 282 | * @param assertionSets represents the collection of policy alternatives of the policy object created. The list is directly |
aoqi@0 | 283 | * assigned to the policy object internal attribute. Subsequent manipulations on the collection must be handled with |
aoqi@0 | 284 | * care. |
aoqi@0 | 285 | * @param vocabulary represents the vocabulary of the policy object. Subsequent manipulations on the collection |
aoqi@0 | 286 | * must be handled with care. |
aoqi@0 | 287 | */ |
aoqi@0 | 288 | private Policy(final String name, final String policyId, final List<AssertionSet> assertionSets, final Set<QName> vocabulary) { |
aoqi@0 | 289 | this.nsVersion = NamespaceVersion.getLatestVersion(); |
aoqi@0 | 290 | this.toStringName = POLICY_TOSTRING_NAME; |
aoqi@0 | 291 | this.name = name; |
aoqi@0 | 292 | this.policyId = policyId; |
aoqi@0 | 293 | this.assertionSets = assertionSets; |
aoqi@0 | 294 | this.vocabulary = vocabulary; |
aoqi@0 | 295 | this.immutableVocabulary = Collections.unmodifiableCollection(this.vocabulary); |
aoqi@0 | 296 | } |
aoqi@0 | 297 | |
aoqi@0 | 298 | /** |
aoqi@0 | 299 | * Constructor that should be overridden by child implementation. The constructor allows for easy toString() output |
aoqi@0 | 300 | * customization. |
aoqi@0 | 301 | * |
aoqi@0 | 302 | * @param toStringName a general name of the object (such as 'policy' or 'nested policy') that will be used in the |
aoqi@0 | 303 | * toString() method to identify the object. |
aoqi@0 | 304 | * @param sets represents the collection of policy alternatives of the policy object created. During the creation of |
aoqi@0 | 305 | * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, |
aoqi@0 | 306 | * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. The |
aoqi@0 | 307 | * collection may be {@code null} or empty. In such case a 'NULL' policy object is constructed. |
aoqi@0 | 308 | */ |
aoqi@0 | 309 | Policy(final String toStringName, final Collection<AssertionSet> sets) { |
aoqi@0 | 310 | this.nsVersion = NamespaceVersion.getLatestVersion(); |
aoqi@0 | 311 | this.toStringName = toStringName; |
aoqi@0 | 312 | |
aoqi@0 | 313 | if (sets == null || sets.isEmpty()) { |
aoqi@0 | 314 | this.assertionSets = NULL_POLICY_ASSERTION_SETS; |
aoqi@0 | 315 | this.vocabulary = EMPTY_VOCABULARY; |
aoqi@0 | 316 | this.immutableVocabulary = EMPTY_VOCABULARY; |
aoqi@0 | 317 | } else { |
aoqi@0 | 318 | this.assertionSets = new LinkedList<AssertionSet>(); |
aoqi@0 | 319 | this.vocabulary = new TreeSet<QName>(PolicyUtils.Comparison.QNAME_COMPARATOR); |
aoqi@0 | 320 | this.immutableVocabulary = Collections.unmodifiableCollection(this.vocabulary); |
aoqi@0 | 321 | |
aoqi@0 | 322 | addAll(sets); |
aoqi@0 | 323 | } |
aoqi@0 | 324 | } |
aoqi@0 | 325 | |
aoqi@0 | 326 | /** |
aoqi@0 | 327 | * Constructor that should be overridden by child implementation. The constructor allows for easy toString() output |
aoqi@0 | 328 | * customization. |
aoqi@0 | 329 | * |
aoqi@0 | 330 | * @param toStringName a general name of the object (such as 'policy' or 'nested policy') that will be used in the |
aoqi@0 | 331 | * toString() method to identify the object. |
aoqi@0 | 332 | * @param name global URI of the policy. May be {@code null}. |
aoqi@0 | 333 | * @param policyId local URI of the policy. May be {@code null}. |
aoqi@0 | 334 | * @param sets represents the collection of policy alternatives of the policy object created. During the creation of |
aoqi@0 | 335 | * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, |
aoqi@0 | 336 | * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. The |
aoqi@0 | 337 | * collection may be {@code null} or empty. In such case a 'NULL' policy object is constructed. |
aoqi@0 | 338 | */ |
aoqi@0 | 339 | Policy(final String toStringName, final String name, final String policyId, final Collection<AssertionSet> sets) { |
aoqi@0 | 340 | this(toStringName, sets); |
aoqi@0 | 341 | this.name = name; |
aoqi@0 | 342 | this.policyId = policyId; |
aoqi@0 | 343 | } |
aoqi@0 | 344 | |
aoqi@0 | 345 | /** |
aoqi@0 | 346 | * A most flexible policy object constructor that allows private creation of policy objects and direct setting |
aoqi@0 | 347 | * of all its attributes. |
aoqi@0 | 348 | * |
aoqi@0 | 349 | * @param nsVersion Policy namespace version to be used when marshalling the policy expression |
aoqi@0 | 350 | * @param name global URI of the policy. May be {@code null}. |
aoqi@0 | 351 | * @param policyId local URI of the policy. May be {@code null}. |
aoqi@0 | 352 | * @param assertionSets represents the collection of policy alternatives of the policy object created. The list is directly |
aoqi@0 | 353 | * assigned to the policy object internal attribute. Subsequent manipulations on the collection must be handled with |
aoqi@0 | 354 | * care. |
aoqi@0 | 355 | * @param vocabulary represents the vocabulary of the policy object. Subsequent manipulations on the collection |
aoqi@0 | 356 | * must be handled with care. |
aoqi@0 | 357 | */ |
aoqi@0 | 358 | private Policy(final NamespaceVersion nsVersion, final String name, final String policyId, final List<AssertionSet> assertionSets, final Set<QName> vocabulary) { |
aoqi@0 | 359 | this.nsVersion = nsVersion; |
aoqi@0 | 360 | this.toStringName = POLICY_TOSTRING_NAME; |
aoqi@0 | 361 | this.name = name; |
aoqi@0 | 362 | this.policyId = policyId; |
aoqi@0 | 363 | this.assertionSets = assertionSets; |
aoqi@0 | 364 | this.vocabulary = vocabulary; |
aoqi@0 | 365 | this.immutableVocabulary = Collections.unmodifiableCollection(this.vocabulary); |
aoqi@0 | 366 | } |
aoqi@0 | 367 | |
aoqi@0 | 368 | /** |
aoqi@0 | 369 | * Constructor that should be overridden by child implementation. The constructor allows for easy toString() output |
aoqi@0 | 370 | * customization. |
aoqi@0 | 371 | * |
aoqi@0 | 372 | * @param nsVersion Policy namespace version to be used when marshalling the policy expression |
aoqi@0 | 373 | * @param toStringName a general name of the object (such as 'policy' or 'nested policy') that will be used in the |
aoqi@0 | 374 | * toString() method to identify the object. |
aoqi@0 | 375 | * @param sets represents the collection of policy alternatives of the policy object created. During the creation of |
aoqi@0 | 376 | * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, |
aoqi@0 | 377 | * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. The |
aoqi@0 | 378 | * collection may be {@code null} or empty. In such case a 'NULL' policy object is constructed. |
aoqi@0 | 379 | */ |
aoqi@0 | 380 | Policy(final NamespaceVersion nsVersion, final String toStringName, final Collection<AssertionSet> sets) { |
aoqi@0 | 381 | this.nsVersion = nsVersion; |
aoqi@0 | 382 | this.toStringName = toStringName; |
aoqi@0 | 383 | |
aoqi@0 | 384 | if (sets == null || sets.isEmpty()) { |
aoqi@0 | 385 | this.assertionSets = NULL_POLICY_ASSERTION_SETS; |
aoqi@0 | 386 | this.vocabulary = EMPTY_VOCABULARY; |
aoqi@0 | 387 | this.immutableVocabulary = EMPTY_VOCABULARY; |
aoqi@0 | 388 | } else { |
aoqi@0 | 389 | this.assertionSets = new LinkedList<AssertionSet>(); |
aoqi@0 | 390 | this.vocabulary = new TreeSet<QName>(PolicyUtils.Comparison.QNAME_COMPARATOR); |
aoqi@0 | 391 | this.immutableVocabulary = Collections.unmodifiableCollection(this.vocabulary); |
aoqi@0 | 392 | |
aoqi@0 | 393 | addAll(sets); |
aoqi@0 | 394 | } |
aoqi@0 | 395 | } |
aoqi@0 | 396 | |
aoqi@0 | 397 | /** |
aoqi@0 | 398 | * Constructor that should be overridden by child implementation. The constructor allows for easy toString() output |
aoqi@0 | 399 | * customization. |
aoqi@0 | 400 | * |
aoqi@0 | 401 | * @param nsVersion Policy namespace version to be used when marshalling the policy expression |
aoqi@0 | 402 | * @param toStringName a general name of the object (such as 'policy' or 'nested policy') that will be used in the |
aoqi@0 | 403 | * toString() method to identify the object. |
aoqi@0 | 404 | * @param name global URI of the policy. May be {@code null}. |
aoqi@0 | 405 | * @param policyId local URI of the policy. May be {@code null}. |
aoqi@0 | 406 | * @param sets represents the collection of policy alternatives of the policy object created. During the creation of |
aoqi@0 | 407 | * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, |
aoqi@0 | 408 | * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. The |
aoqi@0 | 409 | * collection may be {@code null} or empty. In such case a 'NULL' policy object is constructed. |
aoqi@0 | 410 | */ |
aoqi@0 | 411 | Policy(final NamespaceVersion nsVersion, final String toStringName, final String name, final String policyId, final Collection<AssertionSet> sets) { |
aoqi@0 | 412 | this(nsVersion, toStringName, sets); |
aoqi@0 | 413 | this.name = name; |
aoqi@0 | 414 | this.policyId = policyId; |
aoqi@0 | 415 | } |
aoqi@0 | 416 | |
aoqi@0 | 417 | /** |
aoqi@0 | 418 | * Adds single alternative to the internal alternatives set of the policy object. |
aoqi@0 | 419 | * |
aoqi@0 | 420 | * @param set assertion set (policy alternative) object to be added. May be {@code null}; in such case the method |
aoqi@0 | 421 | * returns false. |
aoqi@0 | 422 | * |
aoqi@0 | 423 | * @return {@code true} or {@code false} depending on whether the new alternative was added to the policy object or not. |
aoqi@0 | 424 | */ |
aoqi@0 | 425 | private boolean add(final AssertionSet set) { |
aoqi@0 | 426 | if (set == null) { |
aoqi@0 | 427 | return false; |
aoqi@0 | 428 | } |
aoqi@0 | 429 | |
aoqi@0 | 430 | if (this.assertionSets.contains(set)) { |
aoqi@0 | 431 | return false; |
aoqi@0 | 432 | } else { |
aoqi@0 | 433 | this.assertionSets.add(set); |
aoqi@0 | 434 | this.vocabulary.addAll(set.getVocabulary()); |
aoqi@0 | 435 | return true; |
aoqi@0 | 436 | } |
aoqi@0 | 437 | } |
aoqi@0 | 438 | |
aoqi@0 | 439 | /** |
aoqi@0 | 440 | * Adds all alternatives from the input collection of assertion sets to the policy object's internal set of alternatives. |
aoqi@0 | 441 | * The policy object's vocabulary structure is updated as well. |
aoqi@0 | 442 | * |
aoqi@0 | 443 | * @param sets collection of new alternatives. Must NOT be {@code null} or empty. The check for null or empty input |
aoqi@0 | 444 | * parameter is performed with help of {@code assert} keyword, thus during the testing and development of this class |
aoqi@0 | 445 | * {@code -ea} switch should be turned on for this class. |
aoqi@0 | 446 | * |
aoqi@0 | 447 | * @return {@code true} if all elements in the input collection were added to internal structure, {@code false} otherwise. |
aoqi@0 | 448 | */ |
aoqi@0 | 449 | private boolean addAll(final Collection<AssertionSet> sets) { |
aoqi@0 | 450 | assert (sets != null && !sets.isEmpty()) : LocalizationMessages.WSP_0036_PRIVATE_METHOD_DOES_NOT_ACCEPT_NULL_OR_EMPTY_COLLECTION(); |
aoqi@0 | 451 | |
aoqi@0 | 452 | boolean result = true; |
aoqi@0 | 453 | for (AssertionSet set : sets) { |
aoqi@0 | 454 | result &= add(set); // this is here to ensure that vocabulary is built correctly as well |
aoqi@0 | 455 | } |
aoqi@0 | 456 | Collections.sort(this.assertionSets); |
aoqi@0 | 457 | |
aoqi@0 | 458 | return result; |
aoqi@0 | 459 | } |
aoqi@0 | 460 | |
aoqi@0 | 461 | Collection<AssertionSet> getContent() { |
aoqi@0 | 462 | return assertionSets; |
aoqi@0 | 463 | } |
aoqi@0 | 464 | |
aoqi@0 | 465 | /** |
aoqi@0 | 466 | * Returns the policy identifier that serves as a local relative policy URI. |
aoqi@0 | 467 | * |
aoqi@0 | 468 | * @return policy identifier - a local relative policy URI. If no policy identifier is set, returns {@code null}. |
aoqi@0 | 469 | */ |
aoqi@0 | 470 | public String getId() { |
aoqi@0 | 471 | return policyId; |
aoqi@0 | 472 | } |
aoqi@0 | 473 | |
aoqi@0 | 474 | /** |
aoqi@0 | 475 | * Returns the policy name that serves as a global policy URI. |
aoqi@0 | 476 | * |
aoqi@0 | 477 | * @return policy name - a global policy URI. If no policy name is set, returns {@code null}. |
aoqi@0 | 478 | */ |
aoqi@0 | 479 | public String getName() { |
aoqi@0 | 480 | return name; |
aoqi@0 | 481 | } |
aoqi@0 | 482 | |
aoqi@0 | 483 | public NamespaceVersion getNamespaceVersion() { |
aoqi@0 | 484 | return nsVersion; |
aoqi@0 | 485 | } |
aoqi@0 | 486 | |
aoqi@0 | 487 | /** |
aoqi@0 | 488 | * Returns the policy ID or if that is null the policy name. May return null |
aoqi@0 | 489 | * if both attributes are null. |
aoqi@0 | 490 | * |
aoqi@0 | 491 | * @see #getId() |
aoqi@0 | 492 | * @see #getName() |
aoqi@0 | 493 | * @return The policy ID if it was set, or the name or null if no attribute was set. |
aoqi@0 | 494 | */ |
aoqi@0 | 495 | public String getIdOrName() { |
aoqi@0 | 496 | if (policyId != null) { |
aoqi@0 | 497 | return policyId; |
aoqi@0 | 498 | } |
aoqi@0 | 499 | return name; |
aoqi@0 | 500 | } |
aoqi@0 | 501 | |
aoqi@0 | 502 | /** |
aoqi@0 | 503 | * Method returns how many policy alternatives this policy instance contains. |
aoqi@0 | 504 | * |
aoqi@0 | 505 | * @return number of policy alternatives contained in this policy instance |
aoqi@0 | 506 | */ |
aoqi@0 | 507 | public int getNumberOfAssertionSets() { |
aoqi@0 | 508 | return assertionSets.size(); |
aoqi@0 | 509 | } |
aoqi@0 | 510 | |
aoqi@0 | 511 | /** |
aoqi@0 | 512 | * A policy usually contains one or more assertion sets. Each assertion set |
aoqi@0 | 513 | * corresponds to a policy alternative as defined by WS-Policy. |
aoqi@0 | 514 | * |
aoqi@0 | 515 | * @return An iterator to iterate through all contained assertion sets |
aoqi@0 | 516 | */ |
aoqi@0 | 517 | public Iterator<AssertionSet> iterator() { |
aoqi@0 | 518 | return assertionSets.iterator(); |
aoqi@0 | 519 | } |
aoqi@0 | 520 | |
aoqi@0 | 521 | /** |
aoqi@0 | 522 | * Returns {@code true} if the policy instance represents "nothing allowed" policy expression |
aoqi@0 | 523 | * |
aoqi@0 | 524 | * @return {@code true} if the policy instance represents "nothing allowed" policy expression, {@code false} otherwise. |
aoqi@0 | 525 | */ |
aoqi@0 | 526 | public boolean isNull() { |
aoqi@0 | 527 | return assertionSets.size() == 0; |
aoqi@0 | 528 | } |
aoqi@0 | 529 | |
aoqi@0 | 530 | /** |
aoqi@0 | 531 | * Returns {@code true} if the policy instance represents "anything allowed" policy expression |
aoqi@0 | 532 | * |
aoqi@0 | 533 | * @return {@code true} if the policy instance represents "anything allowed" policy expression, {@code false} otherwise. |
aoqi@0 | 534 | */ |
aoqi@0 | 535 | public boolean isEmpty() { |
aoqi@0 | 536 | return assertionSets.size() == 1 && assertionSets.get(0).isEmpty(); |
aoqi@0 | 537 | } |
aoqi@0 | 538 | |
aoqi@0 | 539 | /** |
aoqi@0 | 540 | * Returns true if the policy contains the assertion names with specified namespace in its vocabulary |
aoqi@0 | 541 | * |
aoqi@0 | 542 | * @param namespaceUri the assertion namespace URI (identifying assertion domain) |
aoqi@0 | 543 | * @return {@code true}, if an assertion with the given name could be found in the policy vocabulary {@code false} otherwise. |
aoqi@0 | 544 | */ |
aoqi@0 | 545 | public boolean contains(final String namespaceUri) { |
aoqi@0 | 546 | for (QName entry : vocabulary) { |
aoqi@0 | 547 | if (entry.getNamespaceURI().equals(namespaceUri)) { |
aoqi@0 | 548 | return true; |
aoqi@0 | 549 | } |
aoqi@0 | 550 | } |
aoqi@0 | 551 | |
aoqi@0 | 552 | return false; |
aoqi@0 | 553 | } |
aoqi@0 | 554 | |
aoqi@0 | 555 | /** |
aoqi@0 | 556 | * Retrieves the vocabulary of this policy expression. The vocabulary is represented by an immutable collection of |
aoqi@0 | 557 | * unique QName objects. Each of those objects represents single assertion type contained in the policy. |
aoqi@0 | 558 | * |
aoqi@0 | 559 | * @return immutable collection of assertion types contained in the policy (a policy vocabulary). |
aoqi@0 | 560 | */ |
aoqi@0 | 561 | public Collection<QName> getVocabulary() { |
aoqi@0 | 562 | return immutableVocabulary; |
aoqi@0 | 563 | } |
aoqi@0 | 564 | |
aoqi@0 | 565 | /** |
aoqi@0 | 566 | * Determines if the policy instance contains the assertion with the name specified in its vocabulary. |
aoqi@0 | 567 | * |
aoqi@0 | 568 | * @param assertionName the name of the assertion to be tested. |
aoqi@0 | 569 | * |
aoqi@0 | 570 | * @return {@code true} if the assertion with the specified name is part of the policy instance's vocabulary, |
aoqi@0 | 571 | * {@code false} otherwise. |
aoqi@0 | 572 | */ |
aoqi@0 | 573 | public boolean contains(final QName assertionName) { |
aoqi@0 | 574 | return vocabulary.contains(assertionName); |
aoqi@0 | 575 | } |
aoqi@0 | 576 | |
aoqi@0 | 577 | /** |
aoqi@0 | 578 | * An {@code Object.equals(Object obj)} method override. |
aoqi@0 | 579 | */ |
aoqi@0 | 580 | @Override |
aoqi@0 | 581 | public boolean equals(final Object obj) { |
aoqi@0 | 582 | if (this == obj) { |
aoqi@0 | 583 | return true; |
aoqi@0 | 584 | } |
aoqi@0 | 585 | |
aoqi@0 | 586 | if (!(obj instanceof Policy)) { |
aoqi@0 | 587 | return false; |
aoqi@0 | 588 | } |
aoqi@0 | 589 | |
aoqi@0 | 590 | final Policy that = (Policy) obj; |
aoqi@0 | 591 | |
aoqi@0 | 592 | boolean result = true; |
aoqi@0 | 593 | |
aoqi@0 | 594 | result = result && this.vocabulary.equals(that.vocabulary); |
aoqi@0 | 595 | result = result && this.assertionSets.size() == that.assertionSets.size() && this.assertionSets.containsAll(that.assertionSets); |
aoqi@0 | 596 | |
aoqi@0 | 597 | return result; |
aoqi@0 | 598 | } |
aoqi@0 | 599 | |
aoqi@0 | 600 | /** |
aoqi@0 | 601 | * An {@code Object.hashCode()} method override. |
aoqi@0 | 602 | */ |
aoqi@0 | 603 | @Override |
aoqi@0 | 604 | public int hashCode() { |
aoqi@0 | 605 | int result = 17; |
aoqi@0 | 606 | |
aoqi@0 | 607 | result = 37 * result + vocabulary.hashCode(); |
aoqi@0 | 608 | result = 37 * result + assertionSets.hashCode(); |
aoqi@0 | 609 | |
aoqi@0 | 610 | return result; |
aoqi@0 | 611 | } |
aoqi@0 | 612 | |
aoqi@0 | 613 | /** |
aoqi@0 | 614 | * An {@code Object.toString()} method override. |
aoqi@0 | 615 | */ |
aoqi@0 | 616 | @Override |
aoqi@0 | 617 | public String toString() { |
aoqi@0 | 618 | return toString(0, new StringBuffer()).toString(); |
aoqi@0 | 619 | } |
aoqi@0 | 620 | |
aoqi@0 | 621 | /** |
aoqi@0 | 622 | * A helper method that appends indented string representation of this instance to the input string buffer. |
aoqi@0 | 623 | * |
aoqi@0 | 624 | * @param indentLevel indentation level to be used. |
aoqi@0 | 625 | * @param buffer buffer to be used for appending string representation of this instance |
aoqi@0 | 626 | * @return modified buffer containing new string representation of the instance |
aoqi@0 | 627 | */ |
aoqi@0 | 628 | StringBuffer toString(final int indentLevel, final StringBuffer buffer) { |
aoqi@0 | 629 | final String indent = PolicyUtils.Text.createIndent(indentLevel); |
aoqi@0 | 630 | final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1); |
aoqi@0 | 631 | final String innerDoubleIndent = PolicyUtils.Text.createIndent(indentLevel + 2); |
aoqi@0 | 632 | |
aoqi@0 | 633 | buffer.append(indent).append(toStringName).append(" {").append(PolicyUtils.Text.NEW_LINE); |
aoqi@0 | 634 | buffer.append(innerIndent).append("namespace version = '").append(nsVersion.name()).append('\'').append(PolicyUtils.Text.NEW_LINE); |
aoqi@0 | 635 | buffer.append(innerIndent).append("id = '").append(policyId).append('\'').append(PolicyUtils.Text.NEW_LINE); |
aoqi@0 | 636 | buffer.append(innerIndent).append("name = '").append(name).append('\'').append(PolicyUtils.Text.NEW_LINE); |
aoqi@0 | 637 | |
aoqi@0 | 638 | buffer.append(innerIndent).append("vocabulary {").append(PolicyUtils.Text.NEW_LINE); |
aoqi@0 | 639 | if (vocabulary.isEmpty()) { |
aoqi@0 | 640 | buffer.append(innerDoubleIndent).append("no entries").append(PolicyUtils.Text.NEW_LINE); |
aoqi@0 | 641 | } else { |
aoqi@0 | 642 | int index = 1; |
aoqi@0 | 643 | for (QName entry : vocabulary) { |
aoqi@0 | 644 | buffer.append(innerDoubleIndent).append(index++).append(". entry = '").append(entry.getNamespaceURI()).append(':').append(entry.getLocalPart()).append('\'').append(PolicyUtils.Text.NEW_LINE); |
aoqi@0 | 645 | } |
aoqi@0 | 646 | } |
aoqi@0 | 647 | buffer.append(innerIndent).append('}').append(PolicyUtils.Text.NEW_LINE); |
aoqi@0 | 648 | |
aoqi@0 | 649 | if (assertionSets.isEmpty()) { |
aoqi@0 | 650 | buffer.append(innerIndent).append("no assertion sets").append(PolicyUtils.Text.NEW_LINE); |
aoqi@0 | 651 | } else { |
aoqi@0 | 652 | for (AssertionSet set : assertionSets) { |
aoqi@0 | 653 | set.toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE); |
aoqi@0 | 654 | } |
aoqi@0 | 655 | } |
aoqi@0 | 656 | |
aoqi@0 | 657 | buffer.append(indent).append('}'); |
aoqi@0 | 658 | |
aoqi@0 | 659 | return buffer; |
aoqi@0 | 660 | } |
aoqi@0 | 661 | } |