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.fcrepo.kernel.modeshape.utils.NamespaceTools.getNamespaces; 030import static org.slf4j.LoggerFactory.getLogger; 031 032import javax.ws.rs.GET; 033import javax.ws.rs.NotFoundException; 034import javax.ws.rs.Path; 035import javax.ws.rs.PathParam; 036import javax.ws.rs.Produces; 037 038import com.google.common.annotations.VisibleForTesting; 039import org.fcrepo.http.commons.responses.HtmlTemplate; 040import org.fcrepo.http.commons.responses.RdfNamespacedStream; 041import org.fcrepo.kernel.api.models.FedoraBinary; 042import org.fcrepo.kernel.api.rdf.DefaultRdfStream; 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 @PathParam("path") protected String externalPath; 061 062 063 /** 064 * Default JAX-RS entry point 065 */ 066 public FedoraFixity() { 067 super(); 068 } 069 070 /** 071 * Create a new FedoraNodes instance for a given path 072 * @param externalPath the external path 073 */ 074 @VisibleForTesting 075 public FedoraFixity(final String externalPath) { 076 this.externalPath = externalPath; 077 } 078 079 /** 080 * Get the results of a fixity check for a path 081 * 082 * GET /path/to/some/datastream/fcr:fixity 083 * 084 * @return datastream fixity in the given format 085 */ 086 @GET 087 @Timed 088 @HtmlTemplate(value = "fcr:fixity") 089 @Produces({TURTLE + ";qs=10", JSON_LD + ";qs=8", 090 N3, N3_ALT2, RDF_XML, NTRIPLES, APPLICATION_XML, TEXT_PLAIN, TURTLE_X, 091 TEXT_HTML, APPLICATION_XHTML_XML, "*/*"}) 092 public RdfNamespacedStream getDatastreamFixity() { 093 094 if (!(resource() instanceof FedoraBinary)) { 095 throw new NotFoundException(resource() + " is not a binary"); 096 } 097 098 LOGGER.info("Get fixity for '{}'", externalPath); 099 return new RdfNamespacedStream( 100 new DefaultRdfStream(asNode(resource()), 101 ((FedoraBinary)resource()).getFixity(translator())), 102 getNamespaces(session())); 103 } 104 105 @Override 106 protected String externalPath() { 107 return externalPath; 108 } 109}