*) refactoring of Blacklist_p.java (moving entries might be slightly slower, but the code is more tidy now)

*) added edit functionality for blacklist entries

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4616 6c8d7289-2bf4-0310-a012-ef5d649a1542
This commit is contained in:
low012 2008-03-29 20:39:46 +00:00
parent 466d49e90c
commit 4c3f1b67ad
2 changed files with 161 additions and 163 deletions

View File

@ -7,7 +7,7 @@
<body id="Blacklist">
#%env/templates/header.template%#
#%env/templates/submenuBlacklist.template%#
<h2>Blacklist Administration</h2>
<p>Used Blacklist engine: <span class="settingsValue">#[blacklistEngine]#</span></p>
<p>
@ -103,6 +103,17 @@
</select>
<br />
<input type="submit" name="deleteBlacklistEntry" value="Delete URL pattern" />
<br /><br />
#(edit)#
<input type="submit" name="editBlacklistEntry" value="Edit URL pattern" />
::
<form action="Blacklist_p.html" method="post" enctype="multipart/form-data">
<input type="hidden" name="currentBlacklist" value="#[currentBlacklist]#" />
<input type="text" name="editedBlacklistEntry" size="50" value="#[item]#"/>
<input type="hidden" name="selectedBlacklistEntry" value="#[item]#"/>
<input type="submit" name="editBlacklistEntry" value="Save URL pattern" />
</form>
#(/edit)#
<br />
<br />
<input type="submit" name="moveBlacklistEntry" value="Move URL pattern to" />

View File

