Search in sources :

Example 41 with ValueFactory

use of javax.jcr.ValueFactory in project sling by apache.

the class RepositoryDistributionPackageImporterTest method testImportPackageWithRequiredPrivileges.

@Test
public void testImportPackageWithRequiredPrivileges() throws Exception {
    SlingRepository repository = mock(SlingRepository.class);
    String serviceName = "admin";
    Session session = mock(Session.class);
    ValueFactory vf = mock(ValueFactory.class);
    when(session.getValueFactory()).thenReturn(vf);
    AccessControlManager acm = mock(AccessControlManager.class);
    String privilegeName = "jcr:read";
    Privilege privilege = mock(Privilege.class);
    String path = "/var/something";
    Node rootNode = mock(Node.class);
    Node createdNode = mock(Node.class);
    Node jcrContentNode = mock(Node.class);
    when(createdNode.addNode(JcrConstants.JCR_CONTENT, NodeType.NT_RESOURCE)).thenReturn(jcrContentNode);
    when(rootNode.addNode(any(String.class), any(String.class))).thenReturn(createdNode);
    when(session.getNode(path)).thenReturn(rootNode);
    when(acm.hasPrivileges(path, new Privilege[] { privilege })).thenReturn(true);
    when(acm.privilegeFromName(privilegeName)).thenReturn(privilege);
    when(session.getAccessControlManager()).thenReturn(acm);
    when(repository.loginService(serviceName, null)).thenReturn(session);
    RepositoryDistributionPackageImporter repositoryDistributionPackageImporter = new RepositoryDistributionPackageImporter(repository, serviceName, path, privilegeName);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    DistributionPackage distributionPackage = mock(DistributionPackage.class);
    when(distributionPackage.getId()).thenReturn("someid");
    InputStream stream = mock(InputStream.class);
    when(distributionPackage.createInputStream()).thenReturn(stream);
    repositoryDistributionPackageImporter.importPackage(resourceResolver, distributionPackage);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) AccessControlManager(javax.jcr.security.AccessControlManager) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) InputStream(java.io.InputStream) Node(javax.jcr.Node) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ValueFactory(javax.jcr.ValueFactory) Privilege(javax.jcr.security.Privilege) Session(javax.jcr.Session) Test(org.junit.Test)

Example 42 with ValueFactory

use of javax.jcr.ValueFactory in project camel by apache.

the class JcrNodePathCreationTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    Session session = openSession();
    ValueFactory valFact = session.getValueFactory();
    multiValued = new Value[] { valFact.createValue("value-1"), valFact.createValue("value-2") };
    session.logout();
}
Also used : ValueFactory(javax.jcr.ValueFactory) Session(javax.jcr.Session) Before(org.junit.Before)

Example 43 with ValueFactory

use of javax.jcr.ValueFactory in project jackrabbit by apache.

the class ACLProvider method init.

//----------------------------------------------< AccessControlProvider >---
/**
     * @see org.apache.jackrabbit.core.security.authorization.AccessControlProvider#init(javax.jcr.Session, java.util.Map)
     */
@Override
public void init(Session systemSession, Map configuration) throws RepositoryException {
    super.init(systemSession, configuration);
    NodeImpl root = (NodeImpl) session.getRootNode();
    if (root.hasNode(N_ACCESSCONTROL)) {
        acRoot = root.getNode(N_ACCESSCONTROL);
        if (!acRoot.isNodeType(NT_REP_ACCESS_CONTROL)) {
            throw new RepositoryException("Error while initializing Access Control Provider: Found ac-root to be wrong node type " + acRoot.getPrimaryNodeType().getName());
        }
    } else {
        acRoot = root.addNode(N_ACCESSCONTROL, NT_REP_ACCESS_CONTROL, null);
    }
    editor = new ACLEditor(session, session.getQPath(acRoot.getPath()));
    entriesCache = new EntriesCache(session, editor, acRoot.getPath());
    // TODO: replace by configurable default policy (see JCR-2331)
    if (!configuration.containsKey(PARAM_OMIT_DEFAULT_PERMISSIONS)) {
        try {
            log.debug("Install initial permissions: ...");
            ValueFactory vf = session.getValueFactory();
            Map<String, Value> restrictions = new HashMap<String, Value>();
            restrictions.put(session.getJCRName(ACLTemplate.P_NODE_PATH), vf.createValue(root.getPath(), PropertyType.PATH));
            PrincipalManager pMgr = session.getPrincipalManager();
            AccessControlManager acMgr = session.getAccessControlManager();
            // initial default permissions for the administrators group                
            String pName = SecurityConstants.ADMINISTRATORS_NAME;
            if (pMgr.hasPrincipal(pName)) {
                Principal administrators = pMgr.getPrincipal(pName);
                installDefaultPermissions(administrators, new Privilege[] { acMgr.privilegeFromName(Privilege.JCR_ALL) }, restrictions, editor);
            } else {
                log.info("Administrators principal group is missing -> Not adding default permissions.");
            }
            // initialize default permissions for the everyone group
            installDefaultPermissions(pMgr.getEveryone(), new Privilege[] { acMgr.privilegeFromName(Privilege.JCR_READ) }, restrictions, editor);
            session.save();
        } catch (RepositoryException e) {
            log.error("Failed to set-up minimal access control for root node of workspace " + session.getWorkspace().getName());
            session.getRootNode().refresh(false);
        }
    }
}
Also used : PrincipalManager(org.apache.jackrabbit.api.security.principal.PrincipalManager) AccessControlManager(javax.jcr.security.AccessControlManager) NodeImpl(org.apache.jackrabbit.core.NodeImpl) HashMap(java.util.HashMap) Value(javax.jcr.Value) RepositoryException(javax.jcr.RepositoryException) ValueFactory(javax.jcr.ValueFactory) Principal(java.security.Principal)

