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