Mercurial > hg > AnnotationManagerN4J
view src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java @ 70:2b1e6df5e21a
added lgpl_v3 license information.
author | casties |
---|---|
date | Thu, 06 Mar 2014 15:09:04 +0100 |
parents | c0dd5314bada |
children | 25eb2e1df106 |
line wrap: on
line source
/** * ReST API for accessing groups in the Annotation store. */ 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 * <http://www.gnu.org/licenses/lgpl-3.0.html>. * #L% */ import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.neo4j.graphdb.Node; 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.Resource; import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; /** * API for accessing resource objects in the Annotation store. * * @author dwinter * */ public class AnnotatorResources extends AnnotatorResourceImpl { protected String getAllowedMethodsForHeader() { return "OPTIONS,GET"; } /** * GET with JSON content-type. * Parameters: user: short user name uri: user uri * * @param entity * @return */ @Get("json") public Representation doGetJSON(Representation entity) { logger.debug("AnnotatorResources doGetJSON!"); setCorsHeaders(); String jsonId = (String) getRequest().getAttributes().get("id"); if (jsonId != null) { // URL decode try { jsonId = URLDecoder.decode(jsonId, "UTF-8"); } catch (UnsupportedEncodingException e) { // this shouldn't happen } } String uri = decodeJsonId(jsonId); logger.debug("resources-id=" + uri); if (uri == null) { return getAllResources(); } else { return getResource(uri); } } protected Representation getResource(String uri) { AnnotationStore store = getAnnotationStore(); Node resNode = store.getResourceNodeByUri(uri); Resource resource = store.createResourceFromNode(resNode); JSONObject jo = new JSONObject(); try { jo.put("id", encodeJsonId(resource.getUri())); jo.put("uri", resource.getUri()); } catch (JSONException e) { } return new JsonRepresentation(jo); } protected Representation getAllResources() { JSONArray results = new JSONArray(); AnnotationStore store = getAnnotationStore(); List<Resource> resources = store.getResources(null, null); for (Resource resource : resources) { JSONObject jo = new JSONObject(); try { jo.put("id", encodeJsonId(resource.getUri())); jo.put("uri", resource.getUri()); } catch (JSONException e) { } results.put(jo); } // assemble result object JSONObject result = new JSONObject(); try { result.put("rows", results); result.put("total", results.length()); } catch (JSONException e) { setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); return null; } logger.debug("sending:"); logger.debug(result); return new JsonRepresentation(result); } }