source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.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 
1/**
2 * Implements the "search" uri of the Annotator API.
3 */
4package de.mpiwg.itgroup.annotations.restlet;
5
6/*
7 * #%L
8 * AnnotationManager
9 * %%
10 * Copyright (C) 2012 - 2014 MPIWG Berlin
11 * %%
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as
14 * published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Lesser Public License for more details.
21 *
22 * You should have received a copy of the GNU General Lesser Public
23 * License along with this program.  If not, see
24 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
25 * #L%
26 */
27
28import java.util.ArrayList;
29import java.util.List;
30
31import org.json.JSONArray;
32import org.json.JSONException;
33import org.json.JSONObject;
34import org.restlet.data.Form;
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 * Implements the "search" uri of the Annotator API. see
47 * <https://github.com/okfn/annotator/wiki/Storage>
48 *
49 * @author casties
50 *
51 */
52public class AnnotatorSearch extends AnnotatorResourceImpl {
53
54    protected String getAllowedMethodsForHeader() {
55        return "OPTIONS,GET";
56    }
57
58    /**
59     * result for JSON content-type. optional search parameters: uri, user, limit,
60     * offset, sortBy.
61     *
62     * @param entity
63     * @return
64     */
65    @Get("json")
66    public Representation doGetJSON(Representation entity) {
67        logger.fine("AnnotatorSearch doGetJSON!");
68        setCorsHeaders();
69        // do authentication
70        Person authUser = getUserFromAuthToken(entity);
71        logger.fine("request authenticated=" + authUser);
72
73        Form form = getRequest().getResourceRef().getQueryAsForm();
74        String uri = form.getFirstValue("uri");
75        String user = form.getFirstValue("user");
76        int limit = getInt(form.getFirstValue("limit"));
77        int offset = getInt(form.getFirstValue("offset"));
78        String sortBy = form.getFirstValue("sortBy");
79
80        // do search
81        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
82        logger.fine(String.format("searching for uri=%s user=%s", uri, user));
83        AnnotationStore store = getAnnotationStore();
84        List<Annotation> annots = store.searchAnnotationByUriUser(uri, user);
85        for (Annotation annot : annots) {
86            // check permission
87            if (!annot.isActionAllowed("read", authUser, store)) continue;
88            JSONObject jo = createAnnotatorJson(annot, (authUser == null));
89            if (jo != null) {
90                results.add(jo);
91            } else {
92                setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
93                return null;
94            }
95        }
96
97        // sort if necessary
98        if (sortBy != null) {
99            JSONObjectComparator.sortAnnotations(results, sortBy);
100        }
101       
102        // put in JSON list
103        JSONArray rows = new JSONArray();
104        int cnt = 0;
105        for (JSONObject result : results) {
106            cnt += 1;
107            if (offset > 0 && cnt < offset) continue;
108            rows.put(result);
109            if (limit > 0 && cnt >= limit) break;
110        }
111
112        // assemble result object
113        JSONObject result = new JSONObject();
114        try {
115            result.put("rows", rows);
116            result.put("total", rows.length());
117        } catch (JSONException e) {
118            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
119            return null;
120        }
121
122        logger.fine("sending:");
123        logger.fine(result.toString());
124        return new JsonRepresentation(result);
125    }
126
127}
Note: See TracBrowser for help on using the repository browser.