From 300b235ce83bdbeb335ee4d2f69a86ddd4362028 Mon Sep 17 00:00:00 2001 From: cominch Date: Sun, 10 Jun 2012 12:58:29 +0200 Subject: [PATCH] Updated Demo Servlet Conflicts: htroot/About.html htroot/DemoServlet.html htroot/DemoServlet.java htroot/interaction/interaction.js source/net/yacy/interaction/Interaction.java --- htroot/DemoServlet.html | 70 + htroot/DemoServlet.java | 68 + htroot/interaction/UploadSingleFile.html | 1 + htroot/interaction/UploadSingleFile.java | 132 ++ htroot/interaction/interaction.js | 45 + source/net/yacy/interaction/Interaction.java | 1313 ++++++++++-------- source/net/yacy/interaction/TripleStore.java | 142 +- source/net/yacy/yacy.java | 4 +- 8 files changed, 1144 insertions(+), 631 deletions(-) create mode 100644 htroot/DemoServlet.html create mode 100644 htroot/DemoServlet.java create mode 100644 htroot/interaction/UploadSingleFile.html create mode 100644 htroot/interaction/UploadSingleFile.java diff --git a/htroot/DemoServlet.html b/htroot/DemoServlet.html new file mode 100644 index 000000000..06ca4174f --- /dev/null +++ b/htroot/DemoServlet.html @@ -0,0 +1,70 @@ + + + + YaCy '#[clientname]#': Demo-Servlet + + + + + + + +
+ Hello world! +
+ +
+
+ +
+ Your user name: #[username]# +
+ +
+

A list of all users on this system:

+ + #{users}# + - #[user]#
+ #{/users}# +
+ +
+
+
+ +
+ Temperature: #[temperature]# +
+ +
+ + + +
+ + + + #{localimg}# + + + + #(checked)#no::yes::maybe#(/checked)# + + #{/localimg}# + +
+
+
+ +
+

Upload a file (will be stored in HTDOCS/upload/username/):

