view src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java @ 15:6c7c4140630d

on the way to updating annotations.
author casties
date Wed, 21 Mar 2012 18:12:45 +0100
parents 9393c9c9b916
children 667d98fd28bd
line wrap: on
line source

/**
 * Implements the "annotations" uri of the Annotator API. see
 * <https://github.com/okfn/annotator/wiki/Storage>
 */
package de.mpiwg.itgroup.annotationManager.restlet;

import java.io.IOException;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.Context;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.security.User;

import de.mpiwg.itgroup.annotationManager.Constants.NS;
import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
import de.mpiwg.itgroup.annotationManager.drupal.AnnotationHandler;
import de.mpiwg.itgroup.annotationManager.drupal.UnknowUserException;
import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;

/**
 * Implements the "annotations" uri of the Annotator API. see <https://github.com/okfn/annotator/wiki/Storage>
 * 
 * @author dwinter, casties
 * 
 */
public class AnnotatorAnnotations extends AnnotatorResourceImpl {

    protected String getAllowedMethodsForHeader() {
        return "OPTIONS,GET,POST,PUT,DELETE";
    }

    /**
     * GET with JSON content-type.
     * 
     * @param entity
     * @return
     */
    @Get("json")
    public Representation doGetJSON(Representation entity) {
        logger.debug("AnnotatorAnnotations doGetJSON!");
        setCorsHeaders();
        // id from URI /annotations/{id}
        String id = (String) getRequest().getAttributes().get("id");
        logger.debug("annotation-id="+id);

        // TODO: what to do with authentication?
        boolean authenticated = isAuthenticated(entity);
        logger.debug("request authenticated=" + authenticated);
        
        // TODO: Annotator read request does not use parameters
        Form form = getRequest().getResourceRef().getQueryAsForm();
        String uri = form.getFirstValue("uri");
        String user = form.getFirstValue("user");

        String limit = form.getFirstValue("limit");
        String offset = form.getFirstValue("offset");

        RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file

        JSONArray results;
        try {
            List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset);

            results = new JSONArray();
            for (Convert.Annotation annot : annots) {
                JSONObject jo = createAnnotatorJson(annot);
                if (jo != null) {
                    results.put(createAnnotatorJson(annot));
                } else {
                    setStatus(Status.SERVER_ERROR_INTERNAL, "JSon Error");
                    return null;
                }
            }
        } catch (TripleStoreHandlerException e) {
            e.printStackTrace();
            setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
            return null;
        } catch (TripleStoreSearchError e) {
            e.printStackTrace();
            setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
            return null;
        }

        // annotator read request returns a list of annotation objects
        logger.debug("sending:");
        logger.debug(results);
        return new JsonRepresentation(results);
    }

    /**
     * POST with JSON content-type. 
     * 
     * json hash: username: name des users xpointer: xpointer auf den Ausschnitt (incl. der URL des
     * Dokumentes) text: text der annotation annoturl: url auf eine Annotation falls extern
     * 
     * @return
     */
    @Post("json")
    public Representation doPostJson(Representation entity) {
        logger.debug("AnnotatorAnnotations doPostJSON!");
        // set headers
        setCorsHeaders();
        Annotation annot = null;
        try {
            JsonRepresentation jrep = new JsonRepresentation(entity);
            JSONObject jo = jrep.getJsonObject();
            if (jo == null) {
                setStatus(Status.SERVER_ERROR_INTERNAL);
                return null;
            }
            // get Annotation object from posted JSON
            annot = createAnnotation(jo, entity);
        } catch (IOException e1) {
            setStatus(Status.SERVER_ERROR_INTERNAL);
            return null;
        } catch (JSONException e) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return null;
        }
        if (annot == null || annot.xpointer == null || annot.creator == null) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return null;
        }
        Annotation storedAnnot;
        try {
            // store Annotation
            storedAnnot = new Convert("file:///annotations").storeAnnotation(annot);
        } catch (TripleStoreStoreError e) {
            e.printStackTrace();
            setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
            return null;
        }
        // return stored annotation
        JSONObject jo = createAnnotatorJson(storedAnnot);
        JsonRepresentation retRep = new JsonRepresentation(jo);
        return retRep;
    }

    /**
     * POST with HTML content-type.
     * 
     * @param entity
     * @return
     */
    @Post("html")
    public Representation doPostHtml(Representation entity) {
        logger.debug("AnnotatorAnnotations doPostHtml!");
        Convert.Annotation annot;
        annot = handleForm(entity);
        if (annot.xpointer == null || annot.creator == null) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);

            return null;
        }

        Annotation retValAnnot;
        try {
            retValAnnot = new Convert("file:///annotations").storeAnnotation(annot);
        } catch (TripleStoreStoreError e) {
            e.printStackTrace();
            setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
            return null;
        }
        if (retValAnnot == null) {
            return null;
        }
        String retVal = retValAnnot.annotationUri;
        if (retVal == null) {
            return null;
        }

        String text = String.format("<html><body><a href=\"%s\">%s</a></body></html>", retVal.replace(">", "").replace("<", ""),
                retVal.replace(">", "&gt;").replace("<", "&lt;"));
        Representation retRep = new StringRepresentation(text, MediaType.TEXT_HTML);
        return retRep;
    }

    /**
     * 
     * @param entity
     *            should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
     * 
     *            username,password is optional, if not given BasicAuthentification is used.
     * 
     *            If username given as a URI, the username will be transformed to an URI, username will be added to the MPIWG
     *            namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
     * 
     * @return
     */
    protected Convert.Annotation handleForm(Representation entity) {
        Convert.Annotation annot;
        Form form = new Form(entity);
        String username = form.getValues("username");
        String mode = form.getValues("mode");
        String password = form.getValues("password");
        String xpointer = form.getValues("xpointer");
        String text = form.getValues("text");
        String title = form.getValues("title");
        String url = form.getValues("url");
        String type = form.getValues("type");
        RestServer restServer = (RestServer) getApplication();

        // falls user and password nicht null sind:
        User userFromForm = null;
        if (username != null && password != null) {
            if (restServer.authenticate(username, password, getRequest())) {
                userFromForm = new User(username);
            }
        }
        User authUser = null;

        if (userFromForm == null) {
            authUser = getHttpAuthUser(entity);
        }

        // weder BasicAuth noch FormAuth
        if (authUser == null && userFromForm == null) {
            setStatus(Status.CLIENT_ERROR_FORBIDDEN);
            return null;
        }

        if (userFromForm != null) {
            username = userFromForm.getIdentifier();
        } else {
            username = authUser.getIdentifier();
        }

        // username should be a URI, if not it will set to the MPIWG namespace defined in
        // de.mpiwg.itgroup.annotationManager.Constants.NS
        String usernameOrig = username;
        if (!username.startsWith("http"))
            username = NS.MPIWG_PERSONS + username;

        if (mode.equals("complexAnnotation")) {// Annotation mit text in externer ressource

            Context context = getContext();
            String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");

            AnnotationHandler ah = new AnnotationHandler(drupalPath);
            JSONObject newAnnot;
            try {
                newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
            } catch (UnknowUserException e1) {
                setStatus(Status.CLIENT_ERROR_FORBIDDEN);
                e1.printStackTrace();
                return null;
            }
            try {
                annot = new Convert.Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                setStatus(Status.SERVER_ERROR_INTERNAL);
                return null;
            }
        } else
            annot = new Convert.Annotation(xpointer, username, null, text, type, url);
        return annot;
    }

}