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;
019
020import static javax.ws.rs.core.HttpHeaders.LINK;
021import static org.fcrepo.http.commons.domain.RDFMediaType.JSON_LD;
022import static org.fcrepo.http.commons.domain.RDFMediaType.N3_WITH_CHARSET;
023import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2_WITH_CHARSET;
024import static org.fcrepo.http.commons.domain.RDFMediaType.NTRIPLES;
025import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
026import static org.fcrepo.http.commons.domain.RDFMediaType.TEXT_HTML_WITH_CHARSET;
027import static org.fcrepo.http.commons.domain.RDFMediaType.TEXT_PLAIN_WITH_CHARSET;
028import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_WITH_CHARSET;
029import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_X;
030import static org.fcrepo.kernel.api.RdfLexicon.LDP_NAMESPACE;
031import static org.slf4j.LoggerFactory.getLogger;
032
033import javax.ws.rs.GET;
034import javax.ws.rs.NotFoundException;
035import javax.ws.rs.Path;
036import javax.ws.rs.PathParam;
037import javax.ws.rs.Produces;
038import javax.ws.rs.core.Link;
039import com.google.common.annotations.VisibleForTesting;
040import org.fcrepo.http.commons.responses.HtmlTemplate;
041import org.fcrepo.http.commons.responses.RdfNamespacedStream;
042import org.fcrepo.kernel.api.models.FedoraBinary;
043import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
044import org.slf4j.Logger;
045import org.springframework.context.annotation.Scope;
046
047import com.codahale.metrics.annotation.Timed;
048
049/**
050 * Run a fixity check on a path
051 *
052 * @author ajs6f
053 * @since Jun 12, 2013
054 */
055@Scope("request")
056@Path("/{path: .*}/fcr:fixity")
057public class FedoraFixity extends ContentExposingResource {
058
059    private static final Logger LOGGER = getLogger(FedoraFixity.class);
060
061    @PathParam("path") protected String externalPath;
062
063
064    /**
065     * Default JAX-RS entry point
066     */
067    public FedoraFixity() {
068        super();
069    }
070
071    /**
072     * Create a new FedoraNodes instance for a given path
073     * @param externalPath the external path
074     */
075    @VisibleForTesting
076    public FedoraFixity(final String externalPath) {
077        this.externalPath = externalPath;
078    }
079
080    /**
081     * Get the results of a fixity check for a path
082     *
083     * GET /path/to/some/datastream/fcr:fixity
084     *
085     * @return datastream fixity in the given format
086     */
087    @GET
088    @Timed
089    @HtmlTemplate(value = "fcr:fixity")
090    @Produces({TURTLE_WITH_CHARSET + ";qs=1.0", JSON_LD + ";qs=0.8", N3_WITH_CHARSET, N3_ALT2_WITH_CHARSET,
091            RDF_XML, NTRIPLES, TEXT_PLAIN_WITH_CHARSET, TURTLE_X, TEXT_HTML_WITH_CHARSET, "*/*"})
092    public RdfNamespacedStream getDatastreamFixity() {
093
094        if (!(resource() instanceof FedoraBinary)) {
095            throw new NotFoundException(resource() + " is not a binary");
096        }
097
098        final Link.Builder resourceLink = Link.fromUri(LDP_NAMESPACE + "Resource").rel("type");
099        servletResponse.addHeader(LINK, resourceLink.build().toString());
100        final Link.Builder rdfSourceLink = Link.fromUri(LDP_NAMESPACE + "RDFSource").rel("type");
101        servletResponse.addHeader(LINK, rdfSourceLink.build().toString());
102
103        LOGGER.info("Get fixity for '{}'", externalPath);
104        return new RdfNamespacedStream(
105                new DefaultRdfStream(asNode(resource()),
106                    ((FedoraBinary)resource()).getFixity(translator())),
107                session().getFedoraSession().getNamespaces());
108    }
109
110    @Override
111    protected String externalPath() {
112        return externalPath;
113    }
114}