changeset 20:ff666592c1c3

Added factory listing '/factories' to web app. artifacts/trunk@51 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 09 Sep 2009 14:33:25 +0000
parents 1259d192e3c3
children 1cd268e07e44
files Changelog TODO artifact-database/pom.xml artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/FactoriesResource.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java contrib/run.sh
diffstat 8 files changed, 181 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/Changelog	Wed Sep 09 07:55:44 2009 +0000
+++ b/Changelog	Wed Sep 09 14:33:25 2009 +0000
@@ -1,3 +1,25 @@
+2009-09-09	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/pom.xml: Added dependency to restlet.ext.xml
+	for XML handling in representations.
+
+	* contrib/run.sh: Adjusted to fit new dependency.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java:
+	New. Handle some boiler plate XML stuff. Move more stuff of this kind
+	into this file.
+
+	* TODO: Added remark to move more XML stuff into XMLUtils.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java:
+	Tell at startup on which port we are listing.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/FactoriesResource.java:
+	Representation to list the available factories.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java:
+	Attach new factory listing to web app.
+
 2009-09-09	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/doc/artifactdb-example-conf.xml: removed
--- a/TODO	Wed Sep 09 07:55:44 2009 +0000
+++ b/TODO	Wed Sep 09 14:33:25 2009 +0000
@@ -2,3 +2,4 @@
 	* Document the XML of the configuration file.
 	* Implement a "killer" thread in artifact database which
 	  periodically kills all outdated artifacts.
+    * move XML stuff from Config to XMLUtils.
--- a/artifact-database/pom.xml	Wed Sep 09 07:55:44 2009 +0000
+++ b/artifact-database/pom.xml	Wed Sep 09 14:33:25 2009 +0000
@@ -52,6 +52,11 @@
       <version>2.0-M3</version>
     </dependency>
     <dependency>
+      <groupId>org.restlet</groupId>
+      <artifactId>org.restlet.ext.xml</artifactId>
+      <version>2.0-M3</version>
+    </dependency>
+    <dependency>
       <groupId>com.h2database</groupId>
       <artifactId>h2</artifactId>
       <version>1.1.117</version>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java	Wed Sep 09 14:33:25 2009 +0000
