Search in sources :

Example 1 with UpdateRequest

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

the class sdbupdate method execOne.

private void execOne(String requestString, Dataset store) {
    UpdateRequest req = UpdateFactory.create(requestString, Syntax.syntaxARQ);
    UpdateExecutionFactory.create(req, store).execute();
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest)

Example 2 with UpdateRequest

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

the class UpdateTest method runTestForReal.

@Override
protected void runTestForReal() {
    try {
        UpdateRequest request = UpdateFactory.read(updateFile, Syntax.syntaxSPARQL_11);
        UpdateAction.execute(request, input);
        boolean b = datasetSame(input, output, false);
        if (!b) {
            System.out.println("---- " + getName());
            System.out.println("---- Got: ");
            System.out.println(input.asDatasetGraph());
            System.out.println("---- Expected");
            System.out.println(output.asDatasetGraph());
            datasetSame(input, output, true);
            System.out.println("----------------------------------------");
        }
        assertTrue("Datasets are different", b);
    } catch (RuntimeException ex) {
        ex.printStackTrace(System.err);
        throw ex;
    }
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest)

Example 3 with UpdateRequest

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

the class UpdateValidator method execute.

//static final String paramSyntaxExtended   = "extendedSyntax" ;
@Override
protected void execute(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        //            if ( log.isInfoEnabled() )
        //                log.info("validation request") ;
        String[] args = httpRequest.getParameterValues(paramUpdate);
        if (args == null || args.length == 0) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "No update parameter to validator");
            return;
        }
        if (args.length > 1) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Too many update parameters");
            return;
        }
        final String updateString = httpRequest.getParameter(paramUpdate).replaceAll("(\r|\n| )*$", "");
        String updateSyntax = httpRequest.getParameter(paramSyntax);
        if (updateSyntax == null || updateSyntax.equals(""))
            updateSyntax = "SPARQL";
        Syntax language = Syntax.lookup(updateSyntax);
        if (language == null) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + updateSyntax);
            return;
        }
        String lineNumbersArg = httpRequest.getParameter(paramLineNumbers);
        String[] a = httpRequest.getParameterValues(paramFormat);
        // Currently default.
        boolean outputSPARQL = true;
        boolean lineNumbers = true;
        if (lineNumbersArg != null)
            lineNumbers = lineNumbersArg.equalsIgnoreCase("true") || lineNumbersArg.equalsIgnoreCase("yes");
        // Headers
        setHeaders(httpResponse);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        outStream.println("<html>");
        printHead(outStream, "SPARQL Update Validation Report");
        outStream.println("<body>");
        outStream.println("<h1>SPARQL Update Validator</h1>");
        // Print as received
        {
            outStream.println("<p>Input:</p>");
            // Not Java's finest hour.
            Content c = new Content() {

                @Override
                public void print(IndentedWriter out) {
                    out.print(updateString);
                }
            };
            output(outStream, c, lineNumbers);
        }
        // Attempt to parse it.
        UpdateRequest request = null;
        try {
            request = UpdateFactory.create(updateString, "http://example/base/", language);
        } catch (ARQException ex) {
            // Over generous exception (should be QueryException)
            // but this makes the code robust.
            outStream.println("<p>Syntax error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        } catch (RuntimeException ex) {
            outStream.println("<p>Internal error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        }
        // Because we pass into anon inner classes
        final UpdateRequest updateRequest = request;
        // OK?  Pretty print
        if (updateRequest != null && outputSPARQL) {
            outStream.println("<p>Formatted, parsed update request:</p>");
            Content c = new Content() {

                @Override
                public void print(IndentedWriter out) {
                    updateRequest.output(out);
                }
            };
            output(outStream, c, lineNumbers);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in doGet", ex);
    }
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) ServletOutputStream(javax.servlet.ServletOutputStream) UpdateRequest(org.apache.jena.update.UpdateRequest) ARQException(org.apache.jena.sparql.ARQException) Syntax(org.apache.jena.query.Syntax) ARQException(org.apache.jena.sparql.ARQException) IOException(java.io.IOException)

Example 4 with UpdateRequest

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

the class TestAuth method update_with_auth_11.

@Test
public void update_with_auth_11() {
    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
    // Also using pre-emptive auth
    BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
    URI scope = URI.create(authServiceUpdate);
    credsProv.setCredentials(new AuthScope(scope.getHost(), scope.getPort()), new UsernamePasswordCredentials("allowed", "password"));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(new HttpHost(scope.getHost()), basicAuth);
    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProv);
    context.setAuthCache(authCache);
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProv).build();
    ue.setClient(client);
    ue.setHttpContext(context);
    ue.execute();
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) UpdateRequest(org.apache.jena.update.UpdateRequest) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) UpdateProcessRemoteBase(org.apache.jena.sparql.modify.UpdateProcessRemoteBase) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) URI(java.net.URI) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 5 with UpdateRequest

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

