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 com.google.common.annotations.VisibleForTesting;
019import org.fcrepo.kernel.api.models.FedoraResource;
020import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
021import org.slf4j.Logger;
022import org.springframework.context.annotation.Scope;
023
024import javax.jcr.RepositoryException;
025import javax.ws.rs.DELETE;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.core.Response;
029
030import static javax.ws.rs.core.Response.noContent;
031import static org.slf4j.LoggerFactory.getLogger;
032
033/**
034 * CRUD operations on Fedora tombstones
035 *
036 * @author cbeer
037 */
038@Scope("request")
039@Path("/{path: .*}/fcr:tombstone")
040public class FedoraTombstones extends FedoraBaseResource {
041
042    private static final Logger LOGGER = getLogger(FedoraTombstones.class);
043
044    @PathParam("path") protected String externalPath;
045
046    /**
047     * Default JAX-RS entry point
048     */
049    public FedoraTombstones() {
050        super();
051    }
052
053    /**
054     * Create a new FedoraNodes instance for a given path
055     * @param externalPath the external path
056     */
057    @VisibleForTesting
058    public FedoraTombstones(final String externalPath) {
059        this.externalPath = externalPath;
060    }
061
062
063    /**
064     * Delete a tombstone resource (freeing the original resource to be reused)
065     * @return the free resource
066     */
067    @DELETE
068    public Response delete() {
069        LOGGER.info("Delete tombstone: {}", resource());
070        resource().delete();
071
072        try {
073            session.save();
074        } catch (RepositoryException e) {
075            throw new RepositoryRuntimeException(e);
076        }
077
078        return noContent().build();
079    }
080
081    protected FedoraResource resource() {
082        return translator().convert(translator().toDomain(externalPath));
083    }
084}