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