Search in sources :

Example 26 with ValueFactory

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

the class RepositoryTest method testReferenceBinary.

@Test
public void testReferenceBinary() throws RepositoryException {
    ValueFactory valueFactory = getAdminSession().getValueFactory();
    Binary binary = valueFactory.createBinary(new RandomInputStream(1, 256 * 1024));
    String reference = binary instanceof ReferenceBinary ? ((ReferenceBinary) binary).getReference() : null;
    assumeTrue(reference != null);
    Session session = createAdminSession();
    try {
        valueFactory = session.getValueFactory();
        assertEquals(binary, valueFactory.createValue(new SimpleReferenceBinary(reference)).getBinary());
    } finally {
        session.logout();
    }
}
Also used : SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ValueFactory(javax.jcr.ValueFactory) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Binary(javax.jcr.Binary) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) Session(javax.jcr.Session) Test(org.junit.Test)

Example 27 with ValueFactory

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

the class TestContentLoader method addNodeTestData.

/**
     * Creates three nodes under the given node: one of type nt:resource and the
     * other nodes referencing it.
     */
private static void addNodeTestData(Node node) throws RepositoryException, IOException {
    if (node.hasNode("multiReference")) {
        node.getNode("multiReference").remove();
    }
    if (node.hasNode("resReference")) {
        node.getNode("resReference").remove();
    }
    if (node.hasNode("myResource")) {
        node.getNode("myResource").remove();
    }
    Node resource = node.addNode("myResource", "nt:resource");
    // nt:resource not longer referenceable since JCR 2.0
    resource.addMixin("mix:referenceable");
    resource.setProperty("jcr:encoding", ENCODING);
    resource.setProperty("jcr:mimeType", "text/plain");
    resource.setProperty("jcr:data", "Hello wörld.", PropertyType.BINARY);
    resource.setProperty("jcr:lastModified", Calendar.getInstance());
    Node resReference = getOrAddNode(node, "reference");
    resReference.setProperty("ref", resource);
    // make this node itself referenceable
    resReference.addMixin("mix:referenceable");
    Node multiReference = node.addNode("multiReference");
    ValueFactory factory = node.getSession().getValueFactory();
    multiReference.setProperty("ref", new Value[] { factory.createValue(resource), factory.createValue(resReference) });
    // NodeDefTest requires a test node with a mandatory child node
    JcrUtils.putFile(node, "testFile", "text/plain", new ByteArrayInputStream("Hello, World!".getBytes("UTF-8")));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory)

Example 28 with ValueFactory

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

the class TestContentLoader method addPropertyTestData.

/**
     * Creates a boolean, double, long, calendar and a path property at the
     * given node.
     */
private static void addPropertyTestData(Node node) throws RepositoryException {
    node.setProperty("boolean", true);
    node.setProperty("double", Math.PI);
    node.setProperty("long", 90834953485278298L);
    Calendar c = Calendar.getInstance();
    c.set(2005, 6, 18, 17, 30);
    node.setProperty("calendar", c);
    ValueFactory factory = node.getSession().getValueFactory();
    node.setProperty("path", factory.createValue("/", PropertyType.PATH));
    node.setProperty("multi", new String[] { "one", "two", "three" });
}
Also used : Calendar(java.util.Calendar) ValueFactory(javax.jcr.ValueFactory)

Example 29 with ValueFactory

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

the class ACLTest method testMvRestrictions.

