source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.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: 3.9 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.ArrayList;
26import java.util.List;
27
28import org.json.JSONArray;
29import org.json.JSONException;
30import org.json.JSONObject;
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.Annotation;
38import de.mpiwg.itgroup.annotations.Person;
39import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
40import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
41
42/**
43 * Implements the "search" uri of the Annotator API. see
44 * <https://github.com/okfn/annotator/wiki/Storage>
45 *
46 * @author casties
47 *
48 */
49public class AnnotatorSearch extends AnnotatorResourceImpl {
50
51    /**
52     * result for JSON content-type. optional search parameters: uri, user, limit,
53     * offset, sortBy.
54     *
55     * @param entity
56     * @return
57     */
58    @Get("json")
59    public Representation doGetJSON(Representation entity) {
60        logger.fine("AnnotatorSearch doGetJSON!");
61        // do authentication
62        Person authUser = getUserFromAuthToken(entity);
63        logger.fine("request authenticated=" + authUser);
64
65        Form form = getRequest().getResourceRef().getQueryAsForm();
66        String uri = form.getFirstValue("uri");
67        String user = form.getFirstValue("user");
68        int limit = getInt(form.getFirstValue("limit"));
69        int offset = getInt(form.getFirstValue("offset"));
70        String sortBy = form.getFirstValue("sortBy");
71
72        // do search
73        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
74        logger.fine(String.format("searching for uri=%s user=%s", uri, user));
75        AnnotationStore store = getAnnotationStore();
76        List<Annotation> annots = store.searchAnnotationByUriUser(uri, user);
77        for (Annotation annot : annots) {
78            // check permission
79            if (!annot.isActionAllowed("read", authUser, store)) continue;
80            JSONObject jo = createAnnotatorJson(annot, (authUser == null));
81            if (jo != null) {
82                results.add(jo);
83            } else {
84                setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
85                return null;
86            }
87        }
88
89        // sort if necessary
90        if (sortBy != null) {
91            JSONObjectComparator.sortAnnotations(results, sortBy);
92        }
93       
94        // put in JSON list
95        JSONArray rows = new JSONArray();
96        int cnt = 0;
97        for (JSONObject result : results) {
98            cnt += 1;
99            if (offset > 0 && cnt < offset) continue;
100            rows.put(result);
101            if (limit > 0 && cnt >= limit) break;
102        }
103
104        // assemble result object
105        JSONObject result = new JSONObject();
106        try {
107            result.put("rows", rows);
108            result.put("total", rows.length());
109        } catch (JSONException e) {
110            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
111            return null;
112        }
113
114        logger.fine("sending response");
115        logger.finest(result.toString());
116        return new JsonRepresentation(result);
117    }
118
119}
Note: See TracBrowser for help on using the repository browser.