annotate src/de/mpiwg/itgroup/annotationManager/restlet/SearchAnnotations.java @ 7:97f68ab3430f

support both read and search api of Annotator. some cleanup of imports.
author casties
date Mon, 19 Mar 2012 12:01:39 +0100
parents
children 667d98fd28bd
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;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
32 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
33 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
34 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
35 import de.mpiwg.itgroup.annotationManager.drupal.AnnotationHandler;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
36 import de.mpiwg.itgroup.annotationManager.drupal.UnknowUserException;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
37 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
40 public class SearchAnnotations extends ServerResource {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
41
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
42 private Logger logger = Logger.getRootLogger();
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
46 @Options
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
47 public void doOptions(Representation entity) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
48 Form responseHeaders = (Form) getResponse().getAttributes().get(
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
49 "org.restlet.http.headers");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
50 if (responseHeaders == null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
51 responseHeaders = new Form();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
52 getResponse().getAttributes().put("org.restlet.http.headers",
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
53 responseHeaders);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
54 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
55 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
56 String origin = requestHeaders.getFirstValue("Origin", true);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
57 if (origin == null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
58 responseHeaders.add("Access-Control-Allow-Origin", "*");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
59 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
60 responseHeaders.add("Access-Control-Allow-Origin", origin);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
61 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
62 responseHeaders.add("Access-Control-Allow-Methods", "OPTIONS,GET");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
63 String allowHeaders = requestHeaders.getFirstValue("Access-Control-Request-Headers", true);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
64 if (allowHeaders != null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
65 responseHeaders.add("Access-Control-Allow-Headers", allowHeaders);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
66 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
67 //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
68 responseHeaders.add("Access-Control-Allow-Credentials", "true");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
69 responseHeaders.add("Access-Control-Max-Age", "60");
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
72 @Get("html")
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
73 public Representation doGetHTML(Representation entity){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
74
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
75 doOptions(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
76 Form form = getRequest().getResourceRef().getQueryAsForm();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
77 String uri = form.getFirstValue("uri");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
78 String user = form.getFirstValue("user");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
79
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
80 String limit=form.getFirstValue("limit");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
81 String offset=form.getFirstValue("offset");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
82
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
83 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
84 if (uri!=null){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
85 uri = URLDecoder.decode(uri, "utf-8");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
86 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
87 } catch (UnsupportedEncodingException e1) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
88 e1.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
89 setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
90 return null;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
93 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
94
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
95 String retString="<html><body><table>";
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
96 String lineFormat="<tr><td><a href=\"%s\">%s</a></td>" +
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
97 "<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
98 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
99
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
100 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
101
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
102 for (Convert.Annotation annot:annots){
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
105 RestServer restServer = (RestServer) getApplication();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
106 String userName=restServer.getUserNameFromLdap(annot.creator);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
107 List<String> xpointer = new ArrayList<String>();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
108
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
109 if (annot.xpointers==null || annot.xpointers.size()==0)
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
110 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,annot.xpointer,annot.xpointer,annot.annotationUri,annot.annotationUri);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
111 else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
112 for(String xpointerString:annot.xpointers){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
113 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.annotationUri,annot.annotationUri);
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 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
118 } catch (TripleStoreHandlerException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
119 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
120 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
121 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
122 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
123 } catch (TripleStoreSearchError e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
124 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
125 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
126 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
127 return null;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
130 retString+="</table></body></html>";
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
131
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
132 logger.debug("sending:");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
133 logger.debug(retString);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
134 return new StringRepresentation(retString,MediaType.TEXT_HTML);
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 /**
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
138 * 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
139 * @param annot
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
140 * @return
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
141 */
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
142 public JSONObject annot2AnnotatorJSON(Convert.Annotation annot){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
143 JSONObject jo = new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
144 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
145 jo.put("text", annot.text);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
146 jo.put("uri",annot.url);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
147
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
148 JSONObject userObject= new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
149 userObject.put("id",annot.creator);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
150
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
151 RestServer restServer = (RestServer) getApplication();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
152
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
153 String userID= annot.creator;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
154 if (userID.startsWith(NS.MPIWG_PERSONS)){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
155 userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
156 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
157 String userName=restServer.getUserNameFromLdap(userID);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
158 userObject.put("name",userName);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
159
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
160 jo.put("user",userObject);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
161
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
162 List<String> xpointer = new ArrayList<String>();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
163
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
164 if (annot.xpointers==null || annot.xpointers.size()==0)
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
165 xpointer.add(annot.xpointer);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
166 else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
167 for(String xpointerString:annot.xpointers){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
168 xpointer.add(xpointerString);
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 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
171 jo.put("ranges", transformToRanges(xpointer));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
172 jo.put("id", annot.annotationUri);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
173 return jo;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
174 } catch (JSONException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
175 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
176 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
177 return null;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
181 @Get("json")
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
182 public Representation doGetJSON(Representation entity){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
183
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
184 doOptions(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
185 Form form = getRequest().getResourceRef().getQueryAsForm();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
186 String uri = form.getFirstValue("uri");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
187 String user = form.getFirstValue("user");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
188
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
189 String limit=form.getFirstValue("limit");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
190 String offset=form.getFirstValue("offset");
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 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
194 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
195
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
196 JSONArray ja;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
197 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
198
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
199 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
200
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
201 ja = new JSONArray();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
202 for (Convert.Annotation annot:annots){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
203 // JSONObject jo = new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
204 // jo.put("text", annot.text);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
205 // jo.put("uri",annot.url);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
206 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
207 // JSONObject userObject= new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
208 // userObject.put("id",annot.creator);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
209 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
210 // RestServer restServer = (RestServer) getApplication();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
211 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
212 // String userID= annot.creator;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
213 // if (userID.startsWith(NS.MPIWG_PERSONS)){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
214 // userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
215 // }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
216 // String userName=restServer.getUserNameFromLdap(userID);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
217 // userObject.put("name",userName);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
218 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
219 // jo.put("user",userObject);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
220 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
221 // List<String> xpointer = new ArrayList<String>();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
222 //
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
223 // if (annot.xpointers==null || annot.xpointers.size()==0)
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
224 // xpointer.add(annot.xpointer);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
225 // else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
226 // for(String xpointerString:annot.xpointers){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
227 // xpointer.add(xpointerString);
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 // }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
230 // jo.put("ranges", transformToRanges(xpointer));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
231 JSONObject jo = annot2AnnotatorJSON(annot);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
232 if (jo!=null){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
233 ja.put(annot2AnnotatorJSON(annot));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
234 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
235 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
236 return null;
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 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
239 } catch (TripleStoreHandlerException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
240 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
241 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
242 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
243 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
244 } catch (TripleStoreSearchError e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
245 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
246 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
247 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
248 return null;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
251 JSONObject result = new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
252 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
253 result.put("rows",ja);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
254 result.put("total",ja.length());
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
255 } catch (JSONException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
256 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
257 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
258 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
259 return null;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
262 logger.debug("sending:");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
263 logger.debug(result);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
264 return new JsonRepresentation(result);
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
267 private JSONArray transformToRanges(List<String> xpointers) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
268
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
269 JSONArray ja = new JSONArray();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
270
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
271 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
272 Pattern rg1 = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
276 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
277 for(String xpointer:xpointers){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
278 String decoded =URLDecoder.decode(xpointer,"utf-8");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
279 Matcher m=rg.matcher(decoded);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
280
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
281 if (m.find()){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
282 {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
283 JSONObject jo = new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
284 jo.put("start", m.group(1));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
285 jo.put("startOffset", m.group(2));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
286 jo.put("end", m.group(3));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
287 jo.put("endOffset", m.group(4));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
288 ja.put(jo);
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 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
291 m=rg1.matcher(xpointer);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
292 if (m.find()){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
293 JSONObject jo = new JSONObject();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
294 jo.put("start", m.group(1));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
295 jo.put("startOffset", m.group(2));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
296
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
297 ja.put(jo);
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 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
302 } catch (JSONException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
303 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
304 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
305 } catch (UnsupportedEncodingException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
306 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
307 e.printStackTrace();
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
311 return ja;
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 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
318 * @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
319 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
320 * username,password is optional, if not given BasicAuthentification is used.
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 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
324 * 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
325 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
326 * @return
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
327 */
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
328 protected Convert.Annotation handleForm(Representation entity) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
329 Convert.Annotation annot;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
330 Form form = new Form(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
331 String username = form.getValues("username");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
332 String mode = form.getValues("mode");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
333 String password = form.getValues("password");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
334 String xpointer = form.getValues("xpointer");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
335 String text = form.getValues("text");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
336 String title = form.getValues("title");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
337 String url = form.getValues("url");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
338 String type = form.getValues("type");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
339 RestServer restServer = (RestServer) getApplication();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
340
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
341 // falls user and password nicht null sind:
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
342 User userFromForm = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
343 if (username != null && password != null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
344 if (restServer.authenticate(username, password, getRequest())) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
345 userFromForm = new User(username);
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 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
348 User authUser = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
349
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
350 if (userFromForm == null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
351 authUser = handleBasicAuthentification(entity);
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
354 // weder BasicAuth noch FormAuth
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
355 if (authUser == null && userFromForm == null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
356 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
357 return null;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
360 if (userFromForm != null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
361 username = userFromForm.getIdentifier();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
362 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
363 username = authUser.getIdentifier();
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
366 //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
367 String usernameOrig=username;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
368 if (!username.startsWith("http"))
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
369 username=NS.MPIWG_PERSONS+username;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
370
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
371 if (mode.equals("complexAnnotation")){// Annotation mit text in externer ressource
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
372
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
373 Context context = getContext();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
374 String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
377 AnnotationHandler ah = new AnnotationHandler(drupalPath);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
378 JSONObject newAnnot;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
379 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
380 newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
381 } catch (UnknowUserException e1) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
382 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
383 e1.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
384 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
385 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
386 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
387 annot= new Convert.Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
388 } catch (JSONException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
389 // TODO Auto-generated catch block
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
390 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
391 setStatus(Status.SERVER_ERROR_INTERNAL);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
392 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
393 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
394 } else
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
395 annot = new Convert.Annotation(xpointer, username, null, text,
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
396 type, url);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
397 return annot;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
400 private User handleBasicAuthentification(Representation entity) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
401 RestServer restServer = (RestServer) getApplication();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
402 if (!restServer.authenticate(getRequest(), getResponse())) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
403 // Not authenticated
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
404 return null;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
407 ClientInfo ci = getRequest().getClientInfo();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
408 logger.debug(ci);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
409 return getRequest().getClientInfo().getUser();
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 /**
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
414 * using a minimal annotation format based on the annotea specification
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
415 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
416 * @param jo
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
417 * must contain xpointer, text,url,type and can contain a
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
418 * username, if not the username form the authentification will
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
419 * be used.
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
420 * @param authUser
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
421 * user object
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
422 * 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
423
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
424 * @return
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
425 * @throws JSONException
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
426 */
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
427 public Annotation handleAnnotea(JSONObject jo, Representation entity)
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
428 throws JSONException {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
429
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
430 User authUser = handleBasicAuthentification(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
431 String username = jo.getString("username"); // not required, if no
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
432 // username given authuser
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
433 // will be used.
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
434 String xpointer = jo.getString("xpointer");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
435 String text = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
436 if (jo.has("text"))
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
437 text = jo.getString("text");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
438
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
439 String url = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
440 if (jo.has("url"))
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
441 url = jo.getString("url");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
442
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
443 String type = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
444 if (jo.has("type"))
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
445 type = jo.getString("type");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
446
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
447 if (username == null)
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
448 username = authUser.getIdentifier();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
449
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
450 //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
451 if (!username.startsWith("http"))
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
452 username=NS.MPIWG_PERSONS+username;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
453
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
454 return new Convert.Annotation(xpointer, username, null, text, type, url);
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 /**
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
458 * uses the specification from the annotator project.
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
459 *
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
460 * @see{https://github.com/okfn/annotator/wiki/Annotation-format} The user
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
461 * object must
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
462 * contain an
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
463 * id and
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
464 * password or
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
465 * basic
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
466 * authentification
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
467 * is used.
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
468 * 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
469 * @param jo
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
470 * @param authUser
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
471 * @return
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
472 * @throws JSONException
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
473 */
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
474 public Convert.Annotation handleAnnotatorSchema(JSONObject jo,
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
475 Representation entity) throws JSONException {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
476 Convert.Annotation annot;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
477 String url = jo.getString("uri");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
478 String text = jo.getString("text");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
479
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
480 String username = null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
481 if (jo.has("user")) { // not required, if no username given authuser
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
482 // will be used otherwise username and password
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
483 // has to be submitted
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
484 JSONObject user = jo.getJSONObject("user");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
485 if (user.has("id")) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
486 username = user.getString("id");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
487 if(!user.has("password")){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
488 User authUser = handleBasicAuthentification(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
489 if (authUser==null){
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
490 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
491 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
492 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
493 username = authUser.getIdentifier();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
494 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
495 String password = user.getString("password");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
496 if (!((RestServer) getApplication()).authenticate(username,
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
497 password, getRequest())) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
498 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
499 return null;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
504 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
505 User authUser = handleBasicAuthentification(entity);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
506 if (authUser == null) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
507 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
508 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
509 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
510 username = authUser.getIdentifier();
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
513 String xpointer;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
514 if (jo.has("ranges")) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
515 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
516 String start = ranges.getString("start");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
517 String end = ranges.getString("end");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
518 String startOffset = ranges.getString("startOffset");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
519 String endOffset = ranges.getString("endOffset");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
520
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
521 try {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
522 xpointer = url+"#"+
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
523 URLEncoder.encode(String.format(
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
524 "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
525 start, startOffset, end, endOffset),"utf-8");
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
526 } catch (UnsupportedEncodingException e) {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
527 e.printStackTrace();
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
528 setStatus(Status.SERVER_ERROR_INTERNAL);
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
529 return null;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
530 }
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
531 } else {
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
532 xpointer = url;
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
535 //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
536 if (!username.startsWith("http"))
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
537 username=NS.MPIWG_PERSONS+username;
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
538
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
539 return new Convert.Annotation(xpointer, username, null, text, null);
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
97f68ab3430f support both read and search api of Annotator.
casties
parents:
diff changeset
542 }