/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.jackrabbit.oak.core;

import org.apache.jackrabbit.mk.api.MicroKernel;
import org.apache.jackrabbit.oak.api.ContentSession;
import org.apache.jackrabbit.oak.kernel.NodeState;
import org.apache.jackrabbit.oak.api.Branch;
import org.apache.jackrabbit.oak.api.QueryEngine;
import org.apache.jackrabbit.oak.api.ContentRepository;
import org.apache.jackrabbit.oak.kernel.KernelNodeStore;
import org.apache.jackrabbit.oak.query.QueryEngineImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.GuestCredentials;
import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.SimpleCredentials;
import javax.security.auth.login.LoginException;

/**
 * {@link MicroKernel}-based implementation of
 * the {@link ContentRepository} interface.
 */
public class KernelContentRepository implements ContentRepository {

    /** Logger instance */
    private static final Logger log =
            LoggerFactory.getLogger(KernelContentRepository.class);

    // TODO: retrieve default wsp-name from configuration
    private static final String DEFAULT_WORKSPACE_NAME = "default";

    private final MicroKernel microKernel;
    private final KernelNodeStore nodeStore;

    public KernelContentRepository(MicroKernel mk) {
        microKernel = mk;
        nodeStore = new KernelNodeStore(microKernel);
    }

    @Override
    public ContentSession login(Object credentials, String workspaceName)
            throws LoginException, NoSuchWorkspaceException {

        // TODO: add proper implementation
        // TODO  - authentication against configurable spi-authentication
        // TODO  - validation of workspace name (including access rights for the given 'user')

        final SimpleCredentials sc;
        if (credentials == null || credentials instanceof GuestCredentials) {
            sc = new SimpleCredentials("anonymous", new char[0]);
        } else if (credentials instanceof SimpleCredentials) {
            sc = (SimpleCredentials) credentials;
        } else {
            sc = null;
        }

        if (sc == null) {
            throw new LoginException("login failed");
        }

        final String wspName = (workspaceName == null) ? DEFAULT_WORKSPACE_NAME : workspaceName;
        final String revision = getRevision(credentials);

        // FIXME: workspace setup must be done elsewhere...
        if (wspName.equals(DEFAULT_WORKSPACE_NAME)) {
            NodeState root = nodeStore.getRoot();
            NodeState wspNode = root.getChildNode(wspName);
            if (wspNode == null) {
                Branch branch = nodeStore.branch(root);
                branch.getNode("/").addNode(wspName);
                nodeStore.merge(branch);
            }
        }

        QueryEngine queryEngine = new QueryEngineImpl(microKernel);
        // TODO set revision!?
        NodeState wspRoot = nodeStore.getRoot().getChildNode(workspaceName);
        if (wspRoot == null) {
            throw new NoSuchWorkspaceException(workspaceName);
        }

        return new KernelContentSession(
                sc, workspaceName, nodeStore, wspRoot, queryEngine);
    }

    /**
     * @param credentials The credentials object used for authentication.
     * @return The microkernel revision or {@code null} if the give credentials doesn't
     * specify a specific revision number.
     */
    private String getRevision(Object credentials) {
        // TODO: define if/how desired revision can be passed in by the credentials object.
        return null;
    }

}

@@ -2122,24 +2122,397 @@
 icroKernel);
+%0A%0A        // FIXME: workspace setup must be done elsewhere...%0A        NodeState root = nodeStore.getRoot();%0A        NodeState wspNode = root.getChildNode(DEFAULT_WORKSPACE_NAME);%0A        if (wspNode == null) %7B%0A            Branch branch = nodeStore.branch(root);%0A            branch.getNode(%22/%22).addNode(DEFAULT_WORKSPACE_NAME);%0A            nodeStore.merge(branch);%0A        %7D
 %0A    %7D%0A%0A    
@@ -2656,16 +2656,115 @@
 eption %7B
+%0A        if (workspaceName == null) %7B%0A            workspaceName = DEFAULT_WORKSPACE_NAME;%0A        %7D
 %0A%0A      
@@ -3417,1374 +3417,389 @@
     
-final String wspName = (workspaceName == null) ? DEFAULT_WORKSPACE_NAME : workspaceName;%0A        final String revision = getRevision(credentials);%0A%0A        // FIXME: workspace setup must be done elsewhere...%0A        if (wspName.equals(DEFAULT_WORKSPACE_NAME)) %7B%0A            NodeState root = nodeStore.getRoot();%0A            NodeState wspNode = root.getChildNode(wspName);%0A            if (wspNode == null) %7B%0A                Branch branch = nodeStore.branch(root);%0A                branch.getNode(%22/%22).addNode(wspName);%0A                nodeStore.merge(branch);%0A            %7D%0A        %7D%0A%0A        QueryEngine queryEngine = new QueryEngineImpl(microKernel);%0A        // TODO set revision!?%0A        NodeState wspRoot = nodeStore.getRoot().getChildNode(workspaceName);%0A        if (wspRoot == null) %7B%0A            throw new NoSuchWorkspaceException(workspaceName);%0A        %7D%0A%0A        return new KernelContentSession(%0A                sc, workspaceName, nodeStore, wspRoot, queryEngine);%0A    %7D%0A%0A    /**%0A     * @param credentials The credentials object used for authentication.%0A     * @return The microkernel revision or %7B@code null%7D if the give credentials doesn't%0A     * specify a specific revision number.%0A     */%0A    private String getRevision(Object credentials) %7B%0A        // TODO: define if/how desired revision can be passed in by the credentials object.%0A        return null
+QueryEngine queryEngine = new QueryEngineImpl(microKernel);%0A        // TODO set revision!?%0A        NodeState wspRoot = nodeStore.getRoot().getChildNode(workspaceName);%0A        if (wspRoot == null) %7B%0A            throw new NoSuchWorkspaceException(workspaceName);%0A        %7D%0A%0A        return new KernelContentSession(%0A                sc, workspaceName, nodeStore, wspRoot, queryEngine)
 ;%0A  

Define Oak API