Search in sources :

Example 1 with MockSecurityEvaluator

use of org.apache.jena.permissions.MockSecurityEvaluator in project jena by apache.

the class DataSetTest method testRestrictedQueryType.

@Test
public void testRestrictedQueryType() {
    eval = new MockSecurityEvaluator(true, true, true, true, true, true) {

        @Override
        public boolean evaluate(final Object principal, final Action action, final Node graphIRI, final Triple triple) {
            if (triple.getSubject().isURI() && triple.getSubject().getURI().equals("http://example.com/resource/1")) {
                return false;
            }
            return super.evaluate(principal, action, graphIRI, triple);
        }

        @Override
        public boolean evaluateAny(Object principal, Set<Action> action, Node graphIRI, Triple triple) {
            if (triple.getSubject().isURI() && triple.getSubject().getURI().equals("http://example.com/resource/1")) {
                return false;
            }
            return super.evaluateAny(principal, action, graphIRI, triple);
        }
    };
    setup();
    try {
        final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#>  " + " SELECT ?foo ?bar WHERE " + " { ?foo a <http://example.com/class> ; " + "?bar [] ." + "  } ";
        try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
            final ResultSet results = qexec.execSelect();
            int count = 0;
            for (; results.hasNext(); ) {
                count++;
                results.nextSolution();
            }
            Assert.assertEquals(4, count);
        }
    } finally {
        dataset.close();
    }
}
Also used : Triple(org.apache.jena.graph.Triple) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) Node(org.apache.jena.graph.Node) Test(org.junit.Test)

Example 2 with MockSecurityEvaluator

use of org.apache.jena.permissions.MockSecurityEvaluator in project jena by apache.

the class QueryEngineTest method testOpenQueryType.

@Test
public void testOpenQueryType() {
    final SecurityEvaluator eval = new MockSecurityEvaluator(true, true, true, true, true, true);
    final SecuredModel model = Factory.getInstance(eval, "http://example.com/securedModel", baseModel);
    try {
        final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#>  " + " SELECT ?foo ?bar WHERE " + " { ?foo a <http://example.com/class> ; " + "?bar [] ." + "  } ";
        try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
            final ResultSet results = qexec.execSelect();
            int count = 0;
            for (; results.hasNext(); ) {
                count++;
                results.nextSolution();
            }
            Assert.assertEquals(8, count);
        }
    } finally {
        model.close();
    }
}
Also used : MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) ResultSet(org.apache.jena.query.ResultSet) SecurityEvaluator(org.apache.jena.permissions.SecurityEvaluator) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) SecuredModel(org.apache.jena.permissions.model.SecuredModel) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 3 with MockSecurityEvaluator

use of org.apache.jena.permissions.MockSecurityEvaluator in project jena by apache.

the class QueryEngineTest method testRestrictedQueryType.

@Test
public void testRestrictedQueryType() {
    final SecurityEvaluator eval = new MockSecurityEvaluator(true, true, true, true, true, true) {

        @Override
        public boolean evaluate(final Object principal, final Action action, final Node graphIRI, final Triple triple) {
            if (triple.getSubject().isURI() && triple.getSubject().getURI().equals("http://example.com/resource/1")) {
                return false;
            }
            return super.evaluate(principal, action, graphIRI, triple);
        }
    };
    final SecuredModel model = Factory.getInstance(eval, "http://example.com/securedModel", baseModel);
    try {
        final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#>  " + " SELECT ?foo ?bar WHERE " + " { ?foo a <http://example.com/class> ; " + "?bar [] ." + "  } ";
        try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
            final ResultSet results = qexec.execSelect();
            int count = 0;
            for (; results.hasNext(); ) {
                count++;
                results.nextSolution();
            }
            Assert.assertEquals(4, count);
        }
    } finally {
        model.close();
    }
}
Also used : Triple(org.apache.jena.graph.Triple) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) Node(org.apache.jena.graph.Node) ResultSet(org.apache.jena.query.ResultSet) SecurityEvaluator(org.apache.jena.permissions.SecurityEvaluator) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) SecuredModel(org.apache.jena.permissions.model.SecuredModel) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 4 with MockSecurityEvaluator

use of org.apache.jena.permissions.MockSecurityEvaluator in project jena by apache.

the class OpRewriterTest method testBGP.

