[ all classes ]
[ com.acciente.oacc ]
Coverage Summary for Class: NotAuthorizedException (com.acciente.oacc)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
NotAuthorizedException | 100% (1/ 1) | 90% (18/ 20) | 79.3% (23/ 29) |
1 /*
2 * Copyright 2009-2018, Acciente LLC
3 *
4 * Acciente LLC licenses this file to you under the
5 * Apache License, Version 2.0 (the "License"); you
6 * may not use this file except in compliance with the
7 * License. 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
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
14 * OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License.
17 */
18 package com.acciente.oacc;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Set;
24
25 public class NotAuthorizedException extends AuthorizationException {
26 private static final long serialVersionUID = 1L;
27
28 public NotAuthorizedException(String message) {
29 super(message);
30 }
31
32 public NotAuthorizedException(String message, Throwable cause) {
33 super(message, cause);
34 }
35
36 public NotAuthorizedException(Throwable cause) {
37 super(cause);
38 }
39
40 // custom static factory methods to help craft uniform error messages
41 public static NotAuthorizedException newInstanceForAction(Resource accessorResource,
42 String action) {
43 return new NotAuthorizedException("Resource "
44 + String.valueOf(accessorResource)
45 + " is not authorized to "
46 + action);
47 }
48
49 public static NotAuthorizedException newInstanceForActionOnResource(Resource accessorResource,
50 String action,
51 Resource accessedResource) {
52 return new NotAuthorizedException("Resource "
53 + String.valueOf(accessorResource)
54 + " is not authorized to "
55 + action
56 + " resource "
57 + String.valueOf(accessedResource));
58 }
59
60 public static NotAuthorizedException newInstanceForDomainCreatePermissions(Resource accessorResource,
61 Set<DomainCreatePermission> domainCreatePermissions) {
62 return new NotAuthorizedException("Resource "
63 + String.valueOf(accessorResource)
64 + " does not have domain create permission(s) "
65 + String.valueOf(domainCreatePermissions));
66 }
67
68 public static NotAuthorizedException newInstanceForDomainCreatePermissions(Resource accessorResource,
69 DomainCreatePermission domainCreatePermission,
70 DomainCreatePermission... domainCreatePermissions) {
71 return new NotAuthorizedException("Resource "
72 + String.valueOf(accessorResource)
73 + " does not have domain create permission(s) "
74 + toString(domainCreatePermission, domainCreatePermissions));
75 }
76
77 public static NotAuthorizedException newInstanceForDomainPermissions(Resource accessorResource,
78 String domainName,
79 Set<DomainPermission> domainPermissions) {
80 return new NotAuthorizedException("Resource "
81 + String.valueOf(accessorResource)
82 + " does not have domain permission(s) "
83 + String.valueOf(domainPermissions)
84 + " on domain "
85 + domainName);
86 }
87
88 public static NotAuthorizedException newInstanceForDomainPermissions(Resource accessorResource,
89 String domainName,
90 DomainPermission domainPermission,
91 DomainPermission... domainPermissions) {
92 return new NotAuthorizedException("Resource "
93 + String.valueOf(accessorResource)
94 + " does not have domain permission(s) "
95 + toString(domainPermission, domainPermissions)
96 + " on domain "
97 + domainName);
98 }
99
100 public static NotAuthorizedException newInstanceForPostCreateDomainPermissions(Resource accessorResource,
101 Set<DomainPermission> domainPermissions) {
102 return new NotAuthorizedException("Resource "
103 + String.valueOf(accessorResource)
104 + " is not authorized to receive "
105 + String.valueOf(domainPermissions)
106 + " domain permission(s) after creating a domain");
107 }
108
109 public static NotAuthorizedException newInstanceForPostCreateDomainPermissions(Resource accessorResource,
110 DomainPermission domainPermission,
111 DomainPermission... domainPermissions) {
112 return new NotAuthorizedException("Resource "
113 + String.valueOf(accessorResource)
114 + " is not authorized to receive "
115 + toString(domainPermission, domainPermissions)
116 + " domain permission(s) after creating a domain");
117 }
118
119 public static NotAuthorizedException newInstanceForResourceCreatePermissions(Resource accessorResource,
120 Set<ResourceCreatePermission> resourceCreatePermissions) {
121 return new NotAuthorizedException("Resource "
122 + String.valueOf(accessorResource)
123 + " does not have resource create permission(s) "
124 + String.valueOf(resourceCreatePermissions));
125 }
126
127 public static NotAuthorizedException newInstanceForResourceCreatePermissions(Resource accessorResource,
128 ResourceCreatePermission resourceCreatePermission,
129 ResourceCreatePermission... resourceCreatePermissions) {
130 return new NotAuthorizedException("Resource "
131 + String.valueOf(accessorResource)
132 + " does not have resource create permission(s) "
133 + toString(resourceCreatePermission, resourceCreatePermissions));
134 }
135
136 public static NotAuthorizedException newInstanceForResourcePermissions(Resource accessorResource,
137 Resource accessedResource,
138 Set<ResourcePermission> resourcePermissions) {
139 return new NotAuthorizedException("Resource "
140 + String.valueOf(accessorResource)
141 + " does not have permission(s) "
142 + String.valueOf(resourcePermissions)
143 + " on resource "
144 + String.valueOf(accessedResource));
145 }
146
147 public static NotAuthorizedException newInstanceForResourcePermissions(Resource accessorResource,
148 Resource accessedResource,
149 ResourcePermission resourcePermission,
150 ResourcePermission... resourcePermissions) {
151 return new NotAuthorizedException("Resource "
152 + String.valueOf(accessorResource)
153 + " does not have permission(s) "
154 + toString(resourcePermission, resourcePermissions)
155 + " on resource "
156 + String.valueOf(accessedResource));
157 }
158
159 public static NotAuthorizedException newInstanceForGlobalResourcePermissions(Resource accessorResource,
160 String resourceClassName,
161 String domainName,
162 Set<ResourcePermission> resourcePermissions) {
163 return new NotAuthorizedException("Resource "
164 + String.valueOf(accessorResource)
165 + " does not have global permission(s) "
166 + String.valueOf(resourcePermissions)
167 + " on resources of class "
168 + resourceClassName
169 + " in domain "
170 + domainName);
171 }
172
173 public static NotAuthorizedException newInstanceForGlobalResourcePermissions(Resource accessorResource,
174 String resourceClassName,
175 String domainName,
176 ResourcePermission resourcePermission,
177 ResourcePermission... resourcePermissions) {
178 return new NotAuthorizedException("Resource "
179 + String.valueOf(accessorResource)
180 + " does not have global permission(s) "
181 + toString(resourcePermission, resourcePermissions)
182 + " on resources of class "
183 + resourceClassName
184 + " in domain "
185 + domainName);
186 }
187
188 public static NotAuthorizedException newInstanceForPostCreateResourcePermissions(Resource accessorResource,
189 String resourceClassName,
190 String domainName,
191 Set<ResourcePermission> resourcePermissions) {
192 return new NotAuthorizedException(accessorResource
193 + "receive "
194 + String.valueOf(resourcePermissions)
195 + " permission(s) after creating a "
196 + resourceClassName
197 + " resource in domain "
198 + domainName);
199 }
200
201 public static NotAuthorizedException newInstanceForPostCreateResourcePermissions(Resource accessorResource,
202 String resourceClassName,
203 String domainName,
204 ResourcePermission resourcePermission,
205 ResourcePermission... resourcePermissions) {
206 return new NotAuthorizedException(accessorResource
207 + "receive "
208 + toString(resourcePermission, resourcePermissions)
209 + " permission(s) after creating a "
210 + resourceClassName
211 + " resource in domain "
212 + domainName);
213 }
214
215
216 /**
217 * Returns a String representation of the specified vararg sequence with a mandatory first element.
218 *
219 * <pre><code>
220 * first | others | result*
221 * -------|-----------|--------
222 * null | [] | [null]
223 * null | null | [null, null]
224 * a | [] | [a]
225 * a | null | [a, null]
226 * a | [b, a] | [a, b, a]
227 * a | [b, null] | [a, b, null]
228 * </code></pre>
229 * (*) the returned String representation will not guarantee any order of elements and will not de-duplicate
230 */
231 @SafeVarargs
232 private static <T> String toString(T first, T... others) {
233 List<T> resultList;
234
235 if (others == null) {
236 resultList = new ArrayList<>(2);
237 resultList.add(null);
238 }
239 else {
240 resultList = new ArrayList<>(others.length + 1);
241 Collections.addAll(resultList, others);
242 }
243
244 resultList.add(first);
245
246 return resultList.toString();
247 }
248 }