source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorGroups.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: 4.0 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.util.List;
26
27import org.json.JSONArray;
28import org.json.JSONException;
29import org.json.JSONObject;
30import org.neo4j.graphdb.Node;
31import org.restlet.data.Form;
32import org.restlet.data.Status;
33import org.restlet.ext.json.JsonRepresentation;
34import org.restlet.representation.Representation;
35import org.restlet.resource.Get;
36
37import de.mpiwg.itgroup.annotations.Actor;
38import de.mpiwg.itgroup.annotations.Group;
39import de.mpiwg.itgroup.annotations.Person;
40import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
41
42
43/**
44 * API for accessing groups in the Annotation store.
45 *
46 * @author casties
47 *
48 */
49public class AnnotatorGroups extends AnnotatorResourceImpl {
50
51    /**
52     * GET with JSON content-type.
53     * Parameters:
54     *   user: short user name
55     *   uri: user uri
56     *   
57     * @param entity
58     * @return
59     */
60    @Get("json")
61    public Representation doGetJSON(Representation entity) {
62        logger.fine("AnnotatorGroups doGetJSON!");
63        // get user from auth token (preferred)
64        Person authUser = getUserFromAuthToken(entity);
65        JSONArray results = null;
66        if (authUser != null && authUser.groups != null) {
67            results  = getGroupsFromPerson(authUser);
68        } else {
69            // get user or uri from request
70            Form form = getRequest().getResourceRef().getQueryAsForm();
71            String user = form.getFirstValue("user");
72            String uri = form.getFirstValue("uri");
73            results = getGroupsFromStore(uri, user);
74        }
75        // assemble result object
76        JSONObject result = new JSONObject();
77        try {
78            result.put("rows", results);
79            result.put("total", results.length());
80        } catch (JSONException e) {
81            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
82            return null;
83        }
84        logger.fine("sending:");
85        logger.fine(result.toString());
86        return new JsonRepresentation(result);
87    }
88
89    public JSONArray getGroupsFromPerson(Person person) {
90        JSONArray results = new JSONArray();
91        for (String group : person.groups) {
92            JSONObject jo = new JSONObject();
93            try {
94                jo.put("id", group);
95                jo.put("name", group);
96            } catch (JSONException e) {
97            }
98            results.put(jo);
99        }
100        return results;
101    }
102
103    public JSONArray getGroupsFromStore(String uri, String user) {
104        JSONArray results = new JSONArray();
105        if (uri == null || uri.isEmpty()) {
106            // get uri from user-id
107            uri = Actor.getUriFromId(user, false);
108        }
109        AnnotationStore store = getAnnotationStore();
110        Node person = store.getPersonNodeByUri(uri);
111        if (person != null) {
112            List<Group> groups = store.getGroupsForPersonNode(person);
113            for (Group group : groups) {
114                JSONObject jo = new JSONObject();
115                try {
116                    jo.put("id", group.getId());
117                    jo.put("name", group.getName());
118                    jo.put("uri", group.getUriString());
119                } catch (JSONException e) {
120                }
121                results.put(jo);
122            }
123        }
124        return results;
125    }
126}
Note: See TracBrowser for help on using the repository browser.