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 com.hp.hpl.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.Node;
030import javax.jcr.Property;
031import javax.jcr.RepositoryException;
032import javax.jcr.Session;
033import javax.jcr.Value;
034
035import java.util.Objects;
036import java.util.stream.Stream;
037import java.util.function.Function;
038
039import static org.fcrepo.kernel.api.RequiredRdfContext.PROPERTIES;
040import static org.fcrepo.kernel.modeshape.identifiers.NodeResourceConverter.nodeConverter;
041import static org.fcrepo.kernel.modeshape.rdf.converters.ValueConverter.nodeForValue;
042import static org.fcrepo.kernel.modeshape.rdf.impl.ReferencesRdfContext.REFERENCE_TYPES;
043import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.getJcrNode;
044import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isSkolemNode;
045import static org.fcrepo.kernel.modeshape.utils.UncheckedPredicate.uncheck;
046import static org.fcrepo.kernel.modeshape.utils.StreamUtils.iteratorToStream;
047import static org.slf4j.LoggerFactory.getLogger;
048
049/**
050 * Embed triples describing all skolem nodes in the RDF stream
051 *
052 * @author cabeer
053 * @author ajs6f
054 * @since 10/9/14
055 */
056public class SkolemNodeRdfContext extends NodeRdfContext {
057
058    private static final Logger LOGGER = getLogger(SkolemNodeRdfContext.class);
059
060    /**
061     * Default constructor.
062     *
063     * @param resource the resource
064     * @param idTranslator the idTranslator
065     * @throws RepositoryException if a repository exception occurred
066     */
067    public SkolemNodeRdfContext(final FedoraResource resource,
068                               final IdentifierConverter<Resource, FedoraResource> idTranslator)
069            throws RepositoryException {
070        super(resource, idTranslator);
071
072        concat(getBlankNodes(resource).flatMap(n -> nodeConverter.convert(n).getTriples(idTranslator,
073                    PROPERTIES)));
074    }
075
076    @SuppressWarnings("unchecked")
077    private static Stream<Node> getBlankNodes(final FedoraResource resource) throws RepositoryException {
078        final Function<Value, Node> valueToNode = sessionValueToNode.apply(getJcrNode(resource).getSession());
079        final Stream<Property> refs = iteratorToStream(getJcrNode(resource).getProperties())
080                .filter(uncheck((final Property p) -> REFERENCE_TYPES.contains(p.getType())));
081        return iteratorToStream(new PropertyValueIterator(refs.iterator()))
082                .map(valueToNode)
083                .filter(Objects::nonNull)
084                .filter(isSkolemNode);
085    }
086
087    private static final Function<Session, Function<Value, Node>> sessionValueToNode = session -> v -> {
088        try {
089            return nodeForValue(session, v);
090
091        } catch (final AccessDeniedException e) {
092            LOGGER.error("Link inaccessible by requesting user: {}, {}", v, session.getUserID());
093            return null;
094
095        } catch (final RepositoryException e) {
096            throw new RepositoryRuntimeException(e);
097        }
098    };
099}