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.modeshape.utils;
019
020import org.fcrepo.kernel.api.utils.CacheEntry;
021import org.fcrepo.kernel.api.utils.FixityResult;
022
023import static java.util.Objects.hash;
024
025import java.net.URI;
026import java.util.EnumSet;
027import java.util.Set;
028
029/**
030 * Structure for presenting the results of a fixity check
031 * (and any repair operations that may have occurred)
032 *
033 * @author bbpennel
034 */
035public class FixityResultImpl implements FixityResult {
036
037    /**
038     * the size computed by the fixity check
039     * @todo make this private
040     */
041    private final long computedSize;
042
043    /**
044     * the checksum computed by the fixity check
045     * @todo make this private
046     */
047    private final URI computedChecksum;
048
049    private final String storeIdentifier;
050
051    /**
052     * Prepare a fixity result given the computed checksum and size
053     * @param size the given size
054     * @param checksum the given checksum
055     */
056    public FixityResultImpl(final long size, final URI checksum) {
057        this("comparison-only-identifier", size, checksum);
058    }
059
060    /**
061     * Prepare a fixity result with the expected size and checksum
062     * @param entry the entry
063     * @param size the expected size
064     * @param checksum the checksum
065     */
066    public FixityResultImpl(final CacheEntry entry, final long size,
067                        final URI checksum) {
068        this(entry.getExternalIdentifier(), size, checksum);
069    }
070
071    /**
072     *
073     * @param storeIdentifier the store identifier
074     * @param size the size
075     * @param checksum the checksum
076     */
077    public FixityResultImpl(final String storeIdentifier, final long size, final URI checksum) {
078        this.storeIdentifier = storeIdentifier;
079        computedSize = size;
080        computedChecksum = checksum;
081    }
082
083    /**
084     * Get the identifier for the entry's store
085     * @return the store identifier
086     */
087    @Override
088    public String getStoreIdentifier() {
089        return storeIdentifier;
090    }
091
092    @Override
093    public boolean equals(final Object obj) {
094
095        boolean result = false;
096        if (obj instanceof FixityResult) {
097            final FixityResult that = (FixityResult) obj;
098            result =
099                    computedSize == that.getComputedSize() &&
100                            computedChecksum.equals(that.getComputedChecksum());
101        }
102
103        return result;
104    }
105
106    @Override
107    public int hashCode() {
108        return hash(computedSize, computedChecksum);
109    }
110
111    @Override
112    public String toString() {
113        return "Fixity: checksum: " + computedChecksum + " / " + computedSize;
114    }
115
116    /**
117     * Check if the fixity result matches the given checksum URI
118     * @param checksum the checksum uri
119     * @return true if the checksums match
120     */
121    @Override
122    public boolean matches(final URI checksum) {
123        return computedChecksum.equals(checksum);
124    }
125
126    /**
127     * Check if the fixity result matches the given size
128     * @param size the size
129     * @return true if fixity result matches the given size
130     */
131    @Override
132    public boolean matches(final long size) {
133        return computedSize == size;
134    }
135
136    /**
137     * Does the fixity entry match the given size and checksum?
138     * @param size bitstream size in bytes
139     * @param checksum checksum URI in the form urn:DIGEST:RESULT
140     * @return true if both conditions matched
141     */
142    @Override
143    public boolean matches(final long size, final URI checksum) {
144        return matches(size) && matches(checksum);
145    }
146
147    /**
148     * @return the status
149     */
150    @Override
151    public Set<FixityState> getStatus(final long size, final URI checksum) {
152
153        final Set<FixityState> status = EnumSet.noneOf(FixityState.class);
154
155
156        if (matches(size, checksum)) {
157            status.add(FixityState.SUCCESS);
158        } else {
159            if (!matches(size)) {
160                status.add(FixityState.BAD_SIZE);
161            }
162
163            if (!matches(checksum)) {
164                status.add(FixityState.BAD_CHECKSUM);
165            }
166        }
167
168        return status;
169    }
170
171    /**
172     * @return the computedSize
173     */
174    @Override
175    public long getComputedSize() {
176        return computedSize;
177    }
178
179    /**
180     * @return the computedChecksum
181     */
182    @Override
183    public URI getComputedChecksum() {
184        return computedChecksum;
185    }
186
187}