[Ds-java-dev] svn commit r19051 - in trunk/solutions/data-services/java/modules/www: . resources/web src/org/wso2/solutions/internal

saminda at wso2.com saminda at wso2.com
Wed Jul 9 11:12:06 PDT 2008


Author: saminda
Date: Wed Jul  9 11:12:05 2008
New Revision: 19051
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=19051

Log:
filled handleSecurity section of HttpContext


Added:
   trunk/solutions/data-services/java/modules/www/resources/web/login.jsp
Modified:
   trunk/solutions/data-services/java/modules/www/pom.xml
   trunk/solutions/data-services/java/modules/www/src/org/wso2/solutions/internal/Activator.java

Modified: trunk/solutions/data-services/java/modules/www/pom.xml
URL: http://wso2.org/svn/browse/wso2/trunk/solutions/data-services/java/modules/www/pom.xml?rev=19051&r1=19050&r2=19051&view=diff
==============================================================================
--- trunk/solutions/data-services/java/modules/www/pom.xml	(original)
+++ trunk/solutions/data-services/java/modules/www/pom.xml	Wed Jul  9 11:12:05 2008
@@ -28,6 +28,11 @@
         </dependency>
 
         <dependency>
+            <groupId>org.wso2.carbon</groupId>
+            <artifactId>org.wso2.carbon.registry.service</artifactId>
+        </dependency>
+
+        <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.framework</artifactId>
         </dependency>
@@ -102,6 +107,7 @@
                         <Private-Package>org.wso2.solutions.internal</Private-Package>
                         <Import-Package>
                             javax.servlet; version=2.4,
+                            org.wso2.carbon.registry.service; version=0.0.0,
                             *;resolution:=optional
                         </Import-Package>
                         <Bundle-Activator>org.wso2.solutions.internal.Activator</Bundle-Activator>

Added: trunk/solutions/data-services/java/modules/www/resources/web/login.jsp
URL: http://wso2.org/svn/browse/wso2/trunk/solutions/data-services/java/modules/www/resources/web/login.jsp?pathrev=19051
==============================================================================
--- (empty file)
+++ trunk/solutions/data-services/java/modules/www/resources/web/login.jsp	Wed Jul  9 11:12:05 2008
@@ -0,0 +1,37 @@
+<!--
+Copyright 2004 The Apache Software Foundation
+
+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.
+-->
+<html>
+<head>
+    <title>Login Page for Examples</title>
+<body bgcolor="white">
+<form method="POST" action='login.action'>
+    <table border="0" cellspacing="5">
+        <tr>
+            <th align="right">Username:</th>
+            <td align="left"><input type="text" name="username"></td>
+        </tr>
+        <tr>
+            <th align="right">Password:</th>
+            <td align="left"><input type="password" name="password"></td>
+        </tr>
+        <tr>
+            <td align="right"><input type="submit" value="Log In"></td>
+            <td align="left"><input type="reset"></td>
+        </tr>
+    </table>
+</form>
+</body>
+</html>

Modified: trunk/solutions/data-services/java/modules/www/src/org/wso2/solutions/internal/Activator.java
URL: http://wso2.org/svn/browse/wso2/trunk/solutions/data-services/java/modules/www/src/org/wso2/solutions/internal/Activator.java?rev=19051&r1=19050&r2=19051&view=diff
==============================================================================
--- trunk/solutions/data-services/java/modules/www/src/org/wso2/solutions/internal/Activator.java	(original)
+++ trunk/solutions/data-services/java/modules/www/src/org/wso2/solutions/internal/Activator.java	Wed Jul  9 11:12:05 2008
@@ -15,17 +15,24 @@
  */
 package org.wso2.solutions.internal;
 
+import org.eclipse.equinox.http.helper.ContextPathServletAdaptor;
+import org.eclipse.equinox.jsp.jasper.JspServlet;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
-import org.osgi.service.http.HttpService;
 import org.osgi.service.http.HttpContext;
