Search in sources :

Example 1 with ACL

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;
}
Also used : IdentifierService(org.trellisldp.api.IdentifierService) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) XSD(org.trellisldp.vocabulary.XSD) DeletedResource(org.trellisldp.vocabulary.Trellis.DeletedResource) DatabaseMgr.connectDatasetGraph(org.apache.jena.tdb2.DatabaseMgr.connectDatasetGraph) RDF.type(org.trellisldp.vocabulary.RDF.type) QuadAcc(org.apache.jena.sparql.modify.request.QuadAcc) Resource(org.trellisldp.api.Resource) Collections.singletonList(java.util.Collections.singletonList) SUBJECT(org.trellisldp.triplestore.TriplestoreUtils.SUBJECT) Txn.executeWrite(org.apache.jena.system.Txn.executeWrite) TrellisUtils.normalizeIdentifier(org.trellisldp.api.TrellisUtils.normalizeIdentifier) PreferAudit(org.trellisldp.vocabulary.Trellis.PreferAudit) Collectors.toMap(java.util.stream.Collectors.toMap) Arrays.asList(java.util.Arrays.asList) JenaCommonsRDF.toJena(org.apache.jena.commonsrdf.JenaCommonsRDF.toJena) Map(java.util.Map) PreferServerManaged(org.trellisldp.vocabulary.Trellis.PreferServerManaged) Collections.synchronizedList(java.util.Collections.synchronizedList) DC(org.trellisldp.vocabulary.DC) Set(java.util.Set) Literal(org.apache.commons.rdf.api.Literal) Instant(java.time.Instant) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Var(org.apache.jena.sparql.core.Var) Stream.builder(java.util.stream.Stream.builder) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Query(org.apache.jena.query.Query) FOAF(org.trellisldp.vocabulary.FOAF) Arrays.stream(java.util.Arrays.stream) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Instant.now(java.time.Instant.now) NodeFactory.createURI(org.apache.jena.graph.NodeFactory.createURI) ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) RDFTerm(org.apache.commons.rdf.api.RDFTerm) ACL(org.trellisldp.vocabulary.ACL) PreferAccessControl(org.trellisldp.vocabulary.Trellis.PreferAccessControl) UpdateDeleteInsert(org.apache.jena.sparql.modify.request.UpdateDeleteInsert) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Supplier(java.util.function.Supplier) RDF(org.apache.commons.rdf.api.RDF) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Metadata(org.trellisldp.api.Metadata) Quad(org.apache.jena.sparql.core.Quad) ResourceService(org.trellisldp.api.ResourceService) LDP(org.trellisldp.vocabulary.LDP) DatasetFactory.wrap(org.apache.jena.query.DatasetFactory.wrap) Dataset(org.apache.commons.rdf.api.Dataset) RDFFactory(org.trellisldp.api.RDFFactory) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) RDFConnectionFactory.connect(org.apache.jena.rdfconnection.RDFConnectionFactory.connect) Logger(org.slf4j.Logger) PREDICATE(org.trellisldp.triplestore.TriplestoreUtils.PREDICATE) QuadDataAcc(org.apache.jena.sparql.modify.request.QuadDataAcc) JenaCommonsRDF(org.apache.jena.commonsrdf.JenaCommonsRDF) TriplestoreUtils.getObject(org.trellisldp.triplestore.TriplestoreUtils.getObject) UpdateDeleteWhere(org.apache.jena.sparql.modify.request.UpdateDeleteWhere) IRI(org.apache.commons.rdf.api.IRI) UpdateDataInsert(org.apache.jena.sparql.modify.request.UpdateDataInsert) Node(org.apache.jena.graph.Node) DatasetFactory.createTxnMem(org.apache.jena.query.DatasetFactory.createTxnMem) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) UpdateRequest(org.apache.jena.update.UpdateRequest) PreferUserManaged(org.trellisldp.vocabulary.Trellis.PreferUserManaged) OBJECT(org.trellisldp.triplestore.TriplestoreUtils.OBJECT) Quad(org.apache.jena.sparql.core.Quad) UpdateDeleteWhere(org.apache.jena.sparql.modify.request.UpdateDeleteWhere) UpdateRequest(org.apache.jena.update.UpdateRequest) UpdateDataInsert(org.apache.jena.sparql.modify.request.UpdateDataInsert) ArrayList(java.util.ArrayList) PreferServerManaged(org.trellisldp.vocabulary.Trellis.PreferServerManaged) QuadDataAcc(org.apache.jena.sparql.modify.request.QuadDataAcc) QuadAcc(org.apache.jena.sparql.modify.request.QuadAcc)

