Search in sources :

Example 61 with ContentSession

use of org.apache.jackrabbit.oak.api.ContentSession in project jackrabbit-oak by apache.

the class ImmutableRootTest method setUp.

@Before
public void setUp() throws CommitFailedException {
    ContentSession session = createContentSession();
    // Add test content
    Root root = session.getLatestRoot();
    Tree tree = root.getTree("/");
    Tree x = tree.addChild("x");
    Tree y = x.addChild("y");
    Tree z = y.addChild("z");
    root.commit();
    // Acquire a fresh new root to avoid problems from lingering state
    this.root = new ImmutableRoot(session.getLatestRoot());
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) ContentSession(org.apache.jackrabbit.oak.api.ContentSession) Tree(org.apache.jackrabbit.oak.api.Tree) Before(org.junit.Before)

Example 62 with ContentSession

use of org.apache.jackrabbit.oak.api.ContentSession in project jackrabbit-oak by apache.

the class OakServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Credentials credentials = null;
        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);
            // Find the longest part of the given path that matches
            // an existing node. The tail part might be used when
            // creating new nodes or when exposing virtual resources.
            // Note that we need to traverse the path in reverse
            // direction as some parent nodes may be read-protected.
            String head = request.getPathInfo();
            String tail = "";
            Tree tree = root.getTree(head);
            while (!tree.exists()) {
                if (tree.isRoot()) {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
                tail = "/" + tree.getName() + tail;
                tree = tree.getParent();
            }
            request.setAttribute("tree", tree);
            request.setAttribute("path", tail);
            super.service(request, response);
        } finally {
            session.close();
        }
    } catch (NoSuchWorkspaceException e) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    } catch (LoginException e) {
        response.setHeader("WWW-Authenticate", "Basic realm=\"Oak\"");
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    }
}
Also used : NoSuchWorkspaceException(javax.jcr.NoSuchWorkspaceException) SimpleCredentials(javax.jcr.SimpleCredentials) Root(org.apache.jackrabbit.oak.api.Root) LoginException(javax.security.auth.login.LoginException) ContentSession(org.apache.jackrabbit.oak.api.ContentSession) Tree(org.apache.jackrabbit.oak.api.Tree) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials)

Example 63 with ContentSession

use of org.apache.jackrabbit.oak.api.ContentSession in project jackrabbit-oak by apache.

the class PrivateStoreValidatorProviderTest method testDefaultMount.

@Test
public void testDefaultMount() throws Exception {
    setUp();
    ContentSession s = repository.login(null, null);
    Root r = s.getLatestRoot();
    Tree t = r.getTree("/").addChild("test");
    t.addChild("node1").setProperty("jcr:primaryType", "nt:base");
    t.addChild("node2").setProperty("jcr:primaryType", "nt:base");
    t.addChild("node3").setProperty("jcr:primaryType", "nt:base");
    r.commit();
    t.getChild("node1").removeProperty("jcr:primaryType");
    r.commit();
    t.getChild("node1").remove();
    r.commit();
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) ContentSession(org.apache.jackrabbit.oak.api.ContentSession) Tree(org.apache.jackrabbit.oak.api.Tree) Test(org.junit.Test)

Example 64 with ContentSession

use of org.apache.jackrabbit.oak.api.ContentSession in project jackrabbit-oak by apache.

the class UserInitializerTest method testAnonymousConfiguration.

/**
     * @since OAK 1.0 The anonymous user is optional.
     */
