source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 22:b1fb0d117877

Last change on this file since 22:b1fb0d117877 was 22:b1fb0d117877, checked in by casties, 12 years ago

adding and listing groups via html works now.
no editing of group membership yet.
no authentication yet.

File size: 7.9 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.JSONException;
10import org.json.JSONObject;
11import org.restlet.data.Status;
12import org.restlet.ext.json.JsonRepresentation;
13import org.restlet.representation.Representation;
14import org.restlet.resource.Delete;
15import org.restlet.resource.Get;
16import org.restlet.resource.Post;
17import org.restlet.resource.Put;
18
19import de.mpiwg.itgroup.annotations.Annotation;
20import de.mpiwg.itgroup.annotations.Person;
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        if (id == null) {
52            // TODO: what to return without id - list all annotations?
53            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
54            return null;
55        }
56
57        // do authentication
58        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
59        logger.debug("request authenticated=" + authUser);
60
61        AnnotationStore store = getAnnotationStore();
62        Annotation annot = store.getAnnotationById(id);
63        if (annot != null) {
64            if (! annot.isActionAllowed("read", authUser, store)) {
65                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
66                return null;
67            }
68            JSONObject result = createAnnotatorJson(annot, (authUser == null));
69            logger.debug("sending:");
70            logger.debug(result);
71            return new JsonRepresentation(result);
72        } else {
73            // not found
74            setStatus(Status.CLIENT_ERROR_NOT_FOUND);
75            return null;
76        }
77    }
78
79    /**
80     * POST with JSON content-type. Creates a new Annotation.
81     *
82     * @return
83     */
84    @Post("json")
85    public Representation doPostJson(Representation entity) {
86        logger.debug("AnnotatorAnnotations doPostJSON!");
87        // set headers
88        setCorsHeaders();
89       
90        // do authentication TODO: who's allowed to create?
91        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
92        logger.debug("request authenticated=" + authUser);
93        if (authUser == null) {
94            setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
95            return null;
96        }
97
98        Annotation annot = null;
99        try {
100            JsonRepresentation jrep = new JsonRepresentation(entity);
101            JSONObject jo = jrep.getJsonObject();
102            if (jo == null) {
103                setStatus(Status.SERVER_ERROR_INTERNAL);
104                return null;
105            }
106            // make sure id is not set for POST
107            jo.remove("id");
108            // get Annotation object from posted JSON
109            annot = createAnnotation(jo, entity);
110        } catch (IOException e1) {
111            setStatus(Status.SERVER_ERROR_INTERNAL);
112            return null;
113        } catch (JSONException e) {
114            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
115            return null;
116        }
117        if (annot == null) {
118            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
119            return null;
120        }
121        Annotation storedAnnot;
122        // store Annotation
123        storedAnnot = getAnnotationStore().storeAnnotation(annot);
124        /*
125         * according to https://github.com/okfn/annotator/wiki/Storage we should
126         * return 303: see other. For now we return the annotation.
127         */
128        JSONObject jo = createAnnotatorJson(storedAnnot, (authUser == null));
129        JsonRepresentation retRep = new JsonRepresentation(jo);
130        return retRep;
131    }
132
133    /**
134     * PUT with JSON content-type. Modifies an Annotation.
135     *
136     * @param entity
137     * @return
138     */
139    @Put("json")
140    public Representation doPutJSON(Representation entity) {
141        logger.debug("AnnotatorAnnotations doPutJSON!");
142        setCorsHeaders();
143        // id from URI /annotations/{id}
144        String jsonId = (String) getRequest().getAttributes().get("id");
145        String id = decodeJsonId(jsonId);
146        logger.debug("annotation-id=" + id);
147
148        // do authentication
149        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
150        logger.debug("request authenticated=" + authUser);
151
152        Annotation annot = null;
153        AnnotationStore store = getAnnotationStore();
154        try {
155            JsonRepresentation jrep = new JsonRepresentation(entity);
156            JSONObject jo = jrep.getJsonObject();
157            if (jo == null) {
158                setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
159                return null;
160            }
161            // get stored Annotation
162            Annotation storedAnnot = store.getAnnotationById(id);
163            if (storedAnnot == null) {
164                setStatus(Status.CLIENT_ERROR_NOT_FOUND);
165                return null;
166            }
167            if (! storedAnnot.isActionAllowed("update", authUser, store)) {
168                setStatus(Status.CLIENT_ERROR_FORBIDDEN);
169                return null;
170            }
171            // update from posted JSON
172            annot = updateAnnotation(storedAnnot, jo, entity);
173            // store Annotation
174            storedAnnot = store.storeAnnotation(annot);
175            /*
176             * according to https://github.com/okfn/annotator/wiki/Storage we
177             * should return 303: see other. but the client doesn't like it
178             * setStatus(Status.REDIRECTION_SEE_OTHER); // go to same URL as
179             * this one Reference thisUrl = this.getReference();
180             * this.getResponse().setLocationRef(thisUrl);
181             */
182            // return new annotation
183            jo = createAnnotatorJson(storedAnnot, (authUser == null));
184            JsonRepresentation retRep = new JsonRepresentation(jo);
185            return retRep;
186        } catch (JSONException e) {
187            e.printStackTrace();
188            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
189        } catch (IOException e) {
190            e.printStackTrace();
191            setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
192        }
193        return null;
194    }
195
196    /**
197     * DELETE with JSON content-type. Deletes an Annotation.
198     *
199     * @param entity
200     * @return
201     */
202    @Delete("json")
203    public Representation doDeleteJSON(Representation entity) {
204        logger.debug("AnnotatorAnnotations doDeleteJSON!");
205        setCorsHeaders();
206        // id from URI /annotations/{id}
207        String jsonId = (String) getRequest().getAttributes().get("id");
208        String id = decodeJsonId(jsonId);
209        logger.debug("annotation-id=" + id);
210
211        // do authentication
212        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
213        logger.debug("request authenticated=" + authUser);
214        AnnotationStore store = getAnnotationStore();
215        Annotation annot = store.getAnnotationById(id);
216        if (annot != null) {
217            if (! annot.isActionAllowed("delete", authUser, store)) {
218                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
219                return null;
220            }
221        }
222       
223        // delete annotation
224        store.deleteById(id);
225        setStatus(Status.SUCCESS_NO_CONTENT);
226        return null;
227    }
228
229}
Note: See TracBrowser for help on using the repository browser.