@Override public Set<IRI> getAccessModes(final IRI identifier, final Session session) { requireNonNull(session, "A non-null session must be provided!"); if (Trellis.AdministratorAgent.equals(session.getAgent())) { return unmodifiableSet(allModes); } final Set<IRI> cachedModes = cache.get(getCacheKey(identifier, session.getAgent()), k -> getAuthz(identifier, session.getAgent())); return session.getDelegatedBy().map(delegate -> { final Set<IRI> delegatedModes = new HashSet<>(cache.get(getCacheKey(identifier, delegate), k -> getAuthz(identifier, delegate))); delegatedModes.retainAll(cachedModes); return unmodifiableSet(delegatedModes); }).orElseGet(() -> unmodifiableSet(cachedModes)); }
@Test public void testPassthroughCache() { final List<String> list = new CopyOnWriteArrayList<>(); final Function<String, String> mapper = key -> { list.add(key); return key + "-some-suffix"; }; final CacheService<String, String> cache = new NoopCacheService<>(); assertEquals("one-some-suffix", cache.get("one", mapper), "Cache mapper didn't handle 'one'"); assertEquals("two-some-suffix", cache.get("two", mapper), "Cache mapper didn't handle 'two'"); assertEquals("one-some-suffix", cache.get("one", mapper), "Cache mapper didn't handle 'one'"); assertEquals(3L, list.size(), "Cache calls didn't match the internal record-keeping!"); } }
@Test public void testPassthroughCache() { final List<String> list = new CopyOnWriteArrayList<>(); final Function<String, String> mapper = key -> { list.add(key); return key + "-some-suffix"; }; final CacheService<String, String> cache = new NoopProfileCache(); assertEquals("one-some-suffix", cache.get("one", mapper), "Cache response didn't match!"); assertEquals("two-some-suffix", cache.get("two", mapper), "Cache response didn't match!"); assertEquals("one-some-suffix", cache.get("one", mapper), "Cache response didn't match!"); assertEquals(3L, list.size(), "Incorrect invocation count!"); } }
@BeforeEach @SuppressWarnings("unchecked") public void setUp() { initMocks(this); final Map<String, String> namespaces = new HashMap<>(); namespaces.put("dcterms", DCTerms.NS); namespaces.put("rdf", RDF.uri); service = new JenaIOService(mockNamespaceService, null, mockCache, "http://www.w3.org/ns/anno.jsonld,,,", "http://www.trellisldp.org/ns/"); service2 = new JenaIOService(mockNamespaceService, mockHtmlSerializer, mockCache, emptySet(), singleton("http://www.w3.org/ns/")); service3 = new JenaIOService(mockNamespaceService, null, mockCache, emptySet(), emptySet()); when(mockNamespaceService.getNamespaces()).thenReturn(namespaces); when(mockCache.get(anyString(), any(Function.class))).thenAnswer(inv -> { final String key = inv.getArgument(0); final Function<String, String> mapper = inv.getArgument(1); return mapper.apply(key); }); }
private void writeJsonLd(final OutputStream output, final DatasetGraph graph, final IRI... profiles) { final String profile = getCustomJsonLdProfile(profiles); final RDFFormat format = canUseCustomJsonLdProfile(profile) ? JSONLD_COMPACT_FLAT : getJsonLdProfile(profiles); final WriterDatasetRIOT writer = RDFDataMgr.createDatasetWriter(format); final PrefixMap pm = RiotLib.prefixMap(graph); final String base = null; final JsonLDWriteContext ctx = new JsonLDWriteContext(); if (canUseCustomJsonLdProfile(profile)) { LOGGER.debug("Setting JSON-LD context with profile: {}", profile); final String c = cache.get(profile, p -> { try (final TypedInputStream res = HttpOp.execHttpGet(profile)) { return IOUtils.toString(res.getInputStream(), UTF_8); } catch (final IOException | HttpException ex) { LOGGER.warn("Error fetching profile {}: {}", p, ex.getMessage()); return null; } }); if (nonNull(c)) { ctx.setJsonLDContext(c); ctx.setJsonLDContextSubstitution("\"" + profile + "\""); } } writer.write(output, graph, pm, base, ctx); }
private void writeJsonLd(final OutputStream output, final DatasetGraph graph, final IRI... profiles) { final String profile = getCustomJsonLdProfile(profiles); final RDFFormat format = canUseCustomJsonLdProfile(profile) ? JSONLD_COMPACT_FLAT : getJsonLdProfile(profiles); final WriterDatasetRIOT writer = RDFDataMgr.createDatasetWriter(format); final PrefixMap pm = RiotLib.prefixMap(graph); final String base = null; final JsonLDWriteContext ctx = new JsonLDWriteContext(); if (canUseCustomJsonLdProfile(profile)) { LOGGER.debug("Setting JSON-LD context with profile: {}", profile); final String c = cache.get(profile, p -> { try (final TypedInputStream res = HttpOp.execHttpGet(profile)) { return IOUtils.toString(res.getInputStream(), UTF_8); } catch (final IOException | HttpException ex) { LOGGER.warn("Error fetching profile {}: {}", p, ex.getMessage()); return null; } }); if (nonNull(c)) { ctx.setJsonLDContext(c); ctx.setJsonLDContextSubstitution("\"" + profile + "\""); } } writer.write(output, graph, pm, base, ctx); }
@BeforeEach @SuppressWarnings("unchecked") public void setUp() { initMocks(this); testService = new WebACService(mockResourceService); when(mockCache.get(anyString(), any(Function.class))).thenAnswer(inv -> { final String key = inv.getArgument(0); final Function<String, String> mapper = inv.getArgument(1); return mapper.apply(key); }); setUpResourceService(); setUpChildResource(); setUpRootResource(); setUpMemberResource(); when(mockResource.hasAcl()).thenReturn(false); when(mockResource.getIdentifier()).thenReturn(resourceIRI); when(mockResource.getInteractionModel()).thenReturn(LDP.RDFSource); when(mockResource.getMembershipResource()).thenReturn(empty()); when(mockParentResource.hasAcl()).thenReturn(false); when(mockParentResource.getIdentifier()).thenReturn(parentIRI); when(mockParentResource.getInteractionModel()).thenReturn(LDP.Container); when(mockParentResource.getMembershipResource()).thenReturn(empty()); when(mockSession.getAgent()).thenReturn(agentIRI); when(mockSession.getDelegatedBy()).thenReturn(empty()); }