annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java @ 88:b406507a953d

upped version to 0.5. can use display name and groups from auth token.
author casties
date Tue, 03 Feb 2015 19:01:27 +0100
parents 25eb2e1df106
children cf44d9e1a4a7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
1 package de.mpiwg.itgroup.annotations.restlet;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
2
70
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
3 /*
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
4 * #%L
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
5 * AnnotationManager
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
6 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
7 * Copyright (C) 2012 - 2014 MPIWG Berlin
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
8 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
9 * This program is free software: you can redistribute it and/or modify
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
10 * it under the terms of the GNU Lesser General Public License as
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
11 * published by the Free Software Foundation, either version 3 of the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
12 * License, or (at your option) any later version.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
13 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
14 * This program is distributed in the hope that it will be useful,
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
17 * GNU General Lesser Public License for more details.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
18 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
19 * You should have received a copy of the GNU General Lesser Public
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
20 * License along with this program. If not, see
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
22 * #L%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
23 */
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
24
65
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
25 import java.io.UnsupportedEncodingException;
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
26 import java.net.URLDecoder;
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
27 import java.util.ArrayList;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
28 import java.util.List;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
29
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
30 import org.json.JSONArray;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
31 import org.json.JSONException;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
32 import org.json.JSONObject;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
33 import org.restlet.data.Form;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
34 import org.restlet.data.Status;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
35 import org.restlet.ext.json.JsonRepresentation;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
36 import org.restlet.representation.Representation;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
37 import org.restlet.resource.Get;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
38
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
39 import de.mpiwg.itgroup.annotations.Annotation;
37
34b9d044d0bf authorisation added
dwinter
parents: 31
diff changeset
40 import de.mpiwg.itgroup.annotations.Person;
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
41 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
42 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
43
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
44 /**
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
45 * API for accessing tags in the Annotation store.
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
46 *
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
47 * @author dwinter
65
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
48 *
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
49 */
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
50 public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl {
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
51 protected String getAllowedMethodsForHeader() {
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
52 return "OPTIONS,GET";
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
53 }
65
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
54
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
55 @Get("json")
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
56 public Representation doGetJSON(Representation entity) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
57 logger.fine("AnnotatorAnnotatonsBytag doGetJSON!");
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
58 setCorsHeaders();
65
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
59
37
34b9d044d0bf authorisation added
dwinter
parents: 31
diff changeset
60 // do authentication
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
61 Person authUser = getUserFromAuthToken(entity);
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
62 logger.fine("request authenticated=" + authUser);
37
34b9d044d0bf authorisation added
dwinter
parents: 31
diff changeset
63
65
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
64 String id = (String) getRequest().getAttributes().get("id");
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
65 // URL decode
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
66 try {
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
67 id = URLDecoder.decode(id, "UTF-8");
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
68 } catch (UnsupportedEncodingException e) {
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
69 // this shouldn't happen
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
70 }
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
71 logger.fine("annotation-id=" + id);
65
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
72
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
73 Form form = getRequest().getResourceRef().getQueryAsForm();
65
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
74 String sortBy = form.getFirstValue("sortBy");
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
75
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
76 AnnotationStore store = getAnnotationStore();
58
f5c0e6df7e88 made uri prefixes in store configurable.
casties
parents: 50
diff changeset
77 String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
78 List<Annotation> annotations = store.getAnnotationsByTag(tagUri);
65
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
79
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
80 // JSONArray results = new JSONArray();
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
81 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
65
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
82
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
83 for (Annotation annot : annotations) {
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
84 // check permission
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
85 if (!annot.isActionAllowed("read", authUser, store))
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
86 continue;
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
87
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
88 JSONObject jo = createAnnotatorJson(annot, false);
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
89 results.add(jo);
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
90 }
65
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
91
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
92 if (sortBy != null) {
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
93 JSONObjectComparator.sortAnnotations(results, sortBy);
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
94 }
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
95
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
96 JSONArray resultsJa = new JSONArray();
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
97 for (JSONObject result : results) {
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
98 resultsJa.put(result);
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
99 }
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
100
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
101 // assemble result object
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
102 JSONObject result = new JSONObject();
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
103 try {
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
104 result.put("rows", resultsJa);
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
105 result.put("total", resultsJa.length());
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
106 } catch (JSONException e) {
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
107 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
108 return null;
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
109 }
c0dd5314bada deal with special characters in urls.
casties
parents: 58
diff changeset
110 return new JsonRepresentation(result);
28
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
111 }
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
112
f4ed2ed33e5b Restinterface zur Anzeige von Tags hinzugefuegt-
dwinter
parents:
diff changeset
113 }