diff 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
line wrap: on
line diff
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java	Mon Mar 19 14:50:28 2012 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java	Mon Mar 19 21:26:20 2012 +0100
@@ -26,152 +26,170 @@
 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
 
 /**
- * Implements the "search" uri of the Annotator API.
- * see <https://github.com/okfn/annotator/wiki/Storage>
+ * Implements the "search" uri of the Annotator API. see
+ * <https://github.com/okfn/annotator/wiki/Storage>
  * 
  * @author casties
- *
+ * 
  */
 public class AnnotatorSearch extends AnnotatorResourceImpl {
-    
+
     private Logger logger = Logger.getRootLogger();
 
     protected String getAllowedMethodsForHeader() {
         return "OPTIONS,GET";
     }
-    
+
     /**
-     * JSON content type result.
+     * result for JSON content-type. optional search parameters: uri user limit
+     * offset
      * 
      * @param entity
      * @return
      */
     @Get("json")
-    public Representation doGetJSON(Representation entity){
-        
+    public Representation doGetJSON(Representation entity) {
+
         doOptions(entity);
+
+        // check authToken
+        Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
+        String ck = requestHeaders.getFirstValue("x-annotator-consumer-key", true);
+        if (ck != null) {
+            RestServer restServer = (RestServer) getApplication();
+            String cs = restServer.getConsumerSecret(ck);
+            logger.debug("requested consumer key=" + ck + " secret=" + cs);
+        }
+
         Form form = getRequest().getResourceRef().getQueryAsForm();
         String uri = form.getFirstValue("uri");
         String user = form.getFirstValue("user");
 
-        String limit=form.getFirstValue("limit");
-        String offset=form.getFirstValue("offset");
+        String limit = form.getFirstValue("limit");
+        String offset = form.getFirstValue("offset");
 
-        RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
+        RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO
+                                                                       // should
+                                                                       // ge
+                                                                       // into
+                                                                       // config
+                                                                       // file
 
         JSONArray ja;
         try {
-            
-            List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
+
+            List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset);
 
             ja = new JSONArray();
-            for (Convert.Annotation annot:annots){
+            for (Convert.Annotation annot : annots) {
                 JSONObject jo = annot2AnnotatorJSON(annot);
-                if (jo!=null){
+                if (jo != null) {
                     ja.put(annot2AnnotatorJSON(annot));
                 } else {
-                    setStatus(Status.SERVER_ERROR_INTERNAL,"JSON Error");
+                    setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
                     return null;
                 }
             }
         } catch (TripleStoreHandlerException e) {
-            // TODO Auto-generated catch block
             e.printStackTrace();
-            setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
+            setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
             return null;
         } catch (TripleStoreSearchError e) {
-            // TODO Auto-generated catch block
             e.printStackTrace();
-            setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
+            setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
             return null;
-        } 
+        }
 
         JSONObject result = new JSONObject();
         try {
-            result.put("rows",ja);
-            result.put("total",ja.length());
+            result.put("rows", ja);
+            result.put("total", ja.length());
         } catch (JSONException e) {
-            // TODO Auto-generated catch block
             e.printStackTrace();
-            setStatus(Status.SERVER_ERROR_INTERNAL,"JSON Error");
+            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
             return null;
         }
-        
+
         logger.debug("sending:");
         logger.debug(result);
         return new JsonRepresentation(result);
     }
 
     /**
-     * HTML content type result.
+     * result for HTML content-type.
      * 
      * @param entity
      * @return
      */
     @Get("html")
-    public Representation doGetHTML(Representation entity){
-        
+    public Representation doGetHTML(Representation entity) {
+
         doOptions(entity);
         Form form = getRequest().getResourceRef().getQueryAsForm();
         String uri = form.getFirstValue("uri");
         String user = form.getFirstValue("user");
 
-        String limit=form.getFirstValue("limit");
-        String offset=form.getFirstValue("offset");
+        String limit = form.getFirstValue("limit");
+        String offset = form.getFirstValue("offset");
 
         try {
-            if (uri!=null){
-            uri = URLDecoder.decode(uri, "utf-8");
+            if (uri != null) {
+                uri = URLDecoder.decode(uri, "utf-8");
             }
         } catch (UnsupportedEncodingException e1) {
             e1.printStackTrace();
             setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
             return null;
         }
-        
-        RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
 
-        String retString="<html><body><table>";
-        String lineFormat="<tr><td><a href=\"%s\">%s</a></td>" +
-                "<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>";
+        RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO
+                                                                       // should
+                                                                       // ge
+                                                                       // into
+                                                                       // config
+                                                                       // file
+
+        String retString = "<html><body><table>";
+        String lineFormat = "<tr><td><a href=\"%s\">%s</a></td>"
+                + "<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>";
         try {
-            
-            List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
+
+            List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset);
 
-            for (Convert.Annotation annot:annots){
-                
-                
+            for (Convert.Annotation annot : annots) {
+
                 RestServer restServer = (RestServer) getApplication();
-                String userName=restServer.getUserNameFromLdap(annot.creator);
+                String userName = restServer.getUserNameFromLdap(annot.creator);
                 List<String> xpointer = new ArrayList<String>();
 
-                if (annot.xpointers==null || annot.xpointers.size()==0)
-                    retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,annot.xpointer,annot.xpointer,annot.annotationUri,annot.annotationUri);
+                if (annot.xpointers == null || annot.xpointers.size() == 0)
+                    retString += String.format(lineFormat, userName, userName, annot.url, annot.url, annot.time, annot.text,
+                            annot.xpointer, annot.xpointer, annot.annotationUri, annot.annotationUri);
                 else {
-                    for(String xpointerString:annot.xpointers){
-                        retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.annotationUri,annot.annotationUri);
+                    for (String xpointerString : annot.xpointers) {
+                        retString += String.format(lineFormat, userName, userName, annot.url, annot.url, annot.time, annot.text,
+                                xpointerString, xpointerString, annot.annotationUri, annot.annotationUri);
                     }
                 }
-            
+
             }
         } catch (TripleStoreHandlerException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
-            setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
+            setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
             return null;
         } catch (TripleStoreSearchError e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
-            setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
+            setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
             return null;
-        } 
+        }
 
-        retString+="</table></body></html>";
-        
+        retString += "</table></body></html>";
+
         logger.debug("sending:");
         logger.debug(retString);
-        return new StringRepresentation(retString,MediaType.TEXT_HTML);
+        return new StringRepresentation(retString, MediaType.TEXT_HTML);
     }
 
-
 }