@Test
public void testBGP() {
    SecurityEvaluator securityEvaluator = new MockSecurityEvaluator(true, true, true, true, true, true);
    rewriter = new OpRewriter(securityEvaluator, "http://example.com/dummy");
    rewriter.visit(new OpBGP(BasicPattern.wrap(Arrays.asList(triples))));
    Op op = rewriter.getResult();
    Assert.assertTrue("Should have been an OpFilter", op instanceof OpFilter);
    OpFilter filter = (OpFilter) op;
    ExprList eLst = filter.getExprs();
    Assert.assertEquals(1, eLst.size());
    Assert.assertTrue("Should have been a SecuredFunction", eLst.get(0) instanceof SecuredFunction);
    op = filter.getSubOp();
    Assert.assertTrue("Should have been a OpBGP", op instanceof OpBGP);
    BasicPattern basicPattern = ((OpBGP) op).getPattern();
    Assert.assertEquals(3, basicPattern.size());
    Triple t = basicPattern.get(0);
    Assert.assertEquals(NodeFactory.createVariable("foo"), t.getSubject());
    Assert.assertEquals(RDF.type.asNode(), t.getPredicate());
    Assert.assertEquals(NodeFactory.createURI("http://example.com/class"), t.getObject());
    t = basicPattern.get(1);
    Assert.assertEquals(NodeFactory.createVariable("foo"), t.getSubject());
    Assert.assertTrue("Should have been blank", t.getPredicate().isBlank());
    Assert.assertEquals(NodeFactory.createVariable("bar"), t.getObject());
    t = basicPattern.get(2);
    Assert.assertEquals(NodeFactory.createVariable("bar"), t.getSubject());
    Assert.assertTrue("Should have been blank", t.getPredicate().isBlank());
    Assert.assertEquals(NodeFactory.createVariable("baz"), t.getObject());
}
Also used : Triple(org.apache.jena.graph.Triple) Op(org.apache.jena.sparql.algebra.Op) SecuredFunction(org.apache.jena.permissions.query.rewriter.SecuredFunction) ExprList(org.apache.jena.sparql.expr.ExprList) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) SecurityEvaluator(org.apache.jena.permissions.SecurityEvaluator) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) BasicPattern(org.apache.jena.sparql.core.BasicPattern) OpFilter(org.apache.jena.sparql.algebra.op.OpFilter) OpRewriter(org.apache.jena.permissions.query.rewriter.OpRewriter) Test(org.junit.Test)

Example 5 with MockSecurityEvaluator

use of org.apache.jena.permissions.MockSecurityEvaluator in project jena by apache.

the class DataSetTest method testOpenQueryType.

@Test
public void testOpenQueryType() {
    eval = new MockSecurityEvaluator(true, true, true, true, true, true);
    setup();
    try {
        final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#>  " + " SELECT ?foo ?bar WHERE " + " { ?foo a <http://example.com/class> ; " + "?bar [] ." + "  } ";
        try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
            final ResultSet results = qexec.execSelect();
            int count = 0;
            for (; results.hasNext(); ) {
                count++;
                results.nextSolution();
            }
            Assert.assertEquals(8, count);
        }
    } finally {
        dataset.close();
    }
}
Also used : MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) Test(org.junit.Test)

Aggregations

MockSecurityEvaluator (org.apache.jena.permissions.MockSecurityEvaluator)8 Test (org.junit.Test)8 Triple (org.apache.jena.graph.Triple)6 SecurityEvaluator (org.apache.jena.permissions.SecurityEvaluator)5 Node (org.apache.jena.graph.Node)4 SecuredModel (org.apache.jena.permissions.model.SecuredModel)3 QueryExecution (org.apache.jena.query.QueryExecution)3 ResultSet (org.apache.jena.query.ResultSet)3 OpRewriter (org.apache.jena.permissions.query.rewriter.OpRewriter)2 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)2 SecuredFunction (org.apache.jena.permissions.query.rewriter.SecuredFunction)1 ReadDeniedException (org.apache.jena.shared.ReadDeniedException)1 Op (org.apache.jena.sparql.algebra.Op)1 OpFilter (org.apache.jena.sparql.algebra.op.OpFilter)1 BasicPattern (org.apache.jena.sparql.core.BasicPattern)1 ExprList (org.apache.jena.sparql.expr.ExprList)1