Improved Blacklist API:

*) added JSON support
*) fixed Exception in case of missing parameters
*) renamed parameter for items in "add entry" and "delete entry" from
"entry" to "item" to match term in XML
pull/1/head
Marc Nause 11 years ago
parent 390f03e041
commit f98ccf952f

@ -13,14 +13,14 @@ 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_NEW_ENTRY = "item";
private static final String KEY_CURRENT_BLACKLIST = "list";
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)) {
if (post!= null && 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();

@ -0,0 +1,3 @@
{
"status":"#(status)#error::ok#(/status)#"
}

@ -13,14 +13,14 @@ 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_NEW_ENTRY = "item";
private static final String KEY_CURRENT_BLACKLIST = "list";
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)) {
if (post!= null && 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();

@ -0,0 +1,3 @@
{
"status":"#(status)#error::ok#(/status)#"
}

@ -13,6 +13,8 @@ import net.yacy.server.serverSwitch;
public class get_list_p {
private static final BlacklistType[] BLACKLIST_TYPE_VALUES = BlacklistType.values();
private static final String KEY_CURRENT_BLACKLIST = "list";
private static final String ITEMS = "items";
private static final String POSTFIX_ITEM = "_item";
@ -23,9 +25,14 @@ public class get_list_p {
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 POSTFIX_COMMA = "_comma";
private static final String TYPES_EXT = ".BlackLists";
private static final String BLACK_LISTS_SHARED = "BlackLists.Shared";
private static final int lastTypeIndex = BLACKLIST_TYPE_VALUES.length - 1;
private static final String EMPTY_STRING = "";
private static final String COMMA_STRING = ",";
public static serverObjects respond(@SuppressWarnings("unused") final RequestHeader header, final serverObjects post, @SuppressWarnings("unused") final serverSwitch env) {
final serverObjects prop = new serverObjects();
@ -34,7 +41,6 @@ public class get_list_p {
final String blackListName = (post == null) ? "" : post.get(KEY_CURRENT_BLACKLIST, "");
int count;
if (dirlist != null) {
for (final String element : dirlist) {
if (element.equals(blackListName)) {
@ -44,10 +50,17 @@ public class get_list_p {
prop.put(SHARED, ListManager.listSetContains(BLACK_LISTS_SHARED, element));
int j = 0;
for (final BlacklistType type : BlacklistType.values()) {
for (final BlacklistType type : BLACKLIST_TYPE_VALUES) {
prop.putXML(PREFIX_TYPES + j + POSTFIX_NAME, type.toString());
prop.put(PREFIX_TYPES + j + POSTFIX_VALUE,
ListManager.listSetContains(type + TYPES_EXT, element));
if (j < lastTypeIndex) {
prop.put(PREFIX_TYPES + j + POSTFIX_COMMA, COMMA_STRING);
} else {
prop.put(PREFIX_TYPES + j + POSTFIX_COMMA, EMPTY_STRING);
}
j++;
}
prop.put(TYPES, BlacklistType.values().length);
@ -56,12 +69,20 @@ public class get_list_p {
final Collection<String> list = FileUtils.getListArray(new File(ListManager.listsPath, element));
count = 0;
int count = 0;
final int lastItemCount = list.size() - 1;
for (final String entry : list){
if (entry.isEmpty()) continue;
if (entry.charAt(0) == '#') continue;
prop.putXML(PREFIX_ITEMS + count + POSTFIX_ITEM, entry);
if (count < lastItemCount) {
prop.put(PREFIX_ITEMS + count + POSTFIX_COMMA, COMMA_STRING);
} else {
prop.put(PREFIX_ITEMS + count + POSTFIX_COMMA, EMPTY_STRING);
}
count++;
}
prop.put(ITEMS, count);

@ -0,0 +1,9 @@
{
"name": "#[name]#",
"shared": "#[shared]#",
"types": [#{types}#{"#[name]#": "#[value]#"}#[comma]# #{/types}#]
"items":
[
#{items}#"#[item]#"#[comma]#
#{/items}#]
}

@ -12,43 +12,64 @@ import net.yacy.server.serverSwitch;
public class get_metadata_p {
private static final BlacklistType[] BLACKLIST_TYPE_VALUES = BlacklistType.values();
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 POSTFIX_COMMA = "_comma";
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";
private static final int lastTypeIndex = BLACKLIST_TYPE_VALUES.length - 1;
private static final String EMPTY_STRING = "";
private static final String COMMA_STRING = ",";
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;
final int lastBlacklistCount = dirlist.size() - 1;
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()) {
for (final BlacklistType type : BLACKLIST_TYPE_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));
if (j < lastTypeIndex) {
prop.put(PREFIX_LISTS + blacklistCount + INFIX_TYPES + j + POSTFIX_COMMA, COMMA_STRING);
} else {
prop.put(PREFIX_LISTS + blacklistCount + INFIX_TYPES + j + POSTFIX_COMMA, EMPTY_STRING);
}
j++;
}
prop.put(PREFIX_LISTS + blacklistCount + POSTFIX_TYPES, BlacklistType.values().length);
prop.put(PREFIX_LISTS + blacklistCount + POSTFIX_TYPES, BLACKLIST_TYPE_VALUES.length);
if (blacklistCount < lastBlacklistCount) {
prop.put(PREFIX_LISTS + blacklistCount + POSTFIX_COMMA, COMMA_STRING);
} else {
prop.put(PREFIX_LISTS + blacklistCount + POSTFIX_COMMA, EMPTY_STRING);
}
blacklistCount++;
}
}
prop.put(LISTS, blacklistCount);
return prop;
}

@ -0,0 +1,8 @@
[
#{lists}#{
"name": "#[name]#",
"shared": "#[shared]#",
"types": [#{types}#{"#[name]#": "#[value]#"}#[comma]# #{/types}#]
}#[comma]#
#{/lists}#
]
Loading…
Cancel
Save