Search in sources :

Example 1 with FusekiServer

use of org.apache.jena.fuseki.main.FusekiServer in project jena by apache.

the class ExFuseki_03_AddService_ContentType 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/special3", "special3", "Custom operation");
    // Service endpoint name.
    String endpointName = "special";
    String contentType = "application/special";
    // The handled for the new operation.
    ActionService customHandler = new DemoService();
    FusekiServer server = FusekiServer.create().port(PORT).verbose(true).registerOperation(myOperation, contentType, 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 datasetURL = SERVER_URL + DATASET;
    try {
        // Dataset endpoint name : POST, with Content-type.
        try (TypedInputStream stream = HttpOp.httpPostStream(datasetURL, contentType, BodyPublishers.ofString(""), "text/plain")) {
            String s2 = FileUtils.readWholeFileAsUTF8(stream);
            System.out.print(s2);
            if (s2 == null)
                System.out.println();
        } catch (IOException ex) {
            IO.exception(ex);
        }
    } finally {
        server.stop();
    }
}
Also used : 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 2 with FusekiServer

use of org.apache.jena.fuseki.main.FusekiServer in project jena by apache.

the class ExFuseki_06_DataAccessCtl method main.

public static void main(String... a) {
    FusekiLogging.setLogging();
    int port = WebLib.choosePort();
    String datasetName = "/ds";
    String URL = format("http://localhost:%d%s", port, datasetName);
    // ---- Set up the registry.
    AuthorizationService authorizeSvc;
    {
        SecurityRegistry reg = new SecurityRegistry();
        // user1 can see the default graph and :g1
        reg.put("user1", new SecurityContextView("http://example/g1", Quad.defaultGraphIRI.getURI()));
        // user2 can see :g1
        reg.put("user2", new SecurityContextView("http://example/g1"));
        // user3 can see :g1 and :g2
        reg.put("user3", new SecurityContextView("http://example/g1", "http://example/g2"));
        // Hide implementation.
        authorizeSvc = reg;
    }
    // ---- Some data
    DatasetGraph dsg = createData();
    // ---- User authentication database (Jetty specific)
    UserStore userStore = new UserStore();
    addUserPassword(userStore, "user1", "pw1", "**");
    addUserPassword(userStore, "user2", "pw2", "**");
    try {
        userStore.start();
    } catch (Exception ex) {
        throw new RuntimeException("UserStore", ex);
    }
    // ---- Build server, start server.
    FusekiServer server = fuseki(port, userStore, authorizeSvc, datasetName, dsg);
    server.start();
    // ---- HttpClient connection with user and password basic authentication.
    Authenticator authenticator = AuthLib.authenticator("user1", "pw1");
    HttpClient client = HttpClient.newBuilder().authenticator(authenticator).connectTimeout(Duration.ofSeconds(10)).build();
    // ---- Use it.
    try (RDFConnection conn = RDFConnectionRemote.newBuilder().destination(URL).httpClient(client).build()) {
        // What can we see of the database? user1 can see g1 and the default graph
        System.out.println("\nFetch dataset");
        Dataset ds1 = conn.fetchDataset();
        RDFDataMgr.write(System.out, ds1, RDFFormat.TRIG_FLAT);
        // Get a graph.
        System.out.println("\nFetch named graph");
        Model m1 = conn.fetch("http://example/g1");
        RDFDataMgr.write(System.out, m1, RDFFormat.TURTLE_FLAT);
        // Get a graph. user tries to get a graph they have no permission for ==> 404
        System.out.println("\nFetch unexistent named graph");
        try {
            Model m2 = conn.fetch("http://example/g2");
        } catch (HttpException ex) {
            System.out.println(ex.getMessage());
        }
    }
    // Need to exit the JVM : there is a background server
    System.exit(0);
}
Also used : Dataset(org.apache.jena.query.Dataset) SecurityContextView(org.apache.jena.fuseki.access.SecurityContextView) FusekiServer(org.apache.jena.fuseki.main.FusekiServer) HttpException(org.apache.jena.atlas.web.HttpException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) UserStore(org.eclipse.jetty.security.UserStore) AuthorizationService(org.apache.jena.fuseki.access.AuthorizationService) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) HttpClient(java.net.http.HttpClient) Model(org.apache.jena.rdf.model.Model) HttpException(org.apache.jena.atlas.web.HttpException) Authenticator(java.net.Authenticator) SecurityRegistry(org.apache.jena.fuseki.access.SecurityRegistry)

Example 3 with FusekiServer

use of org.apache.jena.fuseki.main.FusekiServer in project jena by apache.

the class ExFuseki_09_Https_Auth method codeHttpsAuth.

public static FusekiServer codeHttpsAuth() {
    FusekiLogging.setLogging();
    // Some empty dataset
    DatasetGraph dsg = DatasetGraphFactory.createTxnMem();
    FusekiServer server = FusekiServer.create().verbose(true).https(3443, KEYSTORE, KEYSTOREPASSWORD).auth(AuthScheme.BASIC).passwordFile(PASSWD).add("/ds", dsg).build();
    server.start();
    // server.join();
    return server;
}
Also used : FusekiServer(org.apache.jena.fuseki.main.FusekiServer) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 4 with FusekiServer

