Changeset 65:c0dd5314bada in AnnotationManagerN4J for src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java
r58 r65 1 1 package de.mpiwg.itgroup.annotations.restlet; 2 2 3 import java.io.UnsupportedEncodingException; 4 import java.net.URLDecoder; 3 5 import java.util.ArrayList; 4 6 import java.util.List; … … 8 10 import org.json.JSONObject; 9 11 import org.restlet.data.Form; 10 import org.restlet.data.Parameter;11 12 import org.restlet.data.Status; 12 13 import org.restlet.ext.json.JsonRepresentation; … … 23 24 * 24 25 * @author dwinter 25 * 26 * 26 27 */ 27 28 public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl { … … 29 30 return "OPTIONS,GET"; 30 31 } 31 32 32 33 @Get("json") 33 34 public Representation doGetJSON(Representation entity) { 34 35 logger.debug("AnnotatorAnnotatonsBytag doGetJSON!"); 35 36 setCorsHeaders(); 36 37 37 38 // do authentication 38 39 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 39 40 logger.debug("request authenticated=" + authUser); 40 41 41 42 String jsonId = (String) getRequest().getAttributes().get("id"); 43 42 String id = (String) getRequest().getAttributes().get("id"); 43 // URL decode 44 try { 45 id = URLDecoder.decode(id, "UTF-8"); 46 } catch (UnsupportedEncodingException e) { 47 // this shouldn't happen 48 } 49 logger.debug("annotation-id=" + id); 50 44 51 Form form = getRequest().getResourceRef().getQueryAsForm(); 45 String sortBy=null; 46 for (Parameter parameter : form) { 47 if (parameter.getName().equals("sortBy")){ 48 sortBy = parameter.getValue(); 49 } 50 } 51 52 //String id = decodeJsonId(jsonId); 53 String id = jsonId; 54 logger.debug("annotation-id=" + id); 55 52 String sortBy = form.getFirstValue("sortBy"); 53 56 54 AnnotationStore store = getAnnotationStore(); 57 55 String tagUri = BaseRestlet.TAGS_URI_PREFIX + id; 58 56 List<Annotation> annotations = store.getAnnotationsByTag(tagUri); 59 60 //JSONArray results = new JSONArray();57 58 // JSONArray results = new JSONArray(); 61 59 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); 62 63 for (Annotation annot : annotations) { 64 //check permission 65 if (!annot.isActionAllowed("read", authUser, store)) continue; 66 67 JSONObject jo = createAnnotatorJson(annot,false); 60 61 for (Annotation annot : annotations) { 62 // check permission 63 if (!annot.isActionAllowed("read", authUser, store)) 64 continue; 65 66 JSONObject jo = createAnnotatorJson(annot, false); 68 67 results.add(jo); 69 68 } 70 71 if (sortBy!=null){ 72 JSONObjectComparator.sortAnnotations(results,sortBy); 73 } 74 75 JSONArray resultsJa = new JSONArray(); 76 for (JSONObject result:results){ 77 resultsJa.put(result); 78 } 79 80 // assemble result object 81 JSONObject result = new JSONObject(); 82 try { 83 result.put("rows", resultsJa); 84 result.put("total", resultsJa.length()); 85 } catch (JSONException e) { 86 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); 87 return null; 69 70 if (sortBy != null) { 71 JSONObjectComparator.sortAnnotations(results, sortBy); 72 } 73 74 JSONArray resultsJa = new JSONArray(); 75 for (JSONObject result : results) { 76 resultsJa.put(result); 77 } 78 79 // assemble result object 80 JSONObject result = new JSONObject(); 81 try { 82 result.put("rows", resultsJa); 83 result.put("total", resultsJa.length()); 84 } catch (JSONException e) { 85 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); 86 return null; 87 } 88 return new JsonRepresentation(result); 88 89 } 89 logger.debug("sending:"); 90 logger.debug(result); 91 return new JsonRepresentation(result); 90 92 91 } 93 94 95 96 }
Note: See TracChangeset
for help on using the changeset viewer.