Example 2 with ACL

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);
        }
    }
}
Also used : TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) Collections.unmodifiableList(java.util.Collections.unmodifiableList) Resource(org.trellisldp.api.Resource) Model(org.apache.jena.rdf.model.Model) Graph(org.apache.commons.rdf.api.Graph) CacheService(org.trellisldp.api.CacheService) Collectors.toSet(java.util.stream.Collectors.toSet) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) TYPE(java.lang.annotation.ElementType.TYPE) FIELD(java.lang.annotation.ElementType.FIELD) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Set(java.util.Set) TrellisUtils.toGraph(org.trellisldp.api.TrellisUtils.toGraph) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) FOAF(org.trellisldp.vocabulary.FOAF) JenaCommonsRDF.fromJena(org.apache.jena.commonsrdf.JenaCommonsRDF.fromJena) RiotException(org.apache.jena.riot.RiotException) TURTLE(org.apache.jena.riot.Lang.TURTLE) Session(org.trellisldp.api.Session) RDFTerm(org.apache.commons.rdf.api.RDFTerm) ACL(org.trellisldp.vocabulary.ACL) PARAMETER(java.lang.annotation.ElementType.PARAMETER) CompletableFuture(java.util.concurrent.CompletableFuture) TrellisUtils.getContainer(org.trellisldp.api.TrellisUtils.getContainer) VCARD(org.trellisldp.vocabulary.VCARD) Function(java.util.function.Function) RDF(org.apache.commons.rdf.api.RDF) Triple(org.apache.commons.rdf.api.Triple) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Metadata(org.trellisldp.api.Metadata) String.join(java.lang.String.join) ResourceService(org.trellisldp.api.ResourceService) Objects.requireNonNull(java.util.Objects.requireNonNull) Dataset(org.apache.commons.rdf.api.Dataset) Trellis(org.trellisldp.vocabulary.Trellis) RDFFactory(org.trellisldp.api.RDFFactory) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) RDFParser(org.apache.jena.riot.RDFParser) IOException(java.io.IOException) TrellisUtils(org.trellisldp.api.TrellisUtils) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) METHOD(java.lang.annotation.ElementType.METHOD) Collectors.toList(java.util.stream.Collectors.toList) ModelFactory.createDefaultModel(org.apache.jena.rdf.model.ModelFactory.createDefaultModel) IRI(org.apache.commons.rdf.api.IRI) Quad(org.apache.commons.rdf.api.Quad) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) Collections(java.util.Collections) InputStream(java.io.InputStream) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) Dataset(org.apache.commons.rdf.api.Dataset) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) RiotException(org.apache.jena.riot.RiotException) IOException(java.io.IOException) PostConstruct(javax.annotation.PostConstruct)

Example 3 with ACL

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;
}
Also used : TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) Collections.unmodifiableList(java.util.Collections.unmodifiableList) Resource(org.trellisldp.api.Resource) Model(org.apache.jena.rdf.model.Model) Graph(org.apache.commons.rdf.api.Graph) CacheService(org.trellisldp.api.CacheService) Collectors.toSet(java.util.stream.Collectors.toSet) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) TYPE(java.lang.annotation.ElementType.TYPE) FIELD(java.lang.annotation.ElementType.FIELD) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Set(java.util.Set) TrellisUtils.toGraph(org.trellisldp.api.TrellisUtils.toGraph) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) FOAF(org.trellisldp.vocabulary.FOAF) JenaCommonsRDF.fromJena(org.apache.jena.commonsrdf.JenaCommonsRDF.fromJena) RiotException(org.apache.jena.riot.RiotException) TURTLE(org.apache.jena.riot.Lang.TURTLE) Session(org.trellisldp.api.Session) RDFTerm(org.apache.commons.rdf.api.RDFTerm) ACL(org.trellisldp.vocabulary.ACL) PARAMETER(java.lang.annotation.ElementType.PARAMETER) CompletableFuture(java.util.concurrent.CompletableFuture) TrellisUtils.getContainer(org.trellisldp.api.TrellisUtils.getContainer) VCARD(org.trellisldp.vocabulary.VCARD) Function(java.util.function.Function) RDF(org.apache.commons.rdf.api.RDF) Triple(org.apache.commons.rdf.api.Triple) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Metadata(org.trellisldp.api.Metadata) String.join(java.lang.String.join) ResourceService(org.trellisldp.api.ResourceService) Objects.requireNonNull(java.util.Objects.requireNonNull) Dataset(org.apache.commons.rdf.api.Dataset) Trellis(org.trellisldp.vocabulary.Trellis) RDFFactory(org.trellisldp.api.RDFFactory) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) RDFParser(org.apache.jena.riot.RDFParser) IOException(java.io.IOException) TrellisUtils(org.trellisldp.api.TrellisUtils) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) METHOD(java.lang.annotation.ElementType.METHOD) Collectors.toList(java.util.stream.Collectors.toList) ModelFactory.createDefaultModel(org.apache.jena.rdf.model.ModelFactory.createDefaultModel) IRI(org.apache.commons.rdf.api.IRI) Quad(org.apache.commons.rdf.api.Quad) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) Collections(java.util.Collections) InputStream(java.io.InputStream) RiotException(org.apache.jena.riot.RiotException) Dataset(org.apache.commons.rdf.api.Dataset) InputStream(java.io.InputStream) Model(org.apache.jena.rdf.model.Model) ModelFactory.createDefaultModel(org.apache.jena.rdf.model.ModelFactory.createDefaultModel) IOException(java.io.IOException)

