annotate src/main/java/de/mpiwg/itgroup/annotationManager/restlet/SearchAnnotations.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
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
1 //TODO: handle XML-Post des Annoteaprotocolls http://www.w3.org/2001/Annotea/User/Protocol.html
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
2
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
3 package de.mpiwg.itgroup.annotationManager.restlet;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
4
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
5 import java.io.UnsupportedEncodingException;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
6 import java.net.URLDecoder;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
7 import java.net.URLEncoder;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
8 import java.util.ArrayList;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
9 import java.util.List;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
10 import java.util.regex.Matcher;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
11 import java.util.regex.Pattern;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
12
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
13 import org.apache.log4j.Logger;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
14 import org.json.JSONArray;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
15 import org.json.JSONException;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
16 import org.json.JSONObject;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
17 import org.restlet.Context;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
18 import org.restlet.data.ClientInfo;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
19 import org.restlet.data.Form;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
20 import org.restlet.data.MediaType;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
21 import org.restlet.data.Status;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
22 import org.restlet.ext.json.JsonRepresentation;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
23 import org.restlet.representation.Representation;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
24 import org.restlet.representation.StringRepresentation;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
25 import org.restlet.resource.Get;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
26 import org.restlet.resource.Options;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
27 import org.restlet.resource.ServerResource;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
28 import org.restlet.security.User;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
29
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
30 import de.mpiwg.itgroup.annotationManager.Constants.NS;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
31 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
32 import de.mpiwg.itgroup.annotationManager.RDFHandling.Annotation;
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
33 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
34 import de.mpiwg.itgroup.annotationManager.drupal.AnnotationHandler;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
35 import de.mpiwg.itgroup.annotationManager.drupal.UnknowUserException;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
36 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
37
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
38
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
39 public class SearchAnnotations extends ServerResource {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
40
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
41 private Logger logger = Logger.getRootLogger();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
42
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
43
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
44
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
45 @Options
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
46 public void doOptions(Representation entity) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
47 Form responseHeaders = (Form) getResponse().getAttributes().get(
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
48 "org.restlet.http.headers");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
49 if (responseHeaders == null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
50 responseHeaders = new Form();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
51 getResponse().getAttributes().put("org.restlet.http.headers",
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
52 responseHeaders);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
53 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
54 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
55 String origin = requestHeaders.getFirstValue("Origin", true);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
56 if (origin == null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
57 responseHeaders.add("Access-Control-Allow-Origin", "*");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
58 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
59 responseHeaders.add("Access-Control-Allow-Origin", origin);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
60 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
61 responseHeaders.add("Access-Control-Allow-Methods", "OPTIONS,GET");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
62 String allowHeaders = requestHeaders.getFirstValue("Access-Control-Request-Headers", true);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
63 if (allowHeaders != null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
64 responseHeaders.add("Access-Control-Allow-Headers", allowHeaders);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
65 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
66 //responseHeaders.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Annotator-Account-Id, X-Annotator-User-Id, X-Annotator-Auth-Token-Valid-Until, X-Annotator-Auth-Token");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
67 responseHeaders.add("Access-Control-Allow-Credentials", "true");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
68 responseHeaders.add("Access-Control-Max-Age", "60");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
69 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
70
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
71 @Get("html")
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
72 public Representation doGetHTML(Representation entity){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
73
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
74 doOptions(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
75 Form form = getRequest().getResourceRef().getQueryAsForm();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
76 String uri = form.getFirstValue("uri");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
77 String user = form.getFirstValue("user");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
78
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
79 String limit=form.getFirstValue("limit");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
80 String offset=form.getFirstValue("offset");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
81
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
82 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
83 if (uri!=null){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
84 uri = URLDecoder.decode(uri, "utf-8");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
85 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
86 } catch (UnsupportedEncodingException e1) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
87 e1.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
88 setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
89 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
90 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
91
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
92 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
93
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
94 String retString="<html><body><table>";
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
95 String lineFormat="<tr><td><a href=\"%s\">%s</a></td>" +
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
96 "<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>";
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
97 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
98
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
99 List<Annotation> annots=searcher.searchByUriUser(uri,user,limit,offset);
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
100
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
101 for (Annotation annot:annots){
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
102
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
103
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
104 RestServer restServer = (RestServer) getApplication();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
105 String userName=restServer.getUserNameFromLdap(annot.creator);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
106 List<String> xpointer = new ArrayList<String>();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
107
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
108 if (annot.xpointers==null || annot.xpointers.size()==0)
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
109 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,annot.xpointer,annot.xpointer,annot.getAnnotationUri(),annot.getAnnotationUri());
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
110 else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
111 for(String xpointerString:annot.xpointers){
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
112 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.getAnnotationUri(),annot.getAnnotationUri());
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
113 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
114 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
115
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
116 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
117 } catch (TripleStoreHandlerException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
118 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
119 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
120 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
121 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
122 } catch (TripleStoreSearchError e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
123 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
124 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
125 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
126 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
127 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
128
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
129 retString+="</table></body></html>";
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
130
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
131 logger.debug("sending:");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
132 logger.debug(retString);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
133 return new StringRepresentation(retString,MediaType.TEXT_HTML);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
134 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
135
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
136 /**
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
137 * Erzeugt aus einer Annotation, das f�r den Annotator notwendige JSON-Format
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
138 * @param annot
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
139 * @return
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
140 */
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
141 public JSONObject annot2AnnotatorJSON(Annotation annot){
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
142 JSONObject jo = new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
143 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
144 jo.put("text", annot.text);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
145 jo.put("uri",annot.url);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
146
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
147 JSONObject userObject= new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
148 userObject.put("id",annot.creator);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
149
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
150 RestServer restServer = (RestServer) getApplication();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
151
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
152 String userID= annot.creator;
26
235b91ba8dff on the way to new annotations...
casties
parents: 23
diff changeset
153 if (userID.startsWith(NS.MPIWG_PERSONS_URL)){
235b91ba8dff on the way to new annotations...
casties
parents: 23
diff changeset
154 userID=userID.replace(NS.MPIWG_PERSONS_URL, ""); //entferne NAMESPACE
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
155 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
156 String userName=restServer.getUserNameFromLdap(userID);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
157 userObject.put("name",userName);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
158
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
159 jo.put("user",userObject);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
160
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
161 List<String> xpointer = new ArrayList<String>();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
162
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
163 if (annot.xpointers==null || annot.xpointers.size()==0)
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
164 xpointer.add(annot.xpointer);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
165 else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
166 for(String xpointerString:annot.xpointers){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
167 xpointer.add(xpointerString);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
168 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
169 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
170 jo.put("ranges", transformToRanges(xpointer));
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
171 jo.put("id", annot.getAnnotationUri());
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
172 return jo;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
173 } catch (JSONException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
174 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
175 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
176 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
177 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
178 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
179
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
180 @Get("json")
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
181 public Representation doGetJSON(Representation entity){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
182
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
183 doOptions(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
184 Form form = getRequest().getResourceRef().getQueryAsForm();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
185 String uri = form.getFirstValue("uri");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
186 String user = form.getFirstValue("user");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
187
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
188 String limit=form.getFirstValue("limit");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
189 String offset=form.getFirstValue("offset");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
190
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
191
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
192 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
193 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
194
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
195 JSONArray ja;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
196 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
197
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
198 List<Annotation> annots=searcher.searchByUriUser(uri,user,limit,offset);
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
199
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
200 ja = new JSONArray();
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
201 for (Annotation annot:annots){
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
202 // JSONObject jo = new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
203 // jo.put("text", annot.text);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
204 // jo.put("uri",annot.url);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
205 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
206 // JSONObject userObject= new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
207 // userObject.put("id",annot.creator);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
208 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
209 // RestServer restServer = (RestServer) getApplication();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
210 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
211 // String userID= annot.creator;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
212 // if (userID.startsWith(NS.MPIWG_PERSONS)){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
213 // userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
214 // }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
215 // String userName=restServer.getUserNameFromLdap(userID);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
216 // userObject.put("name",userName);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
217 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
218 // jo.put("user",userObject);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
219 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
220 // List<String> xpointer = new ArrayList<String>();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
221 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
222 // if (annot.xpointers==null || annot.xpointers.size()==0)
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
223 // xpointer.add(annot.xpointer);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
224 // else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
225 // for(String xpointerString:annot.xpointers){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
226 // xpointer.add(xpointerString);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
227 // }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
228 // }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
229 // jo.put("ranges", transformToRanges(xpointer));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
230 JSONObject jo = annot2AnnotatorJSON(annot);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
231 if (jo!=null){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
232 ja.put(annot2AnnotatorJSON(annot));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
233 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
234 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
235 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
236 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
237 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
238 } catch (TripleStoreHandlerException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
239 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
240 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
241 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
242 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
243 } catch (TripleStoreSearchError e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
244 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
245 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
246 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
247 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
248 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
249
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
250 JSONObject result = new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
251 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
252 result.put("rows",ja);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
253 result.put("total",ja.length());
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
254 } catch (JSONException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
255 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
256 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
257 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
258 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
259 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
260
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
261 logger.debug("sending:");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
262 logger.debug(result);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
263 return new JsonRepresentation(result);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
264 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
265
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
266 private JSONArray transformToRanges(List<String> xpointers) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
267
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
268 JSONArray ja = new JSONArray();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
269
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
270 Pattern rg = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
271 Pattern rg1 = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
272
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
273
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
274
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
275 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
276 for(String xpointer:xpointers){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
277 String decoded =URLDecoder.decode(xpointer,"utf-8");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
278 Matcher m=rg.matcher(decoded);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
279
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
280 if (m.find()){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
281 {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
282 JSONObject jo = new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
283 jo.put("start", m.group(1));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
284 jo.put("startOffset", m.group(2));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
285 jo.put("end", m.group(3));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
286 jo.put("endOffset", m.group(4));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
287 ja.put(jo);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
288 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
289 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
290 m=rg1.matcher(xpointer);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
291 if (m.find()){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
292 JSONObject jo = new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
293 jo.put("start", m.group(1));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
294 jo.put("startOffset", m.group(2));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
295
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
296 ja.put(jo);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
297 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
298
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
299
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
300 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
301 } catch (JSONException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
302 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
303 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
304 } catch (UnsupportedEncodingException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
305 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
306 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
307 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
308
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
309
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
310 return ja;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
311 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
312
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
313
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
314
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
315 /**
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
316 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
317 * @param entity should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
318 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
319 * username,password is optional, if not given BasicAuthentification is used.
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
320 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
321 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
322 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
323 * If username given as a URI, the username will be transformed to an URI, username will be added to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
324 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
325 * @return
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
326 */
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
327 protected Annotation handleForm(Representation entity) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
328 Annotation annot;
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
329 Form form = new Form(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
330 String username = form.getValues("username");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
331 String mode = form.getValues("mode");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
332 String password = form.getValues("password");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
333 String xpointer = form.getValues("xpointer");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
334 String text = form.getValues("text");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
335 String title = form.getValues("title");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
336 String url = form.getValues("url");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
337 String type = form.getValues("type");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
338 RestServer restServer = (RestServer) getApplication();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
339
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
340 // falls user and password nicht null sind:
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
341 User userFromForm = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
342 if (username != null && password != null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
343 if (restServer.authenticate(username, password, getRequest())) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
344 userFromForm = new User(username);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
345 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
346 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
347 User authUser = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
348
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
349 if (userFromForm == null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
350 authUser = handleBasicAuthentification(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
351 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
352
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
353 // weder BasicAuth noch FormAuth
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
354 if (authUser == null && userFromForm == null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
355 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
356 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
357 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
358
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
359 if (userFromForm != null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
360 username = userFromForm.getIdentifier();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
361 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
362 username = authUser.getIdentifier();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
363 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
364
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
365 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
366 String usernameOrig=username;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
367 if (!username.startsWith("http"))
26
235b91ba8dff on the way to new annotations...
casties
parents: 23
diff changeset
368 username=NS.MPIWG_PERSONS_URL+username;
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
369
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
370 if (mode.equals("complexAnnotation")){// Annotation mit text in externer ressource
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
371
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
372 Context context = getContext();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
373 String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
374
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
375
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
376 AnnotationHandler ah = new AnnotationHandler(drupalPath);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
377 JSONObject newAnnot;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
378 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
379 newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
380 } catch (UnknowUserException e1) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
381 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
382 e1.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
383 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
384 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
385 try {
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
386 annot= new Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
387 } catch (JSONException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
388 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
389 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
390 setStatus(Status.SERVER_ERROR_INTERNAL);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
391 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
392 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
393 } else
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
394 annot = new Annotation(xpointer, username, null, text,
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
395 type, url);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
396 return annot;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
397 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
398
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
399 private User handleBasicAuthentification(Representation entity) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
400 RestServer restServer = (RestServer) getApplication();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
401 if (!restServer.authenticate(getRequest(), getResponse())) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
402 // Not authenticated
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
403 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
404 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
405
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
406 ClientInfo ci = getRequest().getClientInfo();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
407 logger.debug(ci);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
408 return getRequest().getClientInfo().getUser();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
409
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
410 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
411
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
412 /**
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
413 * using a minimal annotation format based on the annotea specification
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
414 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
415 * @param jo
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
416 * must contain xpointer, text,url,type and can contain a
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
417 * username, if not the username form the authentification will
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
418 * be used.
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
419 * @param authUser
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
420 * user object
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
421 * The username will be transformed to an URI if not given already as URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
422
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
423 * @return
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
424 * @throws JSONException
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
425 */
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
426 public Annotation handleAnnotea(JSONObject jo, Representation entity)
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
427 throws JSONException {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
428
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
429 User authUser = handleBasicAuthentification(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
430 String username = jo.getString("username"); // not required, if no
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
431 // username given authuser
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
432 // will be used.
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
433 String xpointer = jo.getString("xpointer");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
434 String text = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
435 if (jo.has("text"))
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
436 text = jo.getString("text");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
437
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
438 String url = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
439 if (jo.has("url"))
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
440 url = jo.getString("url");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
441
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
442 String type = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
443 if (jo.has("type"))
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
444 type = jo.getString("type");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
445
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
446 if (username == null)
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
447 username = authUser.getIdentifier();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
448
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
449 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
450 if (!username.startsWith("http"))
26
235b91ba8dff on the way to new annotations...
casties
parents: 23
diff changeset
451 username=NS.MPIWG_PERSONS_URL+username;
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
452
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
453 return new Annotation(xpointer, username, null, text, type, url);
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
454 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
455
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
456 /**
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
457 * uses the specification from the annotator project.
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
458 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
459 * @see{https://github.com/okfn/annotator/wiki/Annotation-format} The user
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
460 * object must
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
461 * contain an
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
462 * id and
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
463 * password or
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
464 * basic
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
465 * authentification
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
466 * is used.
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
467 * The username will be transformed to an URI if not given already as URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
468 * @param jo
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
469 * @param authUser
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
470 * @return
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
471 * @throws JSONException
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
472 */
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
473 public Annotation handleAnnotatorSchema(JSONObject jo,
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
474 Representation entity) throws JSONException {
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
475 Annotation annot;
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
476 String url = jo.getString("uri");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
477 String text = jo.getString("text");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
478
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
479 String username = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
480 if (jo.has("user")) { // not required, if no username given authuser
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
481 // will be used otherwise username and password
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
482 // has to be submitted
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
483 JSONObject user = jo.getJSONObject("user");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
484 if (user.has("id")) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
485 username = user.getString("id");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
486 if(!user.has("password")){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
487 User authUser = handleBasicAuthentification(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
488 if (authUser==null){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
489 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
490 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
491 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
492 username = authUser.getIdentifier();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
493 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
494 String password = user.getString("password");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
495 if (!((RestServer) getApplication()).authenticate(username,
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
496 password, getRequest())) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
497 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
498 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
499 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
500 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
501 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
502
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
503 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
504 User authUser = handleBasicAuthentification(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
505 if (authUser == null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
506 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
507 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
508 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
509 username = authUser.getIdentifier();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
510 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
511
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
512 String xpointer;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
513 if (jo.has("ranges")) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
514 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
515 String start = ranges.getString("start");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
516 String end = ranges.getString("end");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
517 String startOffset = ranges.getString("startOffset");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
518 String endOffset = ranges.getString("endOffset");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
519
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
520 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
521 xpointer = url+"#"+
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
522 URLEncoder.encode(String.format(
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
523 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))",
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
524 start, startOffset, end, endOffset),"utf-8");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
525 } catch (UnsupportedEncodingException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
526 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
527 setStatus(Status.SERVER_ERROR_INTERNAL);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
528 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
529 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
530 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
531 xpointer = url;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
532 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
533
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
534 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
535 if (!username.startsWith("http"))
26
235b91ba8dff on the way to new annotations...
casties
parents: 23
diff changeset
536 username=NS.MPIWG_PERSONS_URL+username;
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
537
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
538 return new Annotation(xpointer, username, null, text, null);
7
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
539 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
540
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
541 }