@@ -26,9 +26,9 @@ public synchronized static PriorityMap getInstance() throws InternalException {
2626 String anonUserName ;
2727 boolean anonDownloadEnabled ;
2828 private int defaultPriority ;
29- private int authenticatedPriority ;
29+ private HashMap < String , Integer > authenticatedMapping = new HashMap <>() ;
3030 private HashMap <String , Integer > userMapping = new HashMap <>();
31- private HashMap <Integer , String > mapping = new HashMap <>();
31+ private HashMap <Integer , String > queryMapping = new HashMap <>();
3232 private Logger logger = LoggerFactory .getLogger (PriorityMap .class );
3333
3434 public PriorityMap () {
@@ -40,32 +40,22 @@ public PriorityMap() {
4040 + "authenticated priority will be used as default level" );
4141 }
4242 anonDownloadEnabled = Boolean .parseBoolean (properties .getProperty ("anonDownloadEnabled" , "true" ));
43- String defaultString ;
44- if (anonDownloadEnabled ) {
45- defaultString = properties .getProperty ("queue.priority.default" , "0" );
46- defaultPriority = Integer .valueOf (defaultString );
47- } else {
48- defaultString = "0" ;
49- defaultPriority = 0 ;
50- }
43+ String defaultString = properties .getProperty ("queue.priority.default" , "0" );
44+ defaultPriority = Integer .valueOf (defaultString );
45+
5146
52- String authenticatedString = properties .getProperty ("queue.priority.authenticated" , defaultString );
53- setAuthenticatedPriority (authenticatedString );
47+ String authenticatedString = properties .getProperty ("queue.priority.authenticated" , "{}" );
48+ parseObject (authenticatedString , authenticatedMapping );
5449
5550 String userString = properties .getProperty ("queue.priority.user" , "{}" );
56- JsonReader reader = Json .createReader (new ByteArrayInputStream (userString .getBytes ()));
57- JsonObject object = reader .readObject ();
58- for (String key : object .keySet ()) {
59- int priority = object .getInt (key );
60- userMapping .put (key , priority );
61- }
51+ parseObject (userString , userMapping );
6252
6353 String property = "queue.priority.investigationUser.default" ;
64- String investigationUserString = properties .getProperty (property , authenticatedString );
54+ String investigationUserString = properties .getProperty (property , defaultString );
6555 updateMapping (Integer .valueOf (investigationUserString ), "user.investigationUsers IS NOT EMPTY" );
6656
6757 property = "queue.priority.instrumentScientist.default" ;
68- String instrumentScientistString = properties .getProperty (property , authenticatedString );
58+ String instrumentScientistString = properties .getProperty (property , defaultString );
6959 updateMapping (Integer .valueOf (instrumentScientistString ), "user.instrumentScientists IS NOT EMPTY" );
7060
7161 String investigationUserProperty = properties .getProperty ("queue.priority.investigationUser.roles" );
@@ -93,25 +83,18 @@ public void checkAnonDownloadEnabled(String userName) throws ForbiddenException
9383 }
9484
9585 /**
96- * Set the minimum priority for all authenticated Users. This cannot be lower
97- * than the defaultPriority, which will be used instead if this is the case.
86+ * Extracts a String key to priority level mapping for any criteria (user, authn).
9887 *
99- * @param authenticatedString The value read from the run.properties file
88+ * @param propertyString String representing a JsonObject from the run.properties
89+ * file, or {}
90+ * @param mapping HashMap from String key to numeric priority level
10091 */
101- private void setAuthenticatedPriority (String authenticatedString ) {
102- authenticatedPriority = Integer .valueOf (authenticatedString );
103- if (authenticatedPriority < 1 && defaultPriority >= 1 ) {
104- String msg = "queue.priority.authenticated disabled with value " + authenticatedString ;
105- msg += " but queue.priority.default enabled with value " + defaultPriority ;
106- msg += "\n Authenticated users will use default priority if no superseding priority applies" ;
107- logger .warn (msg );
108- authenticatedPriority = defaultPriority ;
109- } else if (authenticatedPriority >= 1 && authenticatedPriority > defaultPriority ) {
110- String msg = "queue.priority.authenticated enabled with value " + authenticatedString ;
111- msg += " but queue.priority.default supersedes with value " + defaultPriority ;
112- msg += "\n Authenticated users will use default priority if no superseding priority applies" ;
113- logger .warn (msg );
114- authenticatedPriority = defaultPriority ;
92+ private void parseObject (String propertyString , HashMap <String , Integer > mapping ) {
93+ JsonReader reader = Json .createReader (new ByteArrayInputStream (propertyString .getBytes ()));
94+ JsonObject object = reader .readObject ();
95+ for (String key : object .keySet ()) {
96+ int priority = object .getInt (key );
97+ mapping .put (key , priority );
11598 }
11699 }
117100
@@ -147,25 +130,25 @@ private void updateMapping(int priority, String newCondition) {
147130 if (priority < 1 ) {
148131 logger .warn ("Non-positive priority found in mapping, ignoring entry" );
149132 return ;
150- } else if (authenticatedPriority >= 1 && priority >= authenticatedPriority ) {
151- logger .warn ("Priority set in mapping would be superseded by queue.priority.authenticated , ignoring entry" );
133+ } else if (defaultPriority >= 1 && priority >= defaultPriority ) {
134+ logger .warn ("Priority set in mapping would be superseded by queue.priority.default , ignoring entry" );
152135 return ;
153136 }
154137
155- String oldCondition = mapping .get (priority );
138+ String oldCondition = queryMapping .get (priority );
156139 if (oldCondition != null ) {
157- mapping .put (priority , oldCondition + " OR " + newCondition );
140+ queryMapping .put (priority , oldCondition + " OR " + newCondition );
158141 } else {
159- mapping .put (priority , newCondition );
142+ queryMapping .put (priority , newCondition );
160143 }
161144 }
162145
163146 /**
164147 * @return Mapping of priority level to a JPQL condition which defines the Users
165148 * who have this priority
166149 */
167- public HashMap <Integer , String > getMapping () {
168- return mapping ;
150+ public HashMap <Integer , String > getQueryMapping () {
151+ return queryMapping ;
169152 }
170153
171154 /**
@@ -177,14 +160,23 @@ public Integer getUserPriority(String userName) {
177160 }
178161
179162 /**
180- * @return The priority which applies to all authenticated users
163+ * @param userName String in the format prefix/userName
164+ * @return Relevant priority if prefix present and recognised,
165+ * otherwise defaultPriority
181166 */
182- public int getAuthenticatedPriority () {
183- return authenticatedPriority ;
167+ public Integer getAuthenticatedPriority (String userName ) {
168+ int index = userName .indexOf ("/" );
169+ if (index < 0 ) {
170+ String format = "No explicit authentication mechanism for {}, using default priority {}" ;
171+ logger .debug (format , userName , defaultPriority );
172+ return defaultPriority ;
173+ }
174+ String prefix = userName .substring (0 , index );
175+ return authenticatedMapping .getOrDefault (prefix , defaultPriority );
184176 }
185177
186178 /**
187- * @return The priority which applies to all users, included anonymous access
179+ * @return The priority which applies to any user without a specific setting.
188180 */
189181 public int getDefaultPriority () {
190182 return defaultPriority ;
0 commit comments