source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.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.8 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.Parameter;
35import org.restlet.data.Status;
36import org.restlet.ext.json.JsonRepresentation;
37import org.restlet.representation.Representation;
38import org.restlet.resource.Get;
39
40import de.mpiwg.itgroup.annotations.Annotation;
41import de.mpiwg.itgroup.annotations.Person;
42import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
43import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
44
45/**
46 * API for accessing tags in the Annotation store.
47 *
48 * @author dwinter
49 *
50 */
51public class AnnotatorAnnotationsByResources extends AnnotatorResourceImpl {
52
53    @Get("json")
54    public Representation doGetJSON(Representation entity) {
55        logger.fine("AnnotatorAnnotatonsByResource doGetJSON!");
56
57        // do authentication
58        Person authUser = getUserFromAuthToken(entity);
59        logger.fine("request authenticated=" + authUser);
60
61        String id = null;
62        String jsonId = (String) getRequest().getAttributes().get("id");
63        if (jsonId != null) {
64            // URL decode
65            try {
66                jsonId = URLDecoder.decode(jsonId, "UTF-8");
67            } catch (UnsupportedEncodingException e) {
68                // this shouldn't happen
69            }
70            id = decodeJsonId(jsonId);
71            // String id = jsonId;
72            logger.fine("ressource-id=" + id);
73        }
74
75        Form form = getRequest().getResourceRef().getQueryAsForm();
76        String sortBy = null;
77        for (Parameter parameter : form) {
78            if (parameter.getName().equals("sortBy")) {
79                sortBy = parameter.getValue();
80            }
81        }
82
83        AnnotationStore store = getAnnotationStore();
84        // String tagUri=NS.MPIWG_TAGS_URL+id;
85        List<Annotation> annotations = store.getAnnotationsByResource(id);
86
87        // JSONArray results = new JSONArray();
88        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
89
90        for (Annotation annot : annotations) {
91            // check permission
92            if (!annot.isActionAllowed("read", authUser, store))
93                continue;
94
95            JSONObject jo = createAnnotatorJson(annot, false);
96            results.add(jo);
97        }
98
99        if (sortBy != null) {
100            JSONObjectComparator.sortAnnotations(results, sortBy);
101        }
102
103        JSONArray resultsJa = new JSONArray();
104        for (JSONObject result : results) {
105            resultsJa.put(result);
106        }
107
108        // assemble result object
109        JSONObject result = new JSONObject();
110        try {
111            result.put("rows", resultsJa);
112            result.put("total", resultsJa.length());
113        } catch (JSONException e) {
114            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
115            return null;
116        }
117        return new JsonRepresentation(result);
118    }
119
120}
Note: See TracBrowser for help on using the repository browser.