annotate src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java @ 13:9393c9c9b916

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