1 /*
2 * Copyright 2008 Members of the EGEE Collaboration.
3 * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.opensaml.xacml.ctx.provider;
19
20 import org.opensaml.xacml.policy.ObligationType;
21 import org.opensaml.xml.util.DatatypeHelper;
22
23 /**
24 * Base class for all obligation handlers.
25 *
26 * Handlers are executed in order of precedence. Handlers with a higher precedence are executed before those with a
27 * lower precedence. Handlers with the same precedence are executed in random order.
28 *
29 * Obligation handlers <strong>must</strong> be stateless.
30 */
31 public abstract class BaseObligationHandler {
32
33 /** ID of the handled obligation. */
34 private String id;
35
36 /** Precedence of this handler. */
37 private int precedence;
38
39 /**
40 * Constructor. Obligation has the lowest precedence
41 *
42 * @param obligationId ID of the handled obligation
43 */
44 protected BaseObligationHandler(String obligationId) {
45 this(obligationId, Integer.MIN_VALUE);
46 }
47
48 /**
49 * Constructor.
50 *
51 * @param obligationId ID of the handled obligation
52 * @param handlerPrecedence precedence of this handler
53 */
54 protected BaseObligationHandler(String obligationId, int handlerPrecedence) {
55 id = DatatypeHelper.safeTrimOrNullString(obligationId);
56 if (id == null) {
57 throw new IllegalArgumentException("Provided obligation ID may not be null or empty");
58 }
59
60 precedence = handlerPrecedence;
61 }
62
63 /**
64 * Gets the ID of the handled obligation.
65 *
66 * @return ID of the handled obligation
67 */
68 public String getObligationId() {
69 return id;
70 }
71
72 /**
73 * Gets the precedence of the handler.
74 *
75 * @return precedence of the handler
76 */
77 public int getHandlerPrecedence() {
78 return precedence;
79 }
80
81 /**
82 * Evaluates the obligation represented by this handler.
83 *
84 * @param context current processing context
85 * @param obligation the obligation as returned by the PDP
86 *
87 * @throws ObligationProcessingException thrown if there is a problem evaluating this handler
88 */
89 public abstract void evaluateObligation(ObligationProcessingContext context, ObligationType obligation)
90 throws ObligationProcessingException;
91
92 /** {@inheritDoc} */
93 public int hashCode() {
94 return getObligationId().hashCode();
95 }
96
97 /** {@inheritDoc} */
98 public boolean equals(Object obj) {
99 if (obj == this) {
100 return true;
101 }
102
103 if (obj instanceof BaseObligationHandler) {
104 return DatatypeHelper.safeEquals(getObligationId(), ((BaseObligationHandler) obj).getObligationId());
105 }
106
107 return false;
108 }
109 }