Mercurial > hg > AnnotationManagerN4J
annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java @ 44:5e9d90461929
rest interface for resources
| author | dwinter |
|---|---|
| date | Wed, 26 Sep 2012 17:01:59 +0200 |
| parents | 0731c4549065 |
| children | 9f8c9611848a |
| rev | line source |
|---|---|
| 4 | 1 /** |
| 2 * Implements the "search" uri of the Annotator API. | |
| 3 */ | |
| 4 package de.mpiwg.itgroup.annotations.restlet; | |
| 5 | |
| 6 import java.util.List; | |
| 7 | |
| 8 import org.json.JSONArray; | |
| 9 import org.json.JSONException; | |
| 10 import org.json.JSONObject; | |
| 11 import org.restlet.data.Form; | |
| 12 import org.restlet.data.Status; | |
| 13 import org.restlet.ext.json.JsonRepresentation; | |
| 14 import org.restlet.representation.Representation; | |
| 15 import org.restlet.resource.Get; | |
| 16 | |
| 17 import de.mpiwg.itgroup.annotations.Annotation; | |
| 15 | 18 import de.mpiwg.itgroup.annotations.Person; |
| 19 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | |
| 4 | 20 |
| 21 /** | |
| 22 * Implements the "search" uri of the Annotator API. see | |
| 23 * <https://github.com/okfn/annotator/wiki/Storage> | |
| 24 * | |
| 25 * @author casties | |
| 26 * | |
| 27 */ | |
| 28 public class AnnotatorSearch extends AnnotatorResourceImpl { | |
| 29 | |
| 30 protected String getAllowedMethodsForHeader() { | |
| 31 return "OPTIONS,GET"; | |
| 32 } | |
| 33 | |
| 34 /** | |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
35 * result for JSON content-type. optional search parameters: uri, user, limit, |
|
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
36 * offset. |
| 4 | 37 * |
| 38 * @param entity | |
| 39 * @return | |
| 40 */ | |
| 41 @Get("json") | |
| 42 public Representation doGetJSON(Representation entity) { | |
| 43 logger.debug("AnnotatorSearch doGetJSON!"); | |
| 44 setCorsHeaders(); | |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
45 // do authentication |
| 15 | 46 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
47 logger.debug("request authenticated=" + authUser); |
| 4 | 48 |
| 49 Form form = getRequest().getResourceRef().getQueryAsForm(); | |
| 50 String uri = form.getFirstValue("uri"); | |
| 51 String user = form.getFirstValue("user"); | |
| 52 String limit = form.getFirstValue("limit"); | |
| 53 String offset = form.getFirstValue("offset"); | |
| 54 | |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
55 JSONArray results = new JSONArray(); |
|
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
56 // do search |
|
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
57 logger.debug(String.format("searching for uri=%s user=%s", uri, user)); |
| 15 | 58 AnnotationStore store = getAnnotationStore(); |
|
32
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
15
diff
changeset
|
59 List<Annotation> annots = store.searchAnnotationByUriUser(uri, user, limit, offset); |
| 4 | 60 for (Annotation annot : annots) { |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
61 // check permission |
| 15 | 62 if (!annot.isActionAllowed("read", authUser, store)) continue; |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
63 JSONObject jo = createAnnotatorJson(annot, (authUser == null)); |
| 4 | 64 if (jo != null) { |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
65 results.put(jo); |
| 4 | 66 } else { |
| 67 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | |
| 68 return null; | |
| 69 } | |
| 70 } | |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
71 // assemble result object |
| 4 | 72 JSONObject result = new JSONObject(); |
| 73 try { | |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
74 result.put("rows", results); |
|
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
75 result.put("total", results.length()); |
| 4 | 76 } catch (JSONException e) { |
| 77 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | |
| 78 return null; | |
| 79 } | |
| 80 | |
| 81 logger.debug("sending:"); | |
| 82 logger.debug(result); | |
| 83 return new JsonRepresentation(result); | |
| 84 } | |
| 85 | |
| 86 } |
