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();
}
}
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")));
}
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" });
}
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);
}
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());
}
Aggregations