1 | package de.mpiwg.itgroup.annotations.restlet; |
---|
2 | |
---|
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 | |
---|
25 | import java.util.ArrayList; |
---|
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; |
---|
38 | import de.mpiwg.itgroup.annotations.Annotation.Action; |
---|
39 | import de.mpiwg.itgroup.annotations.Person; |
---|
40 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
41 | import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator; |
---|
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 | /** |
---|
53 | * result for JSON content-type. optional search parameters: uri, user, limit, |
---|
54 | * offset, sortBy. |
---|
55 | * |
---|
56 | * @param entity |
---|
57 | * @return |
---|
58 | */ |
---|
59 | @Get("json") |
---|
60 | public Representation doGetJSON(Representation entity) { |
---|
61 | logger.fine("AnnotatorSearch doGetJSON!"); |
---|
62 | // do authentication |
---|
63 | Person authUser = getUserFromAuthToken(entity); |
---|
64 | logger.fine("request authenticated=" + authUser); |
---|
65 | |
---|
66 | Form form = getRequest().getResourceRef().getQueryAsForm(); |
---|
67 | String uri = form.getFirstValue("uri"); |
---|
68 | String user = form.getFirstValue("user"); |
---|
69 | int limit = getInt(form.getFirstValue("limit")); |
---|
70 | int offset = getInt(form.getFirstValue("offset")); |
---|
71 | String sortBy = form.getFirstValue("sortBy"); |
---|
72 | |
---|
73 | // do search |
---|
74 | ArrayList<JSONObject> results = new ArrayList<JSONObject>(); |
---|
75 | logger.fine(String.format("searching for uri=%s user=%s", uri, user)); |
---|
76 | AnnotationStore store = getAnnotationStore(); |
---|
77 | List<Annotation> annots = store.searchAnnotationByUriUser(uri, user); |
---|
78 | for (Annotation annot : annots) { |
---|
79 | // check permission |
---|
80 | if (!annot.isActionAllowed(Action.read, authUser, store)) continue; |
---|
81 | JSONObject jo = createAnnotatorJson(annot, (authUser == null)); |
---|
82 | if (jo != null) { |
---|
83 | results.add(jo); |
---|
84 | } else { |
---|
85 | setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); |
---|
86 | return null; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | // sort if necessary |
---|
91 | if (sortBy != null) { |
---|
92 | JSONObjectComparator.sortAnnotations(results, sortBy); |
---|
93 | } |
---|
94 | |
---|
95 | // put in JSON list |
---|
96 | JSONArray rows = new JSONArray(); |
---|
97 | int cnt = 0; |
---|
98 | for (JSONObject result : results) { |
---|
99 | cnt += 1; |
---|
100 | if (offset > 0 && cnt < offset) continue; |
---|
101 | rows.put(result); |
---|
102 | if (limit > 0 && cnt >= limit) break; |
---|
103 | } |
---|
104 | |
---|
105 | // assemble result object |
---|
106 | JSONObject result = new JSONObject(); |
---|
107 | try { |
---|
108 | result.put("rows", rows); |
---|
109 | result.put("total", rows.length()); |
---|
110 | } catch (JSONException e) { |
---|
111 | setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); |
---|
112 | return null; |
---|
113 | } |
---|
114 | |
---|
115 | logger.fine("sending response"); |
---|
116 | logger.finest(result.toString()); |
---|
117 | return new JsonRepresentation(result); |
---|
118 | } |
---|
119 | |
---|
120 | } |
---|