Changeset 88:b406507a953d in AnnotationManagerN4J for src
- Timestamp:
- Feb 3, 2015, 6:01:27 PM (10 years ago)
- Branch:
- default
- Location:
- src/main/java/de/mpiwg/itgroup/annotations
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/Actor.java
r70 r88 58 58 if (isGroup() && store != null) { 59 59 // check if person in group 60 if (person.groups != null) { 61 // check person's groups 62 if (person.groups.contains(this.id)) { 63 return true; 64 } 65 } 66 // check in store 60 67 return store.isPersonInGroup(person, (Group) this); 61 68 } -
src/main/java/de/mpiwg/itgroup/annotations/Person.java
r87 r88 1 /**2 *3 */4 1 package de.mpiwg.itgroup.annotations; 5 2 … … 26 23 */ 27 24 25 import java.util.Set; 26 28 27 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; 29 28 … … 34 33 public class Person extends Actor { 35 34 35 public Set<String> groups; 36 36 37 public Person() { 37 38 } … … 92 93 return name; 93 94 } 95 96 /** 97 * Returns the anonymous Person. 98 * 99 * @return 100 */ 101 public static Person getAnonymous() { 102 return new Person("anonymous"); 103 } 94 104 95 105 /* (non-Javadoc) -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java
r75 r88 1 /**2 * Implements the "annotations" uri of the Annotator API. see3 * <https://github.com/okfn/annotator/wiki/Storage>4 */5 1 package de.mpiwg.itgroup.annotations.restlet; 6 2 … … 88 84 89 85 // do authentication 90 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));86 Person authUser = getUserFromAuthToken(entity); 91 87 logger.fine("request authenticated=" + authUser); 92 88 … … 173 169 174 170 // do authentication TODO: who's allowed to create? 175 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));171 Person authUser = getUserFromAuthToken(entity); 176 172 logger.fine("request authenticated=" + authUser); 177 173 if (authUser == null) { … … 190 186 // make sure id is not set for POST 191 187 jo.remove("id"); 192 // getAnnotation object from posted JSON188 // create Annotation object from posted JSON 193 189 annot = createAnnotation(jo, entity); 194 190 } catch (IOException e1) { … … 231 227 232 228 // do authentication 233 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));229 Person authUser = getUserFromAuthToken(entity); 234 230 logger.fine("request authenticated=" + authUser); 235 231 … … 294 290 295 291 // do authentication 296 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));292 Person authUser = getUserFromAuthToken(entity); 297 293 logger.fine("request authenticated=" + authUser); 298 294 AnnotationStore store = getAnnotationStore(); -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java
r75 r88 60 60 61 61 // do authentication 62 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));62 Person authUser = getUserFromAuthToken(entity); 63 63 logger.fine("request authenticated=" + authUser); 64 64 -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java
r75 r88 59 59 60 60 // do authentication 61 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));61 Person authUser = getUserFromAuthToken(entity); 62 62 logger.fine("request authenticated=" + authUser); 63 63 -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorGroups.java
r75 r88 1 /**2 * ReST API for accessing groups in the Annotation store.3 */4 1 package de.mpiwg.itgroup.annotations.restlet; 5 2 … … 40 37 import de.mpiwg.itgroup.annotations.Actor; 41 38 import de.mpiwg.itgroup.annotations.Group; 39 import de.mpiwg.itgroup.annotations.Person; 42 40 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 43 41 … … 67 65 logger.fine("AnnotatorGroups doGetJSON!"); 68 66 setCorsHeaders(); 69 Form form = getRequest().getResourceRef().getQueryAsForm(); 70 String user = form.getFirstValue("user"); 71 String uri = form.getFirstValue("uri"); 67 // get user from auth token (preferred) 68 Person authUser = getUserFromAuthToken(entity); 69 JSONArray results = null; 70 if (authUser != null && authUser.groups != null) { 71 results = getGroupsFromPerson(authUser); 72 } else { 73 // get user or uri from request 74 Form form = getRequest().getResourceRef().getQueryAsForm(); 75 String user = form.getFirstValue("user"); 76 String uri = form.getFirstValue("uri"); 77 results = getGroupsFromStore(uri, user); 78 } 79 // assemble result object 80 JSONObject result = new JSONObject(); 81 try { 82 result.put("rows", results); 83 result.put("total", results.length()); 84 } catch (JSONException e) { 85 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); 86 return null; 87 } 88 logger.fine("sending:"); 89 logger.fine(result.toString()); 90 return new JsonRepresentation(result); 91 } 92 93 public JSONArray getGroupsFromPerson(Person person) { 94 JSONArray results = new JSONArray(); 95 for (String group : person.groups) { 96 JSONObject jo = new JSONObject(); 97 try { 98 jo.put("id", group); 99 jo.put("name", group); 100 } catch (JSONException e) { 101 } 102 results.put(jo); 103 } 104 return results; 105 } 106 107 public JSONArray getGroupsFromStore(String uri, String user) { 108 JSONArray results = new JSONArray(); 72 109 if (uri == null || uri.isEmpty()) { 73 110 // get uri from user-id 74 111 uri = Actor.getUriFromId(user, false); 75 112 } 76 JSONArray results = new JSONArray();77 113 AnnotationStore store = getAnnotationStore(); 78 114 Node person = store.getPersonNodeByUri(uri); … … 90 126 } 91 127 } 92 // assemble result object 93 JSONObject result = new JSONObject(); 94 try { 95 result.put("rows", results); 96 result.put("total", results.length()); 97 } catch (JSONException e) { 98 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); 99 return null; 100 } 101 logger.fine("sending:"); 102 logger.fine(result.toString()); 103 return new JsonRepresentation(result); 104 128 return results; 105 129 } 106 130 } -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
r86 r88 1 /**2 * Base class for Annotator resource classes.3 */4 1 package de.mpiwg.itgroup.annotations.restlet; 5 2 … … 27 24 28 25 import java.io.UnsupportedEncodingException; 29 import java.security.InvalidKeyException;30 import java.security.SignatureException;31 26 import java.text.SimpleDateFormat; 32 27 import java.util.ArrayList; … … 57 52 import org.restlet.util.Series; 58 53 54 import com.google.gson.JsonArray; 55 import com.google.gson.JsonElement; 56 import com.google.gson.JsonObject; 57 59 58 import de.mpiwg.itgroup.annotations.Actor; 60 59 import de.mpiwg.itgroup.annotations.Annotation; … … 155 154 */ 156 155 public boolean isAuthenticated(Representation entity) { 157 return ( checkAuthToken(entity) != null);156 return (getUserFromAuthToken(entity) != null); 158 157 } 159 158 … … 166 165 * @return user-id 167 166 */ 168 public String checkAuthToken(Representation entity) {167 public Person getUserFromAuthToken(Representation entity) { 169 168 @SuppressWarnings("unchecked") 170 169 Series<Header> requestHeaders = (Series<Header>) getRequest().getAttributes().get("org.restlet.http.headers"); … … 172 171 if (authToken == null) { 173 172 if (!((BaseRestlet) getApplication()).isAuthorizationMode()) { 174 return "anonymous";173 return Person.getAnonymous(); 175 174 } 176 175 return null; 177 176 } 178 // decode token first to get consumer key 179 JsonToken token = new JsonTokenParser(null, null).deserialize(authToken); 180 String userId = token.getParamAsPrimitive("userId").getAsString(); 181 String consumerKey = token.getParamAsPrimitive("consumerKey").getAsString(); 182 // get stored consumer secret for key 183 BaseRestlet restServer = (BaseRestlet) getApplication(); 184 String consumerSecret = restServer.getConsumerSecret(consumerKey); 185 logger.fine("requested consumer key=" + consumerKey + " secret=" + consumerSecret); 186 if (consumerSecret == null) { 187 return null; 188 } 189 // logger.fine(String.format("token=%s tokenString=%s signatureAlgorithm=%s",token,token.getTokenString(),token.getSignatureAlgorithm())); 190 try { 177 Person user = null; 178 try { 179 // decode token first to get consumer key 180 JsonToken token = new JsonTokenParser(null, null).deserialize(authToken); 181 String consumerKey = token.getParamAsPrimitive("consumerKey").getAsString(); 182 // get stored consumer secret for key 183 BaseRestlet restServer = (BaseRestlet) getApplication(); 184 String consumerSecret = restServer.getConsumerSecret(consumerKey); 185 logger.fine("requested consumer key=" + consumerKey + " secret=" + consumerSecret); 186 if (consumerSecret == null) { 187 logger.warning("Error: unknown consumer key: "+consumerKey); 188 return null; 189 } 190 // logger.fine(String.format("token=%s tokenString=%s signatureAlgorithm=%s",token,token.getTokenString(),token.getSignatureAlgorithm())); 191 191 List<Verifier> verifiers = new ArrayList<Verifier>(); 192 192 // we only do HS256 yet … … 194 194 // verify token signature(should really be static...) 195 195 new JsonTokenParser(new SystemClock(), null, (Checker[]) null).verify(token, verifiers); 196 } catch (SignatureException e) { 197 // TODO Auto-generated catch block 198 e.printStackTrace(); 199 } catch (InvalidKeyException e) { 200 // TODO Auto-generated catch block 201 e.printStackTrace(); 202 } catch (UnsupportedEncodingException e) { 203 // TODO Auto-generated catch block 204 e.printStackTrace(); 196 // create Person 197 JsonObject payload = token.getPayloadAsJsonObject(); 198 // userId is mandatory 199 String userId = payload.get("userId").getAsString(); 200 user = new Person(userId); 201 // displayName is optional 202 if (payload.has("displayName")) { 203 user.name = payload.get("displayName").getAsString(); 204 } 205 // memberOf groups is optional 206 if (payload.has("memberOf")) { 207 Set<String> groups = new HashSet<String>(); 208 JsonArray jgroups = payload.get("memberOf").getAsJsonArray(); 209 for (JsonElement jgroup : jgroups) { 210 groups.add(jgroup.getAsString()); 211 } 212 user.groups = groups; 213 } 214 } catch (Exception e) { 215 logger.warning("Error checking auth token: "+e.toString()); 216 return null; 205 217 } 206 218 // must be ok then 207 logger.fine("auth OK! user=" + user Id);208 return user Id;219 logger.fine("auth OK! user=" + user); 220 return user; 209 221 } 210 222 … … 212 224 * creates Annotator-JSON from an Annotation object. 213 225 * 214 * @param annot 226 * @param annot annotation object 215 227 * @param forAnonymous 216 * TODO 217 * @return 228 * @return Annotator-JSON 218 229 */ 219 230 public JSONObject createAnnotatorJson(Annotation annot, boolean forAnonymous) { … … 577 588 * check authentication 578 589 */ 579 String authUser = checkAuthToken(entity);590 Person authUser = getUserFromAuthToken(entity); 580 591 if (authUser == null) { 581 592 /* … … 620 631 } 621 632 if (username == null) { 622 username = authUser ;633 username = authUser.getName(); 623 634 } 624 635 // try to get full name -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java
r79 r88 35 35 public class AnnotatorRestlet extends BaseRestlet { 36 36 37 public final String version = "AnnotationManagerN4J/Annotator 0. 4.0";37 public final String version = "AnnotationManagerN4J/Annotator 0.5.0"; 38 38 39 39 /* -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java
r75 r88 68 68 setCorsHeaders(); 69 69 // do authentication 70 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));70 Person authUser = getUserFromAuthToken(entity); 71 71 logger.fine("request authenticated=" + authUser); 72 72
Note: See TracChangeset
for help on using the changeset viewer.