use of org.trellisldp.vocabulary.ACL in project trellis by trellis-ldp.
the class TriplestoreResourceService method buildUpdateRequest.
/**
* This is equivalent to the SPARQL below.
*
* <p><pre><code>
* DELETE WHERE { GRAPH IDENTIFIER { ?s ?p ?o } };
* DELETE WHERE { GRAPH IDENTIFIER?ext=acl { ?s ?p ?o } };
* DELETE WHERE { GRAPH trellis:PreferServerManaged {
* IDENTIFIER a ldp:NonRDFSource .
* IDENTIFIER dc:hasPart ?s .
* ?s ?p ?o .
* };
* DELETE WHERE { GRAPH trellis:PreferServerManaged { IDENTIFIER ?p ?o } };
* INSERT DATA {
* GRAPH IDENTIFIER { ... }
* GRAPH IDENTIFIER?ext=acl { ... }
* GRAPH trellis:PreferServerManaged { ... }
* GRAPH IDENTIFIER?ext=audit { ... }
* }
* </code></pre></p>
*/
private UpdateRequest buildUpdateRequest(final IRI identifier, final Literal time, final Dataset dataset, final OperationType operation) {
// Set the time
dataset.add(PreferServerManaged, identifier, DC.modified, time);
final UpdateRequest req = new UpdateRequest();
req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(toJena(identifier), SUBJECT, PREDICATE, OBJECT)))));
extensions.forEach((ext, graph) -> req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(getExtIRI(identifier, ext), SUBJECT, PREDICATE, OBJECT))))));
req.add(new UpdateDeleteWhere(new QuadAcc(asList(new Quad(toJena(PreferServerManaged), toJena(identifier), toJena(type), toJena(LDP.NonRDFSource)), new Quad(toJena(PreferServerManaged), toJena(identifier), toJena(DC.hasPart), SUBJECT), new Quad(toJena(PreferServerManaged), SUBJECT, PREDICATE, OBJECT)))));
req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(toJena(PreferServerManaged), toJena(identifier), PREDICATE, OBJECT)))));
final QuadDataAcc sink = new QuadDataAcc(synchronizedList(new ArrayList<>()));
if (operation == OperationType.DELETE) {
dataset.stream().filter(q -> q.getGraphName().filter(PreferServerManaged::equals).isPresent()).map(JenaCommonsRDF::toJena).forEach(sink::addQuad);
} else {
dataset.stream().filter(q -> q.getGraphName().filter(PreferServerManaged::equals).isPresent()).map(JenaCommonsRDF::toJena).forEach(sink::addQuad);
dataset.getGraph(PreferUserManaged).ifPresent(g -> g.stream().map(t -> new Quad(toJena(identifier), toJena(t))).forEach(sink::addQuad));
dataset.getGraph(PreferAudit).ifPresent(g -> g.stream().map(t -> new Quad(getExtIRI(identifier, "audit"), toJena(t))).forEach(sink::addQuad));
extensions.forEach((ext, graph) -> dataset.getGraph(graph).ifPresent(g -> g.stream().map(t -> new Quad(getExtIRI(identifier, ext), toJena(t))).forEach(sink::addQuad)));
}
req.add(new UpdateDataInsert(sink));
return req;
}
use of org.trellisldp.vocabulary.ACL in project trellis by trellis-ldp.
the class WebAcService method initialize.
/**
* Initializes the root ACL, if there is no root ACL.
*/
@PostConstruct
public void initialize() {
defaultRootAuthorizations = unmodifiableList(getDefaultRootAuthorizations(defaultAuthResourceLocation));
if (initializeRoot) {
try (final Dataset dataset = generateDefaultRootAuthorizationsDataset(defaultAuthResourceLocation)) {
this.resourceService.get(root).thenCompose(res -> initialize(res, dataset)).exceptionally(err -> {
LOGGER.warn("Unable to auto-initialize Trellis: {}. See DEBUG log for more info", err.getMessage());
LOGGER.debug("Error auto-initializing Trellis", err);
return null;
}).toCompletableFuture().join();
} catch (final Exception ex) {
throw new TrellisRuntimeException("Error initializing Trellis ACL", ex);
}
}
}
use of org.trellisldp.vocabulary.ACL in project trellis by trellis-ldp.
the class WebAcService method generateDefaultRootAuthorizationsDataset.
static Dataset generateDefaultRootAuthorizationsDataset(final String resource) {
final Dataset dataset = rdf.createDataset();
final Model model = createDefaultModel();
try (final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource)) {
if (is != null) {
LOGGER.debug("Using classpath resource for default root ACL: {}", resource);
RDFParser.source(is).lang(TURTLE).base(TRELLIS_DATA_PREFIX).parse(model);
} else {
LOGGER.debug("Using external resource for default root ACL: {}", resource);
RDFParser.source(resource).lang(TURTLE).base(TRELLIS_DATA_PREFIX).parse(model);
}
fromJena(model.getGraph()).stream().map(triple -> rdf.createQuad(Trellis.PreferAccessControl, triple.getSubject(), triple.getPredicate(), triple.getObject())).forEach(dataset::add);
} catch (final IOException | RiotException ex) {
LOGGER.warn("Couldn't initialize root ACL with {}, falling back to default: {}", resource, ex.getMessage());
} finally {
model.close();
}
// Fallback to manual creation
if (dataset.size() == 0) {
dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.mode, ACL.Read));
dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.mode, ACL.Write));
dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.mode, ACL.Control));
dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.mode, ACL.Append));
dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.agentClass, FOAF.Agent));
dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.default_, root));
dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.accessTo, root));
}
return dataset;
}
use of org.trellisldp.vocabulary.ACL in project trellis by trellis-ldp.
the class WebAcFilterTest method testFilterContainerResponseWithControl.
@Test
void testFilterContainerResponseWithControl() {
final IRI localEffectiveAcl = rdf.createIRI(TRELLIS_DATA_PREFIX + "container");
final MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
final MultivaluedMap<String, String> stringHeaders = new MultivaluedHashMap<>();
stringHeaders.putSingle("Link", "<http://www.w3.org/ns/ldp#BasicContainer>; rel=\"type\"");
when(mockContext.getUriInfo()).thenReturn(mockUriInfo);
when(mockUriInfo.getQueryParameters()).thenReturn(mockQueryParams);
when(mockUriInfo.getPath()).thenReturn("/container/");
when(mockQueryParams.getOrDefault("ext", emptyList())).thenReturn(emptyList());
when(mockResponseContext.getStatusInfo()).thenReturn(OK);
when(mockResponseContext.getHeaders()).thenReturn(headers);
when(mockContext.getProperty(WebAcFilter.SESSION_WEBAC_MODES)).thenReturn(new AuthorizedModes(localEffectiveAcl, allModes));
final WebAcFilter filter = new WebAcFilter();
filter.accessService = mockWebAcService;
filter.resourceService = mockResourceService;
assertTrue(headers.isEmpty());
filter.filter(mockContext, mockResponseContext);
assertFalse(headers.isEmpty());
final List<Object> links = headers.get("Link");
assertTrue(links.stream().map(Link.class::cast).anyMatch(link -> link.getRels().contains("acl") && "/container/?ext=acl".equals(link.getUri().toString())));
assertTrue(links.stream().map(Link.class::cast).anyMatch(link -> "/container/?ext=acl".equals(link.getUri().toString()) && link.getRels().contains(Trellis.effectiveAcl.getIRIString())));
}
use of org.trellisldp.vocabulary.ACL in project trellis by trellis-ldp.
the class WebAcFilterTest method testFilterResourceResponseWithControl.
@Test
void testFilterResourceResponseWithControl() {
final IRI localEffectiveAcl = rdf.createIRI(TRELLIS_DATA_PREFIX + "resource");
final MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
final MultivaluedMap<String, String> stringHeaders = new MultivaluedHashMap<>();
stringHeaders.putSingle("Link", "<http://www.w3.org/ns/ldp#RDFSource>; rel=\"type\"");
when(mockContext.getUriInfo()).thenReturn(mockUriInfo);
when(mockUriInfo.getQueryParameters()).thenReturn(mockQueryParams);
when(mockQueryParams.getOrDefault("ext", emptyList())).thenReturn(emptyList());
when(mockUriInfo.getPath()).thenReturn("");
when(mockResponseContext.getStatusInfo()).thenReturn(OK);
when(mockResponseContext.getHeaders()).thenReturn(headers);
when(mockResponseContext.getStringHeaders()).thenReturn(stringHeaders);
when(mockUriInfo.getPath()).thenReturn("/resource");
when(mockContext.getProperty(WebAcFilter.SESSION_WEBAC_MODES)).thenReturn(new AuthorizedModes(localEffectiveAcl, allModes));
final WebAcFilter filter = new WebAcFilter();
filter.accessService = mockWebAcService;
filter.resourceService = mockResourceService;
assertTrue(headers.isEmpty());
filter.filter(mockContext, mockResponseContext);
assertFalse(headers.isEmpty());
final List<Object> links = headers.get("Link");
assertTrue(links.stream().map(Link.class::cast).anyMatch(link -> link.getRels().contains("acl") && "/resource?ext=acl".equals(link.getUri().toString())));
assertTrue(links.stream().map(Link.class::cast).anyMatch(link -> "/resource?ext=acl".equals(link.getUri().toString()) && link.getRels().contains(Trellis.effectiveAcl.getIRIString())));
}
Aggregations