comparison src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java @ 23:a3e324009990

now Mavenified! removed tiny-mce javascript from html frontend. still needs TripleStoreManager project in Eclipse. jsontoken 1.1 has to be built manually.
author casties
date Tue, 03 Apr 2012 13:05:05 +0200
parents src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java@b0ef5c860464
children e5f5848892a2
comparison
equal deleted inserted replaced
22:0f4428febcc6 23:a3e324009990
1 /**
2 * Implements the "search" uri of the Annotator API.
3 */
4 package de.mpiwg.itgroup.annotationManager.restlet;
5
6 import java.io.UnsupportedEncodingException;
7 import java.net.URLDecoder;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import org.json.JSONArray;
12 import org.json.JSONException;
13 import org.json.JSONObject;
14 import org.restlet.data.Form;
15 import org.restlet.data.MediaType;
16 import org.restlet.data.Status;
17 import org.restlet.ext.json.JsonRepresentation;
18 import org.restlet.representation.Representation;
19 import org.restlet.representation.StringRepresentation;
20 import org.restlet.resource.Get;
21
22 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
23 import de.mpiwg.itgroup.annotationManager.RDFHandling.Annotation;
24 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
25 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
26
27 /**
28 * Implements the "search" uri of the Annotator API. see
29 * <https://github.com/okfn/annotator/wiki/Storage>
30 *
31 * @author casties
32 *
33 */
34 public class AnnotatorSearch extends AnnotatorResourceImpl {
35
36 protected String getAllowedMethodsForHeader() {
37 return "OPTIONS,GET";
38 }
39
40 /**
41 * result for JSON content-type. optional search parameters: uri user limit
42 * offset
43 *
44 * @param entity
45 * @return
46 */
47 @Get("json")
48 public Representation doGetJSON(Representation entity) {
49 logger.debug("AnnotatorSearch doGetJSON!");
50 setCorsHeaders();
51 //TODO: what to do with authentication?
52 boolean authenticated = isAuthenticated(entity);
53 logger.debug("request authenticated="+authenticated);
54
55 Form form = getRequest().getResourceRef().getQueryAsForm();
56 String uri = form.getFirstValue("uri");
57 String user = form.getFirstValue("user");
58
59 String limit = form.getFirstValue("limit");
60 String offset = form.getFirstValue("offset");
61
62 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should go into config file
63
64 JSONArray ja;
65 try {
66
67 List<Annotation> annots = searcher.searchByUriUser(uri, user, limit, offset);
68
69 ja = new JSONArray();
70 for (Annotation annot : annots) {
71 JSONObject jo = createAnnotatorJson(annot);
72 if (jo != null) {
73 ja.put(createAnnotatorJson(annot));
74 } else {
75 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
76 return null;
77 }
78 }
79 } catch (TripleStoreHandlerException e) {
80 e.printStackTrace();
81 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
82 return null;
83 } catch (TripleStoreSearchError e) {
84 e.printStackTrace();
85 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
86 return null;
87 }
88
89 JSONObject result = new JSONObject();
90 try {
91 result.put("rows", ja);
92 result.put("total", ja.length());
93 } catch (JSONException e) {
94 e.printStackTrace();
95 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
96 return null;
97 }
98
99 logger.debug("sending:");
100 logger.debug(result);
101 return new JsonRepresentation(result);
102 }
103
104 /**
105 * result for HTML content-type.
106 *
107 * @param entity
108 * @return
109 */
110 @Get("html")
111 public Representation doGetHTML(Representation entity) {
112 logger.debug("AnnotatorSearch doGetHTML!");
113 doOptions(entity);
114 Form form = getRequest().getResourceRef().getQueryAsForm();
115 String uri = form.getFirstValue("uri");
116 String user = form.getFirstValue("user");
117
118 String limit = form.getFirstValue("limit");
119 String offset = form.getFirstValue("offset");
120
121 try {
122 if (uri != null) {
123 uri = URLDecoder.decode(uri, "utf-8");
124 }
125 } catch (UnsupportedEncodingException e1) {
126 e1.printStackTrace();
127 setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
128 return null;
129 }
130
131 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO
132 // should
133 // ge
134 // into
135 // config
136 // file
137
138 String retString = "<html><body><table>";
139 String lineFormat = "<tr><td><a href=\"%s\">%s</a></td>"
140 + "<td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td><td><a href=\"%s\">%s</a></td><td><a href=\"%s\">%s</a></td></div>";
141 try {
142
143 List<Annotation> annots = searcher.searchByUriUser(uri, user, limit, offset);
144
145 for (Annotation annot : annots) {
146
147 RestServer restServer = (RestServer) getApplication();
148 String userName = restServer.getUserNameFromLdap(annot.creator);
149 List<String> xpointer = new ArrayList<String>();
150
151 if (annot.xpointers == null || annot.xpointers.size() == 0)
152 retString += String.format(lineFormat, userName, userName, annot.url, annot.url, annot.time, annot.text,
153 annot.xpointer, annot.xpointer, annot.getAnnotationUri(), annot.getAnnotationUri());
154 else {
155 for (String xpointerString : annot.xpointers) {
156 retString += String.format(lineFormat, userName, userName, annot.url, annot.url, annot.time, annot.text,
157 xpointerString, xpointerString, annot.getAnnotationUri(), annot.getAnnotationUri());
158 }
159 }
160
161 }
162 } catch (TripleStoreHandlerException e) {
163 // TODO Auto-generated catch block
164 e.printStackTrace();
165 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
166 return null;
167 } catch (TripleStoreSearchError e) {
168 // TODO Auto-generated catch block
169 e.printStackTrace();
170 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
171 return null;
172 }
173
174 retString += "</table></body></html>";
175
176 logger.debug("sending:");
177 logger.debug(retString);
178 return new StringRepresentation(retString, MediaType.TEXT_HTML);
179 }
180
181 }