Search in sources :

Example 31 with UpdateRequest

use of org.apache.jena.update.UpdateRequest in project jena by apache.

the class UpdateBuilderExampleTests method example10.

/**
	 * Example 10:
	 * 
	 * @see https://www.w3.org/TR/sparql11-update/#example_10
	 */
@Test
public void example10() {
    Resource book1 = ResourceFactory.createResource("http://example/book1");
    Resource book3 = ResourceFactory.createResource("http://example/book3");
    Resource book4 = ResourceFactory.createResource("http://example/book4");
    Literal d1996 = ResourceFactory.createTypedLiteral("1996-01-01T00:00:00-02:00", XSDDatatype.XSDdateTime);
    Literal d2000 = ResourceFactory.createTypedLiteral("2000-01-01T00:00:00-02:00", XSDDatatype.XSDdateTime);
    Node graphName1 = NodeFactory.createURI("http://example/bookStore");
    Node graphName2 = NodeFactory.createURI("http://example/bookStore2");
    Model m1 = ModelFactory.createDefaultModel();
    m1.add(book1, DC_11.title, "Fundamentals of Compiler Design");
    m1.add(book1, DC_11.date, d1996);
    m1.add(book1, RDF.type, DCTypes.PhysicalObject);
    m1.add(book3, DC_11.title, "SPARQL 1.1 Tutorial");
    Model m2 = ModelFactory.createDefaultModel();
    m2.add(book4, DC_11.title, "SPARQL 1.1 Tutorial");
    Dataset ds = DatasetFactory.create();
    ds.addNamedModel(graphName1.getURI(), m1);
    ds.addNamedModel(graphName2.getURI(), m2);
    ExprFactory factory = new ExprFactory();
    SelectBuilder ins = new SelectBuilder().addGraph(graphName2, new SelectBuilder().addWhere("?book", "?p", "?v"));
    SelectBuilder whr = new SelectBuilder().addGraph(graphName1, new SelectBuilder().addWhere("?book", DC_11.date, "?date").addFilter(factory.lt("?date", d2000)).addWhere("?book", "?p", "?v"));
    UpdateBuilder builder = new UpdateBuilder().addPrefix("dc", DC_11.NS).addPrefix("xsd", XSD.NS).addInsert(ins).addWhere(whr);
    UpdateRequest req = builder.buildRequest();
    builder = new UpdateBuilder().with(graphName1).addDelete("?book", "?p", "?v").addWhere("?book", DC_11.date, "?date").addWhere("?book", RDF.type, DCTypes.PhysicalObject).addFilter(factory.lt("?date", d2000)).addWhere("?book", "?p", "?v");
    builder.appendTo(req);
    UpdateAction.execute(req, ds);
    m1 = ds.getNamedModel(graphName1.getURI());
    assertEquals(1, m1.listStatements().toList().size());
    assertTrue(m1.contains(book3, DC_11.title, "SPARQL 1.1 Tutorial"));
    m2 = ds.getNamedModel(graphName2.getURI());
    assertEquals(4, m2.listStatements().toList().size());
    assertEquals(3, m2.listStatements(book1, null, (RDFNode) null).toList().size());
    assertTrue(m2.contains(book1, DC_11.title, "Fundamentals of Compiler Design"));
    assertTrue(m2.contains(book1, DC_11.date, d1996));
    assertTrue(m2.contains(book1, RDF.type, DCTypes.PhysicalObject));
    assertEquals(1, m2.listStatements(book4, null, (RDFNode) null).toList().size());
    assertTrue(m2.contains(book4, DC_11.title, "SPARQL 1.1 Tutorial"));
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) Dataset(org.apache.jena.query.Dataset) Literal(org.apache.jena.rdf.model.Literal) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) Resource(org.apache.jena.rdf.model.Resource) Model(org.apache.jena.rdf.model.Model) UpdateBuilder(org.apache.jena.arq.querybuilder.UpdateBuilder) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Example 32 with UpdateRequest

use of org.apache.jena.update.UpdateRequest in project jena by apache.

