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
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    @HtmlTemplate(value = "fcr:fixity")
087    @Produces({TURTLE_WITH_CHARSET + ";qs=1.0", JSON_LD + ";qs=0.8", N3_WITH_CHARSET, N3_ALT2_WITH_CHARSET,
088            RDF_XML, NTRIPLES, TEXT_PLAIN_WITH_CHARSET, TURTLE_X, TEXT_HTML_WITH_CHARSET, "*/*"})
089    public RdfNamespacedStream getDatastreamFixity() {
090
091        if (!(resource() instanceof FedoraBinary)) {
092            throw new NotFoundException(resource() + " is not a binary");
093        }
094
095        final Link.Builder resourceLink = Link.fromUri(LDP_NAMESPACE + "Resource").rel("type");
096        servletResponse.addHeader(LINK, resourceLink.build().toString());
097        final Link.Builder rdfSourceLink = Link.fromUri(LDP_NAMESPACE + "RDFSource").rel("type");
098        servletResponse.addHeader(LINK, rdfSourceLink.build().toString());
099
100        LOGGER.info("Get fixity for '{}'", externalPath);
101        return new RdfNamespacedStream(
102                new DefaultRdfStream(asNode(resource()),
103                    ((FedoraBinary)resource()).getFixity(translator())),
104                namespaceRegistry.getNamespaces());
105    }
106
107    @Override
108    protected String externalPath() {
109        return externalPath;
110    }
111}