Mercurial > hg > AnnotationManagerN4J
diff src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.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/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); + } + + + +}