Mercurial > hg > AnnotationManagerN4J
annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java @ 65:c0dd5314bada
deal with special characters in urls.
| author | casties |
|---|---|
| date | Wed, 05 Dec 2012 15:36:43 +0100 |
| parents | 9f8c9611848a |
| children | 2b1e6df5e21a |
| rev | line source |
|---|---|
| 4 | 1 /** |
| 2 * Implements the "search" uri of the Annotator API. | |
| 3 */ | |
| 4 package de.mpiwg.itgroup.annotations.restlet; | |
| 5 | |
|
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
6 import java.util.ArrayList; |
| 4 | 7 import java.util.List; |
| 8 | |
| 9 import org.json.JSONArray; | |
| 10 import org.json.JSONException; | |
| 11 import org.json.JSONObject; | |
| 12 import org.restlet.data.Form; | |
| 13 import org.restlet.data.Status; | |
| 14 import org.restlet.ext.json.JsonRepresentation; | |
| 15 import org.restlet.representation.Representation; | |
| 16 import org.restlet.resource.Get; | |
| 17 | |
| 18 import de.mpiwg.itgroup.annotations.Annotation; | |
| 15 | 19 import de.mpiwg.itgroup.annotations.Person; |
| 20 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | |
|
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
21 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator; |
| 4 | 22 |
| 23 /** | |
| 24 * Implements the "search" uri of the Annotator API. see | |
| 25 * <https://github.com/okfn/annotator/wiki/Storage> | |
| 26 * | |
| 27 * @author casties | |
| 28 * | |
| 29 */ | |
| 30 public class AnnotatorSearch extends AnnotatorResourceImpl { | |
| 31 | |
| 32 protected String getAllowedMethodsForHeader() { | |
| 33 return "OPTIONS,GET"; | |
| 34 } | |
| 35 | |
| 36 /** | |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
37 * result for JSON content-type. optional search parameters: uri, user, limit, |
|
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
38 * offset, sortBy. |
| 4 | 39 * |
| 40 * @param entity | |
| 41 * @return | |
| 42 */ | |
| 43 @Get("json") | |
| 44 public Representation doGetJSON(Representation entity) { | |
| 45 logger.debug("AnnotatorSearch doGetJSON!"); | |
| 46 setCorsHeaders(); | |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
47 // do authentication |
| 15 | 48 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
49 logger.debug("request authenticated=" + authUser); |
| 4 | 50 |
| 51 Form form = getRequest().getResourceRef().getQueryAsForm(); | |
| 52 String uri = form.getFirstValue("uri"); | |
| 53 String user = form.getFirstValue("user"); | |
|
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
54 int limit = getInt(form.getFirstValue("limit")); |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
55 int offset = getInt(form.getFirstValue("offset")); |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
56 String sortBy = form.getFirstValue("sortBy"); |
| 4 | 57 |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
58 // do search |
|
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
59 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
60 logger.debug(String.format("searching for uri=%s user=%s", uri, user)); |
| 15 | 61 AnnotationStore store = getAnnotationStore(); |
|
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
62 List<Annotation> annots = store.searchAnnotationByUriUser(uri, user); |
| 4 | 63 for (Annotation annot : annots) { |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
64 // check permission |
| 15 | 65 if (!annot.isActionAllowed("read", authUser, store)) continue; |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
66 JSONObject jo = createAnnotatorJson(annot, (authUser == null)); |
| 4 | 67 if (jo != null) { |
|
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
68 results.add(jo); |
| 4 | 69 } else { |
| 70 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | |
| 71 return null; | |
| 72 } | |
| 73 } | |
|
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
74 |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
75 // sort if necessary |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
76 if (sortBy != null) { |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
77 JSONObjectComparator.sortAnnotations(results, sortBy); |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
78 } |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
79 |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
80 // put in JSON list |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
81 JSONArray rows = new JSONArray(); |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
82 int cnt = 0; |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
83 for (JSONObject result : results) { |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
84 cnt += 1; |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
85 if (offset > 0 && cnt < offset) continue; |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
86 rows.put(result); |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
87 if (limit > 0 && cnt >= limit) break; |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
88 } |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
89 |
|
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
90 // assemble result object |
| 4 | 91 JSONObject result = new JSONObject(); |
| 92 try { | |
|
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
93 result.put("rows", rows); |
|
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
94 result.put("total", rows.length()); |
| 4 | 95 } catch (JSONException e) { |
| 96 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | |
| 97 return null; | |
| 98 } | |
| 99 | |
| 100 logger.debug("sending:"); | |
| 101 logger.debug(result); | |
| 102 return new JsonRepresentation(result); | |
| 103 } | |
| 104 | |
| 105 } |
