view src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java @ 88:b406507a953d

upped version to 0.5. can use display name and groups from auth token.
author casties
date Tue, 03 Feb 2015 19:01:27 +0100
parents 25eb2e1df106
children cf44d9e1a4a7
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.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.Parameter;
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 AnnotatorAnnotationsByResources extends AnnotatorResourceImpl {
    protected String getAllowedMethodsForHeader() {
        return "OPTIONS,GET";
    }

    @Get("json")
    public Representation doGetJSON(Representation entity) {
        logger.fine("AnnotatorAnnotatonsByResource doGetJSON!");
        setCorsHeaders();

        // do authentication
        Person authUser = getUserFromAuthToken(entity);
        logger.fine("request authenticated=" + authUser);

        String id = null;
        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
            }
            id = decodeJsonId(jsonId);
            // String id = jsonId;
            logger.fine("ressource-id=" + id);
        }

        Form form = getRequest().getResourceRef().getQueryAsForm();
        String sortBy = null;
        for (Parameter parameter : form) {
            if (parameter.getName().equals("sortBy")) {
                sortBy = parameter.getValue();
            }
        }

        AnnotationStore store = getAnnotationStore();
        // String tagUri=NS.MPIWG_TAGS_URL+id;
        List<Annotation> annotations = store.getAnnotationsByResource(id);

        // JSONArray results = new JSONArray();
        ArrayList<JSONObject> results = new ArrayList<JSONObject>();

        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);
    }

}