[Registry-dev] svn commit r19121 - in trunk/registry/modules/samples/collection-handler-sample: . resources src src/org src/org/wso2 src/org/wso2/registry src/org/wso2/registry/samples src/org/wso2/registry/samples/handlers src/org/wso2/registry/samples/handlers/servers

chathura at wso2.com chathura at wso2.com
Thu Jul 10 23:17:45 PDT 2008


Author: chathura
Date: Thu Jul 10 23:17:45 2008
New Revision: 19121
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=19121

Log:

Added a sample to demonstrate custom UIs for collections.
This sample collection manages a collection of server information.



Added:
   trunk/registry/modules/samples/collection-handler-sample/
   trunk/registry/modules/samples/collection-handler-sample/build.xml
   trunk/registry/modules/samples/collection-handler-sample/resources/
   trunk/registry/modules/samples/collection-handler-sample/resources/server1.si
   trunk/registry/modules/samples/collection-handler-sample/resources/server2.si
   trunk/registry/modules/samples/collection-handler-sample/resources/server3.si
   trunk/registry/modules/samples/collection-handler-sample/src/
   trunk/registry/modules/samples/collection-handler-sample/src/org/
   trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/
   trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/
   trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/
   trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/
   trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/
   trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerCollectionHandler.java
   trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerDetailsEditProcessor.java
   trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerDetailsHandler.java

Added: trunk/registry/modules/samples/collection-handler-sample/build.xml
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/samples/collection-handler-sample/build.xml?pathrev=19121
==============================================================================
--- (empty file)
+++ trunk/registry/modules/samples/collection-handler-sample/build.xml	Thu Jul 10 23:17:45 2008
@@ -0,0 +1,43 @@
+<project name="custom-ui-sample" basedir="." default="run">
+
+	<property name="dest.dir" value="build" />
+	<property name="dest.dir.classes" value="${dest.dir}/classes" />
+	<property name="dest.dir.lib" value="${dest.dir}/lib" />
+	<property name="registry.home" value="../.." />
+
+	<path id="build.class.path">
+		<fileset dir="${registry.home}/lib">
+			<include name="*.jar" />
+		</fileset>
+	</path>
+
+	<path id="client.class.path">
+		<fileset dir="${registry.home}/lib">
+			<include name="*.jar" />
+		</fileset>
+		<fileset dir="${dest.dir.lib}">
+			<include name="*.jar" />
+		</fileset>
+	</path>
+
+	<target name="clean">
+		<delete dir="${dest.dir}" />
+	</target>
+
+	<target name="prepare">
+		<mkdir dir="${dest.dir}" />
+		<mkdir dir="${dest.dir.classes}" />
+	</target>
+
+	<target name="compile" depends="clean,prepare">
+		<javac srcdir="src" destdir="${dest.dir.classes}">
+			<classpath refid="build.class.path" />
+		</javac>
+	</target>
+
+      <target name="run" depends="compile,jar"/>
+	<target name="jar">
+		<jar basedir="${dest.dir.classes}" destfile="${dest.dir}/server-management.jar" />
+	</target>
+</project>
+

Added: trunk/registry/modules/samples/collection-handler-sample/resources/server1.si
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/samples/collection-handler-sample/resources/server1.si?pathrev=19121
==============================================================================
--- (empty file)
+++ trunk/registry/modules/samples/collection-handler-sample/resources/server1.si	Thu Jul 10 23:17:45 2008
@@ -0,0 +1,5 @@
+name: HR mail server
+description: Mail server of the HR department.
+ip: 10.1.100.22
+ports: 8090, 9000
+admin: deep

Added: trunk/registry/modules/samples/collection-handler-sample/resources/server2.si
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/samples/collection-handler-sample/resources/server2.si?pathrev=19121
==============================================================================
--- (empty file)
+++ trunk/registry/modules/samples/collection-handler-sample/resources/server2.si	Thu Jul 10 23:17:45 2008
@@ -0,0 +1,5 @@
+name: Main web server
+description: Main web server of XYZ company. Company main site is hosted in this server.
+ip: 10.1.100.23
+ports: 80, 8080
+admin: deep

Added: trunk/registry/modules/samples/collection-handler-sample/resources/server3.si
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/samples/collection-handler-sample/resources/server3.si?pathrev=19121
==============================================================================
--- (empty file)
+++ trunk/registry/modules/samples/collection-handler-sample/resources/server3.si	Thu Jul 10 23:17:45 2008
@@ -0,0 +1,5 @@
+name: R&D database server
+description: Database of R&D department. This contains highly confidential documents.
+ip: 10.1.100.24
+ports: 8191
+admin: chamith

