|
13 | 13 | import org.apache.nifi.authorization.AuthorizerConfigurationContext; |
14 | 14 | import org.apache.nifi.authorization.exception.AuthorizationAccessException; |
15 | 15 | import org.apache.nifi.authorization.exception.AuthorizerCreationException; |
16 | | -import org.apache.nifi.components.PropertyValue; |
17 | 16 | import org.slf4j.Logger; |
18 | 17 | import org.slf4j.LoggerFactory; |
19 | 18 |
|
20 | 19 | public class RequestCache { |
21 | | - |
| 20 | + |
22 | 21 | private static final Logger logger = LoggerFactory.getLogger(RequestCache.class); |
23 | 22 |
|
24 | 23 | private Cache<RequestKey, AuthorizationResult> cache; |
25 | | - |
26 | | - private final String PROP_CACHE_TIME_SECS = "CACHE_TIME_SECS"; |
27 | | - private int CACHE_TIME_SECS = 30; |
28 | | - |
29 | | - private final String PROP_CACHE_MAX_ENTRY_COUNT = "CACHE_MAX_ENTRY_COUNT"; |
30 | | - private int CACHE_MAX_ENTRY_COUNT = 0; |
31 | | - |
| 24 | + |
| 25 | + private final String CACHE_TIME_SECS_PROPNAME = "CACHE_TIME_SECS"; |
| 26 | + private int CACHE_TIME_SECS; |
| 27 | + private final String CACHE_TIME_SECS_DEFAULT = "30"; |
| 28 | + |
| 29 | + private final String CACHE_MAX_ENTRY_COUNT_PROPNAME = "CACHE_MAX_ENTRY_COUNT"; |
| 30 | + private int CACHE_MAX_ENTRY_COUNT; |
| 31 | + private final String CACHE_MAX_ENTRY_COUNT_DEFAULT = "0"; |
| 32 | + |
32 | 33 | void putCachedResult(AuthorizationRequest request, AuthorizationResult result) throws AuthorizationAccessException { |
33 | | - if(request == null | result == null) |
| 34 | + if (request == null | result == null) |
34 | 35 | return; |
35 | | - |
| 36 | + |
36 | 37 | cache.put(new RequestKey(request), result); |
37 | 38 | } |
38 | | - |
| 39 | + |
39 | 40 | AuthorizationResult getCachedResult(AuthorizationRequest request) throws AuthorizationAccessException { |
40 | 41 | return cache.getIfPresent(new RequestKey(request)); |
41 | 42 | } |
42 | | - |
| 43 | + |
43 | 44 | void clear() { |
44 | 45 | cache.invalidateAll(); |
45 | 46 | } |
46 | 47 |
|
47 | 48 | public void initialize(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException { |
48 | 49 | initializePropertys(configurationContext); |
49 | | - |
| 50 | + |
50 | 51 | cache = Caffeine.newBuilder() |
51 | 52 | .expireAfterWrite(CACHE_TIME_SECS, TimeUnit.SECONDS) |
52 | 53 | .maximumSize(CACHE_MAX_ENTRY_COUNT) |
53 | 54 | .build(); |
54 | 55 | } |
55 | | - |
| 56 | + |
56 | 57 | void initializePropertys(AuthorizerConfigurationContext configurationContext) { |
57 | 58 | try { |
58 | 59 | /* Initialize property of maximum cache time after write */ |
59 | | - PropertyValue cacheTimeProp = configurationContext.getProperty(PROP_CACHE_TIME_SECS); |
60 | | - if(cacheTimeProp.isSet()) |
61 | | - CACHE_TIME_SECS = cacheTimeProp.asInteger(); |
62 | | - |
63 | | - }catch(NumberFormatException nfe) { |
64 | | - String message = MessageFormat.format("An error occured while trying to initialze the Cache property {0}: {1}", PROP_CACHE_TIME_SECS, nfe.getStackTrace().toString()); |
65 | | - logger.error(message); |
66 | | - throw new AuthorizerCreationException(message); |
67 | | - } |
68 | | - |
69 | | - /* Initialize property of maximum entries in the cache at one time*/ |
70 | | - try { |
71 | | - PropertyValue cacheMaxEntryProp = configurationContext.getProperty(PROP_CACHE_MAX_ENTRY_COUNT); |
72 | | - if(cacheMaxEntryProp.isSet()) |
73 | | - CACHE_MAX_ENTRY_COUNT = cacheMaxEntryProp.asInteger(); |
74 | | - |
75 | | - }catch(NumberFormatException nfe) { |
76 | | - String message = MessageFormat.format("An error occured while trying to initialze the Cache property {0}: {1}", PROP_CACHE_MAX_ENTRY_COUNT, nfe.getStackTrace().toString()); |
77 | | - logger.error(message); |
78 | | - throw new AuthorizerCreationException(message); |
| 60 | + String cacheTimeProp = ConfigLoader.getProperty(CACHE_TIME_SECS_PROPNAME, CACHE_TIME_SECS_DEFAULT); |
| 61 | + if (cacheTimeProp != null) |
| 62 | + CACHE_TIME_SECS = Integer.parseInt(cacheTimeProp); |
| 63 | + } catch (NumberFormatException nfe) { |
| 64 | + String message = MessageFormat.format( |
| 65 | + "An error occured while trying to initialze the Cache property {0}: {1}", CACHE_TIME_SECS_PROPNAME, |
| 66 | + nfe.getStackTrace().toString()); |
| 67 | + logger.error(message); |
| 68 | + throw new AuthorizerCreationException(message); |
| 69 | + } |
| 70 | + |
| 71 | + /* Initialize property of maximum entries in the cache at one time */ |
| 72 | + try { |
| 73 | + String cacheMaxEntryProp = ConfigLoader.getProperty(CACHE_MAX_ENTRY_COUNT_PROPNAME, CACHE_MAX_ENTRY_COUNT_DEFAULT); |
| 74 | + if (cacheMaxEntryProp != null) |
| 75 | + CACHE_MAX_ENTRY_COUNT = Integer.parseInt(cacheMaxEntryProp); |
| 76 | + } catch (NumberFormatException nfe) { |
| 77 | + String message = MessageFormat.format( |
| 78 | + "An error occured while trying to initialze the Cache property {0}: {1}", |
| 79 | + CACHE_MAX_ENTRY_COUNT_PROPNAME, nfe.getStackTrace().toString()); |
| 80 | + logger.error(message); |
| 81 | + throw new AuthorizerCreationException(message); |
79 | 82 | } |
80 | 83 | } |
81 | | - |
| 84 | + |
82 | 85 | class RequestKey { |
83 | | - |
| 86 | + |
84 | 87 | private int hashCode; |
85 | | - |
| 88 | + |
86 | 89 | public RequestKey(AuthorizationRequest request) { |
87 | 90 | hashCode = Objects.hash( |
88 | | - |
89 | | - Optional.ofNullable(request.getIdentity()) |
90 | | - .map(Object::hashCode) |
91 | | - .orElse(0), |
92 | | - |
93 | | - Optional.ofNullable(request.getGroups()) |
94 | | - .map(Object::hashCode) |
95 | | - .orElse(0), |
96 | | - |
97 | | - request.getAction().toString().hashCode(), |
98 | | - |
99 | | - Optional.ofNullable(request.getRequestedResource()) |
100 | | - .map(res -> res.getIdentifier() != null ? res.getIdentifier().hashCode() : 0) |
101 | | - .orElse(0), |
102 | | - |
103 | | - Optional.ofNullable(request.getResource()) |
104 | | - .map(res -> res.getIdentifier() != null ? res.getIdentifier().hashCode() : 0) |
105 | | - .orElse(0), |
106 | | - |
107 | | - Optional.ofNullable(request.getResourceContext()) |
108 | | - .map(Object::hashCode) |
109 | | - .orElse(0), |
110 | | - |
111 | | - Optional.ofNullable(request.getUserContext()) |
112 | | - .map(Object::hashCode) |
113 | | - .orElse(0) |
114 | | - ); |
| 91 | + |
| 92 | + Optional.ofNullable(request.getIdentity()) |
| 93 | + .map(Object::hashCode) |
| 94 | + .orElse(0), |
| 95 | + |
| 96 | + Optional.ofNullable(request.getGroups()) |
| 97 | + .map(Object::hashCode) |
| 98 | + .orElse(0), |
| 99 | + |
| 100 | + request.getAction().toString().hashCode(), |
| 101 | + |
| 102 | + Optional.ofNullable(request.getRequestedResource()) |
| 103 | + .map(res -> res.getIdentifier() != null ? res.getIdentifier().hashCode() : 0) |
| 104 | + .orElse(0), |
| 105 | + |
| 106 | + Optional.ofNullable(request.getResource()) |
| 107 | + .map(res -> res.getIdentifier() != null ? res.getIdentifier().hashCode() : 0) |
| 108 | + .orElse(0), |
| 109 | + |
| 110 | + Optional.ofNullable(request.getResourceContext()) |
| 111 | + .map(Object::hashCode) |
| 112 | + .orElse(0), |
| 113 | + |
| 114 | + Optional.ofNullable(request.getUserContext()) |
| 115 | + .map(Object::hashCode) |
| 116 | + .orElse(0)); |
115 | 117 |
|
116 | 118 | } |
117 | | - |
| 119 | + |
118 | 120 | @Override |
119 | 121 | public int hashCode() { |
120 | 122 | return this.hashCode; |
121 | 123 | } |
122 | | - |
| 124 | + |
123 | 125 | @Override |
124 | 126 | public boolean equals(Object obj) { |
125 | 127 | return obj.hashCode() == hashCode; |
|
0 commit comments