@ -70,7 +70,7 @@ import de.anomic.yacy.yacySeed;
import de.anomic.yacy.yacyURL;
public class Blacklist_p {
private final static String DISABLED = "disabled_";
private final static String DISABLED = "disabled_";
private final static String BLACKLIST = "blackLists_";
private final static String BLACKLIST_SHARED = "BlackLists.Shared";
@ -87,7 +87,7 @@ public class Blacklist_p {
String blacklistToUse = null;
serverObjects prop = new serverObjects();
prop.putHTML("blacklistEngine", plasmaSwitchboard.urlBlacklist.getEngineInfo());
prop.putHTML("asd", "0");
// do all post operations
if (post != null) {
@ -213,190 +213,78 @@ public class Blacklist_p {
}
} else if (post.containsKey("deleteBlacklistEntry")) {
/* ===========================================================
* Delete a blacklist entry
* =========================================================== */
// get the current selected blacklist name
blacklistToUse = (String)post.get("currentBlacklist");
if (blacklistToUse == null || blacklistToUse.trim().length() == 0) {
prop.put("LOCATION", "");
String temp = deleteBlacklistEntry((String)post.get("currentBlacklist"),
(String)post.get("selectedEntry"), header, supportedBlacklistTypes);
if (temp != null) {
prop.put("LOCATION", temp);
return prop;
}
// get the entry that should be deleted
String oldEntry = (String)post.get("selectedEntry");
if (oldEntry == null || oldEntry.trim().length() == 0) {
prop.put("LOCATION", header.get("PATH") + "?selectList=&selectedListName=" + blacklistToUse);
return prop;
}
// load blacklist data from file
ArrayList<String> list = listManager.getListArray(new File(listManager.listsPath, blacklistToUse));
// delete the old entry from file
if (list != null) {
for (int i=0; i < list.size(); i++) {
if (((String)list.get(i)).equals(oldEntry)) {
list.remove(i);
break;
}
}
listManager.writeList(new File(listManager.listsPath, blacklistToUse), (String[])list.toArray(new String[list.size()]));
}
// remove the entry from the running blacklist engine
int pos = oldEntry.indexOf("/");
if (pos < 0) {
// add default empty path pattern
pos = oldEntry.length();
oldEntry = oldEntry + "/.*";
}
for (int blTypes=0; blTypes < supportedBlacklistTypes.length; blTypes++) {
if (listManager.listSetContains(supportedBlacklistTypes[blTypes] + ".BlackLists",blacklistToUse)) {
plasmaSwitchboard.urlBlacklist.remove(supportedBlacklistTypes[blTypes],oldEntry.substring(0, pos), oldEntry.substring(pos + 1));
}
}
} else if (post.containsKey("addBlacklistEntry")) {
/* ===========================================================
* Add a new blacklist entry
* =========================================================== */
blacklistToUse = (String)post.get("currentBlacklist");
if (blacklistToUse == null || blacklistToUse.trim().length() == 0) {
prop.put("LOCATION", "");
String temp = addBlacklistEntry((String)post.get("currentBlacklist"),
(String)post.get("newEntry"), header, supportedBlacklistTypes);
if (temp != null) {
prop.put("LOCATION", temp);
return prop;
}
String newEntry = (String)post.get("newEntry");
if (newEntry.trim().length() == 0) {
prop.put("LOCATION", header.get("PATH") + "?selectList=&selectedListName=" + blacklistToUse);
return prop;
}
// TODO: ignore empty entries
if (newEntry.startsWith("http://") ){
newEntry = newEntry.substring(7);
}
int pos = newEntry.indexOf("/");
if (pos < 0) {
// add default empty path pattern
pos = newEntry.length();
newEntry = newEntry + "/.*";
}
// append the line to the file
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter(new File(listManager.listsPath, blacklistToUse), true));
pw.println(newEntry);
pw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null) try { pw.close(); } catch (Exception e){ /* */}
}
// add to blacklist
for (int blTypes=0; blTypes < supportedBlacklistTypes.length; blTypes++) {
if (listManager.listSetContains(supportedBlacklistTypes[blTypes] + ".BlackLists",blacklistToUse)) {
plasmaSwitchboard.urlBlacklist.add(supportedBlacklistTypes[blTypes],newEntry.substring(0, pos), newEntry.substring(pos + 1));
}
}
} else if (post.containsKey("moveBlacklistEntry")) {
/* ===========================================================
* First add entry to blacklist item moves to
* =========================================================== */
blacklistToUse = (String)post.get("targetBlacklist");
if (blacklistToUse == null || blacklistToUse.trim().length() == 0) {
prop.put("LOCATION", "");
String temp = addBlacklistEntry((String)post.get("targetBlacklist"),
(String)post.get("selectedEntry"), header, supportedBlacklistTypes);
if (temp != null) {
prop.put("LOCATION", temp);
return prop;
}
String entry = (String)post.get("selectedEntry");
if (entry == null || entry.trim().length() == 0) {
prop.put("LOCATION",header.get("PATH") + "?selectList=&selectedListName=" + blacklistToUse);
return prop;
}
// TODO: ignore empty entries
if (entry.startsWith("http://") ){
entry = entry.substring(7);
}
int pos = entry.indexOf("/");
if (pos < 0) {
// add default empty path pattern
pos = entry.length();
entry = entry + "/.*";
}
// append the line to the file
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter(new File(listManager.listsPath, blacklistToUse), true));
pw.println(entry);
pw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null) try { pw.close(); } catch (Exception e){ /* */}
}
// add to blacklist
for (int blTypes=0; blTypes < supportedBlacklistTypes.length; blTypes++) {
if (listManager.listSetContains(supportedBlacklistTypes[blTypes] + ".BlackLists",blacklistToUse)) {
plasmaSwitchboard.urlBlacklist.add(supportedBlacklistTypes[blTypes],entry.substring(0, pos), entry.substring(pos + 1));
}
}
/* ===========================================================
* Then delete item from blacklist it is moved away from
* =========================================================== */
// get the current selected blacklist name
blacklistToUse = (String)post.get("currentBlacklist");
if (blacklistToUse == null || blacklistToUse.trim().length() == 0) {
prop.put("LOCATION", "");
temp = deleteBlacklistEntry((String)post.get("currentBlacklist"),
(String)post.get("selectedEntry"), header, supportedBlacklistTypes);
if (temp != null) {
prop.put("LOCATION", temp);
return prop;
}
// load blacklist data from file
ArrayList<String> list = listManager.getListArray(new File(listManager.listsPath, blacklistToUse));
} else if (post.containsKey("editBlacklistEntry")) {
if(post.containsKey("editedBlacklistEntry")) {
// delete the old entry from file
if (list != null) {
for (int i=0; i < list.size(); i++) {
if (((String)list.get(i)).equals(entry)) {
list.remove(i);
break;
}
/* ===========================================================
* First delete old item from blacklist
* =========================================================== */
String temp = deleteBlacklistEntry((String)post.get("currentBlacklist"),
(String)post.get("selectedBlacklistEntry"), header, supportedBlacklistTypes);
if (temp != null) {
prop.put("LOCATION", temp);
return prop;
}
/* ===========================================================
* Thent add new entry to blacklist
* =========================================================== */
temp = addBlacklistEntry((String)post.get("currentBlacklist"),
(String)post.get("editedBlacklistEntry"), header, supportedBlacklistTypes);
if (temp != null) {
prop.put("LOCATION", temp);
return prop;
}
listManager.writeList(new File(listManager.listsPath, blacklistToUse), (String[])list.toArray(new String[list.size()]));
}
// remove the entry from the running blacklist engine
pos = entry.indexOf("/");
if (pos < 0) {
// add default empty path pattern
pos = entry.length();
entry = entry + "/.*";
}
for (int blTypes=0; blTypes < supportedBlacklistTypes.length; blTypes++) {
if (listManager.listSetContains(supportedBlacklistTypes[blTypes] + ".BlackLists",blacklistToUse)) {
plasmaSwitchboard.urlBlacklist.remove(supportedBlacklistTypes[blTypes],entry.substring(0, pos), entry.substring(pos + 1));
} else {
String selectedEntry = (String)post.get("selectedEntry");
String currentBlacklist = (String)post.get("currentBlacklist");
if (selectedEntry != null && currentBlacklist != null) {
prop.put(DISABLED + "edit_item", selectedEntry);
prop.put(DISABLED + "edit_currentBlacklist", currentBlacklist);
prop.put(DISABLED + "edit", "1");
}
}
}
}
@ -498,5 +386,104 @@ public class Blacklist_p {
prop.put("disabled", (blacklistToUse == null) ? "1" : "0");
return prop;
}
/**
* 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
* @return null if no error occured, else a String to put into LOCATION
*/
private static String addBlacklistEntry(String blacklistToUse, String newEntry,
httpHeader header, String[] supportedBlacklistTypes) {
if (blacklistToUse == null || blacklistToUse.trim().length() == 0) {
return "";
}
if (newEntry == null || newEntry.trim().length() == 0) {
return header.get("PATH") + "?selectList=&selectedListName=" + blacklistToUse;
}
// TODO: ignore empty entries
if (newEntry.startsWith("http://") ){
newEntry = newEntry.substring(7);
}
int pos = newEntry.indexOf("/");
if (pos < 0) {
// add default empty path pattern
pos = newEntry.length();
newEntry = newEntry + "/.*";
}
// append the line to the file
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter(new File(listManager.listsPath, blacklistToUse), true));
pw.println(newEntry);
pw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null) try { pw.close(); } catch (Exception e){ /* */}
}
// add to blacklist
for (int blTypes=0; blTypes < supportedBlacklistTypes.length; blTypes++) {
if (listManager.listSetContains(supportedBlacklistTypes[blTypes] + ".BlackLists",blacklistToUse)) {
plasmaSwitchboard.urlBlacklist.add(supportedBlacklistTypes[blTypes],newEntry.substring(0, pos), newEntry.substring(pos + 1));
}
}
return null;
}
/**
* This method deletes a blacklist entry.
* @param blacklistToUse the name of the blacklist the entry is to be deleted from
* @param oldEntry the entry that is to be deleted
* @return null if no error occured, else a String to put into LOCATION
*/
private static String deleteBlacklistEntry(String blacklistToUse, String oldEntry,
httpHeader header, String[] supportedBlacklistTypes) {
if (blacklistToUse == null || blacklistToUse.trim().length() == 0) {
return "";
}
if (oldEntry == null || oldEntry.trim().length() == 0) {
return header.get("PATH") + "?selectList=&selectedListName=" + blacklistToUse;
}
// load blacklist data from file
ArrayList<String> list = listManager.getListArray(new File(listManager.listsPath, blacklistToUse));
// delete the old entry from file
if (list != null) {
for (int i=0; i < list.size(); i++) {
if (((String)list.get(i)).equals(oldEntry)) {
list.remove(i);
break;
}
}
listManager.writeList(new File(listManager.listsPath, blacklistToUse), (String[])list.toArray(new String[list.size()]));
}
// remove the entry from the running blacklist engine
int pos = oldEntry.indexOf("/");
if (pos < 0) {
// add default empty path pattern
pos = oldEntry.length();
oldEntry = oldEntry + "/.*";
}
for (int blTypes=0; blTypes < supportedBlacklistTypes.length; blTypes++) {
if (listManager.listSetContains(supportedBlacklistTypes[blTypes] + ".BlackLists",blacklistToUse)) {
plasmaSwitchboard.urlBlacklist.remove(supportedBlacklistTypes[blTypes],oldEntry.substring(0, pos), oldEntry.substring(pos + 1));
}
}
return null;
}
}