Some more experimental bookmark code...

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7250 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
apfelmaennchen 14 years ago
parent 4270ed696c
commit 7aca763ca8

@ -5,7 +5,6 @@ import java.util.Date;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.kelondro.blob.Tables;
import net.yacy.kelondro.blob.Tables.Data;
import net.yacy.kelondro.data.meta.DigestURI;
import net.yacy.kelondro.index.RowSpaceExceededException;
import net.yacy.kelondro.logging.Log;
import net.yacy.kelondro.util.DateFormatter;
@ -28,15 +27,10 @@ public class add_ymark {
if(isAdmin || isAuthUser) {
final String table = (isAuthUser ? user.getUserName() : "admin")+"_"+YMarkStatics.TABLE_BOOKMARKS_BASENAME;
String url = post.get(YMarkStatics.TABLE_BOOKMARKS_COL_URL,"");
if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://")) {
url="http://"+url;
}
// generate the url hash
byte[] pk = null;
try {
pk = (new DigestURI(url, null)).hash();
pk = YMarkStatics.getBookmarkID(post.get(YMarkStatics.TABLE_BOOKMARKS_COL_URL,""));
} catch (MalformedURLException e) {
Log.logException(e);
}
@ -57,11 +51,11 @@ public class add_ymark {
if (row == null) {
// create and insert new entry
Data data = new Data();
data.put(YMarkStatics.TABLE_BOOKMARKS_COL_URL, url.getBytes());
data.put(YMarkStatics.TABLE_BOOKMARKS_COL_URL, post.get(YMarkStatics.TABLE_BOOKMARKS_COL_URL,"").getBytes());
data.put(YMarkStatics.TABLE_BOOKMARKS_COL_TITLE, post.get(YMarkStatics.TABLE_BOOKMARKS_COL_TITLE,"").getBytes());
data.put(YMarkStatics.TABLE_BOOKMARKS_COL_DESC, post.get(YMarkStatics.TABLE_BOOKMARKS_COL_DESC,"").getBytes());
data.put(YMarkStatics.TABLE_BOOKMARKS_COL_PUBLIC, post.get(YMarkStatics.TABLE_BOOKMARKS_COL_PUBLIC,"false").getBytes());
data.put(YMarkStatics.TABLE_BOOKMARKS_COL_TAGS, post.get(YMarkStatics.TABLE_BOOKMARKS_COL_TAGS,"").getBytes());
data.put(YMarkStatics.TABLE_BOOKMARKS_COL_TAGS, YMarkStatics.cleanTagsString(post.get(YMarkStatics.TABLE_BOOKMARKS_COL_TAGS,"")).getBytes());
byte[] date = DateFormatter.formatShortMilliSecond(new Date()).getBytes();
data.put(YMarkStatics.TABLE_BOOKMARKS_COL_DATE_ADDED, date);
@ -74,7 +68,7 @@ public class add_ymark {
row.put(YMarkStatics.TABLE_BOOKMARKS_COL_TITLE, post.get(YMarkStatics.TABLE_BOOKMARKS_COL_TITLE,row.get(YMarkStatics.TABLE_BOOKMARKS_COL_TITLE,"")).getBytes());
row.put(YMarkStatics.TABLE_BOOKMARKS_COL_DESC, post.get(YMarkStatics.TABLE_BOOKMARKS_COL_DESC,row.get(YMarkStatics.TABLE_BOOKMARKS_COL_DESC,"")).getBytes());
row.put(YMarkStatics.TABLE_BOOKMARKS_COL_PUBLIC, post.get(YMarkStatics.TABLE_BOOKMARKS_COL_PUBLIC,row.get(YMarkStatics.TABLE_BOOKMARKS_COL_PUBLIC,"false")).getBytes());
row.put(YMarkStatics.TABLE_BOOKMARKS_COL_TAGS, post.get(YMarkStatics.TABLE_BOOKMARKS_COL_TAGS,row.get(YMarkStatics.TABLE_BOOKMARKS_COL_TAGS,"")).getBytes());
row.put(YMarkStatics.TABLE_BOOKMARKS_COL_TAGS, YMarkStatics.cleanTagsString(post.get(YMarkStatics.TABLE_BOOKMARKS_COL_TAGS,row.get(YMarkStatics.TABLE_BOOKMARKS_COL_TAGS,""))).getBytes());
// modify date attribute
row.put(YMarkStatics.TABLE_BOOKMARKS_COL_DATE_MODIFIED, DateFormatter.formatShortMilliSecond(new Date()).getBytes());
@ -85,7 +79,7 @@ public class add_ymark {
} catch (IOException e) {
Log.logException(e);
}
Log.logInfo(YMarkStatics.TABLE_BOOKMARKS_LOG, "insertBookmark: "+url);
Log.logInfo(YMarkStatics.TABLE_BOOKMARKS_LOG, "insertBookmark: "+post.get(YMarkStatics.TABLE_BOOKMARKS_COL_URL,""));
prop.put("result", "1");
} else {
prop.put("AUTHENTICATE","Authentication required!");

@ -0,0 +1,49 @@
import java.io.IOException;
import java.net.MalformedURLException;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.kelondro.logging.Log;
import de.anomic.data.YMarkStatics;
import de.anomic.data.userDB;
import de.anomic.search.Switchboard;
import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch;
public class delete_ymark {
public static serverObjects respond(final RequestHeader header, final serverObjects post, final serverSwitch env) {
final Switchboard sb = (Switchboard) env;
final serverObjects prop = new serverObjects();
final userDB.Entry user = sb.userDB.getUser(header);
final boolean isAdmin = (sb.verifyAuthentication(header, true));
final boolean isAuthUser = user!= null && user.hasRight(userDB.Entry.BOOKMARK_RIGHT);
if(isAdmin || isAuthUser) {
final String table = (isAuthUser ? user.getUserName() : "admin")+"_"+YMarkStatics.TABLE_BOOKMARKS_BASENAME;
byte[] pk = null;
if(post.containsKey(YMarkStatics.TABLE_BOOKMARKS_COL_ID)) {
pk = post.get(YMarkStatics.TABLE_BOOKMARKS_COL_ID).getBytes();
} else if(post.containsKey(YMarkStatics.TABLE_BOOKMARKS_COL_URL)) {
try {
pk = YMarkStatics.getBookmarkID(post.get(YMarkStatics.TABLE_BOOKMARKS_COL_URL));
} catch (MalformedURLException e) {
Log.logException(e);
}
} else {
prop.put("result", "0");
return prop;
}
assert pk != null;
try {
sb.tables.delete(table,pk);
prop.put("result", "1");
} catch (IOException e) {
Log.logException(e);
}
}
// return rewrite properties
return prop;
}
}

@ -0,0 +1,2 @@
<?xml version='1.0' encoding="UTF-8" standalone='yes'?>
<result code="#(result)#something went wrong::done#(/result)#" />

@ -1,11 +1,19 @@
package de.anomic.data;
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.Set;
import net.yacy.kelondro.data.meta.DigestURI;
import net.yacy.kelondro.data.word.Word;
public class YMarkStatics {
public final static String TABLE_BOOKMARKS_BASENAME = "bookmarks";
public final static String TABLE_BOOKMARKS_LOG = "BOOKMARKS";
public final static String TABLE_BOOKMARKS_COL_ID = "id";
public final static String TABLE_BOOKMARKS_COL_URL = "url";
public final static String TABLE_BOOKMARKS_COL_TITLE = "title";
public final static String TABLE_BOOKMARKS_COL_DESC = "desc";
@ -17,4 +25,64 @@ public class YMarkStatics {
public final static String TABLE_BOOKMARKS_COL_PUBLIC = "public";
public final static String TABLE_BOOKMARKS_COL_TAGS = "tags";
public final static byte[] getBookmarkID(String url) throws MalformedURLException {
if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://")) {
url="http://"+url;
}
return (new DigestURI(url, null)).hash();
}
public final static byte[] getTagHash(final String tag) {
return Word.word2hash(tag.toLowerCase());
}
public final static Set<String> getTagSet(final String tagsString) {
Set<String>tagSet = new HashSet<String>();
String[] tagArray = cleanTagsString(tagsString).split(",");
for (final String tag : tagArray) {
tagSet.add(tag);
}
return tagSet;
}
public final static Set<byte[]> getTagHashSet(final String tagsString) {
Set<byte[]>tagSet = new HashSet<byte[]>();
String[] tagArray = cleanTagsString(tagsString).split(",");
for (final String tag : tagArray) {
tagSet.add(getTagHash(tag));
}
return tagSet;
}
public final static String cleanTagsString(String tagsString) {
// get rid of heading, trailing and double commas since they are useless
while (tagsString.length() > 0 && tagsString.charAt(0) == ',') {
tagsString = tagsString.substring(1);
}
while (tagsString.endsWith(",")) {
tagsString = tagsString.substring(0,tagsString.length() -1);
}
while (tagsString.contains(",,")){
tagsString = tagsString.replaceAll(",,", ",");
}
// space characters following a comma are removed
tagsString = tagsString.replaceAll(",\\s+", ",");
return tagsString;
}
public final static String cleanFoldersString(String foldersString) {
foldersString = cleanTagsString(foldersString);
// get rid of double and trailing slashes
while (foldersString.endsWith("/")){
foldersString = foldersString.substring(0, foldersString.length() -1);
}
while (foldersString.contains("/,")){
foldersString = foldersString.replaceAll("/,", ",");
}
while (foldersString.contains("//")){
foldersString = foldersString.replaceAll("//", "/");
}
return foldersString;
}
}

Loading…
Cancel
Save