1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.builder; |
11 | |
|
12 | |
import java.util.ArrayList; |
13 | |
import java.util.List; |
14 | |
|
15 | |
import org.openpermis.AuthorizationService; |
16 | |
import org.openpermis.PolicyDecisionPoint; |
17 | |
import org.openpermis.audit.AuditPolicyDecisionPoint; |
18 | |
import org.openpermis.audit.VetoableAccessDecisionListener; |
19 | |
import org.openpermis.basic.BasicAuthorizationService; |
20 | |
import org.openpermis.basic.Clock; |
21 | |
import org.openpermis.policy.PartFactory; |
22 | |
import org.openpermis.policy.bean.basic.BasicPartBeanFactory; |
23 | |
import org.openpermis.repository.SubjectRepository; |
24 | |
import org.openpermis.repository.basic.InternalSubjectRepository; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class AuthorizationServiceBuilder { |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
private PartFactory partFactory; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
private PolicyDecisionPoint policyDecisionPoint; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
private SubjectRepository subjectRepository; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
private Clock clock; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
private final List<VetoableAccessDecisionListener> listeners; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 7 | public AuthorizationServiceBuilder () { |
69 | 7 | this.listeners = new ArrayList<VetoableAccessDecisionListener>(); |
70 | 7 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public AuthorizationServiceBuilder withListener (VetoableAccessDecisionListener listener) { |
81 | 1 | this.listeners.add(listener); |
82 | 1 | return this; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public AuthorizationServiceBuilder withSubjectsFrom (SubjectRepository repository) { |
92 | 2 | if (repository == null) { |
93 | 1 | throw new IllegalArgumentException("Repository must not be null."); |
94 | |
} |
95 | 1 | this.subjectRepository = repository; |
96 | 1 | return this; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public AuthorizationServiceBuilder withClock (Clock clockService) { |
105 | 2 | if (clockService == null) { |
106 | 1 | throw new IllegalArgumentException("Clock must not be null."); |
107 | |
} |
108 | 1 | this.clock = clockService; |
109 | 1 | return this; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public AuthorizationServiceBuilder forPolicyDecisionPoint ( |
119 | |
PolicyDecisionPoint accessPolicyDecisionPoint |
120 | |
) { |
121 | 2 | if (accessPolicyDecisionPoint == null) { |
122 | 1 | throw new IllegalArgumentException("Access policy decision point must not be null."); |
123 | |
} |
124 | 1 | this.policyDecisionPoint = accessPolicyDecisionPoint; |
125 | 1 | return this; |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public AuthorizationService build () { |
134 | 4 | return new BasicAuthorizationService( |
135 | |
getPolicyDecisionPoint(), getSubjectRepository(), getClock() |
136 | |
); |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
private PolicyDecisionPoint getPolicyDecisionPoint () { |
144 | 4 | if (this.policyDecisionPoint == null) { |
145 | 3 | this.policyDecisionPoint = getPartFactory().createPolicy(); |
146 | |
} |
147 | 4 | if (this.listeners.isEmpty()) { |
148 | 3 | return this.policyDecisionPoint; |
149 | |
} |
150 | 1 | return new AuditPolicyDecisionPoint(this.policyDecisionPoint, this.listeners); |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
private PartFactory getPartFactory () { |
157 | 3 | if (this.partFactory == null) { |
158 | 3 | this.partFactory = new BasicPartBeanFactory(); |
159 | |
} |
160 | 3 | return this.partFactory; |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
private SubjectRepository getSubjectRepository () { |
167 | 4 | if (this.subjectRepository == null) { |
168 | 3 | this.subjectRepository = new InternalSubjectRepository(); |
169 | |
} |
170 | 4 | return this.subjectRepository; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
private Clock getClock () { |
177 | 4 | if (this.clock == null) { |
178 | 3 | return SystemClock.INSTANCE; |
179 | |
} |
180 | 1 | return this.clock; |
181 | |
} |
182 | |
|
183 | |
} |