Changeset 14:629e15b345aa in AnnotationManagerN4J for src/main/java/de/mpiwg/itgroup/annotations/restlet
- Timestamp:
- Jul 13, 2012, 6:41:02 PM (13 years ago)
- Branch:
- default
- Location:
- src/main/java/de/mpiwg/itgroup/annotations/restlet
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java
r4 r14 51 51 // TODO: what to return without id - list of all annotations? 52 52 53 // TODO: what to do with authentication? 54 boolean authenticated = isAuthenticated(entity); 55 logger.debug("request authenticated=" + authenticated); 56 57 Annotation annots = getAnnotationStore().getAnnotationById(id); 58 if (annots != null) { 59 // there should be only one 60 JSONObject result = createAnnotatorJson(annots); 53 // do authentication 54 String authUser = this.checkAuthToken(entity); 55 logger.debug("request authenticated=" + authUser); 56 57 Annotation annot = getAnnotationStore().getAnnotationById(id); 58 if (annot != null) { 59 if (! annot.isActionAllowed("read", authUser)) { 60 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 61 return null; 62 } 63 JSONObject result = createAnnotatorJson(annot, (authUser == null)); 61 64 logger.debug("sending:"); 62 65 logger.debug(result); 63 66 return new JsonRepresentation(result); 64 67 } else { 65 JSONArray results = new JSONArray(); 66 // annotator read request returns a list of annotation objects 67 logger.debug("sending:"); 68 logger.debug(results); 69 return new JsonRepresentation(results); 68 // not found 69 setStatus(Status.CLIENT_ERROR_NOT_FOUND); 70 return null; 70 71 } 71 72 } … … 73 74 /** 74 75 * POST with JSON content-type. 75 *76 * json hash: username: name des users xpointer: xpointer auf den Ausschnitt77 * (incl. der URL des Dokumentes) text: text der annotation annoturl: url78 * auf eine Annotation falls extern79 76 * 80 77 * @return … … 85 82 // set headers 86 83 setCorsHeaders(); 84 85 // do authentication TODO: who's allowed to create? 86 String authUser = this.checkAuthToken(entity); 87 logger.debug("request authenticated=" + authUser); 88 if (authUser == null) { 89 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 90 return null; 91 } 92 87 93 Annotation annot = null; 88 94 try { … … 115 121 * return 303: see other. For now we return the annotation. 116 122 */ 117 JSONObject jo = createAnnotatorJson(storedAnnot );123 JSONObject jo = createAnnotatorJson(storedAnnot, (authUser == null)); 118 124 JsonRepresentation retRep = new JsonRepresentation(jo); 119 125 return retRep; … … 135 141 logger.debug("annotation-id=" + id); 136 142 137 // TODO: what to do with authentication? we should check the owner 138 boolean authenticated = isAuthenticated(entity); 139 logger.debug("request authenticated=" + authenticated); 140 if (!authenticated) { 141 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 142 return null; 143 } 143 // do authentication 144 String authUser = this.checkAuthToken(entity); 145 logger.debug("request authenticated=" + authUser); 144 146 145 147 Annotation annot = null; … … 156 158 if (storedAnnot == null) { 157 159 setStatus(Status.CLIENT_ERROR_NOT_FOUND); 160 return null; 161 } 162 if (! storedAnnot.isActionAllowed("update", authUser)) { 163 setStatus(Status.CLIENT_ERROR_FORBIDDEN); 158 164 return null; 159 165 } … … 170 176 */ 171 177 // return new annotation 172 jo = createAnnotatorJson(storedAnnot );178 jo = createAnnotatorJson(storedAnnot, (authUser == null)); 173 179 JsonRepresentation retRep = new JsonRepresentation(jo); 174 180 return retRep; … … 198 204 logger.debug("annotation-id=" + id); 199 205 200 // TODO: what to do with authentication? we should check the owner 201 boolean authenticated = isAuthenticated(entity); 202 logger.debug("request authenticated=" + authenticated); 203 if (!authenticated) { 204 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 205 return null; 206 } 207 206 // do authentication 207 String authUser = this.checkAuthToken(entity); 208 logger.debug("request authenticated=" + authUser); 209 Annotation annot = getAnnotationStore().getAnnotationById(id); 210 if (annot != null) { 211 if (! annot.isActionAllowed("delete", authUser)) { 212 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 213 return null; 214 } 215 } 216 208 217 // delete annotation 209 218 getAnnotationStore().deleteById(id); -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
r10 r14 141 141 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers"); 142 142 String authToken = requestHeaders.getFirstValue("x-annotator-auth-token", true); 143 if (authToken == null) return null; 143 144 // decode token first to get consumer key 144 145 JsonToken token = new JsonTokenParser(null, null).deserialize(authToken); … … 178 179 * 179 180 * @param annot 181 * @param forAnonymous TODO 180 182 * @return 181 183 */ 182 public JSONObject createAnnotatorJson(Annotation annot ) {184 public JSONObject createAnnotatorJson(Annotation annot, boolean forAnonymous) { 183 185 // return user as a JSON object (otherwise just as string) 184 186 boolean makeUserObject = true; … … 233 235 if (adminPerm != null) { 234 236 adminPerms.put(adminPerm.getIdString()); 237 } else if (forAnonymous) { 238 // set something because its not allowed for anonymous 239 adminPerms.put("not-you"); 235 240 } 236 241 // delete … … 240 245 if (deletePerm != null) { 241 246 deletePerms.put(deletePerm.getIdString()); 247 } else if (forAnonymous) { 248 // set something because its not allowed for anonymous 249 deletePerms.put("not-you"); 242 250 } 243 251 // update … … 247 255 if (updatePerm != null) { 248 256 updatePerms.put(updatePerm.getIdString()); 257 } else if (forAnonymous) { 258 // set something because its not allowed for anonymous 259 updatePerms.put("not-you"); 249 260 } 250 261 // read -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java
r4 r14 16 16 17 17 import de.mpiwg.itgroup.annotations.Annotation; 18 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;19 18 20 19 /** … … 32 31 33 32 /** 34 * result for JSON content-type. optional search parameters: uri user limit35 * offset 33 * result for JSON content-type. optional search parameters: uri, user, limit, 34 * offset. 36 35 * 37 36 * @param entity … … 42 41 logger.debug("AnnotatorSearch doGetJSON!"); 43 42 setCorsHeaders(); 44 // TODO: what to do with authentication?45 boolean authenticated = isAuthenticated(entity);46 logger.debug("request authenticated=" + auth enticated);43 // do authentication 44 String authUser = this.checkAuthToken(entity); 45 logger.debug("request authenticated=" + authUser); 47 46 48 47 Form form = getRequest().getResourceRef().getQueryAsForm(); 49 48 String uri = form.getFirstValue("uri"); 50 49 String user = form.getFirstValue("user"); 51 52 50 String limit = form.getFirstValue("limit"); 53 51 String offset = form.getFirstValue("offset"); 54 52 55 AnnotationStore searcher = getAnnotationStore(); 56 57 JSONArray ja; 58 59 List<Annotation> annots = searcher.searchByUriUser(uri, user, limit, offset); 60 61 ja = new JSONArray(); 53 JSONArray results = new JSONArray(); 54 // do search 55 logger.debug(String.format("searching for uri=%s user=%s", uri, user)); 56 List<Annotation> annots = getAnnotationStore().searchByUriUser(uri, user, limit, offset); 62 57 for (Annotation annot : annots) { 63 JSONObject jo = createAnnotatorJson(annot); 58 // check permission 59 if (!annot.isActionAllowed("read", authUser)) continue; 60 JSONObject jo = createAnnotatorJson(annot, (authUser == null)); 64 61 if (jo != null) { 65 ja.put(createAnnotatorJson(annot));62 results.put(jo); 66 63 } else { 67 64 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); … … 69 66 } 70 67 } 71 68 // assemble result object 72 69 JSONObject result = new JSONObject(); 73 70 try { 74 result.put("rows", ja);75 result.put("total", ja.length());71 result.put("rows", results); 72 result.put("total", results.length()); 76 73 } catch (JSONException e) { 77 e.printStackTrace();78 74 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); 79 75 return null;
Note: See TracChangeset
for help on using the changeset viewer.