-import org.eclipse.equinox.http.helper.BundleEntryHttpContext;
-import org.eclipse.equinox.http.helper.ContextPathServletAdaptor;
-import org.eclipse.equinox.jsp.jasper.JspServlet;
+import org.osgi.service.http.HttpService;
 import org.wso2.carbon.component.ComponentEntryHttpContext;
+import org.wso2.carbon.registry.service.RegistryService;
+import org.wso2.registry.Registry;
+import org.wso2.registry.Resource;
+import org.wso2.registry.exceptions.RegistryException;
 
 import javax.servlet.Servlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 
 /*
 * 
@@ -37,8 +44,15 @@
         if (reference != null) {
             final HttpService httpService = (HttpService) context.getService(reference);
             try {
+                ServiceReference regRef =
+                        context.getServiceReference(RegistryService.class.getName());
+                if (regRef == null) {
+                    throw new Exception("RegistryService is not found");
+                }
+                RegistryService registryService = (RegistryService) context.getService(regRef);
+                Registry registry = registryService.getSystemRegistry();
                 HttpContext commonContext =
-                        new ComponentEntryHttpContext(context.getBundle(), "/web");
+                        new DSSHttpContext(context.getBundle(), "/web", registry);
                 httpService.registerResources("/ds", "/",
                                               commonContext);
 
@@ -60,4 +74,84 @@
     public void stop(BundleContext context) throws Exception {
 
     }
+
+    private class DSSHttpContext extends ComponentEntryHttpContext {
+
+        private Registry registry;
+
+        public DSSHttpContext(Bundle bundle) {
+            super(bundle);
+        }
+
+        public DSSHttpContext(Bundle bundle, String s, Registry registry) {
+            super(bundle, s);
+            this.registry = registry;
+        }
+
+        public boolean handleSecurity(HttpServletRequest request,
+                                      HttpServletResponse response) throws IOException {
+            String requestUri = request.getRequestURI();
+            String id = request.getSession().getId();
+            // logic to authenticate request;
+            boolean sessionAuth;
+            try {
+                sessionAuth = authenticate(id, null, null);
+            } catch (RegistryException e) {
+                throw new IOException(e.getMessage());
+            }
+            if (requestUri.endsWith("/ds") || requestUri.endsWith("/ds/")) {
+                if (sessionAuth) {
+                    response.sendRedirect("/ds/index.html");
+                } else {
+                    response.sendRedirect("/ds/login.jsp");
+                }
+                return false;
+            } else {
+                if (requestUri.indexOf("login.jsp") > -1) {
+                    return true;
+                } else if (requestUri.indexOf("login.action") > -1) {
+                    String userName = request.getParameter("username");
+                    String password = request.getParameter("password");
+                    try {
+                        sessionAuth = authenticate(id, userName, password);
+                    } catch (RegistryException e) {
+                        throw new IOException(e.getMessage());
+                    }
+                    if (sessionAuth) {
+                        response.sendRedirect("/ds/index.html");
+                        return false;
+                    } else {
+                        response.sendRedirect("/ds/login.jsp");
+                        return false;
+                    }
+                } else {
+                    if (sessionAuth) {
+                        return true;
+                    } else {
+                        response.sendRedirect("/ds/login.jsp");
+                        return false;
+                    }
+                }
+            }
+        }
+
+        private boolean authenticate(String sessionId, String userName, String password)
+                throws RegistryException {
+            String path = "/ds/login/session/" + sessionId;
+            if (userName == null && password == null) {
+                if (registry.resourceExists(path)) {
+                    return true;
+                }
+            } else if ("admin".equals(userName) && "admin".equals(password)) {
+                Resource resource = registry.newResource();
+                resource.setProperty("username", userName);
+                registry.put(path, resource);
+                return true;
+            } else {
+                //TODO: use security to find admin uses
+                return false;
+            }
+            return false;
+        }
+    }
 }



More information about the Ds-java-dev mailing list