Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
First draft of a blacklist API.
Marc Nausec97da1a0d894
diff --git a/htroot/Blacklist_p.java b/htroot/Blacklist_p.javaindex 7589187d0..2f426877f 100644--- a/htroot/Blacklist_p.java+++ b/htroot/Blacklist_p.java@@ -34,8 +34,6 @@ import java.io.IOException;import java.util.Arrays;import java.util.List;-import net.yacy.cora.document.id.Punycode.PunycodeException;-import net.yacy.cora.protocol.HeaderFramework;import net.yacy.cora.protocol.RequestHeader;import net.yacy.cora.util.ConcurrentLog;import net.yacy.data.ListManager;@@ -43,16 +41,13 @@ import net.yacy.data.WorkTables;import net.yacy.kelondro.util.FileUtils;import net.yacy.repository.Blacklist;import net.yacy.repository.Blacklist.BlacklistType;+import net.yacy.repository.BlacklistHelper;import net.yacy.search.Switchboard;-import net.yacy.search.query.SearchEventCache;import net.yacy.server.serverObjects;import net.yacy.server.serverSwitch;public class Blacklist_p {- /** Used for logging. */- private static final String APP_NAME = "Blacklist";-private final static String EDIT = "edit_";private final static String DISABLED = "disabled_";private final static String BLACKLIST = "blackLists_";@@ -138,7 +133,7 @@ public class Blacklist_p {final File blackListFile = new File(ListManager.listsPath, blacklistToUse);if(!blackListFile.delete()) {- ConcurrentLog.warn(APP_NAME, "file "+ blackListFile +" could not be deleted!");+ ConcurrentLog.warn(BlacklistHelper.APP_NAME, "file "+ blackListFile +" could not be deleted!");}for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {@@ -206,7 +201,7 @@ public class Blacklist_p {if (selectedBlacklistEntries.length > 0) {String temp = null;for (final String selectedBlacklistEntry : selectedBlacklistEntries) {- if ((temp = deleteBlacklistEntry(blacklistToUse, selectedBlacklistEntry, header)) != null) {+ if ((temp = BlacklistHelper.deleteBlacklistEntry(blacklistToUse, selectedBlacklistEntry, header)) != null) {prop.put(serverObjects.ACTION_LOCATION, temp);return prop;}@@ -216,7 +211,7 @@ public class Blacklist_p {Switchboard.urlBlacklist.clear();ListManager.reloadBlacklists();- } else if (post.containsKey("addBlacklistEntry")) {+ } else if (post.containsKey("addBlacklistEntry") || "addBlacklistEntry".equals(action)) {/* ===========================================================* Add new entry to blacklist@@ -228,7 +223,7 @@ public class Blacklist_p {// store this call as api callListManager.switchboard.tables.recordAPICall(post, "Blacklist_p.html", WorkTables.TABLE_API_TYPE_CONFIGURATION, "add to blacklist: " + blentry);- final String temp = addBlacklistEntry(blacklistToUse, blentry, header);+ final String temp = BlacklistHelper.addBlacklistEntry(blacklistToUse, blentry, header);if (temp != null) {prop.put(serverObjects.ACTION_LOCATION, temp);return prop;@@ -255,12 +250,12 @@ public class Blacklist_p {!targetBlacklist.equals(blacklistToUse)) {String temp;for (final String selectedBlacklistEntry : selectedBlacklistEntries) {- if ((temp = addBlacklistEntry(targetBlacklist, selectedBlacklistEntry, header)) != null) {+ if ((temp = BlacklistHelper.addBlacklistEntry(targetBlacklist, selectedBlacklistEntry, header)) != null) {prop.put(serverObjects.ACTION_LOCATION, temp);return prop;}- if ((temp = deleteBlacklistEntry(blacklistToUse, selectedBlacklistEntry, header)) != null) {+ if ((temp = BlacklistHelper.deleteBlacklistEntry(blacklistToUse, selectedBlacklistEntry, header)) != null) {prop.put(serverObjects.ACTION_LOCATION, temp);return prop;@@ -297,12 +292,12 @@ public class Blacklist_p {if (!selectedBlacklistEntries[i].equals(editedBlacklistEntries[i])) {- if ((temp = deleteBlacklistEntry(blacklistToUse, selectedBlacklistEntries[i], header)) != null) {+ if ((temp = BlacklistHelper.deleteBlacklistEntry(blacklistToUse, selectedBlacklistEntries[i], header)) != null) {prop.put(serverObjects.ACTION_LOCATION, temp);return prop;}- if ((temp = addBlacklistEntry(blacklistToUse, editedBlacklistEntries[i], header)) != null) {+ if ((temp = BlacklistHelper.addBlacklistEntry(blacklistToUse, editedBlacklistEntries[i], header)) != null) {prop.put(serverObjects.ACTION_LOCATION, temp);return prop;}@@ -490,112 +485,4 @@ public class Blacklist_p {return prop;}- /**- * This method deletes a blacklist entry.- * @param blacklistToUse the name of the blacklist the entry is to be deleted from- * @param entry the entry that is to be deleted- * @param header- * @param supportedBlacklistTypes- * @return null if no error occurred, else a String to put into LOCATION- */- private static String deleteBlacklistEntry(- final String blacklistToUse,- final String entry,- final RequestHeader header) {- String oldEntry = entry;-- if (blacklistToUse == null || blacklistToUse.isEmpty()) {- return "";- }-- if (oldEntry == null || oldEntry.isEmpty()) {- return header.get(HeaderFramework.CONNECTION_PROP_PATH) + "?selectList=&selectedListName=" + blacklistToUse;- }--- // remove the entry from the running blacklist engine- int pos = oldEntry.indexOf('/',0);- String host = oldEntry.substring(0, pos);- String path = "";- if (pos > 0) {- path = oldEntry.substring(pos + 1);- }-- for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {- if (ListManager.listSetContains(supportedBlacklistType + ".BlackLists",blacklistToUse)) {- Switchboard.urlBlacklist.remove(supportedBlacklistType, blacklistToUse, host, path);- }- }-- SearchEventCache.cleanupEvents(true);- return null;- }-- /**- * This method adds a new entry to the chosen blacklist.- * @param blacklistToUse the name of the blacklist the entry is to be added to- * @param newEntry the entry that is to be added- * @param header- * @param supportedBlacklistTypes- * @return null if no error occurred, else a String to put into LOCATION- */- private static String addBlacklistEntry(- final String blacklistToUse,- final String entry,- final RequestHeader header) {- String newEntry = entry;-- if (blacklistToUse == null || blacklistToUse.isEmpty()) {- return "";- }-- if (newEntry == null || newEntry.isEmpty()) {- return header.get(HeaderFramework.CONNECTION_PROP_PATH) + "?selectList=&selectedListName=" + blacklistToUse;- }-- // ignore empty entries- if(newEntry == null || newEntry.isEmpty()) {- ConcurrentLog.warn(APP_NAME, "skipped adding an empty entry");- return "";- }-- if (newEntry.startsWith("http://") ){- newEntry = newEntry.substring(7);- } else if (newEntry.startsWith("https://")) {- newEntry = newEntry.substring(8);- }-- if (newEntry.indexOf("*") < 0) {- // user did not use any wild cards and just submitted a word-- newEntry = ".*" + newEntry + ".*/.*";- newEntry = ".*.*/.*" + newEntry + ".*";-- } else {-- int pos = newEntry.indexOf('/',0);- if (pos < 0) {- // add default empty path pattern- newEntry = newEntry + "/.*";- }- }-- int pos = newEntry.indexOf('/',0);- String host = newEntry.substring(0, pos);- String path = newEntry.substring(pos + 1);-- for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {- if (ListManager.listSetContains(supportedBlacklistType + ".BlackLists",blacklistToUse)) {- try {- Switchboard.urlBlacklist.add(supportedBlacklistType, blacklistToUse, host, path);- } catch (PunycodeException e) {- ConcurrentLog.warn(APP_NAME, "Unable to add blacklist entry to blacklist " + supportedBlacklistType, e);- }- }- }-- SearchEventCache.cleanupEvents(true);- return null;- }-}diff --git a/htroot/api/blacklists/add_entry_p.java b/htroot/api/blacklists/add_entry_p.javanew file mode 100644index 000000000..f5d7c081c--- /dev/null+++ b/htroot/api/blacklists/add_entry_p.java@@ -0,0 +1,47 @@+package blacklists;++import net.yacy.cora.protocol.RequestHeader;+import net.yacy.data.ListManager;+import net.yacy.data.WorkTables;+import net.yacy.repository.BlacklistHelper;+import net.yacy.search.Switchboard;+import net.yacy.server.serverObjects;+import net.yacy.server.serverSwitch;++public class add_entry_p {++ private static final String RESULT_FAILURE = "0";+ private static final String RESULT_SUCCESS = "1";+ private static final String XML_ITEM_STATUS = "status";+ private static final String KEY_NEW_ENTRY = "entry";+ private static final String KEY_CURRENT_BLACKLIST = "blacklist";++ public static serverObjects respond(final RequestHeader header, final serverObjects post, @SuppressWarnings("unused") final serverSwitch env) {++ final serverObjects prop = new serverObjects();++ if (post.containsKey(KEY_CURRENT_BLACKLIST) && post.containsKey(KEY_NEW_ENTRY)) {++ final String blacklistToUse = post.get(KEY_CURRENT_BLACKLIST, "").trim();+ final String entry = post.get(KEY_NEW_ENTRY, "").trim();++ // store this call as api call+ ListManager.switchboard.tables.recordAPICall(post, "add_entry_p.xml", WorkTables.TABLE_API_TYPE_CONFIGURATION, "add to blacklist: " + entry);++ if (BlacklistHelper.addBlacklistEntry(blacklistToUse, entry, header) == null) {+ prop.put(XML_ITEM_STATUS, RESULT_SUCCESS);++ Switchboard.urlBlacklist.clear();+ ListManager.reloadBlacklists();+ } else {+ prop.put(XML_ITEM_STATUS, RESULT_FAILURE);+ }++ } else {+ prop.put(XML_ITEM_STATUS, RESULT_FAILURE);+ }++ return prop;+ }++}diff --git a/htroot/api/blacklists/add_entry_p.xml b/htroot/api/blacklists/add_entry_p.xmlnew file mode 100644index 000000000..a72c699cc--- /dev/null+++ b/htroot/api/blacklists/add_entry_p.xml@@ -0,0 +1,2 @@+<?xml version='1.0' encoding="UTF-8" standalone='yes'?>+<status code="#(status)#error::ok#(/status)#" />\ No newline at end of filediff --git a/htroot/api/blacklists/delete_entry_p.java b/htroot/api/blacklists/delete_entry_p.javanew file mode 100644index 000000000..a6844f05b--- /dev/null+++ b/htroot/api/blacklists/delete_entry_p.java@@ -0,0 +1,47 @@+package blacklists;++import net.yacy.cora.protocol.RequestHeader;+import net.yacy.data.ListManager;+import net.yacy.data.WorkTables;+import net.yacy.repository.BlacklistHelper;+import net.yacy.search.Switchboard;+import net.yacy.server.serverObjects;+import net.yacy.server.serverSwitch;++public class delete_entry_p {++ private static final String RESULT_FAILURE = "0";+ private static final String RESULT_SUCCESS = "1";+ private static final String XML_ITEM_STATUS = "status";+ private static final String KEY_NEW_ENTRY = "entry";+ private static final String KEY_CURRENT_BLACKLIST = "blacklist";++ public static serverObjects respond(final RequestHeader header, final serverObjects post, @SuppressWarnings("unused") final serverSwitch env) {++ final serverObjects prop = new serverObjects();++ if (post.containsKey(KEY_CURRENT_BLACKLIST) && post.containsKey(KEY_NEW_ENTRY)) {++ final String blacklistToUse = post.get(KEY_CURRENT_BLACKLIST, "").trim();+ final String entry = post.get(KEY_NEW_ENTRY, "").trim();++ // store this call as api call+ ListManager.switchboard.tables.recordAPICall(post, "add_entry_p.xml", WorkTables.TABLE_API_TYPE_CONFIGURATION, "add to blacklist: " + entry);++ if (BlacklistHelper.deleteBlacklistEntry(blacklistToUse, entry, header) == null) {+ prop.put(XML_ITEM_STATUS, RESULT_SUCCESS);++ Switchboard.urlBlacklist.clear();+ ListManager.reloadBlacklists();+ } else {+ prop.put(XML_ITEM_STATUS, RESULT_FAILURE);+ }++ } else {+ prop.put(XML_ITEM_STATUS, RESULT_FAILURE);+ }++ return prop;+ }++}diff --git a/htroot/api/blacklists/delete_entry_p.xml b/htroot/api/blacklists/delete_entry_p.xmlnew file mode 100644index 000000000..a72c699cc--- /dev/null+++ b/htroot/api/blacklists/delete_entry_p.xml@@ -0,0 +1,2 @@+<?xml version='1.0' encoding="UTF-8" standalone='yes'?>+<status code="#(status)#error::ok#(/status)#" />\ No newline at end of filediff --git a/htroot/api/blacklists/get_list_p.java b/htroot/api/blacklists/get_list_p.javanew file mode 100644index 000000000..c33bf90dc--- /dev/null+++ b/htroot/api/blacklists/get_list_p.java@@ -0,0 +1,75 @@+package blacklists;++import java.io.File;+import java.util.Collection;++import net.yacy.cora.protocol.RequestHeader;+import net.yacy.data.ListManager;+import net.yacy.kelondro.util.FileUtils;+import net.yacy.repository.Blacklist;+import net.yacy.repository.Blacklist.BlacklistType;+import net.yacy.server.serverObjects;+import net.yacy.server.serverSwitch;++public class get_list_p {++ private static final String ITEMS = "items";+ private static final String POSTFIX_ITEM = "_item";+ private static final String PREFIX_ITEMS = "items_";+ private static final String SHARED = "shared";+ private static final String NAME = "name";+ private static final String TYPES = "types";+ private static final String PREFIX_TYPES = "types_";+ private static final String POSTFIX_VALUE = "_value";+ private static final String POSTFIX_NAME = "_name";+ private static final String TYPES_EXT = ".BlackLists";+ private static final String BLACK_LISTS_SHARED = "BlackLists.Shared";++ public static serverObjects respond(@SuppressWarnings("unused") final RequestHeader header, final serverObjects post, @SuppressWarnings("unused") final serverSwitch env) {++ final serverObjects prop = new serverObjects();++ final Collection<String> dirlist = FileUtils.getDirListing(ListManager.listsPath, Blacklist.BLACKLIST_FILENAME_FILTER);++ final String blackListName = (post == null) ? "" : post.get("name", "");++ int count;+ if (dirlist != null) {+ for (final String element : dirlist) {+ if (element.equals(blackListName)) {++ prop.putXML(NAME, element);++ prop.put(SHARED, ListManager.listSetContains(BLACK_LISTS_SHARED, element));++ int j = 0;+ for (final BlacklistType type : BlacklistType.values()) {+ prop.putXML(PREFIX_TYPES + j + POSTFIX_NAME, type.toString());+ prop.put(PREFIX_TYPES + j + POSTFIX_VALUE,+ ListManager.listSetContains(type + TYPES_EXT, element));+ j++;+ }+ prop.put(TYPES, BlacklistType.values().length);++ prop.putXML(NAME, element);++ final Collection<String> list = FileUtils.getListArray(new File(ListManager.listsPath, element));++ count = 0;+ for (final String entry : list){+ if (entry.isEmpty()) continue;+ if (entry.charAt(0) == '#') continue;++ prop.putXML(PREFIX_ITEMS + count + POSTFIX_ITEM, entry);+ count++;+ }+ prop.put(ITEMS, count);+ }+ }+ }+++ return prop;+ }++}diff --git a/htroot/api/blacklists/get_list_p.xml b/htroot/api/blacklists/get_list_p.xmlnew file mode 100644index 000000000..716b68aa3--- /dev/null+++ b/htroot/api/blacklists/get_list_p.xml@@ -0,0 +1,9 @@+<?xml version="1.0"?>+<list name="#[name]#" shared="#[shared]#" #{types}#+ #[name]#="#[value]#"#{/types}#>+ <items>+#{items}#+ <item>#[item]#</item>+#{/items}#+ </items>+</list>diff --git a/htroot/api/blacklists/get_metadata_p.java b/htroot/api/blacklists/get_metadata_p.javanew file mode 100644index 000000000..1753e3eb1--- /dev/null+++ b/htroot/api/blacklists/get_metadata_p.java@@ -0,0 +1,55 @@+package blacklists;++import java.util.Collection;++import net.yacy.cora.protocol.RequestHeader;+import net.yacy.data.ListManager;+import net.yacy.kelondro.util.FileUtils;+import net.yacy.repository.Blacklist;+import net.yacy.repository.Blacklist.BlacklistType;+import net.yacy.server.serverObjects;+import net.yacy.server.serverSwitch;++public class get_metadata_p {++ private static final String LISTS = "lists";+ private static final String POSTFIX_SHARED = "_shared";+ private static final String POSTFIX_TYPES = "_types";+ private static final String POSTFIX_VALUE = "_value";+ private static final String POSTFIX_NAME = "_name";+ private static final String TYPES_EXT = ".BlackLists";+ private static final String INFIX_TYPES = "_types_";+ private static final String PREFIX_LISTS = "lists_";+ private static final String BLACK_LISTS_SHARED = "BlackLists.Shared";++ public static serverObjects respond(@SuppressWarnings("unused") final RequestHeader header, @SuppressWarnings("unused") final serverObjects post, @SuppressWarnings("unused") final serverSwitch env) {++ final serverObjects prop = new serverObjects();++ final Collection<String> dirlist = FileUtils.getDirListing(ListManager.listsPath, Blacklist.BLACKLIST_FILENAME_FILTER);+ int blacklistCount=0;++ if (dirlist != null) {+ for (final String element : dirlist) {+ prop.putXML(PREFIX_LISTS + blacklistCount + POSTFIX_NAME, element);++ prop.put(PREFIX_LISTS + blacklistCount + POSTFIX_SHARED, ListManager.listSetContains(BLACK_LISTS_SHARED, element));++ int j = 0;+ for (final BlacklistType type : BlacklistType.values()) {+ prop.putXML(PREFIX_LISTS + blacklistCount + INFIX_TYPES + j + POSTFIX_NAME, type.toString());+ prop.put(PREFIX_LISTS + blacklistCount + INFIX_TYPES + j + POSTFIX_VALUE,+ ListManager.listSetContains(type + TYPES_EXT, element));+ j++;+ }+ prop.put(PREFIX_LISTS + blacklistCount + POSTFIX_TYPES, BlacklistType.values().length);++ blacklistCount++;+ }+ }+ prop.put(LISTS, blacklistCount);++ return prop;+ }++}diff --git a/htroot/api/blacklists/get_metadata_p.xml b/htroot/api/blacklists/get_metadata_p.xmlnew file mode 100644index 000000000..2ff680ad2--- /dev/null+++ b/htroot/api/blacklists/get_metadata_p.xml@@ -0,0 +1,6 @@+<?xml version='1.0' encoding="UTF-8" standalone='yes'?>+<blacklists>+#{lists}#<list name="#[name]#" shared="#[shared]#" #{types}#+ #[name]#="#[value]#"#{/types}# />+#{/lists}#+</blacklists>\ No newline at end of filediff --git a/htroot/api/blacklists_p.java b/htroot/api/blacklists_p.javaindex cba337ffd..07910ebee 100644--- a/htroot/api/blacklists_p.java+++ b/htroot/api/blacklists_p.java@@ -17,7 +17,7 @@ public class blacklists_p {final serverObjects prop = new serverObjects();final List<String> dirlist = FileUtils.getDirListing(ListManager.listsPath, Blacklist.BLACKLIST_FILENAME_FILTER);- int blacklistCount=0;+ int blacklistCount = 0;final String blackListName = (post == null) ? "" : post.get("listname", "");final String attrOnly = (post == null) ? "" : post.get("attrOnly", "");diff --git a/source/net/yacy/repository/BlacklistHelper.java b/source/net/yacy/repository/BlacklistHelper.javanew file mode 100644index 000000000..d5250f4a4--- /dev/null+++ b/source/net/yacy/repository/BlacklistHelper.java@@ -0,0 +1,129 @@+package net.yacy.repository;++import net.yacy.cora.document.id.Punycode.PunycodeException;+import net.yacy.cora.protocol.HeaderFramework;+import net.yacy.cora.protocol.RequestHeader;+import net.yacy.cora.util.ConcurrentLog;+import net.yacy.data.ListManager;+import net.yacy.repository.Blacklist.BlacklistType;+import net.yacy.search.Switchboard;+import net.yacy.search.query.SearchEventCache;++public final class BlacklistHelper {++ /** Used for logging. */+ public static final String APP_NAME = "Blacklist";++ /** Private constructor to avoid instantiation of static helper class. */+ private BlacklistHelper() {+ }++ /**+ * Adds a new entry to the chosen blacklist.+ * @param blacklistToUse the name of the blacklist the entry is to be added to+ * @param newEntry the entry that is to be added+ * @param header+ * @param supportedBlacklistTypes+ * @return null if no error occurred, else a String to put into LOCATION+ */+ public static String addBlacklistEntry(+ final String blacklistToUse,+ final String entry,+ final RequestHeader header) {+ String newEntry = entry;++ if (blacklistToUse == null || blacklistToUse.isEmpty()) {+ return "";+ }++ if (newEntry == null || newEntry.isEmpty()) {+ return header.get(HeaderFramework.CONNECTION_PROP_PATH) + "?selectList=&selectedListName=" + blacklistToUse;+ }++ // ignore empty entries+ if(newEntry == null || newEntry.isEmpty()) {+ ConcurrentLog.warn(APP_NAME, "skipped adding an empty entry");+ return "";+ }++ if (newEntry.startsWith("http://") ){+ newEntry = newEntry.substring(7);+ } else if (newEntry.startsWith("https://")) {+ newEntry = newEntry.substring(8);+ }++ if (newEntry.indexOf("*") < 0) {+ // user did not use any wild cards and just submitted a word++ newEntry = ".*" + newEntry + ".*/.*";+ newEntry = ".*.*/.*" + newEntry + ".*";++ } else {++ int pos = newEntry.indexOf('/',0);+ if (pos < 0) {+ // add default empty path pattern+ newEntry = newEntry + "/.*";+ }+ }++ int pos = newEntry.indexOf('/',0);+ String host = newEntry.substring(0, pos);+ String path = newEntry.substring(pos + 1);++ for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {+ if (ListManager.listSetContains(supportedBlacklistType + ".BlackLists", blacklistToUse)) {+ try {+ Switchboard.urlBlacklist.add(supportedBlacklistType, blacklistToUse, host, path);+ } catch (PunycodeException e) {+ ConcurrentLog.warn(APP_NAME, "Unable to add blacklist entry to blacklist " + supportedBlacklistType, e);+ }+ }+ }++ SearchEventCache.cleanupEvents(true);+ return null;+ }++ /**+ * Deletes a blacklist entry.+ * @param blacklistToUse the name of the blacklist the entry is to be deleted from+ * @param entry the entry that is to be deleted+ * @param header+ * @param supportedBlacklistTypes+ * @return null if no error occurred, else a String to put into LOCATION+ */+ public static String deleteBlacklistEntry(+ final String blacklistToUse,+ final String entry,+ final RequestHeader header) {+ String oldEntry = entry;++ if (blacklistToUse == null || blacklistToUse.isEmpty()) {+ return "";+ }++ if (oldEntry == null || oldEntry.isEmpty()) {+ return header.get(HeaderFramework.CONNECTION_PROP_PATH) + "?selectList=&selectedListName=" + blacklistToUse;+ }+++ // remove the entry from the running blacklist engine+ int pos = oldEntry.indexOf('/',0);+ String host = oldEntry.substring(0, pos);+ String path = "";+ if (pos > 0) {+ path = oldEntry.substring(pos + 1);+ }++ for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {+ if (ListManager.listSetContains(supportedBlacklistType + ".BlackLists",blacklistToUse)) {+ Switchboard.urlBlacklist.remove(supportedBlacklistType, blacklistToUse, host, path);+ }+ }++ SearchEventCache.cleanupEvents(true);+ return null;+ }++}