changeset 72:b01e47317a78

Added '/services' url to web server to list the available services in XML. artifacts/trunk@600 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 22 Jan 2010 11:40:26 +0000
parents c99da6837be0
children d1b8c91b4506
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ServicesResource.java
diffstat 3 files changed, 82 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Jan 22 11:27:57 2010 +0000
+++ b/ChangeLog	Fri Jan 22 11:40:26 2010 +0000
@@ -1,3 +1,12 @@
+2010-01-22	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ServicesResource.java:
+	  New. Serves a list of available services under '/services' as XML. Structure
+	  is much like '/factories' with 's/artifact/service/'.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java:
+	  Register new resource to web server.
+
 2010-01-22	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java	Fri Jan 22 11:27:57 2010 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java	Fri Jan 22 11:40:26 2010 +0000
@@ -35,6 +35,7 @@
 
         Router router = new Router(context);
 
+        router.attach(ServicesResource.PATH,    ServicesResource.class);
         router.attach(FactoriesResource.PATH,   FactoriesResource.class);
         router.attach(CreateResource.PATH,      CreateResource.class);
         router.attach(ArtifactResource.PATH,    ArtifactResource.class);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ServicesResource.java	Fri Jan 22 11:40:26 2010 +0000
@@ -0,0 +1,72 @@
+package de.intevation.artifactdatabase.rest;
+
+import de.intevation.artifactdatabase.XMLUtils.ElementCreator;
+
+import de.intevation.artifactdatabase.XMLUtils;
+
+import de.intevation.artifacts.ArtifactDatabase;
+import de.intevation.artifacts.ArtifactNamespaceContext;
+
+import org.apache.log4j.Logger;
+
+import org.restlet.data.MediaType;
+
+import org.restlet.ext.xml.DomRepresentation;
+
+import org.restlet.representation.Representation;
+
+import org.restlet.resource.ResourceException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation)
+ */
+public class ServicesResource
+extends      BaseResource
+{
+    private static Logger logger = Logger.getLogger(ServicesResource.class);
+
+    public static final String PATH = "/services";
+
+    protected Representation innerGet()
+    throws                   ResourceException
+    {
+        Document document = XMLUtils.newDocument();
+
+        ElementCreator ec = new ElementCreator(
+            document,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.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", "service-list");
+        root.appendChild(type);
+
+        Element factories = ec.create("services");
+        root.appendChild(factories);
+
+        String [][] factoryNames = db.serviceNamesAndDescriptions();
+
+        for (int i = 0; i < factoryNames.length; ++i) {
+            String [] nd = factoryNames[i];
+            Element factoryElement = ec.create("service");
+            ec.addAttr(factoryElement, "name", nd[0]);
+            ec.addAttr(factoryElement, "description", nd[1]);
+            factories.appendChild(factoryElement);
+        }
+
+        document.normalizeDocument();
+
+        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