28
|
1 package de.mpiwg.itgroup.annotations.restlet;
|
|
2
|
70
|
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
|
65
|
25 import java.io.UnsupportedEncodingException;
|
|
26 import java.net.URLDecoder;
|
28
|
27 import java.util.ArrayList;
|
|
28 import java.util.List;
|
|
29
|
|
30 import org.json.JSONArray;
|
|
31 import org.json.JSONException;
|
|
32 import org.json.JSONObject;
|
|
33 import org.restlet.data.Form;
|
|
34 import org.restlet.data.Status;
|
|
35 import org.restlet.ext.json.JsonRepresentation;
|
|
36 import org.restlet.representation.Representation;
|
|
37 import org.restlet.resource.Get;
|
|
38
|
|
39 import de.mpiwg.itgroup.annotations.Annotation;
|
37
|
40 import de.mpiwg.itgroup.annotations.Person;
|
28
|
41 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
|
|
42 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
|
|
43
|
|
44 /**
|
|
45 * API for accessing tags in the Annotation store.
|
|
46 *
|
|
47 * @author dwinter
|
65
|
48 *
|
28
|
49 */
|
|
50 public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl {
|
|
51 protected String getAllowedMethodsForHeader() {
|
|
52 return "OPTIONS,GET";
|
|
53 }
|
65
|
54
|
28
|
55 @Get("json")
|
|
56 public Representation doGetJSON(Representation entity) {
|
75
|
57 logger.fine("AnnotatorAnnotatonsBytag doGetJSON!");
|
28
|
58 setCorsHeaders();
|
65
|
59
|
37
|
60 // do authentication
|
88
|
61 Person authUser = getUserFromAuthToken(entity);
|
75
|
62 logger.fine("request authenticated=" + authUser);
|
37
|
63
|
65
|
64 String id = (String) getRequest().getAttributes().get("id");
|
|
65 // URL decode
|
|
66 try {
|
|
67 id = URLDecoder.decode(id, "UTF-8");
|
|
68 } catch (UnsupportedEncodingException e) {
|
|
69 // this shouldn't happen
|
|
70 }
|
75
|
71 logger.fine("annotation-id=" + id);
|
65
|
72
|
28
|
73 Form form = getRequest().getResourceRef().getQueryAsForm();
|
65
|
74 String sortBy = form.getFirstValue("sortBy");
|
|
75
|
28
|
76 AnnotationStore store = getAnnotationStore();
|
58
|
77 String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
|
28
|
78 List<Annotation> annotations = store.getAnnotationsByTag(tagUri);
|
65
|
79
|
|
80 // JSONArray results = new JSONArray();
|
28
|
81 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
|
65
|
82
|
|
83 for (Annotation annot : annotations) {
|
|
84 // check permission
|
|
85 if (!annot.isActionAllowed("read", authUser, store))
|
|
86 continue;
|
|
87
|
|
88 JSONObject jo = createAnnotatorJson(annot, false);
|
28
|
89 results.add(jo);
|
|
90 }
|
65
|
91
|
|
92 if (sortBy != null) {
|
|
93 JSONObjectComparator.sortAnnotations(results, sortBy);
|
|
94 }
|
|
95
|
|
96 JSONArray resultsJa = new JSONArray();
|
|
97 for (JSONObject result : results) {
|
|
98 resultsJa.put(result);
|
|
99 }
|
|
100
|
|
101 // assemble result object
|
|
102 JSONObject result = new JSONObject();
|
|
103 try {
|
|
104 result.put("rows", resultsJa);
|
|
105 result.put("total", resultsJa.length());
|
|
106 } catch (JSONException e) {
|
|
107 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
|
|
108 return null;
|
|
109 }
|
|
110 return new JsonRepresentation(result);
|
28
|
111 }
|
|
112
|
|
113 } |