use of javax.jcr.query.QueryManager in project jackrabbit by apache.
the class ACLProvider method getEffectivePolicies.
/**
* @see org.apache.jackrabbit.core.security.authorization.AccessControlProvider#getEffectivePolicies(java.util.Set, CompiledPermissions)
*/
public AccessControlPolicy[] getEffectivePolicies(Set<Principal> principals, CompiledPermissions permissions) throws RepositoryException {
String propName = ISO9075.encode(session.getJCRName(P_PRINCIPAL_NAME));
StringBuilder stmt = new StringBuilder("/jcr:root");
stmt.append("//element(*,");
stmt.append(session.getJCRName(NT_REP_ACE));
stmt.append(")[");
int i = 0;
for (Principal principal : principals) {
if (i > 0) {
stmt.append(" or ");
}
stmt.append("@");
stmt.append(propName);
stmt.append("='");
stmt.append(principal.getName().replaceAll("'", "''"));
stmt.append("'");
i++;
}
stmt.append("]");
QueryResult result;
try {
QueryManager qm = session.getWorkspace().getQueryManager();
Query q = qm.createQuery(stmt.toString(), Query.XPATH);
result = q.execute();
} catch (RepositoryException e) {
log.error("Unexpected error while searching effective policies. {}", e.getMessage());
throw new UnsupportedOperationException("Retrieve effective policies for set of principals not supported.", e);
}
Set<AccessControlPolicy> acls = new LinkedHashSet<AccessControlPolicy>();
for (NodeIterator it = result.getNodes(); it.hasNext(); ) {
NodeImpl aclNode = (NodeImpl) it.nextNode().getParent();
Name aclName = aclNode.getQName();
NodeImpl accessControlledNode = (NodeImpl) aclNode.getParent();
if (N_POLICY.equals(aclName) && isAccessControlled(accessControlledNode)) {
if (permissions.canRead(aclNode.getPrimaryPath(), aclNode.getNodeId())) {
acls.add(getACL(accessControlledNode, N_POLICY, accessControlledNode.getPath()));
} else {
throw new AccessDeniedException("Access denied at " + Text.getRelativeParent(aclNode.getPath(), 1));
}
} else if (N_REPO_POLICY.equals(aclName) && isRepoAccessControlled(accessControlledNode)) {
if (permissions.canRead(aclNode.getPrimaryPath(), aclNode.getNodeId())) {
acls.add(getACL(accessControlledNode, N_REPO_POLICY, null));
} else {
throw new AccessDeniedException("Access denied at " + Text.getRelativeParent(aclNode.getPath(), 1));
}
}
// else: not a regular policy node -> ignore.
}
return acls.toArray(new AccessControlPolicy[acls.size()]);
}
use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.
the class QueryTest method testOak1096.
@Test
public void testOak1096() throws RepositoryException {
Session writer = createAdminSession();
Session reader = createAdminSession();
try {
Node rootNode = writer.getRootNode();
Node node = rootNode.addNode("test", "nt:unstructured");
node.setProperty("text", "find me");
writer.save();
QueryManager qm = reader.getWorkspace().getQueryManager();
Query q = qm.createQuery("select * from 'nt:base' where contains(*, 'find me')", Query.JCR_SQL2);
NodeIterator res = q.execute().getNodes();
assertEquals("False amount of hits", 1, res.getSize());
} finally {
if (reader != null) {
reader.logout();
}
if (writer != null) {
writer.logout();
}
}
}
use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.
the class QueryTest method traversalOption.
@Test
public void traversalOption() throws Exception {
Session session = getAdminSession();
QueryManager qm = session.getWorkspace().getQueryManager();
// for union queries:
// both subqueries use an index
assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where ischildnode('/') or [jcr:uuid] = 1 option(traversal fail)"));
// no subquery uses an index
assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where [x] = 1 or [y] = 2 option(traversal fail)"));
// first one does not, second one does
assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where [jcr:uuid] = 1 or [x] = 2 option(traversal fail)"));
// first one does, second one does not
assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where [x] = 2 or [jcr:uuid] = 1 option(traversal fail)"));
// queries that possibly use traversal (depending on the join order)
assertTrue(isValidQuery(qm, "xpath", "/jcr:root/content//*/jcr:content[@jcr:uuid='1'] option(traversal fail)"));
assertTrue(isValidQuery(qm, "xpath", "/jcr:root/content/*/jcr:content[@jcr:uuid='1'] option(traversal fail)"));
assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] as [a] inner join [nt:base] as [b] on ischildnode(b, a) " + "where [a].[jcr:uuid] = 1 option(traversal fail)"));
assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] as [a] inner join [nt:base] as [b] on ischildnode(a, b) " + "where [a].[jcr:uuid] = 1 option(traversal fail)"));
// union with joins
assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] as [a] inner join [nt:base] as [b] on ischildnode(a, b) " + "where ischildnode([a], '/') or [a].[jcr:uuid] = 1 option(traversal fail)"));
assertFalse(isValidQuery(qm, "xpath", "//*[@test] option(traversal fail)"));
assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] option(traversal fail)"));
assertTrue(isValidQuery(qm, "xpath", "//*[@test] option(traversal ok)"));
assertTrue(isValidQuery(qm, "xpath", "//*[@test] option(traversal warn)"));
assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] option(traversal ok)"));
assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] option(traversal warn)"));
// the following is not really traversal, it is just listing child nodes:
assertTrue(isValidQuery(qm, "xpath", "/jcr:root/*[@test] option(traversal fail)"));
// the following is not really traversal; it is just one node:
assertTrue(isValidQuery(qm, "xpath", "/jcr:root/oak:index[@test] option(traversal fail)"));
}
use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.
the class QueryTest method skip.
@Test
public void skip() throws RepositoryException {
Session session = getAdminSession();
Node hello1 = session.getRootNode().addNode("hello1");
hello1.setProperty("id", "1");
hello1.setProperty("data", "x");
session.save();
Node hello3 = hello1.addNode("hello3");
hello3.setProperty("id", "3");
hello3.setProperty("data", "z");
session.save();
Node hello2 = hello3.addNode("hello2");
hello2.setProperty("id", "2");
hello2.setProperty("data", "y");
session.save();
ValueFactory vf = session.getValueFactory();
QueryManager qm = session.getWorkspace().getQueryManager();
Query q = qm.createQuery("select id from [nt:base] where data >= $data order by id", Query.JCR_SQL2);
q.bindValue("data", vf.createValue("x"));
for (int i = -1; i < 5; i++) {
QueryResult r = q.execute();
RowIterator it = r.getRows();
assertEquals(3, r.getRows().getSize());
assertEquals(3, r.getNodes().getSize());
Row row;
try {
it.skip(i);
assertTrue(i >= 0 && i <= 3);
} catch (IllegalArgumentException e) {
assertEquals(-1, i);
} catch (NoSuchElementException e) {
assertTrue(i >= 2);
}
if (i <= 0) {
assertTrue(it.hasNext());
row = it.nextRow();
assertEquals("1", row.getValue("id").getString());
}
if (i <= 1) {
assertTrue(it.hasNext());
row = it.nextRow();
assertEquals("2", row.getValue("id").getString());
}
if (i <= 2) {
assertTrue(it.hasNext());
row = it.nextRow();
assertEquals("3", row.getValue("id").getString());
}
assertFalse(it.hasNext());
}
}
use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.
the class QueryFulltextTest method excerpt.
@Test
public void excerpt() throws Exception {
Session session = getAdminSession();
QueryManager qm = session.getWorkspace().getQueryManager();
Node testRootNode = session.getRootNode().addNode("testroot");
Node n1 = testRootNode.addNode("node1");
n1.setProperty("text", "hello world");
n1.setProperty("desc", "description");
Node n2 = testRootNode.addNode("node2");
n2.setProperty("text", "Hello World");
n2.setProperty("desc", "Description");
session.save();
Query q;
RowIterator it;
Row row;
String s;
String xpath = "//*[jcr:contains(., 'hello')]/rep:excerpt(.) order by @jcr:path";
q = qm.createQuery(xpath, "xpath");
it = q.execute().getRows();
row = it.nextRow();
String path = row.getPath();
s = row.getValue("rep:excerpt(.)").getString();
assertTrue(path + ":" + s + " (1)", s.indexOf("<strong>hello</strong> world") >= 0);
assertTrue(path + ":" + s + " (2)", s.indexOf("description") >= 0);
row = it.nextRow();
path = row.getPath();
s = row.getValue("rep:excerpt(.)").getString();
// TODO is this expected?
assertTrue(path + ":" + s + " (3)", s.indexOf("Hello World") >= 0);
assertTrue(path + ":" + s + " (4)", s.indexOf("Description") >= 0);
xpath = "//*[jcr:contains(., 'hello')]/rep:excerpt(.) order by @jcr:path";
q = qm.createQuery(xpath, "xpath");
it = q.execute().getRows();
row = it.nextRow();
path = row.getPath();
s = row.getValue("rep:excerpt(text)").getString();
assertTrue(path + ":" + s + " (5)", s.indexOf("<strong>hello</strong> world") >= 0);
assertTrue(path + ":" + s + " (6)", s.indexOf("description") < 0);
row = it.nextRow();
path = row.getPath();
s = row.getValue("rep:excerpt(text)").getString();
// TODO is this expected?
assertTrue(path + ":" + s + " (7)", s.indexOf("Hello World") >= 0);
assertTrue(path + ":" + s + " (8)", s.indexOf("Description") < 0);
}
Aggregations