Added: trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerCollectionHandler.java
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerCollectionHandler.java?pathrev=19121
==============================================================================
--- (empty file)
+++ trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerCollectionHandler.java	Thu Jul 10 23:17:45 2008
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry.samples.handlers.servers;
+
+import org.wso2.registry.jdbc.handlers.UIEnabledHandler;
+import org.wso2.registry.jdbc.handlers.RequestContext;
+import org.wso2.registry.Resource;
+import org.wso2.registry.Collection;
+import org.wso2.registry.exceptions.RegistryException;
+
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * This class is a part of server information management sample. This handler is engaged for server
+ * collections (application/vnd.server-collection), which are collections of server information
+ * resources (application/vnd.server.info).
+ *
+ * Server collections can only contain server info resources. This class also generates a table
+ * view to display all child server info resource contents.
+ */
+public class ServerCollectionHandler extends UIEnabledHandler {
+
+    public Resource getRawResource(RequestContext requestContext) throws RegistryException {
+        return null;
+    }
+
+    public Resource getView(RequestContext requestContext) throws RegistryException {
+
+        String path = requestContext.getResourcePath();
+        Resource resource = requestContext.getRepository().get(path);
+        requestContext.setResource(resource);
+
+        Collection c = (Collection) resource;
+
+        String[] serverDetails = (String[]) c.getContent();
+
+        StringBuffer htmlTable = new StringBuffer("<table border=1>" +
+                "<tr><th>Server name</th><th>Description</th><th>IP Address</th><th>Ports</th><th>Adminstrator</th></tr>");
+
+        for (int i = 0; i < serverDetails.length; i++) {
+            String serverDetailPath = serverDetails[i];
+            htmlTable.append(getContentAsTableRow(serverDetailPath, requestContext));
+        }
+        htmlTable.append("</table");
+
+        resource.setContent(htmlTable.toString());
+        return resource;
+    }
+
+    private String getContentAsTableRow(String path, RequestContext requestContext)
+            throws RegistryException {
+
+        Resource serverDetail = requestContext.getRepository().get(path);
+        byte[] contentBytes = (byte[]) serverDetail.getContent();
+        String content = new String(contentBytes);
+
+        String[] fields = content.split("\n");
+
+        Map<String, String> data = new HashMap();
+        for (int i = 0; i < fields.length; i++) {
+            String field = fields[i];
+            String[] parts = field.split(":");
+            data.put(parts[0], parts[1]);
+        }
+
+        String name = data.get("name");
+        String desc = data.get("description");
+	if (desc.length() > 20) {
+            desc = desc.substring(0, 20) + "...";
+        }
+        String ip = data.get("ip");
+        String ports = data.get("ports");
+        String admin = data.get("admin");
+
+        String tableRow = "<tr><td><a href='/wso2registry/web" + path + ";view'>" + name + "</a></td><td>" + desc + "</td><td>" + ip +
+                "</td><td>" + ports + "</td><td>" + admin + "</td></tr>";
+        return tableRow;
+    }
+
+    public Resource getEdit(RequestContext requestContext) throws RegistryException {
+        return null;
+    }
+
+    public void put(RequestContext requestContext) throws RegistryException {
+    }
+
+    public void importResource(RequestContext requestContext) throws RegistryException {
+    }
+
+    public void delete(RequestContext requestContext) throws RegistryException {
+    }
+
+    public void putChild(RequestContext requestContext) throws RegistryException {
+
+        Resource childResource = requestContext.getResource();
+        if (childResource != null) {
+            String mediaType = childResource.getMediaType();
+            if (!"application/vnd.server-info".equals(mediaType)) {
+                String msg = "Invalid child media type. Server collections only allow server " +
+                        "information files (application/vnd.server-info) as child resources.";
+                throw new RegistryException(msg);
+            }
+        }
+    }
+
+    public void importChild(RequestContext requestContext) throws RegistryException {
+    }
+}

Added: trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerDetailsEditProcessor.java
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerDetailsEditProcessor.java?pathrev=19121
==============================================================================
--- (empty file)
+++ trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerDetailsEditProcessor.java	Thu Jul 10 23:17:45 2008
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry.samples.handlers.servers;
+
+import org.wso2.registry.jdbc.handlers.EditProcessor;
+import org.wso2.registry.exceptions.RegistryException;
+import org.wso2.registry.Registry;
+import org.wso2.registry.Resource;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class ServerDetailsEditProcessor extends EditProcessor {
+
+    public void processEdit(String path, HttpServletRequest request, HttpServletResponse response)
+            throws RegistryException {
+
+        String name = request.getParameter("server-name");
+        String desc = request.getParameter("server-desc");
+        String ip = request.getParameter("server-ip");
+        String ports = request.getParameter("server-ports");
+        String admin = request.getParameter("server-admin");
+
+        String content = "name: " + name + "\ndescription: " + desc + "\nip: " + ip +
+                "\nports: " + ports + "\nadmin: " + admin;
+
+        Registry registry = getRegistry(request);
+        Resource resource = registry.get(path);
+        resource.setContent(content);
+
+        registry.put(path, resource);
+    }
+}

