annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java @ 14:629e15b345aa

permissions mostly work. need more server-side checking.
author casties
date Fri, 13 Jul 2012 20:41:02 +0200
parents 3599b29c393f
children 58357a4b86de
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;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
18
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
19 /**
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
20 * Implements the "search" uri of the Annotator API. see
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
21 * <https://github.com/okfn/annotator/wiki/Storage>
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
22 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
23 * @author casties
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
24 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
25 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
26 public class AnnotatorSearch extends AnnotatorResourceImpl {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
27
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
28 protected String getAllowedMethodsForHeader() {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
29 return "OPTIONS,GET";
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
30 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
31
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
32 /**
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
33 * 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
34 * offset.
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
35 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
36 * @param entity
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
37 * @return
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
38 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
39 @Get("json")
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
40 public Representation doGetJSON(Representation entity) {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
41 logger.debug("AnnotatorSearch doGetJSON!");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
42 setCorsHeaders();
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
43 // do authentication
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
44 String authUser = this.checkAuthToken(entity);
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
45 logger.debug("request authenticated=" + authUser);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
46
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
47 Form form = getRequest().getResourceRef().getQueryAsForm();
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
48 String uri = form.getFirstValue("uri");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
49 String user = form.getFirstValue("user");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
50 String limit = form.getFirstValue("limit");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
51 String offset = form.getFirstValue("offset");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
52
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
53 JSONArray results = new JSONArray();
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
54 // do search
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
55 logger.debug(String.format("searching for uri=%s user=%s", uri, user));
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
56 List<Annotation> annots = getAnnotationStore().searchByUriUser(uri, user, limit, offset);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
57 for (Annotation annot : annots) {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
58 // check permission
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
59 if (!annot.isActionAllowed("read", authUser)) continue;
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
60 JSONObject jo = createAnnotatorJson(annot, (authUser == null));
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
61 if (jo != null) {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
62 results.put(jo);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
63 } else {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
64 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
65 return null;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
66 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
67 }
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
68 // assemble result object
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
69 JSONObject result = new JSONObject();
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
70 try {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
71 result.put("rows", results);
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
72 result.put("total", results.length());
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
73 } catch (JSONException e) {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
74 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
75 return null;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
76 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
77
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
78 logger.debug("sending:");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
79 logger.debug(result);
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
80 return new JsonRepresentation(result);
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
81 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
82
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
83 }