001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.kernel.api.exception;
007
008import static java.time.ZoneOffset.UTC;
009import static java.time.format.DateTimeFormatter.ISO_INSTANT;
010
011import java.time.format.DateTimeFormatter;
012import java.util.Objects;
013
014import org.fcrepo.kernel.api.models.FedoraResource;
015
016/**
017 * Exception when a Tombstone {@link org.fcrepo.kernel.api.models.FedoraResource}
018 * is used where a real object is expected
019 *
020 * @author cabeer
021 * @since 10/16/14
022 */
023public class TombstoneException extends RepositoryRuntimeException {
024
025    private static final long serialVersionUID = 1L;
026
027    private final String uri;
028
029    private static DateTimeFormatter isoFormatter = ISO_INSTANT.withZone(UTC);
030
031    /**
032     * Construct a new tombstone exception for a resource
033     * @param resource the fedora resource
034     */
035    public TombstoneException(final FedoraResource resource) {
036        this(resource, null);
037    }
038
039    /**
040     * Create a new tombstone exception with a URI to the tombstone resource
041     * @param resource the fedora resource
042     * @param tombstoneUri the uri to the tombstone resource for the Link header.
043     */
044    public TombstoneException(final FedoraResource resource, final String tombstoneUri) {
045        super("Discovered tombstone resource at " + resource.getFedoraId().getFullIdPath() +
046                (Objects.nonNull(resource.getLastModifiedDate()) ? ", departed at: " +
047                isoFormatter.format(resource.getLastModifiedDate()) : ""));
048        this.uri = tombstoneUri;
049    }
050
051    /**
052     * Get a URI to the tombstone resource
053     * @return the URI to the tombstone resource
054     */
055    public String getURI() {
056        return uri;
057    }
058}