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.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; /** * Implements the "search" uri of the Annotator API. see * * * @author casties * */ public class AnnotatorSearch extends AnnotatorResourceImpl { /** * result for JSON content-type. optional search parameters: uri, user, limit, * offset, sortBy. * * @param entity * @return */ @Get("json") public Representation doGetJSON(Representation entity) { logger.fine("AnnotatorSearch doGetJSON!"); // do authentication Person authUser = getUserFromAuthToken(entity); logger.fine("request authenticated=" + authUser); Form form = getRequest().getResourceRef().getQueryAsForm(); String uri = form.getFirstValue("uri"); String user = form.getFirstValue("user"); int limit = getInt(form.getFirstValue("limit")); int offset = getInt(form.getFirstValue("offset")); String sortBy = form.getFirstValue("sortBy"); // do search ArrayList results = new ArrayList(); logger.fine(String.format("searching for uri=%s user=%s", uri, user)); AnnotationStore store = getAnnotationStore(); List annots = store.searchAnnotationByUriUser(uri, user); for (Annotation annot : annots) { // check permission if (!annot.isActionAllowed("read", authUser, store)) continue; JSONObject jo = createAnnotatorJson(annot, (authUser == null)); if (jo != null) { results.add(jo); } else { setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); return null; } } // sort if necessary if (sortBy != null) { JSONObjectComparator.sortAnnotations(results, sortBy); } // put in JSON list JSONArray rows = new JSONArray(); int cnt = 0; for (JSONObject result : results) { cnt += 1; if (offset > 0 && cnt < offset) continue; rows.put(result); if (limit > 0 && cnt >= limit) break; } // assemble result object JSONObject result = new JSONObject(); try { result.put("rows", rows); result.put("total", rows.length()); } catch (JSONException e) { setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); return null; } logger.fine("sending response"); logger.finest(result.toString()); return new JsonRepresentation(result); } }