source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.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: 7.3 KB
Line 
1/**
2 * Implements the "annotations" uri of the Annotator API. see
3 * <https://github.com/okfn/annotator/wiki/Storage>
4 */
5package de.mpiwg.itgroup.annotations.restlet;
6
7import java.io.IOException;
8
9import org.json.JSONArray;
10import org.json.JSONException;
11import org.json.JSONObject;
12import org.restlet.data.Status;
13import org.restlet.ext.json.JsonRepresentation;
14import org.restlet.representation.Representation;
15import org.restlet.resource.Delete;
16import org.restlet.resource.Get;
17import org.restlet.resource.Post;
18import org.restlet.resource.Put;
19
20import de.mpiwg.itgroup.annotations.Annotation;
21import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
22
23/**
24 * Implements the "annotations" uri of the Annotator API. see
25 * <https://github.com/okfn/annotator/wiki/Storage>
26 *
27 * @author dwinter, casties
28 *
29 */
30public class AnnotatorAnnotations extends AnnotatorResourceImpl {
31
32    protected String getAllowedMethodsForHeader() {
33        return "OPTIONS,GET,POST,PUT,DELETE";
34    }
35
36    /**
37     * GET with JSON content-type.
38     *
39     * @param entity
40     * @return
41     */
42    @Get("json")
43    public Representation doGetJSON(Representation entity) {
44        logger.debug("AnnotatorAnnotations doGetJSON!");
45        setCorsHeaders();
46        // id from URI /annotations/{id}
47        String jsonId = (String) getRequest().getAttributes().get("id");
48        String id = decodeJsonId(jsonId);
49        logger.debug("annotation-id=" + id);
50
51        // TODO: what to return without id - list of all annotations?
52
53        // TODO: what to do with authentication?
54        boolean authenticated = isAuthenticated(entity);
55        logger.debug("request authenticated=" + authenticated);
56
57        Annotation annots = getAnnotationStore().getAnnotationById(id);
58        if (annots != null) {
59            // there should be only one
60            JSONObject result = createAnnotatorJson(annots);
61            logger.debug("sending:");
62            logger.debug(result);
63            return new JsonRepresentation(result);
64        } else {
65            JSONArray results = new JSONArray();
66            // annotator read request returns a list of annotation objects
67            logger.debug("sending:");
68            logger.debug(results);
69            return new JsonRepresentation(results);
70        }
71    }
72
73    /**
74     * POST with JSON content-type.
75     *
76     * json hash: username: name des users xpointer: xpointer auf den Ausschnitt
77     * (incl. der URL des Dokumentes) text: text der annotation annoturl: url
78     * auf eine Annotation falls extern
79     *
80     * @return
81     */
82    @Post("json")
83    public Representation doPostJson(Representation entity) {
84        logger.debug("AnnotatorAnnotations doPostJSON!");
85        // set headers
86        setCorsHeaders();
87        Annotation annot = null;
88        try {
89            JsonRepresentation jrep = new JsonRepresentation(entity);
90            JSONObject jo = jrep.getJsonObject();
91            if (jo == null) {
92                setStatus(Status.SERVER_ERROR_INTERNAL);
93                return null;
94            }
95            // make sure id is not set for POST
96            jo.remove("id");
97            // get Annotation object from posted JSON
98            annot = createAnnotation(jo, entity);
99        } catch (IOException e1) {
100            setStatus(Status.SERVER_ERROR_INTERNAL);
101            return null;
102        } catch (JSONException e) {
103            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
104            return null;
105        }
106        if (annot == null) {
107            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
108            return null;
109        }
110        Annotation storedAnnot;
111        // store Annotation
112        storedAnnot = getAnnotationStore().storeAnnotation(annot);
113        /*
114         * according to https://github.com/okfn/annotator/wiki/Storage we should
115         * return 303: see other. For now we return the annotation.
116         */
117        JSONObject jo = createAnnotatorJson(storedAnnot);
118        JsonRepresentation retRep = new JsonRepresentation(jo);
119        return retRep;
120    }
121
122    /**
123     * PUT with JSON content-type.
124     *
125     * @param entity
126     * @return
127     */
128    @Put("json")
129    public Representation doPutJSON(Representation entity) {
130        logger.debug("AnnotatorAnnotations doPutJSON!");
131        setCorsHeaders();
132        // id from URI /annotations/{id}
133        String jsonId = (String) getRequest().getAttributes().get("id");
134        String id = decodeJsonId(jsonId);
135        logger.debug("annotation-id=" + id);
136
137        // TODO: what to do with authentication? we should check the owner
138        boolean authenticated = isAuthenticated(entity);
139        logger.debug("request authenticated=" + authenticated);
140        if (!authenticated) {
141            setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
142            return null;
143        }
144
145        Annotation annot = null;
146        AnnotationStore store = getAnnotationStore();
147        try {
148            JsonRepresentation jrep = new JsonRepresentation(entity);
149            JSONObject jo = jrep.getJsonObject();
150            if (jo == null) {
151                setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
152                return null;
153            }
154            // get stored Annotation
155            Annotation storedAnnot = store.getAnnotationById(id);
156            if (storedAnnot == null) {
157                setStatus(Status.CLIENT_ERROR_NOT_FOUND);
158                return null;
159            }
160            // update from posted JSON
161            annot = updateAnnotation(storedAnnot, jo, entity);
162            // store Annotation
163            storedAnnot = store.storeAnnotation(annot);
164            /*
165             * according to https://github.com/okfn/annotator/wiki/Storage we
166             * should return 303: see other. but the client doesn't like it
167             * setStatus(Status.REDIRECTION_SEE_OTHER); // go to same URL as
168             * this one Reference thisUrl = this.getReference();
169             * this.getResponse().setLocationRef(thisUrl);
170             */
171            // return new annotation
172            jo = createAnnotatorJson(storedAnnot);
173            JsonRepresentation retRep = new JsonRepresentation(jo);
174            return retRep;
175        } catch (JSONException e) {
176            e.printStackTrace();
177            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
178        } catch (IOException e) {
179            e.printStackTrace();
180            setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
181        }
182        return null;
183    }
184
185    /**
186     * DELETE with JSON content-type.
187     *
188     * @param entity
189     * @return
190     */
191    @Delete("json")
192    public Representation doDeleteJSON(Representation entity) {
193        logger.debug("AnnotatorAnnotations doDeleteJSON!");
194        setCorsHeaders();
195        // id from URI /annotations/{id}
196        String jsonId = (String) getRequest().getAttributes().get("id");
197        String id = decodeJsonId(jsonId);
198        logger.debug("annotation-id=" + id);
199
200        // TODO: what to do with authentication? we should check the owner
201        boolean authenticated = isAuthenticated(entity);
202        logger.debug("request authenticated=" + authenticated);
203        if (!authenticated) {
204            setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
205            return null;
206        }
207
208        // delete annotation
209        getAnnotationStore().deleteById(id);
210        setStatus(Status.SUCCESS_NO_CONTENT);
211        return null;
212    }
213
214}
Note: See TracBrowser for help on using the repository browser.