jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1477506 - in /jackrabbit/oak/trunk: oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java oak-run/src/main/java/org/apache/jackrabbit/oak/run/Main.java oak-run/src/test/java/org/apache/jackrabbit/oak/run/BasicServerTest.java
Date Tue, 30 Apr 2013 08:40:05 GMT
Author: jukka
Date: Tue Apr 30 08:40:05 2013
New Revision: 1477506

URL: http://svn.apache.org/r1477506
Log:
OAK-104: OAK-104: HTTP bindings for Oak

Fix authentication settings
Better repository startup in oak-run

Modified:
    jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java
    jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Main.java
    jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/BasicServerTest.java

Modified: jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java?rev=1477506&r1=1477505&r2=1477506&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java
(original)
+++ jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java
Tue Apr 30 08:40:05 2013
@@ -20,8 +20,10 @@ import java.io.IOException;
 import java.util.Iterator;
 import java.util.Map.Entry;
 
+import javax.jcr.Credentials;
 import javax.jcr.GuestCredentials;
 import javax.jcr.NoSuchWorkspaceException;
+import javax.jcr.SimpleCredentials;
 import javax.security.auth.login.LoginException;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -38,6 +40,7 @@ import org.apache.jackrabbit.oak.api.Con
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.util.Base64;
 import org.apache.tika.mime.MediaType;
 
 public class OakServlet extends HttpServlet {
@@ -65,7 +68,18 @@ public class OakServlet extends HttpServ
             HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         try {
-            ContentSession session = repository.login(new GuestCredentials(), null);
+            Credentials credentials = new GuestCredentials();
+
+            String authorization = request.getHeader("Authorization");
+            if (authorization != null && authorization.startsWith("Basic ")) {
+                String[] basic =
+                        Base64.decode(authorization.substring("Basic ".length())).split(":");
+                credentials = new SimpleCredentials(basic[0], basic[1].toCharArray());
+            } else {
+                throw new LoginException();
+            }
+
+            ContentSession session = repository.login(credentials, null);
             try {
                 Root root = session.getLatestRoot();
                 request.setAttribute("root", root);
@@ -77,19 +91,14 @@ public class OakServlet extends HttpServ
                 // direction as some parent nodes may be read-protected.
                 String head = request.getPathInfo();
                 String tail = "";
-                Tree tree = root.getTreeOrNull(head);
-                while (tree == null) {
-                    if (head.equals("/")) {
+                Tree tree = root.getTree(head);
+                while (!tree.exists()) {
+                    if (tree.isRoot()) {
                         response.sendError(HttpServletResponse.SC_NOT_FOUND);
                         return;
                     }
-                    int slash = head.lastIndexOf('/');
-                    tail = head.substring(slash) + tail;
-                    head = head.substring(0, slash);
-                    if (head.isEmpty()){
-                    	head="/";
-                    }
-                    tree = root.getTreeOrNull(head);
+                    tail = "/" + tree.getName() + tail;
+                    tree = tree.getParent();
                 }
                 request.setAttribute("tree", tree);
                 request.setAttribute("path", tail);
@@ -101,7 +110,8 @@ public class OakServlet extends HttpServ
         } catch (NoSuchWorkspaceException e) {
             response.sendError(HttpServletResponse.SC_NOT_FOUND);
         } catch (LoginException e) {
-            throw new ServletException(e);
+            response.setHeader("WWW-Authenticate", "Basic realm=\"Oak\"");
+            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
         }
     }
 

Modified: jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Main.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Main.java?rev=1477506&r1=1477505&r2=1477506&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Main.java (original)
+++ jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Main.java Tue
Apr 30 08:40:05 2013
@@ -19,6 +19,8 @@ package org.apache.jackrabbit.oak.run;
 import java.io.InputStream;
 import java.util.Properties;
 import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+
 import javax.jcr.Repository;
 
 import org.apache.jackrabbit.mk.api.MicroKernel;
@@ -27,18 +29,11 @@ import org.apache.jackrabbit.oak.Oak;
 import org.apache.jackrabbit.oak.api.ContentRepository;
 import org.apache.jackrabbit.oak.benchmark.BenchmarkRunner;
 import org.apache.jackrabbit.oak.http.OakServlet;
-import org.apache.jackrabbit.oak.jcr.RepositoryImpl;
-import org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider;
-import org.apache.jackrabbit.oak.plugins.commit.JcrConflictHandler;
-import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider;
-import org.apache.jackrabbit.oak.plugins.name.NameValidatorProvider;
-import org.apache.jackrabbit.oak.plugins.name.NamespaceValidatorProvider;
-import org.apache.jackrabbit.oak.plugins.nodetype.RegistrationEditorProvider;
-import org.apache.jackrabbit.oak.plugins.nodetype.TypeEditorProvider;
-import org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent;
-import org.apache.jackrabbit.oak.security.SecurityProviderImpl;
-import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
+import org.apache.jackrabbit.oak.jcr.Jcr;
+import org.apache.jackrabbit.oak.kernel.KernelNodeStore;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
 import org.apache.jackrabbit.webdav.jcr.JCRWebdavServerServlet;
+import org.apache.jackrabbit.webdav.server.AbstractWebdavServlet;
 import org.apache.jackrabbit.webdav.simple.SimpleWebdavServlet;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.servlet.ServletContextHandler;
@@ -67,8 +62,7 @@ public class Main {
         } else if ("benchmark".equals(command)){
             BenchmarkRunner.main(args);
         } else if ("server".equals(command)){
-            HttpServer httpServer = new HttpServer(URI, args);
-            httpServer.start();
+            new HttpServer(URI, args);
         } else {
             System.err.println("Unknown command: " + command);
             System.exit(1);
@@ -111,26 +105,30 @@ public class Main {
 
         private final MicroKernel[] kernels;
 
-        public HttpServer(String uri, String[] args) {
+        private final ScheduledExecutorService executor;
+
+        public HttpServer(String uri, String[] args) throws Exception {
             int port = java.net.URI.create(uri).getPort();
             if (port == -1) {
                 // use default
                 port = PORT;
             }
 
-            context = new ServletContextHandler(ServletContextHandler.SECURITY);
+            context = new ServletContextHandler();
             context.setContextPath("/");
 
+            executor = Executors.newScheduledThreadPool(3);
+
             if (args.length == 0) {
                 System.out.println("Starting an in-memory repository");
                 System.out.println(uri + " -> [memory]");
                 kernels = new MicroKernel[] { new MicroKernelImpl() };
-                addServlets(kernels[0], "");
+                addServlets(new KernelNodeStore(kernels[0]), "");
             } else if (args.length == 1) {
                 System.out.println("Starting a standalone repository");
                 System.out.println(uri + " -> " + args[0]);
                 kernels = new MicroKernel[] { new MicroKernelImpl(args[0]) };
-                addServlets(kernels[0], "");
+                addServlets(new KernelNodeStore(kernels[0]), "");
             } else {
                 System.out.println("Starting a clustered repository");
                 kernels = new MicroKernel[args.length];
@@ -138,15 +136,12 @@ public class Main {
                     // FIXME: Use a clustered MicroKernel implementation
                     System.out.println(uri + "/node" + i + "/ -> " + args[i]);
                     kernels[i] = new MicroKernelImpl(args[i]);
-                    addServlets(kernels[i], "/node" + i);
+                    addServlets(new KernelNodeStore(kernels[i]), "/node" + i);
                 }
             }
 
             server = new Server(port);
             server.setHandler(context);
-        }
-
-        public void start() throws Exception {
             server.start();
         }
 
@@ -156,27 +151,20 @@ public class Main {
 
         public void stop() throws Exception {
             server.stop();
+            executor.shutdown();
         }
 
-        private void addServlets(MicroKernel kernel, String path) {
-            SecurityProvider securityProvider = new SecurityProviderImpl();
-            ContentRepository repository = new Oak(kernel)
-                .with(JcrConflictHandler.JCR_CONFLICT_HANDLER)
-                .with(new ConflictValidatorProvider())
-                .with(new NameValidatorProvider())
-                .with(new NamespaceValidatorProvider())
-                .with(new TypeEditorProvider())
-                .with(new RegistrationEditorProvider())
-                .with(new PropertyIndexEditorProvider())
-                .with(securityProvider)
-                .with(new InitialContent())
-                .createContentRepository();
+        private void addServlets(NodeStore store, String path) {
+            Oak oak = new Oak(store);
+            Jcr jcr = new Jcr(oak).with(executor);
+
+            ContentRepository repository = oak.createContentRepository();
 
-            ServletHolder oak = new ServletHolder(new OakServlet(repository));
-            context.addServlet(oak, path + "/*");
+            ServletHolder holder =
+                    new ServletHolder(new OakServlet(repository));
+            context.addServlet(holder, path + "/*");
 
-            final Repository jcrRepository = new RepositoryImpl(
-                    repository, Executors.newScheduledThreadPool(1), securityProvider);
+            final Repository jcrRepository = jcr.createRepository();
 
             ServletHolder webdav =
                     new ServletHolder(new SimpleWebdavServlet() {
@@ -189,8 +177,8 @@ public class Main {
                     SimpleWebdavServlet.INIT_PARAM_RESOURCE_PATH_PREFIX,
                     path + "/webdav");
             webdav.setInitParameter(
-                    SimpleWebdavServlet.INIT_PARAM_MISSING_AUTH_MAPPING,
-                    "admin:admin");
+                    AbstractWebdavServlet.INIT_PARAM_AUTHENTICATE_HEADER,
+                    "Basic realm=\"Oak\"");
             context.addServlet(webdav, path + "/webdav/*");
 
             ServletHolder davex =
@@ -203,9 +191,9 @@ public class Main {
             davex.setInitParameter(
                     JCRWebdavServerServlet.INIT_PARAM_RESOURCE_PATH_PREFIX,
                     path + "/davex");
-            davex.setInitParameter(
-                    JCRWebdavServerServlet.INIT_PARAM_MISSING_AUTH_MAPPING,
-                    "admin:admin");
+            webdav.setInitParameter(
+                    AbstractWebdavServlet.INIT_PARAM_AUTHENTICATE_HEADER,
+                    "Basic realm=\"Oak\"");
             context.addServlet(davex, path + "/davex/*");
         }
 

Modified: jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/BasicServerTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/BasicServerTest.java?rev=1477506&r1=1477505&r2=1477506&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/BasicServerTest.java
(original)
+++ jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/BasicServerTest.java
Tue Apr 30 08:40:05 2013
@@ -24,7 +24,6 @@ import java.net.URL;
 import org.apache.jackrabbit.util.Base64;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import static org.junit.Assert.assertEquals;
 
@@ -46,7 +45,6 @@ public class BasicServerTest {
     @Before
     public void startServer() throws Exception {
         server = new Main.HttpServer(SERVER_URL, new String[0]);
-        server.start();
     }
 
     @After
@@ -54,13 +52,12 @@ public class BasicServerTest {
         server.stop();
     }
 
-    @Test @Ignore("TODO: check permissions")
+    @Test
     public void testServerOk() throws Exception {
-
         URL server = new URL(SERVER_URL);
         HttpURLConnection conn = (HttpURLConnection) server.openConnection();
-        conn.setRequestProperty("Authorization",
-                "Basic " + Base64.encode("admin:admin"));
+        conn.setRequestProperty(
+                "Authorization", "Basic " + Base64.encode("admin:admin"));
         assertEquals(200, conn.getResponseCode());
     }
 }



Mime
View raw message