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.auth.webac;
017
018import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
019import static org.fcrepo.auth.webac.URIConstants.WEBAC_ACCESS_CONTROL_VALUE;
020import static org.slf4j.LoggerFactory.getLogger;
021
022import javax.jcr.Session;
023import javax.ws.rs.core.Link;
024import javax.ws.rs.core.UriInfo;
025
026import org.fcrepo.http.commons.api.UriAwareHttpHeaderFactory;
027import org.fcrepo.http.commons.session.SessionFactory;
028import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
029import org.fcrepo.kernel.api.models.FedoraResource;
030import org.fcrepo.kernel.api.services.NodeService;
031import org.fcrepo.kernel.modeshape.rdf.impl.DefaultIdentifierTranslator;
032import org.fcrepo.kernel.modeshape.rdf.impl.PropertiesRdfContext;
033
034import org.slf4j.Logger;
035
036import com.google.common.collect.ArrayListMultimap;
037import com.google.common.collect.ListMultimap;
038import com.google.common.collect.Multimap;
039import com.hp.hpl.jena.rdf.model.Model;
040import com.hp.hpl.jena.rdf.model.Resource;
041
042import org.springframework.beans.factory.annotation.Autowired;
043import org.springframework.stereotype.Component;
044
045/**
046 * Insert WebAC Link headers to responses
047 *
048 * @author whikloj
049 * @since 2015-10-30
050 */
051@Component
052public class LinkHeaderProvider implements UriAwareHttpHeaderFactory {
053
054    private static final Logger LOGGER = getLogger(LinkHeaderProvider.class);
055
056    @Autowired
057    private SessionFactory sessionFactory;
058
059    @Autowired
060    private NodeService nodeService;
061
062    @Override
063    public Multimap<String, String> createHttpHeadersForResource(final UriInfo uriInfo, final FedoraResource resource) {
064
065        final Session internalSession = sessionFactory.getInternalSession();
066        final IdentifierConverter<Resource, FedoraResource> translator =
067                new DefaultIdentifierTranslator(internalSession);
068        final Model model = createDefaultModel();
069        final ListMultimap<String, String> headers = ArrayListMultimap.create();
070
071        LOGGER.debug("Adding WebAC Link Header for Resource: {}", resource);
072
073        nodeService.find(internalSession, resource.getPath()).getTriples(translator, PropertiesRdfContext.class)
074        .filter(t -> model.asStatement(t).getPredicate().hasURI(WEBAC_ACCESS_CONTROL_VALUE))
075        .filter(t -> t.getObject().isURI())
076        .forEachRemaining(t -> {
077                    headers.put("Link", Link.fromUri(uriInfo.getBaseUriBuilder()
078                            .path(translator.convert(model.asStatement(t).getObject().asResource()).getPath())
079                            .toString()).rel("acl").build().toString());
080        });
081
082        return headers;
083    }
084
085
086}