annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorGroups.java @ 91:cf44d9e1a4a7

let CORS be handled by Restlet 2.3 CorsFilter.
author casties
date Sun, 08 Feb 2015 18:09:00 +0100
parents b406507a953d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
1 package de.mpiwg.itgroup.annotations.restlet;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
2
70
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
3 /*
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
4 * #%L
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
5 * AnnotationManager
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
6 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
7 * Copyright (C) 2012 - 2014 MPIWG Berlin
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
8 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
9 * This program is free software: you can redistribute it and/or modify
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
10 * it under the terms of the GNU Lesser General Public License as
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
11 * published by the Free Software Foundation, either version 3 of the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
12 * License, or (at your option) any later version.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
13 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
14 * This program is distributed in the hope that it will be useful,
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
17 * GNU General Lesser Public License for more details.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
18 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
19 * You should have received a copy of the GNU General Lesser Public
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
20 * License along with this program. If not, see
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
22 * #L%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
23 */
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 15
diff changeset
24
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
25 import java.util.List;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
26
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
27 import org.json.JSONArray;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
28 import org.json.JSONException;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
29 import org.json.JSONObject;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
30 import org.neo4j.graphdb.Node;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
31 import org.restlet.data.Form;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
32 import org.restlet.data.Status;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
33 import org.restlet.ext.json.JsonRepresentation;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
34 import org.restlet.representation.Representation;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
35 import org.restlet.resource.Get;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
36
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
37 import de.mpiwg.itgroup.annotations.Actor;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
38 import de.mpiwg.itgroup.annotations.Group;
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
39 import de.mpiwg.itgroup.annotations.Person;
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
40 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
41
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
42
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
43 /**
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
44 * API for accessing groups in the Annotation store.
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
45 *
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
46 * @author casties
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
47 *
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
48 */
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
49 public class AnnotatorGroups extends AnnotatorResourceImpl {
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
50
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
51 /**
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
52 * GET with JSON content-type.
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
53 * Parameters:
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
54 * user: short user name
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
55 * uri: user uri
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
56 *
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
57 * @param entity
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
58 * @return
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
59 */
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
60 @Get("json")
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
61 public Representation doGetJSON(Representation entity) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
62 logger.fine("AnnotatorGroups doGetJSON!");
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
63 // get user from auth token (preferred)
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
64 Person authUser = getUserFromAuthToken(entity);
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
65 JSONArray results = null;
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
66 if (authUser != null && authUser.groups != null) {
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
67 results = getGroupsFromPerson(authUser);
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
68 } else {
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
69 // get user or uri from request
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
70 Form form = getRequest().getResourceRef().getQueryAsForm();
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
71 String user = form.getFirstValue("user");
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
72 String uri = form.getFirstValue("uri");
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
73 results = getGroupsFromStore(uri, user);
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
74 }
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
75 // assemble result object
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
76 JSONObject result = new JSONObject();
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
77 try {
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
78 result.put("rows", results);
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
79 result.put("total", results.length());
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
80 } catch (JSONException e) {
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
81 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
82 return null;
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
83 }
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
84 logger.fine("sending:");
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
85 logger.fine(result.toString());
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
86 return new JsonRepresentation(result);
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
87 }
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
89 public JSONArray getGroupsFromPerson(Person person) {
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
90 JSONArray results = new JSONArray();
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
91 for (String group : person.groups) {
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
92 JSONObject jo = new JSONObject();
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
93 try {
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
94 jo.put("id", group);
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
95 jo.put("name", group);
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
96 } catch (JSONException e) {
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
97 }
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
98 results.put(jo);
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
99 }
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
100 return results;
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
101 }
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
102
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
103 public JSONArray getGroupsFromStore(String uri, String user) {
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
104 JSONArray results = new JSONArray();
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
105 if (uri == null || uri.isEmpty()) {
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
106 // get uri from user-id
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
107 uri = Actor.getUriFromId(user, false);
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
108 }
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
109 AnnotationStore store = getAnnotationStore();
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
110 Node person = store.getPersonNodeByUri(uri);
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
111 if (person != null) {
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
112 List<Group> groups = store.getGroupsForPersonNode(person);
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
113 for (Group group : groups) {
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
114 JSONObject jo = new JSONObject();
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
115 try {
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
116 jo.put("id", group.getId());
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
117 jo.put("name", group.getName());
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
118 jo.put("uri", group.getUriString());
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
119 } catch (JSONException e) {
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
120 }
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
121 results.put(jo);
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
122 }
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
123 }
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
124 return results;
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
125 }
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents:
diff changeset
126 }