annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java @ 15:58357a4b86de

ASSIGNED - # 249: Annotations shared in groups https://it-dev.mpiwg-berlin.mpg.de/tracs/mpdl-project-software/ticket/249
author casties
date Tue, 28 Aug 2012 20:23:12 +0200
parents 629e15b345aa
children 0731c4549065
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
1 /**
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
2 * Implements the "search" uri of the Annotator API.
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
3 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
4 package de.mpiwg.itgroup.annotations.restlet;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
5
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
6 import java.util.List;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
7
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
8 import org.json.JSONArray;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
9 import org.json.JSONException;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
10 import org.json.JSONObject;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
11 import org.restlet.data.Form;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
12 import org.restlet.data.Status;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
13 import org.restlet.ext.json.JsonRepresentation;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
14 import org.restlet.representation.Representation;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
15 import org.restlet.resource.Get;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
16
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
17 import de.mpiwg.itgroup.annotations.Annotation;
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
18 import de.mpiwg.itgroup.annotations.Person;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
19 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
20
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
21 /**
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
22 * Implements the "search" uri of the Annotator API. see
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
23 * <https://github.com/okfn/annotator/wiki/Storage>
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
24 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
25 * @author casties
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
26 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
27 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
28 public class AnnotatorSearch extends AnnotatorResourceImpl {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
29
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
30 protected String getAllowedMethodsForHeader() {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
31 return "OPTIONS,GET";
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
32 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
33
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
37 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
38 * @param entity
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
39 * @return
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
40 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
41 @Get("json")
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
42 public Representation doGetJSON(Representation entity) {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
43 logger.debug("AnnotatorSearch doGetJSON!");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
44 setCorsHeaders();
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
45 // do authentication
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
48
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
49 Form form = getRequest().getResourceRef().getQueryAsForm();
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
50 String uri = form.getFirstValue("uri");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
51 String user = form.getFirstValue("user");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
52 String limit = form.getFirstValue("limit");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
53 String offset = form.getFirstValue("offset");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
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
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
58 AnnotationStore store = getAnnotationStore();
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
59 List<Annotation> annots = store.searchByUriUser(uri, user, limit, offset);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
60 for (Annotation annot : annots) {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
61 // check permission
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
64 if (jo != null) {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
65 results.put(jo);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
66 } else {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
67 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
68 return null;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
69 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
70 }
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
71 // assemble result object
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
72 JSONObject result = new JSONObject();
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
76 } catch (JSONException e) {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
77 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
78 return null;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
79 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
80
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
81 logger.debug("sending:");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
82 logger.debug(result);
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
83 return new JsonRepresentation(result);
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
84 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
85
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
86 }