Added: trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerDetailsHandler.java
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerDetailsHandler.java?pathrev=19121
==============================================================================
--- (empty file)
+++ trunk/registry/modules/samples/collection-handler-sample/src/org/wso2/registry/samples/handlers/servers/ServerDetailsHandler.java	Thu Jul 10 23:17:45 2008
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry.samples.handlers.servers;
+
+import org.wso2.registry.jdbc.handlers.UIEnabledHandler;
+import org.wso2.registry.jdbc.handlers.RequestContext;
+import org.wso2.registry.Resource;
+import org.wso2.registry.exceptions.RegistryException;
+
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * This class is a part of a sample to demonstrate the use of handler framework to model a server
+ * information repository. Server details files (application/vnd.server-info) contain information
+ * about individual servers. It is a set of name value pairs as shown below.
+ *
+ * name: HR mail server
+ * description: Mail server of the HR department.
+ * ip: 10.1.100.22
+ * ports: 8090, 9000
+ * admin: deep
+ */
+public class ServerDetailsHandler extends UIEnabledHandler {
+
+    public Resource getRawResource(RequestContext requestContext) throws RegistryException {
+        return null;
+    }
+
+    public Resource getView(RequestContext requestContext) throws RegistryException {
+
+        String path = requestContext.getResourcePath();
+        Resource resource = requestContext.getRepository().get(path);
+        byte[] contentBytes = (byte[]) resource.getContent();
+
+        String content = new String(contentBytes);
+        String[] fields = content.split("\n");
+
+        Map <String, String> data = new HashMap();
+        for (int i = 0; i < fields.length; i++) {
+            String field = fields[i];
+            String[] parts = field.split(":");
+            data.put(parts[0], parts[1]);
+        }
+
+        String name = data.get("name");
+        String desc = data.get("description");
+        String ip = data.get("ip");
+        String ports = data.get("ports");
+        String admin = data.get("admin");
+
+        String view = "<h3>Details of server " + name + "</h3>" +
+                "<br/><br/>" + desc +
+                "<br/><br/>IP Address: " + ip +
+                "<br/>Ports: " + ports +
+                "<br/>Administrator: " + admin;
+
+        resource.setContent(view);
+        return resource;
+    }
+
+    public Resource getEdit(RequestContext requestContext) throws RegistryException {
+
+        String path = requestContext.getResourcePath();
+        Resource resource = requestContext.getRepository().get(path);
+        requestContext.setResource(resource);
+        byte[] contentBytes = (byte[]) resource.getContent();
+
+        String content = new String(contentBytes);
+        String[] fields = content.split("\n");
+
+        Map <String, String> data = new HashMap();
+        for (int i = 0; i < fields.length; i++) {
+            String field = fields[i];
+            String[] parts = field.split(":");
+            data.put(parts[0], parts[1]);
+        }
+
+        String name = data.get("name");
+        String desc = data.get("description");
+        String ip = data.get("ip");
+        String ports = data.get("ports");
+        String admin = data.get("admin");
+
+        String view = "<form method='post' action='/wso2registry/custom" + path + "'>" +
+                "<input type='hidden' name='edit-processor' value='application/vnd.server-info'/>" +
+                "<h3>Server name: <input type='text' name='server-name' value='" + name + "'/></h3>" +
+                "<br/><br/>Description: <input type='text' name='server-desc' value='" + desc + "'/>" +
+                "<br/><br/>IP Address: <input type='text' name='server-ip' value='" + ip + "'/>" +
+                "<br/>Ports: <input type='text' name='server-ports' value='" + ports + "'/>" +
+                "<br/>Administrator: <input type='text' name='server-admin' value='" + admin + "'/>" +
+                "<input type='submit' value='Save server details'/>" +
+                "</form>";
+
+        resource.setContent(view);
+        return resource;
+    }
+
+    public void put(RequestContext requestContext) throws RegistryException {
+    }
+
+    public void importResource(RequestContext requestContext) throws RegistryException {
+    }
+
+    public void delete(RequestContext requestContext) throws RegistryException {
+    }
+
+    public void putChild(RequestContext requestContext) throws RegistryException {
+    }
+
+    public void importChild(RequestContext requestContext) throws RegistryException {
+    }
+}



More information about the Registry-dev mailing list