view src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.java @ 91:cf44d9e1a4a7

let CORS be handled by Restlet 2.3 CorsFilter.
author casties
date Sun, 08 Feb 2015 18:09:00 +0100
parents 25eb2e1df106
children
line wrap: on
line source

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.Tag;
import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;

/**
 * API for accessing tags in the Annotation store.
 * 
 * @author dwinter
 * 
 */
public class AnnotatorTags extends AnnotatorResourceImpl {

    /**
     * GET with JSON content-type. 
     * Parameters: 
     *   user: short user name 
     *   uri: user uri
     * 
     * @param entity
     * @return
     */
    @Get("json")
    public Representation doGetJSON(Representation entity) {
        logger.fine("AnnotatorGroups doGetJSON!");

        String jsonId = (String) getRequest().getAttributes().get("id");
        // String id = decodeJsonId(jsonId);
        String id = jsonId;
        logger.fine("annotation-id=" + id);

        if (id == null) {
            return getAllTags();
        } else {
            // URL decode
            try {
                id = URLDecoder.decode(id, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // this shouldn't happen
            }
            return getTag(id);
        }
    }

    protected Representation getTag(String id) {
        AnnotationStore store = getAnnotationStore();
        String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
        Node tagNode = store.getTagNodeByUri(tagUri);
        Tag tag = store.createTagFromNode(tagNode);
        JSONObject jo = new JSONObject();
        try {
            jo.put("id", tag.getId());
            jo.put("name", tag.getName());
            jo.put("uri", tag.getUri());
        } catch (JSONException e) {
        }

        return new JsonRepresentation(jo);
    }

    protected Representation getAllTags() {
        JSONArray results = new JSONArray();
        AnnotationStore store = getAnnotationStore();

        List<Tag> tags = store.getTags(null, null);
        for (Tag tag : tags) {
            JSONObject jo = new JSONObject();
            try {
                jo.put("id", tag.getId());
                jo.put("name", tag.getName());
                jo.put("uri", tag.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.fine("sending:");
        logger.fine(result.toString());
        return new JsonRepresentation(result);
    }

}