source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorGroups.java @ 88:b406507a953d

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

upped version to 0.5.
can use display name and groups from auth token.

File size: 4.1 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    protected String getAllowedMethodsForHeader() {
51        return "OPTIONS,GET";
52    }
53
54    /**
55     * GET with JSON content-type.
56     * Parameters:
57     *   user: short user name
58     *   uri: user uri
59     *   
60     * @param entity
61     * @return
62     */
63    @Get("json")
64    public Representation doGetJSON(Representation entity) {
65        logger.fine("AnnotatorGroups doGetJSON!");
66        setCorsHeaders();
67        // get user from auth token (preferred)
68        Person authUser = getUserFromAuthToken(entity);
69        JSONArray results = null;
70        if (authUser != null && authUser.groups != null) {
71            results  = getGroupsFromPerson(authUser);
72        } else {
73            // get user or uri from request
74            Form form = getRequest().getResourceRef().getQueryAsForm();
75            String user = form.getFirstValue("user");
76            String uri = form.getFirstValue("uri");
77            results = getGroupsFromStore(uri, user);
78        }
79        // assemble result object
80        JSONObject result = new JSONObject();
81        try {
82            result.put("rows", results);
83            result.put("total", results.length());
84        } catch (JSONException e) {
85            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
86            return null;
87        }
88        logger.fine("sending:");
89        logger.fine(result.toString());
90        return new JsonRepresentation(result);
91    }
92
93    public JSONArray getGroupsFromPerson(Person person) {
94        JSONArray results = new JSONArray();
95        for (String group : person.groups) {
96            JSONObject jo = new JSONObject();
97            try {
98                jo.put("id", group);
99                jo.put("name", group);
100            } catch (JSONException e) {
101            }
102            results.put(jo);
103        }
104        return results;
105    }
106
107    public JSONArray getGroupsFromStore(String uri, String user) {
108        JSONArray results = new JSONArray();
109        if (uri == null || uri.isEmpty()) {
110            // get uri from user-id
111            uri = Actor.getUriFromId(user, false);
112        }
113        AnnotationStore store = getAnnotationStore();
114        Node person = store.getPersonNodeByUri(uri);
115        if (person != null) {
116            List<Group> groups = store.getGroupsForPersonNode(person);
117            for (Group group : groups) {
118                JSONObject jo = new JSONObject();
119                try {
120                    jo.put("id", group.getId());
121                    jo.put("name", group.getName());
122                    jo.put("uri", group.getUriString());
123                } catch (JSONException e) {
124                }
125                results.put(jo);
126            }
127        }
128        return results;
129    }
130}
Note: See TracBrowser for help on using the repository browser.