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