@@ -0,0 +1,62 @@
+package de.intevation.artifactdatabase;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Attr;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.log4j.Logger;
+
+/**
+ *  @author Sascha L. Teichmann
+ */
+public final class XMLUtils
+{
+    private static Logger logger = Logger.getLogger(XMLUtils.class);
+
+    private XMLUtils() {
+    }
+
+    public static class ElementCreator
+    {
+        protected Document document;
+        protected String   ns;
+        protected String   prefix;
+
+        public ElementCreator(Document document, String ns, String prefix) {
+            this.document = document;
+            this.ns       = ns;
+            this.prefix   = prefix;
+        }
+
+        public Element create(String name) {
+            Element element = document.createElementNS(ns, name);
+            element.setPrefix(prefix);
+            return element;
+        }
+
+        public void addAttr(Element element, String name, String value) {
+            Attr attr = document.createAttributeNS(ns, name);
+            attr.setValue(value);
+            attr.setPrefix(prefix);
+            element.setAttributeNode(attr);
+        }
+    } // class ElementCreator
+
+    public static final Document newDocument() {
+        try {
+            return DocumentBuilderFactory
+                .newInstance()
+                .newDocumentBuilder()
+                .newDocument();
+        }
+        catch (ParserConfigurationException pce) {
+            logger.error(pce.getLocalizedMessage(), pce);
+        }
+        return null;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/FactoriesResource.java	Wed Sep 09 14:33:25 2009 +0000
@@ -0,0 +1,67 @@
+package de.intevation.artifactdatabase.rest;
+
+
+import de.intevation.artifacts.ArtifactDatabase;
+
+import org.restlet.data.MediaType;
+
+import org.restlet.resource.Get;  
+import org.restlet.resource.ServerResource;
+
+import org.restlet.ext.xml.DomRepresentation;
+
+import java.io.IOException;
+
+import de.intevation.artifactdatabase.XMLUtils;
+import de.intevation.artifactdatabase.XMLUtils.ElementCreator;
+
+import de.intevation.artifactdatabase.DefaultArtifact;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import org.apache.log4j.Logger;
+
+public class FactoriesResource
+extends      ServerResource
+{
+    private static final Logger logger = Logger.getLogger(FactoriesResource.class);
+    
+    public static final String PATH = "/factories";
+
+    @Get
+    public DomRepresentation represent() {
+
+        Document document = XMLUtils.newDocument();
+
+        ElementCreator ec = new ElementCreator(
+            document,
+            DefaultArtifact.NAMESPACE_URI,
+            DefaultArtifact.NAMESPACE_PREFIX);
+
+        ArtifactDatabase db = (ArtifactDatabase)getContext()
+            .getAttributes().get("database");
+
+        Element root = ec.create("result");
+        document.appendChild(root);
+
+        Element type = ec.create("type");
+        ec.addAttr(type, "name", "factory-list");
+        root.appendChild(type);
+
+        Element factories = ec.create("factories");
+        root.appendChild(factories);
+
+        String [] factoryNames = db.getArtifactFactoryNames();
+
+        for (int i = 0; i < factoryNames.length; ++i) {
+            Element factoryElement = ec.create("factory");
+            ec.addAttr(factoryElement, "name", factoryNames[i]);
+            factories.appendChild(factoryElement);
+        }
+
+        return new DomRepresentation(
+            MediaType.APPLICATION_XML, document);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java	Wed Sep 09 07:55:44 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java	Wed Sep 09 14:33:25 2009 +0000
@@ -2,8 +2,11 @@
 
 import de.intevation.artifacts.ArtifactDatabase;
 
+import java.util.concurrent.ConcurrentMap;
+
 import org.restlet.Application;
 import org.restlet.Restlet;
+import org.restlet.Context;
 
 import org.restlet.routing.Router;
 
@@ -25,9 +28,14 @@
 
     public Restlet createRoot() {
 
-        Router router = new Router(getContext());
+        Context context = getContext();
 
-        //router.attach(FactoriesResource.PATH, FactoriesResource.class);
+        ConcurrentMap map = context.getAttributes();
+        map.put("database", database);
+
+        Router router = new Router(context);
+
+        router.attach(FactoriesResource.PATH, FactoriesResource.class);
 
         return router;
     }
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java	Wed Sep 09 07:55:44 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java	Wed Sep 09 14:33:25 2009 +0000
@@ -47,6 +47,8 @@
 
         component.getDefaultHost().attach(app);
 
+        logger.info("Starting rest HTTP server on port " + port);
+
         try {
             component.start();
         }
--- a/contrib/run.sh	Wed Sep 09 07:55:44 2009 +0000
+++ b/contrib/run.sh	Wed Sep 09 14:33:25 2009 +0000
@@ -1,10 +1,15 @@
 #!/bin/bash
-RESTLET=`find ~/.m2 -name org\.restlet\*.jar`
-H2=`find ~/.m2 -name h2-\*.jar`
-LOG4J=`find ~/.m2 -name log4j-1.2.13\*.jar`
-#echo $RESTLET
-#echo $LOG4J
-#echo $H2
+RESTLET_CORE=`find -L ~/.m2 -name org\.restlet-\*M\*.jar`
+RESTLET_XML=`find -L ~/.m2 -name org\.restlet.ext.xml-\*M\*.jar`
+H2=`find -L ~/.m2 -name h2-\*.jar`
+LOG4J=`find -L ~/.m2 -name log4j-1.2.13\*.jar`
+echo $RESTLET
+echo $LOG4J
+echo $H2
 DIR=`dirname $0`/..
-export CLASSPATH=$DIR/artifact-database/target/classes:$DIR/artifacts/target/classes:$RESTLET:$LOG4J:$H2
+CLASSPATH=$DIR/artifact-database/target/classes
+CLASSPATH=$CLASSPATH:$DIR/artifacts/target/classes
+CLASSPATH=$CLASSPATH:$RESTLET_CORE:$RESTLET_XML
+CLASSPATH=$CLASSPATH:$LOG4J:$H2
+export CLASSPATH
 java "$@"

http://dive4elements.wald.intevation.org