Mercurial > hg > AnnotationManagerN4J
annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java @ 105:7417f5915181 default tip
check admin permission before changing permissions.
Enum for typesafe actions.
author | casties |
---|---|
date | Fri, 10 Feb 2017 15:45:35 +0100 |
parents | cf44d9e1a4a7 |
children |
rev | line source |
---|---|
4 | 1 package de.mpiwg.itgroup.annotations.restlet; |
2 | |
70 | 3 /* |
4 * #%L | |
5 * AnnotationManager | |
6 * %% | |
7 * Copyright (C) 2012 - 2014 MPIWG Berlin | |
8 * %% | |
9 * This program is free software: you can redistribute it and/or modify | |
10 * it under the terms of the GNU Lesser General Public License as | |
11 * published by the Free Software Foundation, either version 3 of the | |
12 * License, or (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Lesser Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Lesser Public | |
20 * License along with this program. If not, see | |
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>. | |
22 * #L% | |
23 */ | |
24 | |
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
25 import java.util.ArrayList; |
4 | 26 import java.util.List; |
27 | |
28 import org.json.JSONArray; | |
29 import org.json.JSONException; | |
30 import org.json.JSONObject; | |
31 import org.restlet.data.Form; | |
32 import org.restlet.data.Status; | |
33 import org.restlet.ext.json.JsonRepresentation; | |
34 import org.restlet.representation.Representation; | |
35 import org.restlet.resource.Get; | |
36 | |
37 import de.mpiwg.itgroup.annotations.Annotation; | |
105 | 38 import de.mpiwg.itgroup.annotations.Annotation.Action; |
15 | 39 import de.mpiwg.itgroup.annotations.Person; |
40 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
|
41 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator; |
4 | 42 |
43 /** | |
44 * Implements the "search" uri of the Annotator API. see | |
45 * <https://github.com/okfn/annotator/wiki/Storage> | |
46 * | |
47 * @author casties | |
48 * | |
49 */ | |
50 public class AnnotatorSearch extends AnnotatorResourceImpl { | |
51 | |
52 /** | |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
53 * 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
|
54 * offset, sortBy. |
4 | 55 * |
56 * @param entity | |
57 * @return | |
58 */ | |
59 @Get("json") | |
60 public Representation doGetJSON(Representation entity) { | |
75 | 61 logger.fine("AnnotatorSearch doGetJSON!"); |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
62 // do authentication |
88 | 63 Person authUser = getUserFromAuthToken(entity); |
75 | 64 logger.fine("request authenticated=" + authUser); |
4 | 65 |
66 Form form = getRequest().getResourceRef().getQueryAsForm(); | |
67 String uri = form.getFirstValue("uri"); | |
68 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
|
69 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
|
70 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
|
71 String sortBy = form.getFirstValue("sortBy"); |
4 | 72 |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
73 // 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
|
74 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); |
75 | 75 logger.fine(String.format("searching for uri=%s user=%s", uri, user)); |
15 | 76 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
|
77 List<Annotation> annots = store.searchAnnotationByUriUser(uri, user); |
4 | 78 for (Annotation annot : annots) { |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
79 // check permission |
105 | 80 if (!annot.isActionAllowed(Action.read, authUser, store)) continue; |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
81 JSONObject jo = createAnnotatorJson(annot, (authUser == null)); |
4 | 82 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
|
83 results.add(jo); |
4 | 84 } else { |
85 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | |
86 return null; | |
87 } | |
88 } | |
63
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
89 |
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
90 // 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
|
91 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
|
92 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
|
93 } |
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
94 |
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
95 // 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 } |
9f8c9611848a
fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents:
32
diff
changeset
|
104 |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
105 // assemble result object |
4 | 106 JSONObject result = new JSONObject(); |
107 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
|
108 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
|
109 result.put("total", rows.length()); |
4 | 110 } catch (JSONException e) { |
111 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | |
112 return null; | |
113 } | |
114 | |
89 | 115 logger.fine("sending response"); |
116 logger.finest(result.toString()); | |
4 | 117 return new JsonRepresentation(result); |
118 } | |
119 | |
120 } |