@Test
public void testMvRestrictions() throws Exception {
    ValueFactory vf = getValueFactory();
    Value[] vs = new Value[] { vf.createValue(JcrConstants.NT_FILE, PropertyType.NAME), vf.createValue(JcrConstants.NT_FOLDER, PropertyType.NAME) };
    Map<String, Value[]> mvRestrictions = Collections.singletonMap(REP_NT_NAMES, vs);
    Map<String, Value> restrictions = Collections.singletonMap(REP_GLOB, vf.createValue("/.*"));
    assertTrue(acl.addEntry(testPrincipal, testPrivileges, false, restrictions, mvRestrictions));
    assertFalse(acl.addEntry(testPrincipal, testPrivileges, false, restrictions, mvRestrictions));
    assertEquals(1, acl.getAccessControlEntries().length);
    JackrabbitAccessControlEntry ace = (JackrabbitAccessControlEntry) acl.getAccessControlEntries()[0];
    try {
        ace.getRestriction(REP_NT_NAMES);
        fail();
    } catch (ValueFormatException e) {
    // success
    }
    Value[] vvs = ace.getRestrictions(REP_NT_NAMES);
    assertArrayEquals(vs, vvs);
}
Also used : JackrabbitAccessControlEntry(org.apache.jackrabbit.api.security.JackrabbitAccessControlEntry) Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException) ValueFactory(javax.jcr.ValueFactory) Test(org.junit.Test)

Example 30 with ValueFactory

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

the class ACLEditor method setPolicy.

/**
     * @see AccessControlEditor#setPolicy(String,AccessControlPolicy)
     */
public void setPolicy(String nodePath, AccessControlPolicy policy) throws AccessControlException, PathNotFoundException, RepositoryException {
    checkProtectsNode(nodePath);
    checkValidPolicy(nodePath, policy);
    ACLTemplate acl = (ACLTemplate) policy;
    NodeImpl acNode = getAcNode(nodePath);
    if (acNode == null) {
        throw new PathNotFoundException("No such node " + nodePath);
    }
    // write the entries to the node
    NodeImpl aclNode;
    if (acNode.hasNode(N_POLICY)) {
        aclNode = acNode.getNode(N_POLICY);
        // remove all existing aces
        for (NodeIterator aceNodes = aclNode.getNodes(); aceNodes.hasNext(); ) {
            NodeImpl aceNode = (NodeImpl) aceNodes.nextNode();
            removeItem(aceNode);
        }
    } else {
        /* doesn't exist yet -> create */
        aclNode = addNode(acNode, N_POLICY, NT_REP_ACL);
    }
    /* add all new entries defined on the template */
    AccessControlEntry[] aces = acl.getAccessControlEntries();
    for (AccessControlEntry ace1 : aces) {
        AccessControlEntryImpl ace = (AccessControlEntryImpl) ace1;
        // create the ACE node
        Name nodeName = getUniqueNodeName(aclNode, "entry");
        Name ntName = (ace.isAllow()) ? NT_REP_GRANT_ACE : NT_REP_DENY_ACE;
        NodeImpl aceNode = addNode(aclNode, nodeName, ntName);
        ValueFactory vf = session.getValueFactory();
        // write the rep:principalName property
        setProperty(aceNode, P_PRINCIPAL_NAME, vf.createValue(ace.getPrincipal().getName()));
        // ... and the rep:privileges property
        Privilege[] privs = ace.getPrivileges();
        Value[] vs = new Value[privs.length];
        for (int j = 0; j < privs.length; j++) {
            vs[j] = vf.createValue(privs[j].getName(), PropertyType.NAME);
        }
        setProperty(aceNode, P_PRIVILEGES, vs);
        // store the restrictions:
        Set<Name> restrNames = ace.getRestrictions().keySet();
        for (Name restrName : restrNames) {
            Value value = ace.getRestriction(restrName);
            setProperty(aceNode, restrName, value);
        }
    }
    // mark the parent modified.
    markModified((NodeImpl) aclNode.getParent());
}
Also used : NodeIterator(javax.jcr.NodeIterator) AccessControlEntryImpl(org.apache.jackrabbit.core.security.authorization.AccessControlEntryImpl) NodeImpl(org.apache.jackrabbit.core.NodeImpl) AccessControlEntry(javax.jcr.security.AccessControlEntry) ValueFactory(javax.jcr.ValueFactory) Name(org.apache.jackrabbit.spi.Name) Value(javax.jcr.Value) PathNotFoundException(javax.jcr.PathNotFoundException) Privilege(javax.jcr.security.Privilege)

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