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