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.api.url;
019
020import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel;
021import static org.apache.jena.rdf.model.ResourceFactory.createResource;
022import static java.util.Collections.singletonMap;
023import static org.fcrepo.kernel.api.FedoraTypes.FEDORA_REPOSITORY_ROOT;
024import static org.fcrepo.kernel.api.RdfLexicon.HAS_FIXITY_SERVICE;
025import static org.fcrepo.kernel.api.RdfLexicon.HAS_TRANSACTION_SERVICE;
026import static org.fcrepo.kernel.api.RdfLexicon.HAS_VERSION_HISTORY;
027
028import org.apache.jena.rdf.model.Model;
029import org.apache.jena.rdf.model.Resource;
030
031import org.fcrepo.http.api.FedoraVersioning;
032import org.fcrepo.http.api.repository.FedoraRepositoryTransactions;
033import org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory;
034import org.fcrepo.kernel.api.models.FedoraBinary;
035import org.fcrepo.kernel.api.models.FedoraResource;
036import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
037
038import org.springframework.stereotype.Component;
039
040import javax.ws.rs.core.UriInfo;
041
042import java.util.Map;
043
044/**
045 * Inject our HTTP API methods into the object graphs
046 *
047 * @author awoods
048 */
049@Component
050public class HttpApiResources implements UriAwareResourceModelFactory {
051
052    @Override
053    public Model createModelForResource(final FedoraResource resource,
054        final UriInfo uriInfo, final IdentifierConverter<Resource,FedoraResource> idTranslator) {
055
056        final Model model = createDefaultModel();
057
058        final Resource s = idTranslator.reverse().convert(resource);
059
060        if (resource.hasType(FEDORA_REPOSITORY_ROOT)) {
061            addRepositoryStatements(uriInfo, model, s);
062        } else {
063            addNodeStatements(resource, uriInfo, model, s);
064        }
065
066        if (resource.getDescribedResource() instanceof FedoraBinary) {
067            addContentStatements(idTranslator, (FedoraBinary)resource.getDescribedResource(), model);
068        }
069        return model;
070    }
071
072    private static void addContentStatements(final IdentifierConverter<Resource,FedoraResource> idTranslator,
073                                             final FedoraBinary resource,
074                                             final Model model) {
075        // fcr:fixity
076        final Resource subject = idTranslator.reverse().convert(resource);
077        model.add(subject, HAS_FIXITY_SERVICE, createResource(subject.getURI() +
078                "/fcr:fixity"));
079    }
080
081    private void addNodeStatements(final FedoraResource resource, final UriInfo uriInfo,
082        final Model model, final Resource s) {
083
084        final Map<String, String> pathMap = singletonMap("path", resource.getPath().substring(1));
085
086        // fcr:versions
087        if (resource.isVersioned()) {
088            model.add(s, HAS_VERSION_HISTORY, createResource(uriInfo
089                    .getBaseUriBuilder().path(FedoraVersioning.class).buildFromMap(
090                            pathMap, false).toASCIIString()));
091        }
092    }
093
094    private void addRepositoryStatements(final UriInfo uriInfo, final Model model,
095        final Resource s) {
096        // fcr:tx
097        model.add(s, HAS_TRANSACTION_SERVICE, createResource(uriInfo
098                .getBaseUriBuilder().path(FedoraRepositoryTransactions.class)
099                .build().toASCIIString()));
100    }
101
102}