use of org.apache.jena.fuseki.main.FusekiServer in project jena by apache.

the class TestRDFLinkFusekiBinary method rdflink_fuseki_1.

@Test
public void rdflink_fuseki_1() {
    // Tests all run, in order, on one connection.
    Triple triple = SSE.parseTriple("(:s :p <_:b3456>)");
    Graph graph = GraphFactory.createDefaultGraph();
    graph.add(triple);
    FusekiServer server = createFusekiServer().build().start();
    int port = server.getPort();
    try {
        String dsURL = "http://localhost:" + port + "/ds";
        assertTrue(HttpLib.isFuseki(dsURL));
        RDFLinkHTTPBuilder builder = RDFLinkFuseki.newBuilder().destination(dsURL);
        try (RDFLinkFuseki link = (RDFLinkFuseki) builder.build()) {
            // assertTrue(isFuseki(link));
            // GSP
            link.put(graph);
            checkGraph(link, "b3456");
            // Query forms.
            link.querySelect("SELECT * {?s ?p ?o}", x -> {
                Node obj = x.get(Var.alloc("o"));
                assertTrue(obj.isBlank());
                assertEquals("b3456", obj.getBlankNodeLabel());
            });
            try (QueryExec qExec = link.query("ASK {?s ?p <_:b3456>}")) {
                boolean bool = qExec.ask();
                assertTrue(bool);
            }
            try (QueryExec qExec = link.query("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o . FILTER (sameTerm(?o, <_:b3456>)) }")) {
                Graph graph2 = qExec.construct();
                checkGraph(graph2, "b3456");
            }
            try (QueryExec qExec = link.query("DESCRIBE ?s WHERE { ?s ?p <_:b3456>}")) {
                Graph graph2 = qExec.describe();
                checkGraph(graph2, "b3456");
            }
            // Update
            link.update("CLEAR DEFAULT");
            link.update("INSERT DATA { <x:s> <x:p> <_:b789> }");
            checkGraph(link, "b789");
            link.update("CLEAR DEFAULT");
            link.update("INSERT DATA { <x:s> <x:p> <_:6789> }");
            checkGraph(link, "6789");
        }
    } finally {
        server.stop();
    }
}
Also used : Triple(org.apache.jena.graph.Triple) QueryExec(org.apache.jena.sparql.exec.QueryExec) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) RDFLinkFuseki(org.apache.jena.rdflink.RDFLinkFuseki) FusekiServer(org.apache.jena.fuseki.main.FusekiServer) RDFLinkHTTPBuilder(org.apache.jena.rdflink.RDFLinkHTTPBuilder) Test(org.junit.Test)

Example 5 with FusekiServer

use of org.apache.jena.fuseki.main.FusekiServer in project jena by apache.

the class ExamplesServer method startServerWithAuth.

// Server with users and password.
public static FusekiServer startServerWithAuth(String dsName, DatasetGraph dsg, boolean verbose, String user, String password) {
    Objects.requireNonNull(user);
    Objects.requireNonNull(password);
    UserStore userStore = JettyLib.makeUserStore(user, password);
    SecurityHandler sh = JettyLib.makeSecurityHandler("Fuseki", userStore, AuthScheme.BASIC);
    FusekiServer server = FusekiServer.create().port(0).loopback(true).verbose(verbose).enablePing(true).securityHandler(sh).serverAuthPolicy(Auth.policyAllowSpecific(user)).add(dsName, dsg).build();
    server.start();
    return server;
}
Also used : SecurityHandler(org.eclipse.jetty.security.SecurityHandler) UserStore(org.eclipse.jetty.security.UserStore) FusekiServer(org.apache.jena.fuseki.main.FusekiServer)

Aggregations

FusekiServer (org.apache.jena.fuseki.main.FusekiServer)25 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)10 RDFConnection (org.apache.jena.rdfconnection.RDFConnection)6 Operation (org.apache.jena.fuseki.server.Operation)4 UserStore (org.eclipse.jetty.security.UserStore)4 IOException (java.io.IOException)3 ActionService (org.apache.jena.fuseki.servlets.ActionService)3 SecurityHandler (org.eclipse.jetty.security.SecurityHandler)3 Test (org.junit.Test)3 HttpException (org.apache.jena.atlas.web.HttpException)2 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)2 DataService (org.apache.jena.fuseki.server.DataService)2 Graph (org.apache.jena.graph.Graph)2 Node (org.apache.jena.graph.Node)2 Triple (org.apache.jena.graph.Triple)2 QueryExecution (org.apache.jena.query.QueryExecution)2 Model (org.apache.jena.rdf.model.Model)2 Authenticator (java.net.Authenticator)1 BindException (java.net.BindException)1 HttpClient (java.net.http.HttpClient)1