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.http.commons.api.rdf;
019
020import static java.util.Spliterator.IMMUTABLE;
021import static java.util.Spliterators.spliteratorUnknownSize;
022import static java.util.stream.Stream.concat;
023import static java.util.stream.StreamSupport.stream;
024
025import static org.slf4j.LoggerFactory.getLogger;
026
027import java.util.Map;
028
029import javax.ws.rs.core.UriInfo;
030
031import com.hp.hpl.jena.rdf.model.Resource;
032import com.hp.hpl.jena.rdf.model.Statement;
033
034import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
035import org.fcrepo.kernel.api.models.FedoraResource;
036import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
037import org.fcrepo.kernel.api.RdfStream;
038
039import org.slf4j.Logger;
040import org.springframework.context.ApplicationContext;
041import org.springframework.context.ApplicationContextAware;
042import org.springframework.stereotype.Component;
043
044/**
045 * Utility for injecting HTTP-contextual data into an RdfStream
046 *
047 * @author awoods
048 */
049@Component
050public class HttpTripleUtil implements ApplicationContextAware {
051
052    private static final Logger LOGGER = getLogger(HttpTripleUtil.class);
053
054    private ApplicationContext applicationContext;
055
056    @Override
057    public void setApplicationContext(final ApplicationContext applicationContext) {
058        this.applicationContext = applicationContext;
059    }
060
061    /**
062     * Add additional models to the RDF dataset for the given resource
063     *
064     * @param rdfStream the source stream we'll add named models to
065     * @param resource the FedoraResourceImpl in question
066     * @param uriInfo a JAX-RS UriInfo object to build URIs to resources
067     * @param idTranslator the id translator
068     * @return an RdfStream with the added triples
069     */
070    public RdfStream addHttpComponentModelsForResourceToStream(final RdfStream rdfStream,
071            final FedoraResource resource, final UriInfo uriInfo,
072            final IdentifierConverter<Resource,FedoraResource> idTranslator) {
073
074        LOGGER.debug("Adding additional HTTP context triples to stream");
075        return new DefaultRdfStream(rdfStream.topic(), concat(rdfStream, getUriAwareTripleFactories().entrySet()
076                    .stream().flatMap(e -> {
077            LOGGER.debug("Adding response information using: {}", e.getKey());
078            return stream(spliteratorUnknownSize(e.getValue().createModelForResource(resource, uriInfo,
079                        idTranslator).listStatements(), IMMUTABLE), false).map(Statement::asTriple);
080        })));
081    }
082
083    private Map<String, UriAwareResourceModelFactory> getUriAwareTripleFactories() {
084        return applicationContext
085                .getBeansOfType(UriAwareResourceModelFactory.class);
086    }
087}