Search in sources :

Example 6 with Operation

use of org.apache.jena.fuseki.server.Operation in project jena by apache.

the class ExFuseki_01_NamedService method main.

public static void main(String... args) {
    // Create a new operation: operations are really just names (symbols). The code to
    // run is found by looking up the operation in a per-server table that gives the server-specific
    // implementation as an ActionService.
    Operation myOperation = Operation.alloc("http://example/special1", "special1", "Custom operation");
    // Service endpoint name.
    // This can be different for different datasets even in the same server.
    // c.f. {@code fuseki:serviceQuery}
    String endpointName = "special";
    // The handled for the new operation.
    ActionService customHandler = new DemoService();
    FusekiServer server = FusekiServer.create().port(PORT).verbose(true).registerOperation(myOperation, customHandler).add(DATASET, DatasetGraphFactory.createTxnMem(), true).addEndpoint(DATASET, endpointName, myOperation).build();
    // Start the server. This does not block this thread.
    server.start();
    // Try some operations on the server using the service URL.
    String customOperationURL = SERVER_URL + DATASET + "/" + endpointName;
    try {
        // Service endpoint name : GET
        String s1 = HttpOp.httpGetString(customOperationURL);
        System.out.print(s1);
        if (s1 == null)
            System.out.println();
        // Service endpoint name : POST
        try (TypedInputStream stream = HttpOp.httpPostStream(customOperationURL, "text/plain")) {
            String s2 = FileUtils.readWholeFileAsUTF8(stream);
            System.out.print(s2);
            if (s2 == null)
                System.out.println();
        } catch (IOException ex) {
            IO.exception(ex);
        }
        // Service endpoint name. DELETE -> fails 405
        try {
            HttpOp.httpDelete(customOperationURL);
            throw new IllegalStateException("DELETE succeeded");
        } catch (HttpException ex) {
            if (ex.getStatusCode() != HttpSC.METHOD_NOT_ALLOWED_405)
                System.err.println("Unexpected HTTP Response Code: " + ex.getMessage());
            else
                System.out.println("DELETE rejected correctly: " + ex.getMessage());
        }
    } finally {
        server.stop();
    }
}
Also used : HttpException(org.apache.jena.atlas.web.HttpException) Operation(org.apache.jena.fuseki.server.Operation) FusekiServer(org.apache.jena.fuseki.main.FusekiServer) IOException(java.io.IOException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) ActionService(org.apache.jena.fuseki.servlets.ActionService)

Example 7 with Operation

use of org.apache.jena.fuseki.server.Operation in project jena by apache.

the class ExFuseki_02_Config_DataService method main.

public static void main(String... args) {
    // Register a new operation
    Operation myOperation = Operation.alloc("http://example/special2", "special2", "Custom operation");
    // Service endpoint names.
    String queryEndpoint = "q";
    String customEndpoint = "x";
    // Make a DataService with custom named for endpoints.
    // In this example, "q" for SPARQL query and "x" for our custom extension and no others.
    DatasetGraph dsg = DatasetGraphFactory.createTxnMem();
    DataService dataService = DataService.newBuilder(dsg).addEndpoint(myOperation, customEndpoint).addEndpoint(Operation.Query, queryEndpoint).build();
    // This will be the code to handled for the operation.
    ActionService customHandler = new DemoService();
    FusekiServer server = FusekiServer.create().port(PORT).verbose(true).registerOperation(myOperation, customHandler).add(DATASET, dataService).build();
    server.start();
    // Try some operations on the server using the service URL.
    String customOperationURL = SERVER_URL + DATASET + "/" + customEndpoint;
    String queryOperationURL = SERVER_URL + DATASET + "/" + queryEndpoint;
    Query query = QueryFactory.create("ASK{}");
    try {
        // Try custom name - OK
        try (QueryExecution qExec = QueryExecution.service(queryOperationURL, query)) {
            qExec.execAsk();
        }
        // Try the usual default name, which is not configured in the DataService so expect a 404.
        try (QueryExecution qExec = QueryExecution.service(SERVER_URL + DATASET + "/sparql", query)) {
            qExec.execAsk();
            throw new RuntimeException("Didn't fail");
        } catch (QueryExceptionHTTP ex) {
            if (ex.getStatusCode() != HttpSC.NOT_FOUND_404) {
                throw new RuntimeException("Not a 404", ex);
            }
        }
        // Make an HTTP GET to the custom operation.
        // Service endpoint name : GET
        String s1 = HttpOp.httpGetString(customOperationURL);
        if (s1 == null)
            throw new RuntimeException("Failed: " + customOperationURL);
    } finally {
        server.stop();
    }
}
Also used : Query(org.apache.jena.query.Query) Operation(org.apache.jena.fuseki.server.Operation) FusekiServer(org.apache.jena.fuseki.main.FusekiServer) QueryExceptionHTTP(org.apache.jena.sparql.engine.http.QueryExceptionHTTP) ActionService(org.apache.jena.fuseki.servlets.ActionService) QueryExecution(org.apache.jena.query.QueryExecution) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) DataService(org.apache.jena.fuseki.server.DataService)

