source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java @ 91:cf44d9e1a4a7

Last change on this file since 91:cf44d9e1a4a7 was 91:cf44d9e1a4a7, checked in by casties, 9 years ago

let CORS be handled by Restlet 2.3 CorsFilter?.

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