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.kernel.api.exception;
019
020import static java.time.ZoneOffset.UTC;
021import static java.time.format.DateTimeFormatter.ISO_INSTANT;
022
023import java.time.format.DateTimeFormatter;
024import java.util.Objects;
025
026import org.fcrepo.kernel.api.models.FedoraResource;
027
028/**
029 * Exception when a Tombstone {@link org.fcrepo.kernel.api.models.FedoraResource}
030 * is used where a real object is expected
031 *
032 * @author cabeer
033 * @since 10/16/14
034 */
035public class TombstoneException extends RepositoryRuntimeException {
036
037    private static final long serialVersionUID = 1L;
038
039    private final String uri;
040
041    private static DateTimeFormatter isoFormatter = ISO_INSTANT.withZone(UTC);
042
043    /**
044     * Construct a new tombstone exception for a resource
045     * @param resource the fedora resource
046     */
047    public TombstoneException(final FedoraResource resource) {
048        this(resource, null);
049    }
050
051    /**
052     * Create a new tombstone exception with a URI to the tombstone resource
053     * @param resource the fedora resource
054     * @param tombstoneUri the uri to the tombstone resource for the Link header.
055     */
056    public TombstoneException(final FedoraResource resource, final String tombstoneUri) {
057        super("Discovered tombstone resource at " + resource.getFedoraId().getFullIdPath() +
058                (Objects.nonNull(resource.getLastModifiedDate()) ? ", departed at: " +
059                isoFormatter.format(resource.getLastModifiedDate()) : ""));
060        this.uri = tombstoneUri;
061    }
062
063    /**
064     * Get a URI to the tombstone resource
065     * @return the URI to the tombstone resource
066     */
067    public String getURI() {
068        return uri;
069    }
070}