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 org.fcrepo.kernel.api.FedoraTypes.FEDORA_REPOSITORY_ROOT;
023import static org.fcrepo.kernel.api.RdfLexicon.HAS_FIXITY_SERVICE;
024import static org.fcrepo.kernel.api.RdfLexicon.HAS_TRANSACTION_SERVICE;
025
026import org.apache.jena.rdf.model.Model;
027import org.apache.jena.rdf.model.Resource;
028
029import org.fcrepo.http.api.repository.FedoraRepositoryTransactions;
030import org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory;
031import org.fcrepo.kernel.api.models.FedoraBinary;
032import org.fcrepo.kernel.api.models.FedoraResource;
033import org.fcrepo.kernel.api.models.NonRdfSourceDescription;
034import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
035
036import org.springframework.stereotype.Component;
037
038import javax.ws.rs.core.UriInfo;
039
040/**
041 * Inject our HTTP API methods into the object graphs
042 *
043 * @author awoods
044 */
045@Component
046public class HttpApiResources implements UriAwareResourceModelFactory {
047
048    @Override
049    public Model createModelForResource(final FedoraResource resource,
050        final UriInfo uriInfo, final IdentifierConverter<Resource,FedoraResource> idTranslator) {
051
052        final Model model = createDefaultModel();
053
054        final Resource s = idTranslator.reverse().convert(resource);
055
056        if (resource.hasType(FEDORA_REPOSITORY_ROOT)) {
057            addRepositoryStatements(uriInfo, model, s);
058        }
059
060        if (resource instanceof NonRdfSourceDescription && !resource.isMemento()) {
061            addContentStatements(idTranslator, (FedoraBinary)resource.getDescribedResource(), model);
062        }
063        return model;
064    }
065
066    private static void addContentStatements(final IdentifierConverter<Resource,FedoraResource> idTranslator,
067                                             final FedoraBinary resource,
068                                             final Model model) {
069        // fcr:fixity
070        final Resource subject = idTranslator.reverse().convert(resource);
071        model.add(subject, HAS_FIXITY_SERVICE, createResource(subject.getURI() +
072                "/fcr:fixity"));
073    }
074
075
076    private void addRepositoryStatements(final UriInfo uriInfo, final Model model,
077        final Resource s) {
078        // fcr:tx
079        model.add(s, HAS_TRANSACTION_SERVICE, createResource(uriInfo
080                .getBaseUriBuilder().path(FedoraRepositoryTransactions.class)
081                .build().toASCIIString()));
082    }
083
084}