view artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java @ 68:541814404822

Added interfaces for XML processing services. TODO: Implement them! artifacts/trunk@593 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 22 Jan 2010 09:48:18 +0000
parents 8b72676698b5
children 48d1a9a082c2
line wrap: on
line source
package de.intevation.artifactdatabase.rest;

import org.restlet.resource.ResourceException;

import org.restlet.representation.Representation;
import org.restlet.representation.EmptyRepresentation;

import org.restlet.ext.xml.DomRepresentation;

import de.intevation.artifacts.ArtifactDatabase;
import de.intevation.artifacts.ArtifactDatabaseException;
import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.ArtifactNamespaceContext;

import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.data.MediaType;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;

import java.io.IOException;

import de.intevation.artifactdatabase.XMLUtils;

/**
 * @author Sascha L. Teichmann (sascha.teichmann@intevation)
 */
public class ArtifactResource
extends      BaseResource
{
    private static Logger logger = Logger.getLogger(ArtifactResource.class);

    public static final String XPATH_ACTION = "/art:action/art:type/@name";

    public static final String PATH = "/artifact/{uuid}";

    public static final String NO_ACTION_MESSAGE      = "no action given";
    public static final String NO_SUCH_ACTION_MESSAGE = "no such action";

    public static final String NO_ARTIFACT_FOUND = "Artifact not found";

    public static final String ADVANCE  = "advance";
    public static final String FEED     = "feed";
    public static final String DESCRIBE = "describe";

    protected Representation innerGet()
    throws                   ResourceException
    {
        Request request = getRequest();

        String identifier = (String)request.getAttributes().get("uuid");

        if (logger.isDebugEnabled()) {
            logger.debug("looking for artifact id '" + identifier + "'");
        }

        ArtifactDatabase db = (ArtifactDatabase)getContext()
            .getAttributes().get("database");

        try {
            return new DomRepresentation(
                MediaType.APPLICATION_XML,
                db.describe(identifier, null, getCallMeta()));
        }
        catch (ArtifactDatabaseException adbe) {
            logger.warn(adbe.getLocalizedMessage(), adbe);
            Response response = getResponse();
            response.setStatus(
                Status.CLIENT_ERROR_NOT_FOUND, adbe.getMessage());
            return new EmptyRepresentation();
        }
    }

    protected Representation dispatch(
        String           identifier,
        String           action,
        Document         source,
        ArtifactDatabase db
    ) {
        Document out = null;

        try {
            if (action.equals(FEED)) {
                out = db.feed(identifier, source, getCallMeta());
            }
            else if (action.equals(ADVANCE)) {
                out = db.advance(identifier, source, getCallMeta());
            }
            else if (action.equals(DESCRIBE)) {
                out = db.describe(identifier, source, getCallMeta());
            }
            else {
                throw new ArtifactDatabaseException(NO_SUCH_ACTION_MESSAGE);
            }
        }
        catch (ArtifactDatabaseException adbe) {
            logger.warn(adbe.getLocalizedMessage(), adbe);
            Response response = getResponse();
            response.setStatus(
                Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage());
            return new EmptyRepresentation();
        }

        return new DomRepresentation(MediaType.APPLICATION_XML, out);
    }

    protected Representation innerPost(Representation requestRepr) {

        Document inputDocument = null;
        try {
            DomRepresentation input = new DomRepresentation(requestRepr);
            input.setNamespaceAware(true);
            inputDocument = input.getDocument();
        }
        catch (IOException ioe) {
            logger.error(ioe.getMessage());
            Response response = getResponse();
            response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
            return new EmptyRepresentation();
        }

        String action = XMLUtils.xpathString(
            inputDocument,
            XPATH_ACTION,
            ArtifactNamespaceContext.INSTANCE);

        if (action == null || action.length() == 0) {
            Response response = getResponse();
            response.setStatus(
                Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MESSAGE);
            return new EmptyRepresentation();
        }

        Request request = getRequest();

        String identifier = (String)request.getAttributes().get("uuid");

        ArtifactDatabase db = (ArtifactDatabase)getContext()
            .getAttributes().get("database");

        return dispatch(identifier, action, inputDocument, db);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org