source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java @ 65:c0dd5314bada

Last change on this file since 65:c0dd5314bada was 65:c0dd5314bada, checked in by casties, 11 years ago

deal with special characters in urls.

File size: 3.2 KB
Line 
1package de.mpiwg.itgroup.annotations.restlet;
2
3import java.io.UnsupportedEncodingException;
4import java.net.URLDecoder;
5import java.util.ArrayList;
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.Parameter;
13import org.restlet.data.Status;
14import org.restlet.ext.json.JsonRepresentation;
15import org.restlet.representation.Representation;
16import org.restlet.resource.Get;
17
18import de.mpiwg.itgroup.annotations.Annotation;
19import de.mpiwg.itgroup.annotations.Person;
20import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
21import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
22
23/**
24 * API for accessing tags in the Annotation store.
25 *
26 * @author dwinter
27 *
28 */
29public class AnnotatorAnnotationsByResources extends AnnotatorResourceImpl {
30    protected String getAllowedMethodsForHeader() {
31        return "OPTIONS,GET";
32    }
33
34    @Get("json")
35    public Representation doGetJSON(Representation entity) {
36        logger.debug("AnnotatorAnnotatonsByResource doGetJSON!");
37        setCorsHeaders();
38
39        // do authentication
40        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
41        logger.debug("request authenticated=" + authUser);
42
43        String id = null;
44        String jsonId = (String) getRequest().getAttributes().get("id");
45        if (jsonId != null) {
46            // URL decode
47            try {
48                jsonId = URLDecoder.decode(jsonId, "UTF-8");
49            } catch (UnsupportedEncodingException e) {
50                // this shouldn't happen
51            }
52            id = decodeJsonId(jsonId);
53            // String id = jsonId;
54            logger.debug("ressource-id=" + id);
55        }
56
57        Form form = getRequest().getResourceRef().getQueryAsForm();
58        String sortBy = null;
59        for (Parameter parameter : form) {
60            if (parameter.getName().equals("sortBy")) {
61                sortBy = parameter.getValue();
62            }
63        }
64
65        AnnotationStore store = getAnnotationStore();
66        // String tagUri=NS.MPIWG_TAGS_URL+id;
67        List<Annotation> annotations = store.getAnnotationsByResource(id);
68
69        // JSONArray results = new JSONArray();
70        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
71
72        for (Annotation annot : annotations) {
73            // check permission
74            if (!annot.isActionAllowed("read", authUser, store))
75                continue;
76
77            JSONObject jo = createAnnotatorJson(annot, false);
78            results.add(jo);
79        }
80
81        if (sortBy != null) {
82            JSONObjectComparator.sortAnnotations(results, sortBy);
83        }
84
85        JSONArray resultsJa = new JSONArray();
86        for (JSONObject result : results) {
87            resultsJa.put(result);
88        }
89
90        // assemble result object
91        JSONObject result = new JSONObject();
92        try {
93            result.put("rows", resultsJa);
94            result.put("total", resultsJa.length());
95        } catch (JSONException e) {
96            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
97            return null;
98        }
99        return new JsonRepresentation(result);
100    }
101
102}
Note: See TracBrowser for help on using the repository browser.