Mercurial > hg > AnnotationManagerN4J
diff src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java @ 44:5e9d90461929
rest interface for resources
author | dwinter |
---|---|
date | Wed, 26 Sep 2012 17:01:59 +0200 |
parents | |
children | 64aa756c60cc |
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