/** * Append operations from src to tgt * @param tgt * @param src * @return */ public static UpdateRequest append(UpdateRequest tgt, UpdateRequest src) { src.getOperations().forEach(tgt::add); return tgt; }
public static void applyDatasetDescription(UpdateRequest updateRequest, DatasetDescription dg) { for(Update update : updateRequest.getOperations()) { UpdateUtils.applyDatasetDescriptionIfApplicable(update, dg); } }
public static void applyWithIri(UpdateRequest updateRequest, String withIri) { for(Update update : updateRequest.getOperations()) { UpdateUtils.applyWithIriIfApplicable(update, withIri); } }
public static boolean isomorphic(UpdateRequest req1, UpdateRequest req2) { if ( req1 == req2 ) return true ; if ( ! req1.samePrologue(req2) ) return false ; List<Update> updates1 = req1.getOperations() ; List<Update> updates2 = req2.getOperations() ; if ( updates1.size() != updates2.size() ) return false ; NodeIsomorphismMap isomap = new NodeIsomorphismMap() ; for ( int i = 0 ; i < updates1.size() ; i++ ) { Update upd1 = updates1.get(i) ; Update upd2 = updates2.get(i) ; if ( !isomorphic(upd1, upd2, isomap) ) return false ; } return true ; }
public void executeUpdate(String queryStr) { UpdateRequest request = new UpdateRequest(); UpdateFactory.parse(request, queryStr); for(Update update : request.getOperations()) { MultiMethod.invoke(this, "_update", update); } } }
public static void executeUpdate(SparqlService sparqlService, UpdateRequest request, int batchSize, QuadContainmentChecker containmentChecker, Iterable<DatasetListener> listeners) { //Function<Diff<? extends Iterable<Quad>>, Diff<Set<Quad>>> filter) { QueryExecutionFactory qef = sparqlService.getQueryExecutionFactory(); // UpdateExecutionFactory uef = sparqlService.getUpdateExecutionFactory(); Function<Diff<? extends Iterable<Quad>>, Diff<Set<Quad>>> filter = new FN_QuadDiffUnique(qef, containmentChecker); //SparqlServiceReference ssr = sparqlService.getDatasetDescription(); for(Update update : request.getOperations()) { executeUpdateCore(sparqlService, update, filter, batchSize, listeners); } }
public static UpdateRequest transform(UpdateRequest update, ElementTransform transform, ExprTransform exprTransform) { UpdateRequest req = new UpdateRequest() ; req.getPrefixMapping().setNsPrefixes(update.getPrefixMapping()) ; for (Update up : update.getOperations()) { up = transform(up, transform, exprTransform) ; req.add(up) ; } return req ; }
public static UpdateProcessor executeUnlessEmpty(UpdateExecutionFactory uef, UpdateRequest updateRequest) { UpdateProcessor result; if(updateRequest.getOperations().isEmpty()) { // Create a fake update request UpdateRequest update = UpdateFactory.create("PREFIX ex: <http://example.org/> INSERT { ex:_s ex:_p ex:_o } WHERE { ex:_s ex:_p ex:_o }"); result = org.apache.jena.update.UpdateExecutionFactory.create(update, DatasetGraphFactory.createGeneral()); result.execute(); } else { result = uef.createUpdateProcessor(updateRequest); try { result.execute(); } catch(Exception e) { RuntimeException f = HttpExceptionUtils.makeHumanFriendly(e); throw f; } } return result; }
public static UpdateRequest transform(UpdateRequest update, ElementTransform transform, ExprTransform exprTransform) { UpdateRequest req = new UpdateRequest() ; req.getPrefixMapping().setNsPrefixes(update.getPrefixMapping()) ; for (Update up : update.getOperations()) { up = transform(up, transform, exprTransform) ; req.add(up) ; } return req ; }
public static void fixVarNames(UpdateRequest updateRequest) { List<Update> updates = updateRequest.getOperations(); for(Update update : updates) { if(update instanceof UpdateDeleteInsert) { UpdateDeleteInsert x = (UpdateDeleteInsert)update; Element before = x.getWherePattern(); Element after = ElementUtils.fixVarNames(before); x.setElement(after); } else if(update instanceof UpdateModify) { UpdateModify x = (UpdateModify)update; Element before = x.getWherePattern(); Element after = ElementUtils.fixVarNames(before); x.setElement(after); } } }
@Test public void bNodeSPARQL_Update_1() { String str = "INSERT DATA { <x:s> <x:p> <_:789> }"; UpdateRequest req = UpdateFactory.create(str); Update update = req.getOperations().get(0); UpdateDataInsert ins = (UpdateDataInsert)update; Node obj = ins.getQuads().get(0).getObject(); assertEquals("789", obj.getBlankNodeLabel()); } }
@Test public void test_param_string_injection_14() { // This is a variant on placing a variable bound to a literal inside a // literal resulting in an injection, we now escape ' so prevent this String str = "PREFIX : <http://example/>\nINSERT DATA { <s> <p> 'some text ?var other text' }"; ParameterizedSparqlString pss = new ParameterizedSparqlString(str); pss.setLiteral("var", "' . } ; DROP ALL ; INSERT DATA { <s> <p> <o> }#"); UpdateRequest updates = pss.asUpdate(); Assert.assertEquals(1, updates.getOperations().size()); }
@Test public void test_param_string_injection_03() { // This injection attempt results in a valid update but a failed // injection String str = "PREFIX : <http://example/>\nINSERT DATA { <s> <p> ?var2 . }"; ParameterizedSparqlString pss = new ParameterizedSparqlString(str); pss.setLiteral("var2", "hello\" } ; DROP ALL ; INSERT DATA { <s> <p> <goodbye>"); UpdateRequest updates = pss.asUpdate(); Assert.assertEquals(1, updates.getOperations().size()); }
@Test public void test_param_string_injection_09() { // This injection attempt using comments results in a valid SPARQL // update but a failed injection because the attempt to use comments // ends up being a valid string literal within quotes String str = "PREFIX : <http://example/>\nINSERT DATA { <s> <p> ?var }"; ParameterizedSparqlString pss = new ParameterizedSparqlString(str); pss.setLiteral("var", "\" . } ; DROP ALL ; INSERT DATA { <s> <p> <o> }#"); UpdateRequest updates = pss.asUpdate(); Assert.assertEquals(1, updates.getOperations().size()); }
@Test public void test_param_string_positional_injection_03() { // This injection attempt results in a valid update but a failed // injection String str = "PREFIX : <http://example/>\nINSERT DATA { <s> <p> ? . }"; ParameterizedSparqlString pss = new ParameterizedSparqlString(str); pss.setLiteral(0, "hello\" } ; DROP ALL ; INSERT DATA { <s> <p> <goodbye>"); UpdateRequest updates = pss.asUpdate(); Assert.assertEquals(1, updates.getOperations().size()); }
@Test public void test_param_string_positional_injection_14() { // This is a variant on placing a variable bound to a literal inside a // literal resulting in an injection, we now escape ' so prevent this String str = "PREFIX : <http://example/>\nINSERT DATA { <s> <p> 'some text ? other text' }"; ParameterizedSparqlString pss = new ParameterizedSparqlString(str); pss.setLiteral(0, "' . } ; DROP ALL ; INSERT DATA { <s> <p> <o> }#"); UpdateRequest updates = pss.asUpdate(); Assert.assertEquals(1, updates.getOperations().size()); }
@Test public void test_param_string_injection_13() { // This is a variant on placing a variable bound to a literal inside a // literal resulting in an injection, we now escape ' so prevent this String str = "PREFIX : <http://example/>\nINSERT DATA { <s> <p> ' ?var ' }"; ParameterizedSparqlString pss = new ParameterizedSparqlString(str); pss.setLiteral("var", "' . } ; DROP ALL ; INSERT DATA { <s> <p> <o> }#"); UpdateRequest updates = pss.asUpdate(); Assert.assertEquals(1, updates.getOperations().size()); }
@Test public void test_param_string_positional_injection_09() { // This injection attempt using comments results in a valid SPARQL // update but a failed injection because the attempt to use comments // ends up being a valid string literal within quotes String str = "PREFIX : <http://example/>\nINSERT DATA { <s> <p> ? }"; ParameterizedSparqlString pss = new ParameterizedSparqlString(str); pss.setLiteral(0, "\" . } ; DROP ALL ; INSERT DATA { <s> <p> <o> }#"); UpdateRequest updates = pss.asUpdate(); Assert.assertEquals(1, updates.getOperations().size()); }
@Test public void test_param_string_positional_injection_13() { // This is a variant on placing a variable bound to a literal inside a // literal resulting in an injection, we now escape ' so prevent this String str = "PREFIX : <http://example/>\nINSERT DATA { <s> <p> ' ? ' }"; ParameterizedSparqlString pss = new ParameterizedSparqlString(str); pss.setLiteral(0, "' . } ; DROP ALL ; INSERT DATA { <s> <p> <o> }#"); UpdateRequest updates = pss.asUpdate(); Assert.assertEquals(1, updates.getOperations().size()); }
public static UpdateRequest clone(UpdateRequest request) { UpdateRequest result = new UpdateRequest(); result.setBaseURI(request.getBaseURI()); result.setPrefixMapping(request.getPrefixMapping()); result.setResolver(request.getResolver()); for(Update update : request.getOperations()) { Update clone = UpdateUtils.clone(update); result.add(clone); } return result; }