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.ALLOW;
021import static javax.ws.rs.core.HttpHeaders.LINK;
022import static org.fcrepo.http.commons.domain.RDFMediaType.JSON_LD;
023import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2_WITH_CHARSET;
024import static org.fcrepo.http.commons.domain.RDFMediaType.N3_WITH_CHARSET;
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.TEXT_HTML_WITH_CHARSET;
028import static org.fcrepo.http.commons.domain.RDFMediaType.TEXT_PLAIN_WITH_CHARSET;
029import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_WITH_CHARSET;
030import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_X;
031import static org.fcrepo.kernel.api.RdfLexicon.RDF_SOURCE;
032import static org.fcrepo.kernel.api.RdfLexicon.RESOURCE;
033import static org.slf4j.LoggerFactory.getLogger;
034
035import javax.inject.Inject;
036import javax.ws.rs.DELETE;
037import javax.ws.rs.GET;
038import javax.ws.rs.HEAD;
039import javax.ws.rs.NotFoundException;
040import javax.ws.rs.OPTIONS;
041import javax.ws.rs.POST;
042import javax.ws.rs.PUT;
043import javax.ws.rs.Path;
044import javax.ws.rs.PathParam;
045import javax.ws.rs.Produces;
046import javax.ws.rs.core.Link;
047import javax.ws.rs.core.Response;
048
049import org.fcrepo.http.commons.domain.PATCH;
050import org.fcrepo.http.commons.responses.HtmlTemplate;
051import org.fcrepo.http.commons.responses.RdfNamespacedStream;
052import org.fcrepo.kernel.api.RdfStream;
053import org.fcrepo.kernel.api.models.Binary;
054import org.fcrepo.kernel.api.services.FixityService;
055
056import org.slf4j.Logger;
057import org.springframework.context.annotation.Scope;
058
059import com.google.common.annotations.VisibleForTesting;
060
061import io.micrometer.core.annotation.Timed;
062
063/**
064 * Run a fixity check on a path
065 *
066 * @author ajs6f
067 * @since Jun 12, 2013
068 */
069@Timed
070@Scope("request")
071@Path("/{path: .*}/fcr:fixity")
072public class FedoraFixity extends ContentExposingResource {
073
074    private static final Logger LOGGER = getLogger(FedoraFixity.class);
075
076    private static final String OPTIONS_VALUES = "OPTIONS, GET";
077
078    @PathParam("path") protected String externalPath;
079
080    @Inject private FixityService fixityService;
081
082    /**
083     * Default JAX-RS entry point
084     */
085    public FedoraFixity() {
086        super();
087    }
088
089    /**
090     * Create a new FedoraNodes instance for a given path
091     * @param externalPath the external path
092     */
093    @VisibleForTesting
094    public FedoraFixity(final String externalPath) {
095        this.externalPath = externalPath;
096    }
097
098    /**
099     * Get the results of a fixity check for a path
100     *
101     * GET /path/to/some/datastream/fcr:fixity
102     *
103     * @return datastream fixity in the given format
104     */
105    @GET
106    @HtmlTemplate(value = "fcr:fixity")
107    @Produces({TURTLE_WITH_CHARSET + ";qs=1.0", JSON_LD + ";qs=0.8", N3_WITH_CHARSET, N3_ALT2_WITH_CHARSET,
108            RDF_XML, NTRIPLES, TEXT_PLAIN_WITH_CHARSET, TURTLE_X, TEXT_HTML_WITH_CHARSET, "*/*"})
109    public RdfNamespacedStream getDatastreamFixity() {
110
111        if (!(resource() instanceof Binary)) {
112            throw new NotFoundException("Error: Resource at " + resource().getFedoraId().getFullIdPath() + " is not a" +
113                    " binary");
114        }
115
116        final Link.Builder resourceLink = Link.fromUri(RESOURCE.getURI()).rel("type");
117        servletResponse.addHeader(LINK, resourceLink.build().toString());
118        final Link.Builder rdfSourceLink = Link.fromUri(RDF_SOURCE.getURI()).rel("type");
119        servletResponse.addHeader(LINK, rdfSourceLink.build().toString());
120
121        final Binary binaryResource = (Binary) resource();
122        LOGGER.info("Get fixity for '{}'", externalPath);
123
124        final RdfStream rdfStream = httpRdfService.bodyToExternalStream(getUri(binaryResource).toString(),
125                fixityService.checkFixity(binaryResource), identifierConverter());
126        return new RdfNamespacedStream(rdfStream, namespaceRegistry.getNamespaces());
127    }
128
129    @Override
130    protected String externalPath() {
131        return externalPath;
132    }
133
134    @OPTIONS
135    public Response options() {
136        return Response.ok().header(ALLOW, OPTIONS_VALUES).build();
137    }
138    /*
139     * These methods are disallowed, but need to exist here or the path gets caught by the FedoraLdp path matcher.
140     */
141    @HEAD
142    public Response get() {
143        return methodNotAllowed();
144    }
145    @POST
146    public Response post() {
147        return methodNotAllowed();
148    }
149    @PUT
150    public Response put() {
151        return methodNotAllowed();
152    }
153    @PATCH
154    public Response patch() {
155        return methodNotAllowed();
156    }
157    @DELETE
158    public Response delete() {
159        return methodNotAllowed();
160    }
161
162    private Response methodNotAllowed() {
163        return Response.status(Response.Status.METHOD_NOT_ALLOWED).header(ALLOW, OPTIONS_VALUES).build();
164    }
165}