changeset 44:5e9d90461929

rest interface for resources
author dwinter
date Wed, 26 Sep 2012 17:01:59 +0200
parents d1bef7952bec
children 707902d468f6 c6cab006f799
files src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java src/main/webapp/annotationBrowser/css/resource.css src/main/webapp/annotationBrowser/js/resource.js
diffstat 5 files changed, 310 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java	Wed Sep 26 17:01:59 2012 +0200
@@ -0,0 +1,100 @@
+package de.mpiwg.itgroup.annotations.restlet;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.neo4j.graphdb.Node;
+import org.restlet.data.Form;
+import org.restlet.data.Parameter;
+import org.restlet.data.Status;
+import org.restlet.ext.json.JsonRepresentation;
+import org.restlet.representation.Representation;
+import org.restlet.resource.Get;
+import org.restlet.resource.ServerResource;
+
+import de.mpiwg.itgroup.annotations.Annotation;
+import de.mpiwg.itgroup.annotations.NS;
+import de.mpiwg.itgroup.annotations.Person;
+import de.mpiwg.itgroup.annotations.Tag;
+import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
+import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
+
+/**
+ * API for accessing tags in the Annotation store.
+ * 
+ * @author dwinter
+ *
+ */
+public class AnnotatorAnnotationsByResources extends AnnotatorResourceImpl {
+    protected String getAllowedMethodsForHeader() {
+        return "OPTIONS,GET";
+    }
+    
+    @Get("json")
+    public Representation doGetJSON(Representation entity) {
+        logger.debug("AnnotatorAnnotatonsByResource doGetJSON!");
+        setCorsHeaders();
+       
+        // do authentication
+        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
+        logger.debug("request authenticated=" + authUser);
+
+        
+        String jsonId = (String) getRequest().getAttributes().get("id");
+       
+        Form form = getRequest().getResourceRef().getQueryAsForm();
+        String sortBy=null;
+        for (Parameter parameter : form) {
+          if (parameter.getName().equals("sortBy")){
+          sortBy =  parameter.getValue();
+          }
+        }
+        
+        String id = decodeJsonId(jsonId);
+        //String id = jsonId;
+        logger.debug("ressource-id=" + id);
+        
+        AnnotationStore store = getAnnotationStore();
+  	  	//String tagUri=NS.MPIWG_TAGS_URL+id;
+        List<Annotation> annotations = store.getAnnotationsByResource(id);
+        
+		//JSONArray results = new JSONArray();
+        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
+        
+		for (Annotation annot : annotations) {
+			//check permission
+			if (!annot.isActionAllowed("read", authUser, store)) continue;
+            
+			JSONObject jo = createAnnotatorJson(annot,false);
+            results.add(jo);
+        }
+    
+	if (sortBy!=null){
+		JSONObjectComparator.sortAnnotations(results,sortBy);
+	}
+		
+	JSONArray resultsJa = new JSONArray();
+	for (JSONObject result:results){
+		resultsJa.put(result);
+	}
+	
+    // assemble result object
+    JSONObject result = new JSONObject();
+    try {
+        result.put("rows", resultsJa);
+        result.put("total", resultsJa.length());
+    } catch (JSONException e) {
+        setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
+        return null;
+    }
+    logger.debug("sending:");
+    logger.debug(result);
+    return new JsonRepresentation(result);
+}
+
+    
+	
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java	Wed Sep 26 17:01:59 2012 +0200
@@ -0,0 +1,111 @@
+/**
+ * ReST API for accessing groups in the Annotation store.
+ */
+package de.mpiwg.itgroup.annotations.restlet;
+
+import java.util.List;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.neo4j.graphdb.Node;
+import org.restlet.data.Form;
+import org.restlet.data.Status;
+import org.restlet.ext.json.JsonRepresentation;
+import org.restlet.representation.Representation;
+import org.restlet.resource.Get;
+
+import de.mpiwg.itgroup.annotations.Actor;
+import de.mpiwg.itgroup.annotations.Group;
+import de.mpiwg.itgroup.annotations.NS;
+import de.mpiwg.itgroup.annotations.Resource;
+import de.mpiwg.itgroup.annotations.Tag;
+import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
+import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore.NodeTypes;
+
+
+/**
+ * API for accessing tags in the Annotation store.
+ * 
+ * @author dwinter
+ *
+ */
+public class AnnotatorResources extends AnnotatorResourceImpl {
+    protected String getAllowedMethodsForHeader() {
+        return "OPTIONS,GET";
+    }
+
+    /**
+     * GET with JSON content-type.
+     * Parameters: 
+     *   user: short user name
+     *   uri: user uri
+     *   
+     * @param entity
+     * @return
+     */
+    @Get("json")
+    public Representation doGetJSON(Representation entity) {
+        logger.debug("AnnotatorGroups doGetJSON!");
+        setCorsHeaders();
+       
+        String jsonId = (String) getRequest().getAttributes().get("id");
+        String uri = decodeJsonId(jsonId);
+        
+        logger.debug("resources-id=" + uri);
+
+        if (uri==null){
+        return getAllResources();
+        } else {
+
+        	return getResource(uri);
+        }
+    }
+    
+    protected Representation getResource(String uri){
+    	  AnnotationStore store = getAnnotationStore();
+    	  //String tagUri=NS.MPIWG_TAGS_URL+id;
+          Node tagNode = store.getResourceNodeByUri(uri);
+          Resource resource = store.createResourceFromNode(tagNode);
+          JSONObject jo = new JSONObject();
+          try {
+              jo.put("id", encodeJsonId(resource.getUri()));
+              jo.put("uri", resource.getUri());
+          } catch (JSONException e) {
+          }
+          
+          return new JsonRepresentation(jo);
+    }
+	protected Representation getAllResources() {
+		JSONArray results = new JSONArray();
+        AnnotationStore store = getAnnotationStore();
+        
+       
+            List<Resource> resources = store.getResources(null, null);
+            for (Resource resource : resources) {
+                JSONObject jo = new JSONObject();
+                try {
+                	 jo.put("id", encodeJsonId(resource.getUri()));
+                     jo.put("uri", resource.getUri());
+                } catch (JSONException e) {
+                }
+                results.put(jo);
+            }
+        
+        // assemble result object
+        JSONObject result = new JSONObject();
+        try {
+            result.put("rows", results);
+            result.put("total", results.length());
+        } catch (JSONException e) {
+            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
+            return null;
+        }
+        logger.debug("sending:");
+        logger.debug(result);
+        return new JsonRepresentation(result);
+	}        	
+        
+
+    
+}
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java	Wed Sep 26 16:27:52 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java	Wed Sep 26 17:01:59 2012 +0200
@@ -43,8 +43,10 @@
         router.attach("/groups", AnnotatorGroups.class);
         router.attach("/tags", AnnotatorTags.class);
         router.attach("/tags/{id}", AnnotatorTags.class);
-        router.attach("/tags/{id}", AnnotatorTags.class);
         router.attach("/tags/{id}/annotations", AnnotatorAnnotationsByTags.class);
+        router.attach("/resources", AnnotatorResources.class);
+        router.attach("/resources/{id}", AnnotatorResources.class);
+        router.attach("/resources/{id}/annotations", AnnotatorAnnotationsByResources.class);
         router.attach("/", AnnotatorInfo.class);
         // authenticator.setNext(router);
         // return authenticator;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/webapp/annotationBrowser/css/resource.css	Wed Sep 26 17:01:59 2012 +0200
@@ -0,0 +1,31 @@
+/*
+Cascading Style Sheets, level 2 revision 1. 
+CSS 2.1 Specification is available at 
+http://www.w3.org/TR/2006/WD-CSS21-20061106/
+*/
+
+#viewer {display:table-row}
+#tagList { display:table-cell;
+padding:40px;
+border-right:2px solid;
+}
+
+
+#annotations { display:table-cell}
+
+.annotationRow {padding-bottom:10px;
+border-bottom:2px solid;
+display:table-row}
+
+.annotationCol {
+display:table-cell;
+padding-left:20px;
+padding-bottom:10px;
+}
+.dcMetaData {
+display:table-row;
+}
+
+.annotatonMetaData{
+padding-bottom:30px;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/webapp/annotationBrowser/js/resource.js	Wed Sep 26 17:01:59 2012 +0200
@@ -0,0 +1,65 @@
+function annotations(ressourceId){
+	
+	
+	if (token==""){
+		setTimeout(function(){annotations();},500);
+		return;
+	}
+	
+	var headers = {'x-annotator-auth-token':token};
+	
+	$.ajax({
+		  url: annotServer+'ressources/'+ressourceId+'/annotations?sortBy=uri',
+		  dataType: 'json',
+		  headers:headers,
+		  success: function(data) {
+		//var items = [];
+		  var rows = data["rows"];
+		  
+		  $('#annotations').html("");
+		  for (var i=0;i<rows.length;i++){
+			  var text=rows[i]['text'];
+			  var author=rows[i]['user']['name'];
+			  var docUri=createLinkFromURI(rows[i]['uri']);
+			  
+			  var ret = '<div id="an_'+i+'" class="annotationRow">';
+			  ret +='<div class="annotationCol">'+text+'</div>';
+			  ret +='<div class="annotationCol">'+author+'</div>';
+			  ret +='<div class="annotationCol"><a target="libcoll" href="'+docUri+'">'+docUri+'</a></div>';
+			  ret+="</div>";
+				
+			  $('#annotations').append(ret);
+			  //items.push(ret);
+			  getDCdata(rows[i]['uri'],$('#an_'+i));
+		  }
+		  //$('#annotations').html(items.join(''));
+		 
+		  }
+	});
+}
+
+
+$(document).ready(function(){
+	
+	getToken(tokenGenerator,username);
+	$.getJSON(annotServer+'ressources', function(data) {
+		  var items = [];
+		  var rows = data["rows"];
+		 
+		  for (var i=0;i<rows.length;i++){
+			  	 val=rows[i]['name'];
+				 items.push('<div class="tagLink" onclick="javascript: annotations(\''+rows[i]["id"]+'\');">'+val+'</div>');
+			
+		  }
+		  
+		
+		  $('#tagList').html(items.join(''));
+		  annotations();		 
+		});
+
+
+	
+ });
+
+
+