source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java @ 70:2b1e6df5e21a

Last change on this file since 70:2b1e6df5e21a was 70:2b1e6df5e21a, checked in by casties, 10 years ago

added lgpl_v3 license information.

File size: 3.9 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    protected String getAllowedMethodsForHeader() {
53        return "OPTIONS,GET";
54    }
55
56    @Get("json")
57    public Representation doGetJSON(Representation entity) {
58        logger.debug("AnnotatorAnnotatonsByResource doGetJSON!");
59        setCorsHeaders();
60
61        // do authentication
62        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
63        logger.debug("request authenticated=" + authUser);
64
65        String id = null;
66        String jsonId = (String) getRequest().getAttributes().get("id");
67        if (jsonId != null) {
68            // URL decode
69            try {
70                jsonId = URLDecoder.decode(jsonId, "UTF-8");
71            } catch (UnsupportedEncodingException e) {
72                // this shouldn't happen
73            }
74            id = decodeJsonId(jsonId);
75            // String id = jsonId;
76            logger.debug("ressource-id=" + id);
77        }
78
79        Form form = getRequest().getResourceRef().getQueryAsForm();
80        String sortBy = null;
81        for (Parameter parameter : form) {
82            if (parameter.getName().equals("sortBy")) {
83                sortBy = parameter.getValue();
84            }
85        }
86
87        AnnotationStore store = getAnnotationStore();
88        // String tagUri=NS.MPIWG_TAGS_URL+id;
89        List<Annotation> annotations = store.getAnnotationsByResource(id);
90
91        // JSONArray results = new JSONArray();
92        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
93
94        for (Annotation annot : annotations) {
95            // check permission
96            if (!annot.isActionAllowed("read", authUser, store))
97                continue;
98
99            JSONObject jo = createAnnotatorJson(annot, false);
100            results.add(jo);
101        }
102
103        if (sortBy != null) {
104            JSONObjectComparator.sortAnnotations(results, sortBy);
105        }
106
107        JSONArray resultsJa = new JSONArray();
108        for (JSONObject result : results) {
109            resultsJa.put(result);
110        }
111
112        // assemble result object
113        JSONObject result = new JSONObject();
114        try {
115            result.put("rows", resultsJa);
116            result.put("total", resultsJa.length());
117        } catch (JSONException e) {
118            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
119            return null;
120        }
121        return new JsonRepresentation(result);
122    }
123
124}
Note: See TracBrowser for help on using the repository browser.