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
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
70
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
6 /*
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
7 * #%L
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
8 * AnnotationManager
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
9 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
10 * Copyright (C) 2012 - 2014 MPIWG Berlin
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
11 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
12 * This program is free software: you can redistribute it and/or modify
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
13 * it under the terms of the GNU Lesser General Public License as
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
14 * published by the Free Software Foundation, either version 3 of the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
15 * License, or (at your option) any later version.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
16 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
17 * This program is distributed in the hope that it will be useful,
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
20 * GNU General Lesser Public License for more details.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
21 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
22 * You should have received a copy of the GNU General Lesser Public
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
23 * License along with this program. If not, see
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
24 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
25 * #L%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
26 */
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 63
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
29 import java.util.List;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
30
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
31 import org.json.JSONArray;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
32 import org.json.JSONException;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
33 import org.json.JSONObject;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
34 import org.restlet.data.Form;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
35 import org.restlet.data.Status;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
36 import org.restlet.ext.json.JsonRepresentation;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
37 import org.restlet.representation.Representation;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
38 import org.restlet.resource.Get;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
39
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
40 import de.mpiwg.itgroup.annotations.Annotation;
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
41 import de.mpiwg.itgroup.annotations.Person;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
44
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
45 /**
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
46 * Implements the "search" uri of the Annotator API. see
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
47 * <https://github.com/okfn/annotator/wiki/Storage>
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
48 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
49 * @author casties
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
50 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
51 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
52 public class AnnotatorSearch extends AnnotatorResourceImpl {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
53
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
54 protected String getAllowedMethodsForHeader() {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
55 return "OPTIONS,GET";
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
56 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
57
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
61 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
62 * @param entity
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
63 * @return
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
64 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
65 @Get("json")
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
66 public Representation doGetJSON(Representation entity) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
67 logger.fine("AnnotatorSearch doGetJSON!");
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
68 setCorsHeaders();
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
69 // do authentication
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
70 Person authUser = getUserFromAuthToken(entity);
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
71 logger.fine("request authenticated=" + authUser);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
72
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
73 Form form = getRequest().getResourceRef().getQueryAsForm();
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
74 String uri = form.getFirstValue("uri");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
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
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
82 logger.fine(String.format("searching for uri=%s user=%s", uri, user));
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
85 for (Annotation annot : annots) {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
86 // check permission
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
91 } else {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
92 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
93 return null;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
94 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
113 JSONObject result = new JSONObject();
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
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
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
117 } catch (JSONException e) {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
118 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
119 return null;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
120 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
121
89
247cbbb385de improved logging.
casties
parents: 88
diff changeset
122 logger.fine("sending response");
247cbbb385de improved logging.
casties
parents: 88
diff changeset
123 logger.finest(result.toString());
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
124 return new JsonRepresentation(result);
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
125 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
126
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
127 }