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.MediaType.APPLICATION_XHTML_XML;
021import static javax.ws.rs.core.MediaType.APPLICATION_XML;
022import static javax.ws.rs.core.MediaType.TEXT_HTML;
023import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
024import static org.fcrepo.http.commons.domain.RDFMediaType.JSON_LD;
025import static org.fcrepo.http.commons.domain.RDFMediaType.N3;
026import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2;
027import static org.fcrepo.http.commons.domain.RDFMediaType.NTRIPLES;
028import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
029import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
030import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_X;
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;
038
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 + ";qs=1.0", JSON_LD + ";qs=0.8",
091            N3, N3_ALT2, RDF_XML, NTRIPLES, APPLICATION_XML, TEXT_PLAIN, TURTLE_X,
092            TEXT_HTML, APPLICATION_XHTML_XML, "*/*"})
093    public RdfNamespacedStream getDatastreamFixity() {
094
095        if (!(resource() instanceof FedoraBinary)) {
096            throw new NotFoundException(resource() + " is not a binary");
097        }
098
099        LOGGER.info("Get fixity for '{}'", externalPath);
100        return new RdfNamespacedStream(
101                new DefaultRdfStream(asNode(resource()),
102                    ((FedoraBinary)resource()).getFixity(translator())),
103                namespaceService.getNamespaces(session()));
104    }
105
106    @Override
107    protected String externalPath() {
108        return externalPath;
109    }
110}