/** * Changes existing entry in a blacklist. * * @param blacklistToUse The blacklist which contains the entry. * @param supportedBlacklistTypes Types of blacklists which the entry is to changed in. * @param oldEntry Entry to be changed. * @param newEntry Changed entry. * @return The length of the new entry. */ private static int alterEntries( final String blacklistToUse, final String[] supportedBlacklistTypes, final String[] oldEntry, final String[] newEntry) { removeEntries(blacklistToUse, supportedBlacklistTypes, oldEntry); PrintWriter pw = null; try { pw = new PrintWriter(new FileWriter(new File(ListManager.listsPath, blacklistToUse), true)); String host, path; for (final String n : newEntry) { int pos = n.indexOf('/'); if (pos < 0) { host = n; path = ".*"; } else { host = n.substring(0, pos); path = n.substring(pos + 1); } pw.println(host + "/" + path); for (final String s : supportedBlacklistTypes) { if (ListManager.listSetContains(s + ".BlackLists", blacklistToUse)) { Switchboard.urlBlacklist.add(s, host, path); } } SearchEventCache.cleanupEvents(true); } pw.close(); } catch (final IOException e) { Log.logSevere("BLACKLIST-CLEANER", "error on writing altered entries to blacklist", e); } return newEntry.length; }
/** * Removes existing entries from a blacklist. * * @param blacklistToUse The blacklist which contains the * @param supportedBlacklistTypes Types of blacklists which the entry is to changed in. * @param entries Array of entries to be deleted. * @return Length of the list of entries to be removed. */ private static int removeEntries( final String blacklistToUse, final String[] supportedBlacklistTypes, final String[] entries) { // load blacklist data from file final List<String> list = FileUtils.getListArray(new File(ListManager.listsPath, blacklistToUse)); boolean listChanged = false; // delete the old entry from file for (final String entry : entries) { String s = entry; if (list != null) { // get rid of escape characters which make it impossible to // properly use contains() if (s.contains("\\\\")) { s = s.replaceAll(Pattern.quote("\\\\"), Matcher.quoteReplacement("\\")); } if (list.contains(s)) { listChanged = list.remove(s); } } // remove the entry from the running blacklist engine for (final String supportedBlacklistType : supportedBlacklistTypes) { if (ListManager.listSetContains(supportedBlacklistType + ".BlackLists", blacklistToUse)) { final String host = (s.indexOf('/') == -1) ? s : s.substring(0, s.indexOf('/')); final String path = (s.indexOf('/') == -1) ? ".*" : s.substring(s.indexOf('/') + 1); try { Switchboard.urlBlacklist.remove(supportedBlacklistType, host, path); } catch (final RuntimeException e) { Log.logSevere("BLACKLIST-CLEANER", e.getMessage() + ": " + host + "/" + path); } } } SearchEventCache.cleanupEvents(true); } if (listChanged) { FileUtils.writeList( new File(ListManager.listsPath, blacklistToUse), list.toArray(new String[list.size()])); } return entries.length; }
public static serverObjects respond( final RequestHeader header, final serverObjects post, final serverSwitch env) { final serverObjects prop = new serverObjects(); // initialize the list manager ListManager.switchboard = (Switchboard) env; ListManager.listsPath = new File(env.getDataPath(), env.getConfig("listManager.listsPath", "DATA/LISTS")); String blacklistToUse = null; // get the list of supported blacklist types final String supportedBlacklistTypesStr = Blacklist.BLACKLIST_TYPES_STRING; final String[] supportedBlacklistTypes = supportedBlacklistTypesStr.split(","); prop.put(DISABLED + "checked", "1"); if (post != null) { final boolean allowRegex = post.get("allowRegex", "off").equalsIgnoreCase("on") ? true : false; prop.put(DISABLED + "checked", (allowRegex) ? "1" : "0"); if (post.containsKey("listNames")) { blacklistToUse = post.get("listNames"); if (blacklistToUse.length() == 0 || !ListManager.listSetContains("listManager.listsPath", blacklistToUse)) { prop.put("results", "2"); } } putBlacklists( prop, FileUtils.getDirListing(ListManager.listsPath, BLACKLIST_FILENAME_FILTER), blacklistToUse); if (blacklistToUse != null) { prop.put("results", "1"); if (post.containsKey("delete")) { prop.put(RESULTS + "modified", "1"); prop.put( RESULTS + "modified_delCount", removeEntries( blacklistToUse, supportedBlacklistTypes, getKeysByPrefix(post, "select", true))); } else if (post.containsKey("alter")) { prop.put(RESULTS + "modified", "2"); prop.put( RESULTS + "modified_alterCount", alterEntries( blacklistToUse, supportedBlacklistTypes, getKeysByPrefix(post, "select", false), getValuesByPrefix(post, "entry", false))); } // list illegal entries final Map<String, BlacklistError> illegalEntries = getIllegalEntries(blacklistToUse, Switchboard.urlBlacklist, allowRegex); prop.put(RESULTS + "blList", blacklistToUse); prop.put(RESULTS + "entries", illegalEntries.size()); prop.putHTML(RESULTS + "blEngine", Switchboard.urlBlacklist.getEngineInfo()); prop.put(RESULTS + "disabled", (illegalEntries.isEmpty()) ? "1" : "0"); if (!illegalEntries.isEmpty()) { prop.put(RESULTS + DISABLED + "entries", illegalEntries.size()); int i = 0; String key; for (final Entry<String, BlacklistError> entry : illegalEntries.entrySet()) { key = entry.getKey(); prop.put(RESULTS + DISABLED + ENTRIES + i + "_error", entry.getValue().getLong()); prop.putHTML(RESULTS + DISABLED + ENTRIES + i + "_entry", key); i++; } } } } else { prop.put("results", "0"); putBlacklists( prop, FileUtils.getDirListing(ListManager.listsPath, BLACKLIST_FILENAME_FILTER), blacklistToUse); } return prop; }