/** Forces a reload of the rules */ public void reload() { checkPropertyFile(true); }
/** * Writes the rules back to file system * * @throws IOException */ public void storeRules() throws IOException { OutputStream os = null; try { // turn back the users into a users map Properties p = toProperties(); // write out to the data dir Resource propFile = securityDir.get(propertyFileName); os = propFile.out(); p.store(os, null); lastModified = System.currentTimeMillis(); } catch (Exception e) { if (e instanceof IOException) throw (IOException) e; else throw (IOException) new IOException("Could not write rules to " + propertyFileName) .initCause(e); } finally { if (os != null) os.close(); } }
protected void putMap(Map map) throws Exception { validateMap(map); Set<Object> nonExisting = nonExistingKeys(map); if (!nonExisting.isEmpty()) { String msg = "Unknown rules: " + StringUtils.join(nonExisting.iterator(), ","); throw new RestException(msg, HttpStatus.CONFLICT); } for (Object entry : map.entrySet()) { Comparable rule = convertEntryToRule((Entry<String, String>) entry); // TODO, will not work for REST ruleDAO.removeRule(rule); ruleDAO.addRule(rule); } ruleDAO.storeRules(); }
@DeleteMapping(path = "/**") public void rulesDelete(HttpServletRequest request) throws UnsupportedEncodingException { checkUserIsAdmin(); String thePath = request.getPathInfo(); String ruleString = thePath.substring(getBasePath().length() + 1); ruleString = URLDecoder.decode(ruleString, "utf-8"); String msg = validateRuleKey(ruleString); if (msg != null) throw new RestException(msg, HttpStatus.UNPROCESSABLE_ENTITY); Comparable<?> rule = null; for (Comparable<?> ruleCandidate : ruleDAO.getRules()) { if (ruleString.equals(keyFor(ruleCandidate))) { rule = ruleCandidate; break; } } if (rule == null) { throw new ResourceNotFoundException("Rule not found: " + ruleString); } try { ruleDAO.removeRule(rule); ruleDAO.storeRules(); } catch (Exception e) { throw createRestException(e); } }
loadRules(watcher.getProperties()); } else if (isModified()) { loadRules(watcher.getProperties()); lastModified = System.currentTimeMillis();
protected void postMap(Map map) throws Exception { validateMap(map); Set<Object> commonKeys = intersection(map); if (!commonKeys.isEmpty()) { String msg = "Already existing rules: " + StringUtils.join(commonKeys.iterator(), ","); throw new RestException(msg, HttpStatus.CONFLICT); } for (Object entry : map.entrySet()) { Comparable rule = convertEntryToRule((Entry<String, String>) entry); ruleDAO.addRule(rule); } ruleDAO.storeRules(); }
/** * Calculate the the intersection of the keys * * @param map */ protected Set<Object> intersection(Map map) { Set<Object> result = new HashSet<>(); Set<Object> ruleKeys = new HashSet<>(); for (Comparable<?> rule : ruleDAO.getRules()) { ruleKeys.add(keyFor(rule)); } if (ruleKeys.isEmpty() || map.isEmpty()) return result; for (Object key : ruleKeys) { if (map.containsKey(key)) result.add(key); } return result; }
/** * Calculate the keys not contained in the rule data access object * * @param map */ protected Set<Object> nonExistingKeys(Map map) { List<Comparable<?>> rules = ruleDAO.getRules(); if (rules.isEmpty()) return map.keySet(); Set<Object> result = new HashSet<>(); Set<Object> ruleKeys = new HashSet<>(); for (Comparable<?> rule : rules) { ruleKeys.add(keyFor(rule)); } for (Object key : map.keySet()) { if (!ruleKeys.contains(key)) result.add(key); } return result; }
public RuleMap<String, String> getMap() throws Exception { RuleMap<String, String> result = new RuleMap<>(); for (Comparable<?> rule : ruleDAO.getRules()) { addRuleToMap(rule, result); } return result; }
/** * Returns the list of rules contained in the property file. The returned rules are sorted * against the <code>R</code> natural order */ public List<R> getRules() { checkPropertyFile(false); return new ArrayList<R>(rules); }