source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java @ 14:629e15b345aa

Last change on this file since 14:629e15b345aa was 14:629e15b345aa, checked in by casties, 12 years ago

permissions mostly work. need more server-side checking.

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