001/*
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.http.commons.api.rdf;
017
018import static java.util.Spliterator.IMMUTABLE;
019import static java.util.Spliterators.spliteratorUnknownSize;
020import static java.util.stream.Stream.concat;
021import static java.util.stream.StreamSupport.stream;
022
023import static org.slf4j.LoggerFactory.getLogger;
024
025import java.util.Map;
026
027import javax.ws.rs.core.UriInfo;
028
029import com.hp.hpl.jena.rdf.model.Resource;
030import com.hp.hpl.jena.rdf.model.Statement;
031
032import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
033import org.fcrepo.kernel.api.models.FedoraResource;
034import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
035import org.fcrepo.kernel.api.RdfStream;
036
037import org.slf4j.Logger;
038import org.springframework.context.ApplicationContext;
039import org.springframework.context.ApplicationContextAware;
040import org.springframework.stereotype.Component;
041
042/**
043 * Utility for injecting HTTP-contextual data into an RdfStream
044 *
045 * @author awoods
046 */
047@Component
048public class HttpTripleUtil implements ApplicationContextAware {
049
050    private static final Logger LOGGER = getLogger(HttpTripleUtil.class);
051
052    private ApplicationContext applicationContext;
053
054    @Override
055    public void setApplicationContext(final ApplicationContext applicationContext) {
056        this.applicationContext = applicationContext;
057    }
058
059    /**
060     * Add additional models to the RDF dataset for the given resource
061     *
062     * @param rdfStream the source stream we'll add named models to
063     * @param resource the FedoraResourceImpl in question
064     * @param uriInfo a JAX-RS UriInfo object to build URIs to resources
065     * @param idTranslator the id translator
066     * @return an RdfStream with the added triples
067     */
068    public RdfStream addHttpComponentModelsForResourceToStream(final RdfStream rdfStream,
069            final FedoraResource resource, final UriInfo uriInfo,
070            final IdentifierConverter<Resource,FedoraResource> idTranslator) {
071
072        LOGGER.debug("Adding additional HTTP context triples to stream");
073        return new DefaultRdfStream(rdfStream.topic(), concat(rdfStream, getUriAwareTripleFactories().entrySet()
074                    .stream().flatMap(e -> {
075            LOGGER.debug("Adding response information using: {}", e.getKey());
076            return stream(spliteratorUnknownSize(e.getValue().createModelForResource(resource, uriInfo,
077                        idTranslator).listStatements(), IMMUTABLE), false).map(Statement::asTriple);
078        })));
079    }
080
081    private Map<String, UriAwareResourceModelFactory> getUriAwareTripleFactories() {
082        return applicationContext
083                .getBeansOfType(UriAwareResourceModelFactory.class);
084    }
085}