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