001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.kernel.modeshape.rdf.impl; 019 020import org.apache.jena.rdf.model.Resource; 021 022import org.fcrepo.kernel.api.exception.RepositoryRuntimeException; 023import org.fcrepo.kernel.api.models.FedoraResource; 024import org.fcrepo.kernel.api.identifiers.IdentifierConverter; 025import org.fcrepo.kernel.modeshape.rdf.impl.mappings.PropertyValueIterator; 026import org.slf4j.Logger; 027 028import javax.jcr.AccessDeniedException; 029import javax.jcr.ItemNotFoundException; 030import javax.jcr.Node; 031import javax.jcr.Property; 032import javax.jcr.RepositoryException; 033import javax.jcr.Session; 034import javax.jcr.Value; 035 036import java.util.Objects; 037import java.util.stream.Stream; 038import java.util.function.Function; 039 040import static org.fcrepo.kernel.api.RequiredRdfContext.PROPERTIES; 041import static org.fcrepo.kernel.modeshape.identifiers.NodeResourceConverter.nodeConverter; 042import static org.fcrepo.kernel.modeshape.rdf.converters.ValueConverter.nodeForValue; 043import static org.fcrepo.kernel.modeshape.rdf.impl.ReferencesRdfContext.REFERENCE_TYPES; 044import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.getJcrNode; 045import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isSkolemNode; 046import static org.fcrepo.kernel.modeshape.utils.UncheckedPredicate.uncheck; 047import static org.fcrepo.kernel.modeshape.utils.StreamUtils.iteratorToStream; 048import static org.slf4j.LoggerFactory.getLogger; 049 050/** 051 * Embed triples describing all skolem nodes in the RDF stream 052 * 053 * @author cabeer 054 * @author ajs6f 055 * @since 10/9/14 056 */ 057public class SkolemNodeRdfContext extends NodeRdfContext { 058 059 private static final Logger LOGGER = getLogger(SkolemNodeRdfContext.class); 060 061 /** 062 * Default constructor. 063 * 064 * @param resource the resource 065 * @param idTranslator the idTranslator 066 * @throws RepositoryException if a repository exception occurred 067 */ 068 public SkolemNodeRdfContext(final FedoraResource resource, 069 final IdentifierConverter<Resource, FedoraResource> idTranslator) 070 throws RepositoryException { 071 super(resource, idTranslator); 072 073 concat(getBlankNodes(resource).flatMap(n -> nodeConverter.convert(n).getTriples(idTranslator, 074 PROPERTIES))); 075 } 076 077 @SuppressWarnings("unchecked") 078 private static Stream<Node> getBlankNodes(final FedoraResource resource) throws RepositoryException { 079 final Function<Value, Node> valueToNode = sessionValueToNode.apply(getJcrNode(resource).getSession()); 080 final Stream<Property> refs = iteratorToStream(getJcrNode(resource).getProperties()) 081 .filter(uncheck((final Property p) -> REFERENCE_TYPES.contains(p.getType()))); 082 return iteratorToStream(new PropertyValueIterator(refs.iterator())) 083 .map(valueToNode) 084 .filter(Objects::nonNull) 085 .filter(isSkolemNode); 086 } 087 088 private static final Function<Session, Function<Value, Node>> sessionValueToNode = session -> v -> { 089 try { 090 return nodeForValue(session, v); 091 092 } catch (final AccessDeniedException e) { 093 LOGGER.error("Link inaccessible by requesting user: {}, {}", v, session.getUserID()); 094 return null; 095 096 } catch (final RepositoryException e) { 097 if (e instanceof ItemNotFoundException) { 098 // Effectively skip resources that do not exist 099 LOGGER.warn("Reference to non-existent resource encounterd: {}", v); 100 return null; 101 } 102 throw new RepositoryRuntimeException(e); 103 } 104 }; 105}