package de.mpiwg.itgroup.annotations.restlet; /* * #%L * AnnotationManager * %% * Copyright (C) 2012 - 2014 MPIWG Berlin * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. * * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% */ import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.restlet.data.Form; import org.restlet.data.Status; import org.restlet.ext.json.JsonRepresentation; import org.restlet.representation.Representation; import org.restlet.resource.Get; import de.mpiwg.itgroup.annotations.Annotation; import de.mpiwg.itgroup.annotations.Person; import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator; /** * API for accessing tags in the Annotation store. * * @author dwinter * */ public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl { @Get("json") public Representation doGetJSON(Representation entity) { logger.fine("AnnotatorAnnotatonsBytag doGetJSON!"); // do authentication Person authUser = getUserFromAuthToken(entity); logger.fine("request authenticated=" + authUser); String id = (String) getRequest().getAttributes().get("id"); // URL decode try { id = URLDecoder.decode(id, "UTF-8"); } catch (UnsupportedEncodingException e) { // this shouldn't happen } logger.fine("annotation-id=" + id); Form form = getRequest().getResourceRef().getQueryAsForm(); String sortBy = form.getFirstValue("sortBy"); AnnotationStore store = getAnnotationStore(); String tagUri = BaseRestlet.TAGS_URI_PREFIX + id; List annotations = store.getAnnotationsByTag(tagUri); // JSONArray results = new JSONArray(); ArrayList results = new ArrayList(); for (Annotation annot : annotations) { // check permission if (!annot.isActionAllowed("read", authUser, store)) continue; JSONObject jo = createAnnotatorJson(annot, false); results.add(jo); } if (sortBy != null) { JSONObjectComparator.sortAnnotations(results, sortBy); } JSONArray resultsJa = new JSONArray(); for (JSONObject result : results) { resultsJa.put(result); } // assemble result object JSONObject result = new JSONObject(); try { result.put("rows", resultsJa); result.put("total", resultsJa.length()); } catch (JSONException e) { setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); return null; } return new JsonRepresentation(result); } }