comparison src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java @ 9:e9fd2e1e0979

consumer key and secret store (property file).
author casties
date Mon, 19 Mar 2012 21:26:20 +0100
parents 11baadcdd2c8
children 0bdfe01e30b5
comparison
equal deleted inserted replaced
8:11baadcdd2c8 9:e9fd2e1e0979
24 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert; 24 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
25 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher; 25 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
26 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException; 26 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
27 27
28 /** 28 /**
29 * Implements the "search" uri of the Annotator API. 29 * Implements the "search" uri of the Annotator API. see
30 * see <https://github.com/okfn/annotator/wiki/Storage> 30 * <https://github.com/okfn/annotator/wiki/Storage>
31 * 31 *
32 * @author casties 32 * @author casties
33 * 33 *
34 */ 34 */
35 public class AnnotatorSearch extends AnnotatorResourceImpl { 35 public class AnnotatorSearch extends AnnotatorResourceImpl {
36 36
37 private Logger logger = Logger.getRootLogger(); 37 private Logger logger = Logger.getRootLogger();
38 38
39 protected String getAllowedMethodsForHeader() { 39 protected String getAllowedMethodsForHeader() {
40 return "OPTIONS,GET"; 40 return "OPTIONS,GET";
41 } 41 }
42 42
43 /** 43 /**
44 * JSON content type result. 44 * result for JSON content-type. optional search parameters: uri user limit
45 * offset
45 * 46 *
46 * @param entity 47 * @param entity
47 * @return 48 * @return
48 */ 49 */
49 @Get("json") 50 @Get("json")
50 public Representation doGetJSON(Representation entity){ 51 public Representation doGetJSON(Representation entity) {
51 52
52 doOptions(entity); 53 doOptions(entity);
54
55 // check authToken
56 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
57 String ck = requestHeaders.getFirstValue("x-annotator-consumer-key", true);
58 if (ck != null) {
59 RestServer restServer = (RestServer) getApplication();
60 String cs = restServer.getConsumerSecret(ck);
61 logger.debug("requested consumer key=" + ck + " secret=" + cs);
62 }
63
53 Form form = getRequest().getResourceRef().getQueryAsForm(); 64 Form form = getRequest().getResourceRef().getQueryAsForm();
54 String uri = form.getFirstValue("uri"); 65 String uri = form.getFirstValue("uri");
55 String user = form.getFirstValue("user"); 66 String user = form.getFirstValue("user");
56 67
57 String limit=form.getFirstValue("limit"); 68 String limit = form.getFirstValue("limit");
58 String offset=form.getFirstValue("offset"); 69 String offset = form.getFirstValue("offset");
59 70
60 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file 71 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO
72 // should
73 // ge
74 // into
75 // config
76 // file
61 77
62 JSONArray ja; 78 JSONArray ja;
63 try { 79 try {
64 80
65 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset); 81 List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset);
66 82
67 ja = new JSONArray(); 83 ja = new JSONArray();
68 for (Convert.Annotation annot:annots){ 84 for (Convert.Annotation annot : annots) {
69 JSONObject jo = annot2AnnotatorJSON(annot); 85 JSONObject jo = annot2AnnotatorJSON(annot);
70 if (jo!=null){ 86 if (jo != null) {
71 ja.put(annot2AnnotatorJSON(annot)); 87 ja.put(annot2AnnotatorJSON(annot));
72 } else { 88 } else {
73 setStatus(Status.SERVER_ERROR_INTERNAL,"JSON Error"); 89 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
74 return null; 90 return null;
75 } 91 }
76 } 92 }
77 } catch (TripleStoreHandlerException e) { 93 } catch (TripleStoreHandlerException e) {
78 // TODO Auto-generated catch block
79 e.printStackTrace(); 94 e.printStackTrace();
80 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error"); 95 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
81 return null; 96 return null;
82 } catch (TripleStoreSearchError e) { 97 } catch (TripleStoreSearchError e) {
83 // TODO Auto-generated catch block
84 e.printStackTrace(); 98 e.printStackTrace();
85 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error"); 99 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
86 return null; 100 return null;
87 } 101 }
88 102
89 JSONObject result = new JSONObject(); 103 JSONObject result = new JSONObject();
90 try { 104 try {
91 result.put("rows",ja); 105 result.put("rows", ja);
92 result.put("total",ja.length()); 106 result.put("total", ja.length());
93 } catch (JSONException e) { 107 } catch (JSONException e) {
94 // TODO Auto-generated catch block
95 e.printStackTrace(); 108 e.printStackTrace();
96 setStatus(Status.SERVER_ERROR_INTERNAL,"JSON Error"); 109 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
97 return null; 110 return null;
98 } 111 }
99 112
100 logger.debug("sending:"); 113 logger.debug("sending:");
101 logger.debug(result); 114 logger.debug(result);
102 return new JsonRepresentation(result); 115 return new JsonRepresentation(result);
103 } 116 }
104 117
105 /** 118 /**
106 * HTML content type result. 119 * result for HTML content-type.
107 * 120 *
108 * @param entity 121 * @param entity
109 * @return 122 * @return
110 */ 123 */
111 @Get("html") 124 @Get("html")
112 public Representation doGetHTML(Representation entity){ 125 public Representation doGetHTML(Representation entity) {
113 126
114 doOptions(entity); 127 doOptions(entity);
115 Form form = getRequest().getResourceRef().getQueryAsForm(); 128 Form form = getRequest().getResourceRef().getQueryAsForm();
116 String uri = form.getFirstValue("uri"); 129 String uri = form.getFirstValue("uri");
117 String user = form.getFirstValue("user"); 130 String user = form.getFirstValue("user");
118 131
119 String limit=form.getFirstValue("limit"); 132 String limit = form.getFirstValue("limit");
120 String offset=form.getFirstValue("offset"); 133 String offset = form.getFirstValue("offset");
121 134
122 try { 135 try {
123 if (uri!=null){ 136 if (uri != null) {
124 uri = URLDecoder.decode(uri, "utf-8"); 137 uri = URLDecoder.decode(uri, "utf-8");
125 } 138 }
126 } catch (UnsupportedEncodingException e1) { 139 } catch (UnsupportedEncodingException e1) {
127 e1.printStackTrace(); 140 e1.printStackTrace();
128 setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE); 141 setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
129 return null; 142 return null;
130 } 143 }
131
132 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
133 144
134 String retString="<html><body><table>"; 145 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO
135 String lineFormat="<tr><td><a href=\"%s\">%s</a></td>" + 146 // should
136 "<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>"; 147 // ge
148 // into
149 // config
150 // file
151
152 String retString = "<html><body><table>";
153 String lineFormat = "<tr><td><a href=\"%s\">%s</a></td>"
154 + "<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>";
137 try { 155 try {
138
139 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
140 156
141 for (Convert.Annotation annot:annots){ 157 List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset);
142 158
143 159 for (Convert.Annotation annot : annots) {
160
144 RestServer restServer = (RestServer) getApplication(); 161 RestServer restServer = (RestServer) getApplication();
145 String userName=restServer.getUserNameFromLdap(annot.creator); 162 String userName = restServer.getUserNameFromLdap(annot.creator);
146 List<String> xpointer = new ArrayList<String>(); 163 List<String> xpointer = new ArrayList<String>();
147 164
148 if (annot.xpointers==null || annot.xpointers.size()==0) 165 if (annot.xpointers == null || annot.xpointers.size() == 0)
149 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,annot.xpointer,annot.xpointer,annot.annotationUri,annot.annotationUri); 166 retString += String.format(lineFormat, userName, userName, annot.url, annot.url, annot.time, annot.text,
167 annot.xpointer, annot.xpointer, annot.annotationUri, annot.annotationUri);
150 else { 168 else {
151 for(String xpointerString:annot.xpointers){ 169 for (String xpointerString : annot.xpointers) {
152 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.annotationUri,annot.annotationUri); 170 retString += String.format(lineFormat, userName, userName, annot.url, annot.url, annot.time, annot.text,
171 xpointerString, xpointerString, annot.annotationUri, annot.annotationUri);
153 } 172 }
154 } 173 }
155 174
156 } 175 }
157 } catch (TripleStoreHandlerException e) { 176 } catch (TripleStoreHandlerException e) {
158 // TODO Auto-generated catch block 177 // TODO Auto-generated catch block
159 e.printStackTrace(); 178 e.printStackTrace();
160 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error"); 179 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
161 return null; 180 return null;
162 } catch (TripleStoreSearchError e) { 181 } catch (TripleStoreSearchError e) {
163 // TODO Auto-generated catch block 182 // TODO Auto-generated catch block
164 e.printStackTrace(); 183 e.printStackTrace();
165 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error"); 184 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
166 return null; 185 return null;
167 } 186 }
168 187
169 retString+="</table></body></html>"; 188 retString += "</table></body></html>";
170 189
171 logger.debug("sending:"); 190 logger.debug("sending:");
172 logger.debug(retString); 191 logger.debug(retString);
173 return new StringRepresentation(retString,MediaType.TEXT_HTML); 192 return new StringRepresentation(retString, MediaType.TEXT_HTML);
174 } 193 }
175 194
176
177 } 195 }