changeset 28:019b9f02d523

Added REST handler for 'advance' and 'feed' to be called by HTTP POST '/artifact/{uuid}'. artifacts/trunk@65 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 10 Sep 2009 14:44:25 +0000
parents 75bdaf900473
children 22b03d5c84c5
files Changelog artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java
diffstat 2 files changed, 89 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Changelog	Thu Sep 10 14:21:58 2009 +0000
+++ b/Changelog	Thu Sep 10 14:44:25 2009 +0000
@@ -1,7 +1,12 @@
 2009-09-10	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java:
-	Handler for describe to be fetced by HTTP GET '/artifact/{uuid}'.
+	Added handler for 'advance' and 'feed' to be called by HTTP POST '/artifact/{uuid}'.
+
+2009-09-10	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java:
+	Added handler for 'describe' to be fetched by HTTP GET '/artifact/{uuid}'.
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java:
 	Add route to new describe handler.
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java	Thu Sep 10 14:21:58 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java	Thu Sep 10 14:44:25 2009 +0000
@@ -22,6 +22,12 @@
 
 import org.w3c.dom.Document;
 
+import java.io.IOException;
+
+import de.intevation.artifacts.ArtifactNamespaceContext;
+
+import de.intevation.artifactdatabase.XMLUtils;
+
 /**
  * @author Sascha L. Teichmann (sascha.teichmann@intevation)
  */
@@ -30,10 +36,18 @@
 {
     private static Logger logger = Logger.getLogger(ArtifactResource.class);
 
+    public static final String XPATH_ACTION = "/action/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";
+
     @Get
     public Representation represent() {
 
@@ -66,11 +80,77 @@
             MediaType.APPLICATION_XML, description);
     }
 
-    /*
     @Post
     public Representation representPost() {
-        return new EmptyRepresentation();
+        Request request = getRequest();
+
+        Representation requestRepr = request.getEntity();
+
+        Document inputDocument = null;
+        try {
+            DomRepresentation input = new DomRepresentation(requestRepr);
+            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();
+        }
+
+        int actionType = -1;
+
+             if (FEED   .equals(action)) actionType = 0;
+        else if (ADVANCE.equals(action)) actionType = 1;
+        else {
+            Response response = getResponse();
+            response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, NO_SUCH_ACTION_MESSAGE);
+            return new EmptyRepresentation();
+        }
+
+        String identifier = (String)request.getAttributes().get("uuid");
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("looking for artifact id '" + identifier + "'");
+        }
+
+        ArtifactDatabase db = (ArtifactDatabase)getContext()
+            .getAttributes().get("database");
+
+        Artifact artifact = db.getArtifact(identifier);
+
+        if (artifact == null) {
+            Response response = getResponse();
+            response.setStatus(Status.CLIENT_ERROR_NOT_FOUND, NO_ARTIFACT_FOUND);
+            return new EmptyRepresentation();
+        }
+
+        Document document = null;
+
+        switch (actionType) {
+            case 0:
+                document = artifact.feed(inputDocument, db.getArtifactContext());
+                break;
+            case 1:
+                document = artifact.advance(inputDocument, db.getArtifactContext());
+                break;
+            default:
+                // should not happen
+                return new EmptyRepresentation();
+        }
+
+        return new DomRepresentation(MediaType.APPLICATION_XML, document);
     }
-    */
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org