the class TestAuth method update_with_auth_10.

@Test
public void update_with_auth_10() throws URISyntaxException {
    UpdateRequest updates = UpdateFactory.create("CREATE SILENT GRAPH <http://graph>");
    UpdateProcessRemoteBase ue = (UpdateProcessRemoteBase) UpdateExecutionFactory.createRemote(updates, authServiceUpdate);
    // Auth credentials for valid user with correct password scoped to
    // correct URI
    ue.setClient(withBasicAuth(new AuthScope("localhost", authPort), "allowed", "password"));
    ue.execute();
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) AuthScope(org.apache.http.auth.AuthScope) UpdateProcessRemoteBase(org.apache.jena.sparql.modify.UpdateProcessRemoteBase) Test(org.junit.Test)

Example 33 with UpdateRequest

use of org.apache.jena.update.UpdateRequest in project jena by apache.

the class TestAuth method update_with_auth_02.

@Test(expected = HttpException.class)
public void update_with_auth_02() {
    UpdateRequest updates = UpdateFactory.create("CREATE SILENT GRAPH <http://graph>");
    UpdateProcessRemoteBase ue = (UpdateProcessRemoteBase) UpdateExecutionFactory.createRemote(updates, authServiceUpdate);
    // Auth credentials for valid user with bad password
    ue.setClient(withBasicAuth(ANY, "allowed", "incorrect"));
    ue.execute();
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) UpdateProcessRemoteBase(org.apache.jena.sparql.modify.UpdateProcessRemoteBase) Test(org.junit.Test)

Example 34 with UpdateRequest

use of org.apache.jena.update.UpdateRequest in project jena by apache.

the class TestAuth method update_with_auth_06.

@Test(expected = HttpException.class)
public void update_with_auth_06() {
    UpdateRequest updates = UpdateFactory.create("CREATE SILENT GRAPH <http://graph>");
    UpdateProcessRemoteBase ue = (UpdateProcessRemoteBase) UpdateExecutionFactory.createRemoteForm(updates, authServiceUpdate);
    // Auth credentials for valid user with bad password
    ue.setClient(withBasicAuth(ANY, "allowed", "incorrect"));
    ue.execute();
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) UpdateProcessRemoteBase(org.apache.jena.sparql.modify.UpdateProcessRemoteBase) Test(org.junit.Test)

Example 35 with UpdateRequest

use of org.apache.jena.update.UpdateRequest in project jena by apache.

the class TestAuth method update_with_auth_08.

@Test(expected = HttpException.class)
public void update_with_auth_08() {
    UpdateRequest updates = UpdateFactory.create("CREATE SILENT GRAPH <http://graph>");
    UpdateProcessRemoteBase ue = (UpdateProcessRemoteBase) UpdateExecutionFactory.createRemoteForm(updates, authServiceUpdate);
    // Auth credentials for valid user with correct password BUT not in
    // correct role
    ue.setClient(withBasicAuth(ANY, "forbidden", "password"));
    ue.execute();
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) UpdateProcessRemoteBase(org.apache.jena.sparql.modify.UpdateProcessRemoteBase) Test(org.junit.Test)

Aggregations

UpdateRequest (org.apache.jena.update.UpdateRequest)71 Test (org.junit.Test)46 UpdateProcessRemoteBase (org.apache.jena.sparql.modify.UpdateProcessRemoteBase)13 UpdateProcessor (org.apache.jena.update.UpdateProcessor)7 Node (org.apache.jena.graph.Node)6 RDFNode (org.apache.jena.rdf.model.RDFNode)5 HashMap (java.util.HashMap)4 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)4 Resource (org.apache.jena.rdf.model.Resource)4 AuthScope (org.apache.http.auth.AuthScope)3 Dataset (org.apache.jena.query.Dataset)3 QueryParseException (org.apache.jena.query.QueryParseException)3 Model (org.apache.jena.rdf.model.Model)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)2 Map (java.util.Map)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 CmdException (jena.cmd.CmdException)2 HttpClient (org.apache.http.client.HttpClient)2