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 org.apache.jena.rdf.model.Statement; 032 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 * @return an RdfStream with the added triples 066 */ 067 public RdfStream addHttpComponentModelsForResourceToStream(final RdfStream rdfStream, 068 final FedoraResource resource, final UriInfo uriInfo) { 069 070 LOGGER.debug("Adding additional HTTP context triples to stream"); 071 return new DefaultRdfStream(rdfStream.topic(), concat(rdfStream, getUriAwareTripleFactories().entrySet() 072 .stream().flatMap(e -> { 073 LOGGER.debug("Adding response information using: {}", e.getKey()); 074 return stream(spliteratorUnknownSize(e.getValue().createModelForResource(resource, uriInfo) 075 .listStatements(), IMMUTABLE), false).map(Statement::asTriple); 076 }))); 077 } 078 079 private Map<String, UriAwareResourceModelFactory> getUriAwareTripleFactories() { 080 return applicationContext 081 .getBeansOfType(UriAwareResourceModelFactory.class); 082 } 083}