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