@Test
public void testAnonymousConfiguration() throws Exception {
    Map<String, Object> userParams = new HashMap();
    userParams.put(UserConstants.PARAM_ANONYMOUS_ID, "");
    ConfigurationParameters params = ConfigurationParameters.of(UserConfiguration.NAME, ConfigurationParameters.of(userParams));
    SecurityProvider sp = new SecurityProviderImpl(params);
    final ContentRepository repo = new Oak().with(new InitialContent()).with(new PropertyIndexEditorProvider()).with(new PropertyIndexProvider()).with(new TypeEditorProvider()).with(sp).createContentRepository();
    ContentSession cs = Subject.doAs(SystemSubject.INSTANCE, new PrivilegedExceptionAction<ContentSession>() {

        @Override
        public ContentSession run() throws Exception {
            return repo.login(null, null);
        }
    });
    try {
        Root root = cs.getLatestRoot();
        UserConfiguration uc = sp.getConfiguration(UserConfiguration.class);
        UserManager umgr = uc.getUserManager(root, NamePathMapper.DEFAULT);
        Authorizable anonymous = umgr.getAuthorizable(UserConstants.DEFAULT_ANONYMOUS_ID);
        assertNull(anonymous);
    } finally {
        cs.close();
    }
    // login as admin should fail
    ContentSession anonymousSession = null;
    try {
        anonymousSession = repo.login(new GuestCredentials(), null);
        fail();
    } catch (LoginException e) {
    //success
    } finally {
        if (anonymousSession != null) {
            anonymousSession.close();
        }
    }
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) HashMap(java.util.HashMap) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) ConfigurationParameters(org.apache.jackrabbit.oak.spi.security.ConfigurationParameters) LoginException(javax.security.auth.login.LoginException) InitialContent(org.apache.jackrabbit.oak.InitialContent) PropertyIndexProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexProvider) TypeEditorProvider(org.apache.jackrabbit.oak.plugins.nodetype.TypeEditorProvider) UserManager(org.apache.jackrabbit.api.security.user.UserManager) SecurityProvider(org.apache.jackrabbit.oak.spi.security.SecurityProvider) ContentRepository(org.apache.jackrabbit.oak.api.ContentRepository) Oak(org.apache.jackrabbit.oak.Oak) ContentSession(org.apache.jackrabbit.oak.api.ContentSession) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) LoginException(javax.security.auth.login.LoginException) SecurityProviderImpl(org.apache.jackrabbit.oak.security.SecurityProviderImpl) GuestCredentials(javax.jcr.GuestCredentials) UserConfiguration(org.apache.jackrabbit.oak.spi.security.user.UserConfiguration) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Example 65 with ContentSession

use of org.apache.jackrabbit.oak.api.ContentSession in project jackrabbit-oak by apache.

the class ContentRemoteSessionTest method testReadBinaryIdWithInvalidReference.

@Test
public void testReadBinaryIdWithInvalidReference() {
    Root root = mock(Root.class);
    doReturn(null).when(root).getBlob(anyString());
    ContentSession session = mock(ContentSession.class);
    doReturn(root).when(session).getLatestRoot();
    ContentRemoteSession remoteSession = createSession(session);
    assertNull(remoteSession.readBinaryId("id"));
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) ContentSession(org.apache.jackrabbit.oak.api.ContentSession) Test(org.junit.Test)

Aggregations

ContentSession (org.apache.jackrabbit.oak.api.ContentSession)146 Test (org.junit.Test)132 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)66 SimpleCredentials (javax.jcr.SimpleCredentials)60 Root (org.apache.jackrabbit.oak.api.Root)43 LoginException (javax.security.auth.login.LoginException)35 AuthInfo (org.apache.jackrabbit.oak.api.AuthInfo)26 Tree (org.apache.jackrabbit.oak.api.Tree)25 UserManager (org.apache.jackrabbit.api.security.user.UserManager)19 User (org.apache.jackrabbit.api.security.user.User)17 PermissionProvider (org.apache.jackrabbit.oak.spi.security.authorization.permission.PermissionProvider)15 GuestCredentials (javax.jcr.GuestCredentials)13 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)12 Principal (java.security.Principal)10 TokenCredentials (org.apache.jackrabbit.api.security.authentication.token.TokenCredentials)10 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)9 Group (org.apache.jackrabbit.api.security.user.Group)8 EveryonePrincipal (org.apache.jackrabbit.oak.spi.security.principal.EveryonePrincipal)8 ImpersonationCredentials (org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials)7 PrincipalImpl (org.apache.jackrabbit.oak.spi.security.principal.PrincipalImpl)6