Example 44 with ValueFactory

use of javax.jcr.ValueFactory in project jackrabbit by apache.

the class GarbageCollectorTest method testGC.

public void testGC() throws Exception {
    Node root = testRootNode;
    Session session = root.getSession();
    deleteMyNodes();
    runGC(session, true);
    root.addNode("node1");
    Node node2 = root.addNode("node2");
    Node n = node2.addNode("nodeWithBlob").addNode("sub");
    ValueFactory vf = session.getValueFactory();
    Binary b = vf.createBinary(new RandomInputStream(20, 1000));
    n.setProperty("test", b);
    session.save();
    n = node2.addNode("nodeWithTemporaryBlob");
    n.setProperty("test", vf.createBinary(new RandomInputStream(11, 1000)));
    session.save();
    n.remove();
    session.save();
    GarbageCollector gc = ((SessionImpl) session).createDataStoreGarbageCollector();
    gc.getDataStore().clearInUse();
    gc.setPersistenceManagerScan(false);
    gc.setMarkEventListener(this);
    if (gc.getDataStore() instanceof FileDataStore) {
        // make sure the file is old (access time resolution is 2 seconds)
        Thread.sleep(2000);
    }
    LOG.debug("scanning...");
    gc.mark();
    int count = listIdentifiers(gc);
    LOG.debug("stop scanning; currently " + count + " identifiers");
    LOG.debug("deleting...");
    gc.getDataStore().clearInUse();
    assertTrue(gc.sweep() > 0);
    int count2 = listIdentifiers(gc);
    assertEquals(count - 1, count2);
    // verify the node was moved, and that the binary is still there
    n = root.getNode("node1").getNode("nodeWithBlob").getNode("sub");
    b = n.getProperty("test").getValue().getBinary();
    InputStream in = b.getStream();
    InputStream in2 = new RandomInputStream(20, 1000);
    verifyInputStream(in, in2);
    deleteMyNodes();
    gc.close();
}
Also used : InputStream(java.io.InputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) Binary(javax.jcr.Binary) SessionImpl(org.apache.jackrabbit.core.SessionImpl) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector) GarbageCollector(org.apache.jackrabbit.core.gc.GarbageCollector)

Example 45 with ValueFactory

use of javax.jcr.ValueFactory in project jackrabbit by apache.

the class OpenFilesTest method testStreams.

/**
     * Test opening a large number of streams.
     */
public void testStreams() throws RepositoryException {
    Session session = getHelper().getReadWriteSession();
    try {
        Node test = session.getRootNode().addNode("test");
        ValueFactory vf = session.getValueFactory();
        test.setProperty("data", vf.createBinary(new ByteArrayInputStream(new byte[10 * 1024])));
        session.save();
        for (int i = 0; i < 10000; i++) {
            test.getProperty("data").getValue();
        }
    } catch (Exception e) {
        e.printStackTrace();
        assertFalse(e.getMessage(), true);
    } finally {
        session.logout();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Aggregations

ValueFactory (javax.jcr.ValueFactory)105 Value (javax.jcr.Value)51 Node (javax.jcr.Node)50 Session (javax.jcr.Session)40 Test (org.junit.Test)17 RepositoryException (javax.jcr.RepositoryException)16 InputStream (java.io.InputStream)13 AccessControlManager (javax.jcr.security.AccessControlManager)13 HashMap (java.util.HashMap)12 Privilege (javax.jcr.security.Privilege)12 Property (javax.jcr.Property)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Query (javax.jcr.query.Query)8 Calendar (java.util.Calendar)7 QueryManager (javax.jcr.query.QueryManager)7 RowIterator (javax.jcr.query.RowIterator)7 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)7 NodeIterator (javax.jcr.NodeIterator)6 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)6 IOException (java.io.IOException)5