annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java @ 105:7417f5915181 default tip

check admin permission before changing permissions. Enum for typesafe actions.
author casties
date Fri, 10 Feb 2017 15:45:35 +0100
parents cf44d9e1a4a7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
1 package de.mpiwg.itgroup.annotations.restlet;
5e9d90461929 rest interface for resources
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: 50
diff changeset
25 import java.io.UnsupportedEncodingException;
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
26 import java.net.URLDecoder;
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
27 import java.util.ArrayList;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
28 import java.util.List;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
29
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
30 import org.json.JSONArray;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
31 import org.json.JSONException;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
32 import org.json.JSONObject;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
33 import org.restlet.data.Form;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
34 import org.restlet.data.Parameter;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
35 import org.restlet.data.Status;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
36 import org.restlet.ext.json.JsonRepresentation;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
37 import org.restlet.representation.Representation;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
38 import org.restlet.resource.Get;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
39
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
40 import de.mpiwg.itgroup.annotations.Annotation;
105
7417f5915181 check admin permission before changing permissions.
casties
parents: 91
diff changeset
41 import de.mpiwg.itgroup.annotations.Annotation.Action;
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
42 import de.mpiwg.itgroup.annotations.Person;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
43 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
44 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
45
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
46 /**
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
47 * API for accessing tags in the Annotation store.
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
48 *
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
49 * @author dwinter
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
50 *
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
51 */
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
52 public class AnnotatorAnnotationsByResources extends AnnotatorResourceImpl {
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
53
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
54 @Get("json")
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
55 public Representation doGetJSON(Representation entity) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
56 logger.fine("AnnotatorAnnotatonsByResource doGetJSON!");
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
57
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
58 // do authentication
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
59 Person authUser = getUserFromAuthToken(entity);
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
60 logger.fine("request authenticated=" + authUser);
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
61
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
62 String id = null;
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
63 String jsonId = (String) getRequest().getAttributes().get("id");
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
64 if (jsonId != null) {
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
65 // URL decode
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
66 try {
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
67 jsonId = URLDecoder.decode(jsonId, "UTF-8");
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
68 } catch (UnsupportedEncodingException e) {
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
69 // this shouldn't happen
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
70 }
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
71 id = decodeJsonId(jsonId);
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
72 // String id = jsonId;
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
73 logger.fine("ressource-id=" + id);
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
74 }
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
75
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
76 Form form = getRequest().getResourceRef().getQueryAsForm();
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
77 String sortBy = null;
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
78 for (Parameter parameter : form) {
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
79 if (parameter.getName().equals("sortBy")) {
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
80 sortBy = parameter.getValue();
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
81 }
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
82 }
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
83
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
84 AnnotationStore store = getAnnotationStore();
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
85 // String tagUri=NS.MPIWG_TAGS_URL+id;
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
86 List<Annotation> annotations = store.getAnnotationsByResource(id);
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
87
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
88 // JSONArray results = new JSONArray();
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
89 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
90
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
91 for (Annotation annot : annotations) {
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
92 // check permission
105
7417f5915181 check admin permission before changing permissions.
casties
parents: 91
diff changeset
93 if (!annot.isActionAllowed(Action.read, authUser, store))
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
94 continue;
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
95
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
96 JSONObject jo = createAnnotatorJson(annot, false);
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
97 results.add(jo);
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
98 }
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
99
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
100 if (sortBy != null) {
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
101 JSONObjectComparator.sortAnnotations(results, sortBy);
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
102 }
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
103
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
104 JSONArray resultsJa = new JSONArray();
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
105 for (JSONObject result : results) {
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
106 resultsJa.put(result);
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
107 }
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
108
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
109 // assemble result object
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
110 JSONObject result = new JSONObject();
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
111 try {
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
112 result.put("rows", resultsJa);
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
113 result.put("total", resultsJa.length());
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
114 } catch (JSONException e) {
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
115 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
116 return null;
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
117 }
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
118 return new JsonRepresentation(result);
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
119 }
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
120
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
121 }