001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.http.api;
017
018import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
019import static javax.ws.rs.core.MediaType.APPLICATION_XML;
020import static javax.ws.rs.core.MediaType.TEXT_HTML;
021import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
022import static org.fcrepo.http.commons.domain.RDFMediaType.JSON_LD;
023import static org.fcrepo.http.commons.domain.RDFMediaType.N3;
024import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2;
025import static org.fcrepo.http.commons.domain.RDFMediaType.NTRIPLES;
026import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
027import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
028import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_X;
029import static org.slf4j.LoggerFactory.getLogger;
030
031import javax.inject.Inject;
032import javax.jcr.Session;
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.kernel.models.FedoraBinary;
042import org.fcrepo.kernel.utils.iterators.RdfStream;
043import org.slf4j.Logger;
044import org.springframework.context.annotation.Scope;
045
046import com.codahale.metrics.annotation.Timed;
047
048/**
049 * Run a fixity check on a path
050 *
051 * @author ajs6f
052 * @since Jun 12, 2013
053 */
054@Scope("request")
055@Path("/{path: .*}/fcr:fixity")
056public class FedoraFixity extends ContentExposingResource {
057
058    private static final Logger LOGGER = getLogger(FedoraFixity.class);
059
060    @Inject
061    protected Session session;
062
063    @PathParam("path") protected String externalPath;
064
065
066    /**
067     * Default JAX-RS entry point
068     */
069    public FedoraFixity() {
070        super();
071    }
072
073    /**
074     * Create a new FedoraNodes instance for a given path
075     * @param externalPath the external path
076     */
077    @VisibleForTesting
078    public FedoraFixity(final String externalPath) {
079        this.externalPath = externalPath;
080    }
081
082    /**
083     * Get the results of a fixity check for a path
084     *
085     * GET /path/to/some/datastream/fcr:fixity
086     *
087     * @return datastream fixity in the given format
088     */
089    @GET
090    @Timed
091    @HtmlTemplate(value = "fcr:fixity")
092    @Produces({TURTLE + ";qs=10", JSON_LD + ";qs=8",
093            N3, N3_ALT2, RDF_XML, NTRIPLES, APPLICATION_XML, TEXT_PLAIN, TURTLE_X,
094            TEXT_HTML, APPLICATION_XHTML_XML, "*/*"})
095    public RdfStream getDatastreamFixity() {
096
097        if (!(resource() instanceof FedoraBinary)) {
098            throw new NotFoundException(resource() + " is not a binary");
099        }
100
101        LOGGER.info("Get fixity for '{}'", externalPath);
102        return ((FedoraBinary)resource()).getFixity(translator())
103                .topic(translator().reverse().convert(resource()).asNode())
104                .session(session);
105
106    }
107
108    @Override
109    protected Session session() {
110        return session;
111    }
112
113    @Override
114    protected String externalPath() {
115        return externalPath;
116    }
117}