the class TestEmbeddedFuseki method embedded_04.

@Test
public void embedded_04() {
    DatasetGraph dsg = dataset();
    Txn.executeWrite(dsg, () -> {
        Quad q = SSE.parseQuad("(_ :s :p _:b)");
        dsg.add(q);
    });
    // A service with just being able to do quads operations
    // That is, GET, POST, PUT on  "/data" in N-quads and TriG. 
    DataService dataService = new DataService(dsg);
    dataService.addEndpoint(OperationName.Quads_RW, "");
    dataService.addEndpoint(OperationName.Query, "");
    dataService.addEndpoint(OperationName.Update, "");
    int port = FusekiLib.choosePort();
    FusekiEmbeddedServer server = FusekiEmbeddedServer.create().setPort(port).add("/data", dataService).build();
    server.start();
    try {
        // Put data in.
        String data = "(graph (:s :p 1) (:s :p 2) (:s :p 3))";
        Graph g = SSE.parseGraph(data);
        HttpEntity e = graphToHttpEntity(g);
        HttpOp.execHttpPut("http://localhost:" + port + "/data", e);
        // Get data out.
        try (TypedInputStream in = HttpOp.execHttpGet("http://localhost:" + port + "/data")) {
            Graph g2 = GraphFactory.createDefaultGraph();
            RDFDataMgr.read(g2, in, RDFLanguages.contentTypeToLang(in.getContentType()));
            assertTrue(g.isIsomorphicWith(g2));
        }
        // Query.
        query("http://localhost:" + port + "/data", "SELECT * { ?s ?p ?o}", qExec -> {
            ResultSet rs = qExec.execSelect();
            int x = ResultSetFormatter.consume(rs);
            assertEquals(3, x);
        });
        // Update
        UpdateRequest req = UpdateFactory.create("CLEAR DEFAULT");
        UpdateExecutionFactory.createRemote(req, "http://localhost:" + port + "/data").execute();
        // Query again.
        query("http://localhost:" + port + "/data", "SELECT * { ?s ?p ?o}", qExec -> {
            ResultSet rs = qExec.execSelect();
            int x = ResultSetFormatter.consume(rs);
            assertEquals(0, x);
        });
    } finally {
        server.stop();
    }
}
Also used : Quad(org.apache.jena.sparql.core.Quad) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) HttpEntity(org.apache.http.HttpEntity) UpdateRequest(org.apache.jena.update.UpdateRequest) ResultSet(org.apache.jena.query.ResultSet) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) DataService(org.apache.jena.fuseki.server.DataService) Test(org.junit.Test)

Aggregations

UpdateRequest (org.apache.jena.update.UpdateRequest)128 Test (org.junit.Test)85 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)20 UpdateProcessor (org.apache.jena.update.UpdateProcessor)14 UpdateProcessRemoteBase (org.apache.jena.sparql.modify.UpdateProcessRemoteBase)13 Model (org.apache.jena.rdf.model.Model)10 Dataset (org.apache.jena.query.Dataset)9 UpdateExecution (org.apache.jena.update.UpdateExecution)9 Node (org.apache.jena.graph.Node)6 RDFNode (org.apache.jena.rdf.model.RDFNode)6 Resource (org.apache.jena.rdf.model.Resource)6 HttpTest (org.apache.jena.fuseki.test.HttpTest)5 HashMap (java.util.HashMap)4 Syntax (org.apache.jena.query.Syntax)4 Context (org.apache.jena.sparql.util.Context)4 URI (java.net.URI)3 AuthScope (org.apache.http.auth.AuthScope)3 Literal (org.apache.jena.rdf.model.Literal)3 UpdateException (org.apache.jena.update.UpdateException)3 SQLException (java.sql.SQLException)2