annotate src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java @ 26:235b91ba8dff

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