use of javax.jcr.ValueFactory in project jackrabbit by apache.
the class SearchResourceImpl method queryResultToMultiStatus.
/**
* Build a <code>MultiStatus</code> object from the specified query result.
*
* @param query the query to execute.
* @return <code>MultiStatus</code> object listing the query result in
* Webdav compatible form.
* @throws RepositoryException if an error occurs.
*/
private void queryResultToMultiStatus(QueryResult result, MultiStatus ms) throws RepositoryException {
List<String> columnNames = new ArrayList<String>();
ValueFactory vf = getRepositorySession().getValueFactory();
List<RowValue> descr = new ArrayList<RowValue>();
for (String columnName : result.getColumnNames()) {
if (!isPathOrScore(columnName)) {
columnNames.add(columnName);
descr.add(new PlainValue(columnName, null, vf));
}
}
// add path and score for each selector
String[] sns = result.getSelectorNames();
boolean join = sns.length > 1;
for (String selectorName : sns) {
descr.add(new PathValue(JcrConstants.JCR_PATH, selectorName, vf));
columnNames.add(JcrConstants.JCR_PATH);
descr.add(new ScoreValue(JcrConstants.JCR_SCORE, selectorName, vf));
columnNames.add(JcrConstants.JCR_SCORE);
}
int n = 0;
String root = getHref("/");
String[] selectorNames = createSelectorNames(descr);
String[] colNames = columnNames.toArray(new String[columnNames.size()]);
RowIterator rowIter = result.getRows();
while (rowIter.hasNext()) {
Row row = rowIter.nextRow();
List<Value> values = new ArrayList<Value>();
for (RowValue rv : descr) {
values.add(rv.getValue(row));
}
// create a new ms-response for this row of the result set
String href;
if (join) {
// We need a distinct href for each join result row to
// allow the MultiStatus response to keep them separate
href = root + "?" + n++;
} else {
href = getHref(row.getPath());
}
MultiStatusResponse resp = new MultiStatusResponse(href, null);
// build the s-r-property
SearchResultProperty srp = new SearchResultProperty(colNames, selectorNames, values.toArray(new Value[values.size()]));
resp.add(srp);
ms.addResponse(resp);
}
}
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());
}
use of javax.jcr.ValueFactory in project jackrabbit by apache.
the class ReferencesTest method testMisdirectedBackRefMultiValued.
/**
* Variant of {@link #testMisdirectedBackRef()} for multi-valued props.
*
* @throws Exception on test error
*/
public void testMisdirectedBackRefMultiValued() throws Exception {
Session session2 = createSession();
ValueFactory valFac2 = session2.getValueFactory();
Node ases2 = getTestRootNode(session2).getNode("A");
getTestRootNode(session2).getNode("C").setProperty("ref", new Value[] { valFac2.createValue(ases2), valFac2.createValue(ases2) });
Session session3 = createSession();
ValueFactory valFac3 = session3.getValueFactory();
Node bses3 = getTestRootNode(session3).getNode("A").getNode("B");
getTestRootNode(session3).getNode("C").setProperty("ref", new Value[] { valFac3.createValue(bses3) });
saveAndlogout(session2, session3);
assertRemoveTestNodes();
}
use of javax.jcr.ValueFactory in project jackrabbit by apache.
the class AbstractNode method setProperty.
/**
* Sets the value of the named property.
* <p>
* The default implementation uses the {@link ValueFactory} of the
* current {@link Session} to convert the given value to the given
* type and forwards the call to the
* {@link Node#setProperty(String, Value)} method.
*
* @param name property name
* @param value property value
* @param type property type
* @return modified property
* @throws RepositoryException if an error occurs
*/
public Property setProperty(String name, Value value, int type) throws RepositoryException {
if (value.getType() != type) {
ValueFactory factory = getSession().getValueFactory();
value = factory.createValue(value.getString(), type);
}
return setProperty(name, value);
}
Aggregations