use of org.apache.jena.fuseki.access.AuthorizationService 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);
}
Aggregations