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