source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java @ 32:0731c4549065

Last change on this file since 32:0731c4549065 was 32:0731c4549065, checked in by casties, 12 years ago

UI for editing groups and persons works now. (still no authorisation!)

File size: 2.7 KB
Line 
1/**
2 * Implements the "search" uri of the Annotator API.
3 */
4package de.mpiwg.itgroup.annotations.restlet;
5
6import java.util.List;
7
8import org.json.JSONArray;
9import org.json.JSONException;
10import org.json.JSONObject;
11import org.restlet.data.Form;
12import org.restlet.data.Status;
13import org.restlet.ext.json.JsonRepresentation;
14import org.restlet.representation.Representation;
15import org.restlet.resource.Get;
16
17import de.mpiwg.itgroup.annotations.Annotation;
18import de.mpiwg.itgroup.annotations.Person;
19import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
20
21/**
22 * Implements the "search" uri of the Annotator API. see
23 * <https://github.com/okfn/annotator/wiki/Storage>
24 *
25 * @author casties
26 *
27 */
28public class AnnotatorSearch extends AnnotatorResourceImpl {
29
30    protected String getAllowedMethodsForHeader() {
31        return "OPTIONS,GET";
32    }
33
34    /**
35     * result for JSON content-type. optional search parameters: uri, user, limit,
36     * offset.
37     *
38     * @param entity
39     * @return
40     */
41    @Get("json")
42    public Representation doGetJSON(Representation entity) {
43        logger.debug("AnnotatorSearch doGetJSON!");
44        setCorsHeaders();
45        // do authentication
46        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
47        logger.debug("request authenticated=" + authUser);
48
49        Form form = getRequest().getResourceRef().getQueryAsForm();
50        String uri = form.getFirstValue("uri");
51        String user = form.getFirstValue("user");
52        String limit = form.getFirstValue("limit");
53        String offset = form.getFirstValue("offset");
54
55        JSONArray results = new JSONArray();
56        // do search
57        logger.debug(String.format("searching for uri=%s user=%s", uri, user));
58        AnnotationStore store = getAnnotationStore();
59        List<Annotation> annots = store.searchAnnotationByUriUser(uri, user, limit, offset);
60        for (Annotation annot : annots) {
61            // check permission
62            if (!annot.isActionAllowed("read", authUser, store)) continue;
63            JSONObject jo = createAnnotatorJson(annot, (authUser == null));
64            if (jo != null) {
65                results.put(jo);
66            } else {
67                setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
68                return null;
69            }
70        }
71        // assemble result object
72        JSONObject result = new JSONObject();
73        try {
74            result.put("rows", results);
75            result.put("total", results.length());
76        } catch (JSONException e) {
77            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
78            return null;
79        }
80
81        logger.debug("sending:");
82        logger.debug(result);
83        return new JsonRepresentation(result);
84    }
85
86}
Note: See TracBrowser for help on using the repository browser.