+
+ + +
+
+ + + + + diff --git a/htroot/DemoServlet.java b/htroot/DemoServlet.java new file mode 100644 index 000000000..98d6ce3e3 --- /dev/null +++ b/htroot/DemoServlet.java @@ -0,0 +1,68 @@ +import java.util.Iterator; + +import net.yacy.yacy; +import net.yacy.cora.protocol.RequestHeader; +import net.yacy.interaction.Interaction; +import net.yacy.search.Switchboard; +import de.anomic.data.BookmarkHelper; +import de.anomic.data.UserDB; +import de.anomic.server.serverObjects; +import de.anomic.server.serverSwitch; + +public final class DemoServlet { + + public static serverObjects respond(final RequestHeader header, + final serverObjects post, final serverSwitch env) { + + // return variable that accumulates replacements + final serverObjects prop = new serverObjects(); + + final Switchboard sb = Switchboard.getSwitchboard(); + + prop.put("temperature", "-10°C"); + + // Display currently logged on user + prop.put("username", Interaction.GetLoggedOnUser(header)); + + //Generate Userlist + int numUsers = 0; + for (String user : Interaction.GetUsers()) { + prop.putHTML("users_"+numUsers+"_user", user); + numUsers++; + } + prop.put("users", numUsers); + + + + if (post != null) { + + if (post.containsKey("submit")) { + + prop.put("temperature", post.get("textthing")); + + String filename= post.get("textthing"); + + int counter = 0; + + while (counter < 10) { + + prop.put("localimg_"+counter+"_path","/"+filename); + + prop.put("localimg_"+counter+"_checked", "2"); + counter++; + } + + prop.put("localimg", counter); + + + + prop.put("temperature", yacy.homedir+"/DATA/HTDOCS/"+filename); + } + + } + + // return rewrite properties + return prop; + } + +} diff --git a/htroot/interaction/UploadSingleFile.html b/htroot/interaction/UploadSingleFile.html new file mode 100644 index 000000000..5f222d95c --- /dev/null +++ b/htroot/interaction/UploadSingleFile.html @@ -0,0 +1 @@ +thanks \ No newline at end of file diff --git a/htroot/interaction/UploadSingleFile.java b/htroot/interaction/UploadSingleFile.java new file mode 100644 index 000000000..fe5a1f06b --- /dev/null +++ b/htroot/interaction/UploadSingleFile.java @@ -0,0 +1,132 @@ +package interaction; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; + +import net.yacy.yacy; +import net.yacy.cora.document.UTF8; +import net.yacy.cora.protocol.RequestHeader; +import net.yacy.document.content.SurrogateReader; +import net.yacy.interaction.Interaction; +import net.yacy.kelondro.logging.Log; +import net.yacy.kelondro.order.Digest; +import net.yacy.search.Switchboard; +import de.anomic.data.UserDB; +import de.anomic.data.UserDB.AccessRight; +import de.anomic.server.serverObjects; +import de.anomic.server.serverSwitch; + + +public class UploadSingleFile { + + public static serverObjects respond(final RequestHeader header, + final serverObjects post, final serverSwitch env) { + final Switchboard sb = (Switchboard) env; + final serverObjects prop = new serverObjects(); + + + + + if (post != null){ + + + if (post.containsKey("uploadfile") && !post.get("uploadfile").isEmpty()) { + + UserDB.Entry entry = sb.userDB.getEntry(Interaction.GetLoggedOnUser(header)); + + if (entry != null) { + + if (entry.hasRight(UserDB.AccessRight.UPLOAD_RIGHT)) { + + // the user has the upload right + + } + + } + + String targetfilename = post.get("uploadfile", "target.file"); + + String targetfolder = "/upload/"+Interaction.GetLoggedOnUser(header); + + if (post.containsKey("targetfilename")) { + targetfilename = post.get("targetfilename"); + + } + + if (post.containsKey("targetfolder")) { + targetfolder = post.get("targetfolder"); + + if (!targetfolder.startsWith("/")) { + targetfolder = "/" + targetfolder; + } + + } + + File f = new File(yacy.dataHome_g, "DATA/HTDOCS"+targetfolder+"/"); + + yacy.mkdirsIfNeseccary (f); + + f = new File(f, targetfilename); + + Log.logInfo ("FILEUPLOAD", f.toString()); + + + + try { + + ByteArrayInputStream stream = new ByteArrayInputStream(post + .get("uploadfile$file").getBytes()); + + + if (stream != null) { + + OutputStream out; + + + out = new FileOutputStream(f.toString()); + + + byte[] buf = new byte[1024]; + int len; + while ((len = stream.read(buf)) > 0) { + out.write(buf, 0, len); + } + stream.close(); + out.close(); + } + + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + + } + } + + // return rewrite properties + return prop; + } + + + +} + + + + + + + + + diff --git a/htroot/interaction/interaction.js b/htroot/interaction/interaction.js index 71cb0a637..e92b992c0 100644 --- a/htroot/interaction/interaction.js +++ b/htroot/interaction/interaction.js @@ -1,3 +1,48 @@ + +function newload (name, div) { + +$.get(name, function(data) { + +$('#'+div).html(data); +}); + +} + +function xmlToString (xmlData) { + + var xmlString; + + if (window.ActiveXObject) { + xmlString = xmlData.xml; + } + + else { + xmlString = (new XMLSerializer()).serializeToString(xmlData); + } + + return xmlString; + +} + +function rdfload (datastore) { + +$.ajax({ + type : "GET", + url: "/currentyacypeer/interaction/GetRDF.xml?global=true", + dataType: "xml", + success: function(xml) { + datastore.load(xml); + + } + + +}); + + + +} + + function feedback (url, comment, from) { $.getJSON('/currentyacypeer/interaction/Feedback.json?url='+url+'&comment='+comment+'&from='+from, function(data) { diff --git a/source/net/yacy/interaction/Interaction.java b/source/net/yacy/interaction/Interaction.java index 1c81a243c..a6a56a5af 100644 --- a/source/net/yacy/interaction/Interaction.java +++ b/source/net/yacy/interaction/Interaction.java @@ -1,603 +1,714 @@ -package net.yacy.interaction; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -import net.yacy.cora.document.UTF8; -import net.yacy.cora.protocol.http.HTTPClient; -import net.yacy.kelondro.blob.Tables.Row; -import net.yacy.kelondro.data.meta.DigestURI; -import net.yacy.kelondro.index.RowSpaceExceededException; -import net.yacy.kelondro.logging.Log; -import net.yacy.peers.Seed; -import net.yacy.search.Switchboard; - -import org.apache.http.entity.mime.content.ContentBody; - -import com.hp.hpl.jena.rdf.model.Property; -import com.hp.hpl.jena.rdf.model.Resource; - - -public class Interaction { - -// public static String GetInteractionData (String url) { -// -// -// // Fetch information from external sciencenet server -// -// // TODO: Use internal database -// -// Log.logInfo("INTERACTION", "GetInteractionData: "+url); -// try { -// return (UTF8.String(new HTTPClient().GETbytes("http://sciencenet.kit.edu/GetDomainInfoJSON?DomainURL="+url))); -// } catch (IOException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// return ""; -// } -// -// } - - - public static String GetDomain (String url) { - - String domain = url; - - try { - DigestURI uri = new DigestURI (url); - - domain = uri.getHost(); - } catch (MalformedURLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return domain; - - } - -// public static boolean IsInBookmarks (String domain) { -// -// -// // TODO: Check if this bookmark exists -// -// Boolean result = false; -// -// DigestURI uri; -// try { -// uri = new DigestURI (domain); -// -// Bookmark b = Switchboard.getSwitchboard().bookmarksDB.getBookmark(UTF8.String(uri.hash())); -// -// if (!(b == null)) { -// result = true; -// } -// -// -// } catch (MalformedURLException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// -// return result; -// -// } - -// public static boolean SaveDomainVote (String domain, String vote) { -// -// -// // TODO: Check if this bookmark exists -// -// Boolean result = false; -// -// DigestURI uri; -// try { -// uri = new DigestURI (domain); -// -// Bookmark b = Switchboard.getSwitchboard().bookmarksDB.getBookmark(UTF8.String(uri.hash())); -// -// if (!(b == null)) { -// b.addTag(vote); -// Switchboard.getSwitchboard().bookmarksDB.saveBookmark(b); -// } else { -// Bookmark b2 = Switchboard.getSwitchboard().bookmarksDB.createBookmark(domain, "admin"); -// -// b2.addTag(vote); -// -// Switchboard.getSwitchboard().bookmarksDB.saveBookmark(b2); -// } -// -// -// } catch (MalformedURLException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// -// return result; -// -// } - -// public static boolean DomainWhite (String domain, String username) { -// -// -// // Add userinteraction -// -// Boolean result = false; -// -//// Bookmark b = Interaction.Suggest(domain, username); -//// -//// // -// -// return result; -// -// } - -// public static Usercontribution DoUsercontribution(String domain, String uc, -// String username) { -// -// final Switchboard sb = Switchboard.getSwitchboard(); -// -// Boolean existing = false; -// -// if (username == "") { -// username = "anonymous"; -// } -// -// Usercontribution result = null; -// -// if (!existing) { -// -// Boolean reject = false; -// -// -// -// -// // count elements -// Iterator it = sb.usercontributionsDB.getFeedbackitemsIterator(true); -// -// int count = 0; -// while(it.hasNext()) { -// it.next(); -// count++; -// } -// -// -// if (count > 500) { -// if (username.equals("crawlbot")) { -// reject = true; -// } -// -// } -// -// if (!reject) { -// -// try { -// -// Usercontribution new_uc = Switchboard.getSwitchboard().usercontributionsDB -// .createUsercontribution(UUID.randomUUID().toString(), -// username); -// if (username.equals("anonymous")) { -// new_uc.setPublic(true); -// } else { -// new_uc.setPublic(false); -// } -// -// new_uc.setProperty(Usercontribution.feedbackitem_TITLE, username); -// new_uc.setProperty(Usercontribution.feedbackitem_DESCRIPTION, uc); -// new_uc.setProperty(Usercontribution.feedbackitem_TARGET, new DigestURI(domain).toNormalform(false, false)); -// -// Switchboard.getSwitchboard().usercontributionsDB -// .saveUsercontribution(new_uc); -// -// result = new_uc; -// -// } catch (MalformedURLException e) { -// -// } -// } -// -// } -// -// return result; -// -// } - - - -//public static String GetURLHash (String url) { -// -// -// // TODO: Check if this bookmark exists -// -// String result = ""; -// -// DigestURI uri; -// try { -// uri = new DigestURI (url); -// -// result = UTF8.String(uri.hash()); -// -// -// -// } catch (MalformedURLException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// -// return result; -// -// } - - public static String Suggest (String url, String username) { - - final Switchboard sb = Switchboard.getSwitchboard(); - - if (username == "") { - username = "anonymous"; - } - - Boolean processlocal = false; - - if (!sb.getConfig("interaction.suggest.accumulationpeer", "").equals("")) { - if (sb.getConfig("interaction.suggest.accumulationpeer", "").equals(sb.peers.myName())) { - - // Our peer is meant to process the suggestion. - processlocal = true; - - - } else { - - // Forward suggestion to other peer - Log.logInfo("INTERACTION", "Forwarding suggestion to "+sb.getConfig("interaction.suggest.accumulationpeer", "")+": " + url); - try { - - Seed host = sb.peers.lookupByName(sb.getConfig("interaction.suggest.accumulationpeer", "")); - - return (UTF8.String(new HTTPClient().POSTbytes( - "http://"+host.getPublicAddress()+"/interaction/Suggest.json" - + "?url=" + url + "&username=" + username, - new HashMap(), false))); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } - - if (processlocal) { - - final String date = String.valueOf(System.currentTimeMillis()); - - final Map map = new HashMap(); - - map.put("url", url.getBytes()); - map.put("username", username.getBytes()); - map.put("status", "new".getBytes()); - map.put("timestamp_creation", date.getBytes()); - - try { - sb.tables.insert("suggestion", map); - } catch (final IOException e) { - Log.logException(e); - } catch (RowSpaceExceededException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - // TODO: Remove the following part for future use - - Log.logInfo("INTERACTION", "Forwarding suggestion to bk: " + url); - - try { - - String reply = (UTF8.String(new HTTPClient().POSTbytes( - "http://hwiki.fzk.de/jss/SiteBookmark?suggestUrl=" + url+"&username="+username, - new HashMap(), false))); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - return ""; - - } - - -public static String Feedback(String url, String comment, String from, String peer) { - - final Switchboard sb = Switchboard.getSwitchboard(); - - if (peer == "") { - peer = sb.peers.myName(); - } - - Boolean processlocal = false; - - if (!sb.getConfig("interaction.feedback.accumulationpeer", "").equals("")) { - if (sb.getConfig("interaction.feedback.accumulationpeer", "").equals(sb.peers.myName())) { - - // Our peer is meant to process the feedback. - processlocal = true; - - } else { - - // Forward feedback to other peer - Log.logInfo("INTERACTION", "Forwarding feedback to "+sb.getConfig("interaction.feedback.accumulationpeer", "")+": " + url + ": " - + comment); - try { - - Seed host = sb.peers.lookupByName(sb.getConfig("interaction.feedback.accumulationpeer", "")); - - return (UTF8.String(new HTTPClient().POSTbytes( - "http://"+host.getPublicAddress()+"/interaction/Feedback.json" - + "?url=" + url + "&comment=" + comment - + "&from=" + from + "&peer=" + peer, - new HashMap(), false))); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return ""; - } - } - } - - if (processlocal) { - - final String date = String.valueOf(System.currentTimeMillis()); - - final Map map = new HashMap(); - - map.put("url", url.getBytes()); - map.put("username", from.getBytes()); - map.put("peer", peer.getBytes()); - map.put("status", "new".getBytes()); - map.put("comment", comment.getBytes()); - map.put("timestamp_creation", date.getBytes()); - - try { - sb.tables.insert("feedback", map); - } catch (final IOException e) { - Log.logException(e); - } catch (RowSpaceExceededException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - // TODO: Remove the following part for future use - - try { - return (UTF8.String(new HTTPClient().POSTbytes( - "http://sciencenet.kit.edu/Feedback?Url=" + url + "&Comment=" + comment - + "&From=" + from + "&Peer=" + peer, - new HashMap(), false))); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return ""; - } - } - - return ""; -} - - - -public static String Contribution(String url, String value, String username, String peer) { - return Tableentry(url, "comment", value, username, peer); -} - - +package net.yacy.interaction; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import net.yacy.cora.document.UTF8; +import net.yacy.cora.protocol.HeaderFramework; +import net.yacy.cora.protocol.RequestHeader; +import net.yacy.cora.protocol.http.HTTPClient; +import net.yacy.kelondro.blob.Tables.Row; +import net.yacy.kelondro.data.meta.DigestURI; +import net.yacy.kelondro.index.RowSpaceExceededException; +import net.yacy.kelondro.logging.Log; +import net.yacy.peers.Seed; +import net.yacy.search.Switchboard; + +import org.apache.http.entity.mime.content.ContentBody; + +import com.hp.hpl.jena.rdf.model.Property; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.StmtIterator; + +import de.anomic.data.UserDB; + + +public class Interaction { + +// public static String GetInteractionData (String url) { +// +// +// // Fetch information from external sciencenet server +// +// // TODO: Use internal database +// +// Log.logInfo("INTERACTION", "GetInteractionData: "+url); +// try { +// return (UTF8.String(new HTTPClient().GETbytes("http://sciencenet.kit.edu/GetDomainInfoJSON?DomainURL="+url))); +// } catch (IOException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// return ""; +// } +// +// } + + + public static String GetLoggedOnUser (RequestHeader requestHeader) { + + UserDB.Entry entry = null; + + String result = "anonymous"; + + entry = Switchboard.getSwitchboard().userDB.proxyAuth((requestHeader.get(RequestHeader.AUTHORIZATION, "xxxxxx"))); + if(entry != null){ + + }else{ + entry=Switchboard.getSwitchboard().userDB.cookieAuth(requestHeader.getHeaderCookies()); + + if(entry == null){ + entry=Switchboard.getSwitchboard().userDB.ipAuth((requestHeader.get(HeaderFramework.CONNECTION_PROP_CLIENTIP, "xxxxxx"))); + if(entry != null){ + + } + } + } + + //identified via userDB + if(entry != null){ + + return entry.getUserName(); + + }else if(Switchboard.getSwitchboard().verifyAuthentication(requestHeader)){ + return "staticadmin"; + } + + return ""; + } + + + public static Set GetUsers() { + + Set res = new HashSet(); + + UserDB.Entry entry = null; + + final Iterator it = Switchboard.getSwitchboard().userDB.iterator(true); + + while (it.hasNext()) { + entry = it.next(); + if (entry == null) { + continue; + } + res.add (entry.getUserName()); + + } + + return res; + } + + public static String GetDomain (String url) { + + String domain = url; + + try { + DigestURI uri = new DigestURI (url); + + domain = uri.getHost(); + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return domain; + + } + +// public static boolean IsInBookmarks (String domain) { +// +// +// // TODO: Check if this bookmark exists +// +// Boolean result = false; +// +// DigestURI uri; +// try { +// uri = new DigestURI (domain); +// +// Bookmark b = Switchboard.getSwitchboard().bookmarksDB.getBookmark(UTF8.String(uri.hash())); +// +// if (!(b == null)) { +// result = true; +// } +// +// +// } catch (MalformedURLException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// +// return result; +// +// } + +// public static boolean SaveDomainVote (String domain, String vote) { +// +// +// // TODO: Check if this bookmark exists +// +// Boolean result = false; +// +// DigestURI uri; +// try { +// uri = new DigestURI (domain); +// +// Bookmark b = Switchboard.getSwitchboard().bookmarksDB.getBookmark(UTF8.String(uri.hash())); +// +// if (!(b == null)) { +// b.addTag(vote); +// Switchboard.getSwitchboard().bookmarksDB.saveBookmark(b); +// } else { +// Bookmark b2 = Switchboard.getSwitchboard().bookmarksDB.createBookmark(domain, "admin"); +// +// b2.addTag(vote); +// +// Switchboard.getSwitchboard().bookmarksDB.saveBookmark(b2); +// } +// +// +// } catch (MalformedURLException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// +// return result; +// +// } + +// public static boolean DomainWhite (String domain, String username) { +// +// +// // Add userinteraction +// +// Boolean result = false; +// +//// Bookmark b = Interaction.Suggest(domain, username); +//// +//// // +// +// return result; +// +// } + +// public static Usercontribution DoUsercontribution(String domain, String uc, +// String username) { +// +// final Switchboard sb = Switchboard.getSwitchboard(); +// +// Boolean existing = false; +// +// if (username == "") { +// username = "anonymous"; +// } +// +// Usercontribution result = null; +// +// if (!existing) { +// +// Boolean reject = false; +// +// +// +// +// // count elements +// Iterator it = sb.usercontributionsDB.getFeedbackitemsIterator(true); +// +// int count = 0; +// while(it.hasNext()) { +// it.next(); +// count++; +// } +// +// +// if (count > 500) { +// if (username.equals("crawlbot")) { +// reject = true; +// } +// +// } +// +// if (!reject) { +// +// try { +// +// Usercontribution new_uc = Switchboard.getSwitchboard().usercontributionsDB +// .createUsercontribution(UUID.randomUUID().toString(), +// username); +// if (username.equals("anonymous")) { +// new_uc.setPublic(true); +// } else { +// new_uc.setPublic(false); +// } +// +// new_uc.setProperty(Usercontribution.feedbackitem_TITLE, username); +// new_uc.setProperty(Usercontribution.feedbackitem_DESCRIPTION, uc); +// new_uc.setProperty(Usercontribution.feedbackitem_TARGET, new DigestURI(domain).toNormalform(false, false)); +// +// Switchboard.getSwitchboard().usercontributionsDB +// .saveUsercontribution(new_uc); +// +// result = new_uc; +// +// } catch (MalformedURLException e) { +// +// } +// } +// +// } +// +// return result; +// +// } + + + +//public static String GetURLHash (String url) { +// +// +// // TODO: Check if this bookmark exists +// +// String result = ""; +// +// DigestURI uri; +// try { +// uri = new DigestURI (url); +// +// result = UTF8.String(uri.hash()); +// +// +// +// } catch (MalformedURLException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// +// return result; +// +// } + + public static String Suggest (String url, String username) { + + final Switchboard sb = Switchboard.getSwitchboard(); + + if (username == "") { + username = "anonymous"; + } + + Boolean processlocal = false; + + if (!sb.getConfig("interaction.suggest.accumulationpeer", "").equals("")) { + if (sb.getConfig("interaction.suggest.accumulationpeer", "").equals(sb.peers.myName())) { + + // Our peer is meant to process the suggestion. + processlocal = true; + + + } else { + + // Forward suggestion to other peer + Log.logInfo("INTERACTION", "Forwarding suggestion to "+sb.getConfig("interaction.suggest.accumulationpeer", "")+": " + url); + try { + + Seed host = sb.peers.lookupByName(sb.getConfig("interaction.suggest.accumulationpeer", "")); + + return (UTF8.String(new HTTPClient().POSTbytes( + "http://"+host.getPublicAddress()+"/interaction/Suggest.json" + + "?url=" + url + "&username=" + username, + new HashMap(), false))); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + if (processlocal) { + + final String date = String.valueOf(System.currentTimeMillis()); + + final Map map = new HashMap(); + + map.put("url", url.getBytes()); + map.put("username", username.getBytes()); + map.put("status", "new".getBytes()); + map.put("timestamp_creation", date.getBytes()); + + try { + sb.tables.insert("suggestion", map); + } catch (final IOException e) { + Log.logException(e); + } catch (RowSpaceExceededException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // TODO: Remove the following part for future use + + Log.logInfo("INTERACTION", "Forwarding suggestion to bk: " + url); + + try { + + String reply = (UTF8.String(new HTTPClient().POSTbytes( + "http://hwiki.fzk.de/jss/SiteBookmark?suggestUrl=" + url+"&username="+username, + new HashMap(), false))); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return ""; + + } + + +public static String Feedback(String url, String comment, String from, String peer) { + + final Switchboard sb = Switchboard.getSwitchboard(); + + if (peer == "") { + peer = sb.peers.myName(); + } + + Boolean processlocal = false; + + if (!sb.getConfig("interaction.feedback.accumulationpeer", "").equals("")) { + if (sb.getConfig("interaction.feedback.accumulationpeer", "").equals(sb.peers.myName())) { + + // Our peer is meant to process the feedback. + processlocal = true; + + } else { + + // Forward feedback to other peer + Log.logInfo("INTERACTION", "Forwarding feedback to "+sb.getConfig("interaction.feedback.accumulationpeer", "")+": " + url + ": " + + comment); + try { + + Seed host = sb.peers.lookupByName(sb.getConfig("interaction.feedback.accumulationpeer", "")); + + return (UTF8.String(new HTTPClient().POSTbytes( + "http://"+host.getPublicAddress()+"/interaction/Feedback.json" + + "?url=" + url + "&comment=" + comment + + "&from=" + from + "&peer=" + peer, + new HashMap(), false))); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return ""; + } + } + } + + if (processlocal) { + + final String date = String.valueOf(System.currentTimeMillis()); + + final Map map = new HashMap(); + + map.put("url", url.getBytes()); + map.put("username", from.getBytes()); + map.put("peer", peer.getBytes()); + map.put("status", "new".getBytes()); + map.put("comment", comment.getBytes()); + map.put("timestamp_creation", date.getBytes()); + + try { + sb.tables.insert("feedback", map); + } catch (final IOException e) { + Log.logException(e); + } catch (RowSpaceExceededException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // TODO: Remove the following part for future use + + try { + return (UTF8.String(new HTTPClient().POSTbytes( + "http://sciencenet.kit.edu/Feedback?Url=" + url + "&Comment=" + comment + + "&From=" + from + "&Peer=" + peer, + new HashMap(), false))); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return ""; + } + } + + return ""; +} + + + +public static String Contribution(String url, String value, String username, String peer) { + return Tableentry(url, "comment", value, username, peer); +} + + public static String GetTableentry(String url, String type, String username, String peer) { - - final Switchboard sb = Switchboard.getSwitchboard(); - - try { - Iterator it = sb.tables.iterator(username+"_contribution", "url", url.getBytes()); - - Log.logInfo ("TABLE", "GET "+username+" / "+url+" - "+type+" ..."); - - it = sb.tables.orderBy(it, -1, "timestamp_creation").iterator(); - - while (it.hasNext()) { - Row r = it.next(); - - if (!it.hasNext()) { - - if (r.containsKey(type)) { - Log.logInfo ("TABLE", "GET "+username+" / "+url+" - "+type+" - "+r.get(type, "")); - return r.get(type, ""); - } - } - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return ""; - - -} - -public static String Tableentry(String url, String type, String comment, String from, String peer) { - - final Switchboard sb = Switchboard.getSwitchboard(); - - if (peer == "") { - peer = sb.peers.myName(); - } - - Boolean processlocal = false; - - Log.logInfo ("TABLE", "PUT "+from+" / "+url+" - "+type+" - "+comment); - - if (!sb.getConfig("interaction.contribution.accumulationpeer", "").equals("")) { - if (sb.getConfig("interaction.contribution.accumulationpeer", "").equals(sb.peers.myName())) { - - // Our peer is meant to process the feedback. - processlocal = true; - - } else { - - // Forward feedback to other peer - Log.logInfo("INTERACTION", "Forwarding contribution to "+sb.getConfig("interaction.contribution.accumulationpeer", "")+": " + url + ": " - + comment); - try { - - Seed host = sb.peers.lookupByName(sb.getConfig("interaction.contribution.accumulationpeer", "")); - - return (UTF8.String(new HTTPClient().POSTbytes( - "http://"+host.getPublicAddress()+"/interaction/Contribution.json" - + "?url=" + url + "&comment=" + comment - + "&from=" + from + "&peer=" + peer, - new HashMap(), false))); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return ""; - } - } - } else { - // No forward defined - processlocal = true; - } - - if (processlocal) { - - final String date = String.valueOf(System.currentTimeMillis()); - - final Map map = new HashMap(); - - map.put("url", url.getBytes()); - map.put("username", from.getBytes()); + + final Switchboard sb = Switchboard.getSwitchboard(); + + String retvalue = ""; + + try { + Iterator it = sb.tables.iterator(username+"_contribution", "url", url.getBytes()); + + Log.logInfo ("TABLE", "GET "+username+" / "+url+" - "+type+" ..."); + + it = sb.tables.orderBy(it, -1, "timestamp_creation").iterator(); + + while (it.hasNext()) { + Row r = it.next(); + + if (r.get("type", "").equals (type)) { + + retvalue = r.get("value", ""); + } + + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Log.logInfo ("TABLE", "GET "+username+" / "+url+" - "+type+" - "+retvalue); + + return retvalue; + + +} + +public static String Tableentry(String url, String type, String comment, String from, String peer) { + + final Switchboard sb = Switchboard.getSwitchboard(); + + if (peer == "") { + peer = sb.peers.myName(); + } + + Boolean processlocal = false; + + Log.logInfo ("TABLE", "PUT "+from+" / "+url+" - "+type+" - "+comment); + + if (!sb.getConfig("interaction.contribution.accumulationpeer", "").equals("")) { + + if (sb.getConfig("interaction.contribution.accumulationpeer", "").equals(sb.peers.myName())) { + + // Our peer is meant to process the feedback. + processlocal = true; + + } else { + + // Forward feedback to other peer + Log.logInfo("INTERACTION", "Forwarding contribution to "+sb.getConfig("interaction.contribution.accumulationpeer", "")+": " + url + ": " + + comment); + try { + + Seed host = sb.peers.lookupByName(sb.getConfig("interaction.contribution.accumulationpeer", "")); + + return (UTF8.String(new HTTPClient().POSTbytes( + "http://"+host.getPublicAddress()+"/interaction/Contribution.json" + + "?url=" + url + "&comment=" + comment + + "&from=" + from + "&peer=" + peer, + new HashMap(), false))); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return ""; + } + } + } else { + // No forward defined + processlocal = true; + } + + if (processlocal) { + + final String date = String.valueOf(System.currentTimeMillis()); + + final Map map = new HashMap(); + + map.put("url", url.getBytes()); + map.put("username", from.getBytes()); map.put("peer", peer.getBytes()); - map.put("status", "new".getBytes()); - map.put("type", type.getBytes()); - map.put(type, comment.getBytes()); - map.put("timestamp_creation", date.getBytes()); - - try { - sb.tables.insert(from+"_contribution", map); - } catch (final IOException e) { - Log.logException(e); - } catch (RowSpaceExceededException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - return ""; -} - -public static String Triple(String url, String s, String p, String o, String from) { - - final Switchboard sb = Switchboard.getSwitchboard(); - - Resource r = TripleStore.model.getResource(s); - Property pr = TripleStore.model.createProperty(p); - - r.addProperty(pr, o); - - - return ""; -} - -public static String GetContribution(String url) { - - final Switchboard sb = Switchboard.getSwitchboard(); - - -// Boolean processlocal = false; -// -// if (!sb.getConfig("interaction.contribution.accumulationpeer", "").equals("")) { -// -// if (sb.getConfig("interaction.contribution.accumulationpeer", "").equals(sb.peers.myName())) { -// -// // Our peer is meant to process the feedback. -// processlocal = true; -// -// } else { -// -// // Forward feedback to other peer -// Log.logInfo("INTERACTION", "Fetching contribution from "+sb.getConfig("interaction.contribution.accumulationpeer", "")+": " + url); -// -// try { -// -// Seed host = sb.peers.lookupByName(sb.getConfig("interaction.contribution.accumulationpeer", "")); -// -// return (UTF8.String(new HTTPClient().POSTbytes( -// "http://"+host.getPublicAddress()+"/interaction/Contribution.json" -// + "?url=" + url + "&comment=" + comment -// + "&from=" + from + "&peer=" + peer, -// new HashMap(), false))); -// -// } catch (IOException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// -// return ""; -// } -// } -// } else { -// // No forward defined -// processlocal = true; -// } -// -// if (processlocal) { -// -// final String date = String.valueOf(System.currentTimeMillis()); -// -// final Map map = new HashMap(); -// -// map.put("url", url.getBytes()); -// map.put("username", from.getBytes()); -// map.put("peer", peer.getBytes()); -// map.put("status", "new".getBytes()); -// map.put("comment", comment.getBytes()); -// map.put("timestamp_creation", date.getBytes()); -// -// try { -// sb.tables.insert("contribution", map); -// } catch (final IOException e) { -// Log.logException(e); -// } catch (RowSpaceExceededException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// } - - return ""; -} - - -//public static void Usertracking(String url) { -// -// final Switchboard sb = Switchboard.getSwitchboard(); -// -// Log.logInfo("INTERACTION", "Usertracking "+url); -// -// -// // TOUCH: HIT +1 TO THE DOMAIN ENTRY -// -// try { -// sb.addToIndex(new DigestURI(new DigestURI(url).getHost()), null, null); -// sb.addToIndex(new DigestURI(url), null, null); -// } catch (MalformedURLException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } catch (IOException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } catch (Failure e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// -//} - - -} - + map.put("status", "new".getBytes()); + map.put("type", type.getBytes()); + map.put("value", comment.getBytes()); + map.put("timestamp_creation", date.getBytes()); + + try { + sb.tables.insert(from+"_contribution", map); + } catch (final IOException e) { + Log.logException(e); + } catch (RowSpaceExceededException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return ""; +} + +public static String Triple(String url, String s, String p, String o) { + return Triple (url, s, p, o, ""); +} + +public static String Triple(String url, String s, String p, String o, String username) { + + final Switchboard sb = Switchboard.getSwitchboard(); + + if (username != "") { + username = "/"+username; + } + + Resource r = TripleStore.model.getResource(s+username); + Property pr = TripleStore.model.getProperty(p); + + TripleStore.model.removeAll(r, pr, (Resource) null); + + + r.addProperty(pr, o); + + + Log.logInfo ("TRIPLESTORE", "PUT "+username+" / "+s+" - "+p+" - "+o); + + + return ""; +} + +public static String TripleGet(String s, String p) { + return TripleGet (s, p, ""); +} + +public static String TripleGet(String s, String p, String username) { + + final Switchboard sb = Switchboard.getSwitchboard(); + + if (username != "") { + username = "/"+username; + } + + Resource r = TripleStore.model.getResource(s+username); + Property pr = TripleStore.model.getProperty(p); + + StmtIterator iter = TripleStore.model.listStatements(r, pr, (Resource) null); + + Log.logInfo ("TRIPLESTORE", "GET "+username+" / "+s+" - "+p+" ... "); + + + while (iter.hasNext()) { + + String obj = iter.nextStatement().getObject().toString(); + + Log.logInfo ("TRIPLESTORE", "GET "+username+" / "+s+" - "+p+" - "+obj); + + return (obj); + + } + + return ""; + +} + +public static String GetContribution(String url) { + + final Switchboard sb = Switchboard.getSwitchboard(); + + +// Boolean processlocal = false; +// +// if (!sb.getConfig("interaction.contribution.accumulationpeer", "").equals("")) { +// +// if (sb.getConfig("interaction.contribution.accumulationpeer", "").equals(sb.peers.myName())) { +// +// // Our peer is meant to process the feedback. +// processlocal = true; +// +// } else { +// +// // Forward feedback to other peer +// Log.logInfo("INTERACTION", "Fetching contribution from "+sb.getConfig("interaction.contribution.accumulationpeer", "")+": " + url); +// +// try { +// +// Seed host = sb.peers.lookupByName(sb.getConfig("interaction.contribution.accumulationpeer", "")); +// +// return (UTF8.String(new HTTPClient().POSTbytes( +// "http://"+host.getPublicAddress()+"/interaction/Contribution.json" +// + "?url=" + url + "&comment=" + comment +// + "&from=" + from + "&peer=" + peer, +// new HashMap(), false))); +// +// } catch (IOException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// +// return ""; +// } +// } +// } else { +// // No forward defined +// processlocal = true; +// } +// +// if (processlocal) { +// +// final String date = String.valueOf(System.currentTimeMillis()); +// +// final Map map = new HashMap(); +// +// map.put("url", url.getBytes()); +// map.put("username", from.getBytes()); +// map.put("peer", peer.getBytes()); +// map.put("status", "new".getBytes()); +// map.put("comment", comment.getBytes()); +// map.put("timestamp_creation", date.getBytes()); +// +// try { +// sb.tables.insert("contribution", map); +// } catch (final IOException e) { +// Log.logException(e); +// } catch (RowSpaceExceededException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// } + + return ""; +} + + +//public static void Usertracking(String url) { +// +// final Switchboard sb = Switchboard.getSwitchboard(); +// +// Log.logInfo("INTERACTION", "Usertracking "+url); +// +// +// // TOUCH: HIT +1 TO THE DOMAIN ENTRY +// +// try { +// sb.addToIndex(new DigestURI(new DigestURI(url).getHost()), null, null); +// sb.addToIndex(new DigestURI(url), null, null); +// } catch (MalformedURLException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (IOException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (Failure e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// +//} + + +} + diff --git a/source/net/yacy/interaction/TripleStore.java b/source/net/yacy/interaction/TripleStore.java index 9c7d68cf8..521a665e9 100644 --- a/source/net/yacy/interaction/TripleStore.java +++ b/source/net/yacy/interaction/TripleStore.java @@ -2,30 +2,40 @@ package net.yacy.interaction; -import com.hp.hpl.jena.rdf.model.*; -import com.hp.hpl.jena.util.FileManager; - -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.StringBufferInputStream; +import java.util.Iterator; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; import net.yacy.kelondro.logging.Log; +import net.yacy.search.Switchboard; + +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.util.FileManager; public class TripleStore { - + public static Model model = ModelFactory.createDefaultModel(); - + + public static ConcurrentHashMap privatestorage = null; + public static String file; - - + + public static void Load (String filename) { - + Model tmp = ModelFactory.createDefaultModel(); - + Log.logInfo("TRIPLESTORE", "Loading from " + filename); - + try { InputStream in = FileManager.get().open(filename); - + // read the RDF/XML file tmp.read(in, null); } @@ -33,19 +43,20 @@ public class TripleStore { { model = model.union(tmp); } - + + } - - + + public static void Add (String rdffile) { - + Model tmp = ModelFactory.createDefaultModel(); - - + + try { @SuppressWarnings("deprecation") InputStream in = new StringBufferInputStream(rdffile); - + // read the RDF/XML file tmp.read(in, null); } @@ -53,26 +64,99 @@ public class TripleStore { { model = model.union(tmp); } - + } - + public static void Save (String filename) { - - + + FileOutputStream fout; try { - - + + fout = new FileOutputStream(filename); - + model.write(fout); - + } catch (Exception e) { // TODO Auto-generated catch block Log.logWarning("TRIPLESTORE", "Saving to " + filename+" failed"); } - - + + + + } + + + public static void initPrivateStores(Switchboard switchboard) { + + Log.logInfo("TRIPLESTORE", "Init private stores"); + + if (privatestorage != null) privatestorage.clear(); + + try { + + Iterator it = switchboard.userDB.iterator(true); + + while (it.hasNext()) { + de.anomic.data.UserDB.Entry e = it.next(); + String username = e.getUserName(); + + Log.logInfo("TRIPLESTORE", "Init " + username); + + String filename = new File(switchboard.getConfig("dataRoot", ""), "DATA/TRIPLESTORE").toString()+"/"+username+"_triplestore.rdf"; + + Model tmp = ModelFactory.createDefaultModel(); + + Log.logInfo("TRIPLESTORE", "Loading from " + filename); + + try { + InputStream in = FileManager.get().open(filename); + + // read the RDF/XML file + tmp.read(in, null); + } + finally + { + privatestorage.put(username, tmp); + + } + + } + + } + catch (Exception anyex) { + + } + + + + // create separate model + + } + + public static void savePrivateStores(Switchboard switchboard) { + + if (privatestorage == null) return; + + for (Entry s : privatestorage.entrySet()) { + + String filename = new File(switchboard.getConfig("dataRoot", ""), "DATA/TRIPLESTORE").toString()+"/"+s.getKey()+"_triplestore.rdf"; + + FileOutputStream fout; + try { + + + fout = new FileOutputStream(filename); + + s.getValue().write(fout); + + } catch (Exception e) { + // TODO Auto-generated catch block + Log.logWarning("TRIPLESTORE", "Saving to " + filename+" failed"); + } + + } } } diff --git a/source/net/yacy/yacy.java b/source/net/yacy/yacy.java index fc64d8a06..ac3f377b0 100644 --- a/source/net/yacy/yacy.java +++ b/source/net/yacy/yacy.java @@ -138,6 +138,7 @@ public final class yacy { */ private static Switchboard sb = null; public static String homedir; + public static File dataHome_g; /** * Starts up the whole application. Sets up all datastructures and starts @@ -163,6 +164,7 @@ public final class yacy { // ensure that there is a DATA directory, if not, create one and if that fails warn and die mkdirsIfNeseccary(dataHome); + dataHome_g = dataHome; mkdirsIfNeseccary(appHome); File f = new File(dataHome, "DATA/"); mkdirsIfNeseccary(f); @@ -462,7 +464,7 @@ public final class yacy { * @see File#mkdirs() * @param path */ - private static void mkdirsIfNeseccary(final File path) { + public static void mkdirsIfNeseccary(final File path) { if (!(path.exists())) if(!path.mkdirs()) Log.logWarning("STARTUP", "could not create directories "+ path.toString());