Example 8 with Operation

use of org.apache.jena.fuseki.server.Operation in project jena by apache.

the class Shacl03_FusekiValidationService method main.

public static void main(String... a) throws IOException {
    FusekiLogging.setLogging();
    // If not standard registration...
    Operation op = Operation.alloc(FusekiVocab.NS + "shacl", "shacl", "SHACL valdiation");
    // FusekiExt.registerOperation(op, new SHACL_Validation());
    // FusekiExt.addDefaultEndpoint(op, "shacl");
    // Operation op = Operation.Shacl;
    FusekiServer server = FusekiServer.create().port(3030).add("/ds", DatasetGraphFactory.createTxnMem(), true).addEndpoint("/ds", "shacl", op).build();
    try {
        server.start();
        try (RDFConnection conn = RDFConnection.connect("http://localhost:3030/ds")) {
            conn.put("fu-data.ttl");
        }
        ValidationReport report = validateReport("http://localhost:3030/ds/shacl?graph=default", "fu-shapes.ttl");
        System.out.println();
        ShLib.printReport(report);
        System.out.println();
        System.out.println("- - - - - - - - - - - - - - - - - -");
        System.out.println();
        RDFDataMgr.write(System.out, report.getGraph(), Lang.TTL);
        System.out.println();
    } finally {
        server.stop();
    }
}
Also used : RDFConnection(org.apache.jena.rdfconnection.RDFConnection) ValidationReport(org.apache.jena.shacl.ValidationReport) Operation(org.apache.jena.fuseki.server.Operation) FusekiServer(org.apache.jena.fuseki.main.FusekiServer)

Aggregations

Operation (org.apache.jena.fuseki.server.Operation)8 FusekiServer (org.apache.jena.fuseki.main.FusekiServer)4 ActionService (org.apache.jena.fuseki.servlets.ActionService)4 IOException (java.io.IOException)2 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)2 DataService (org.apache.jena.fuseki.server.DataService)2 Endpoint (org.apache.jena.fuseki.server.Endpoint)2 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 HttpException (org.apache.jena.atlas.web.HttpException)1 FusekiConfigException (org.apache.jena.fuseki.FusekiConfigException)1 Counter (org.apache.jena.fuseki.server.Counter)1 CounterName (org.apache.jena.fuseki.server.CounterName)1 CounterSet (org.apache.jena.fuseki.server.CounterSet)1 Query (org.apache.jena.query.Query)1 QueryExecution (org.apache.jena.query.QueryExecution)1 RDFConnection (org.apache.jena.rdfconnection.RDFConnection)1 ValidationReport (org.apache.jena.shacl.ValidationReport)1 JenaException (org.apache.jena.shared.JenaException)1 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)1