Example 4 with ACL

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())));
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) DELETE(javax.ws.rs.HttpMethod.DELETE) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) Session(org.trellisldp.api.Session) SecurityContext(javax.ws.rs.core.SecurityContext) ACL(org.trellisldp.vocabulary.ACL) Captor(org.mockito.Captor) RDF(org.apache.commons.rdf.api.RDF) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) ResourceService(org.trellisldp.api.ResourceService) Trellis(org.trellisldp.vocabulary.Trellis) RDFFactory(org.trellisldp.api.RDFFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) PREFER(org.trellisldp.common.HttpConstants.PREFER) ForbiddenException(javax.ws.rs.ForbiddenException) OK(javax.ws.rs.core.Response.Status.OK) Set(java.util.Set) FORBIDDEN(javax.ws.rs.core.Response.Status.FORBIDDEN) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) IRI(org.apache.commons.rdf.api.IRI) Principal(java.security.Principal) URI.create(java.net.URI.create) Assertions(org.junit.jupiter.api.Assertions) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) UriInfo(javax.ws.rs.core.UriInfo) Collections(java.util.Collections) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) Link(javax.ws.rs.core.Link) IRI(org.apache.commons.rdf.api.IRI) Link(javax.ws.rs.core.Link) Test(org.junit.jupiter.api.Test)

Example 5 with ACL

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())));
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) DELETE(javax.ws.rs.HttpMethod.DELETE) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) Session(org.trellisldp.api.Session) SecurityContext(javax.ws.rs.core.SecurityContext) ACL(org.trellisldp.vocabulary.ACL) Captor(org.mockito.Captor) RDF(org.apache.commons.rdf.api.RDF) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) ResourceService(org.trellisldp.api.ResourceService) Trellis(org.trellisldp.vocabulary.Trellis) RDFFactory(org.trellisldp.api.RDFFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) PREFER(org.trellisldp.common.HttpConstants.PREFER) ForbiddenException(javax.ws.rs.ForbiddenException) OK(javax.ws.rs.core.Response.Status.OK) Set(java.util.Set) FORBIDDEN(javax.ws.rs.core.Response.Status.FORBIDDEN) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) IRI(org.apache.commons.rdf.api.IRI) Principal(java.security.Principal) URI.create(java.net.URI.create) Assertions(org.junit.jupiter.api.Assertions) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) UriInfo(javax.ws.rs.core.UriInfo) Collections(java.util.Collections) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) Link(javax.ws.rs.core.Link) IRI(org.apache.commons.rdf.api.IRI) Link(javax.ws.rs.core.Link) Test(org.junit.jupiter.api.Test)

Aggregations

Set (java.util.Set)6 IRI (org.apache.commons.rdf.api.IRI)6 RDF (org.apache.commons.rdf.api.RDF)6 RDFFactory (org.trellisldp.api.RDFFactory)6 ResourceService (org.trellisldp.api.ResourceService)6 TRELLIS_DATA_PREFIX (org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX)6 ACL (org.trellisldp.vocabulary.ACL)6 Collections (java.util.Collections)5 HashSet (java.util.HashSet)5 List (java.util.List)5 Session (org.trellisldp.api.Session)5 Trellis (org.trellisldp.vocabulary.Trellis)5 Optional (java.util.Optional)4 CompletionStage (java.util.concurrent.CompletionStage)4 Stream (java.util.stream.Stream)4 PostConstruct (javax.annotation.PostConstruct)4 ApplicationScoped (javax.enterprise.context.ApplicationScoped)4 Inject (javax.inject.Inject)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3