source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java @ 4:3599b29c393f

Last change on this file since 4:3599b29c393f was 4:3599b29c393f, checked in by casties, 12 years ago

store seems to work now :-)

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