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.kernel.modeshape.utils;
017
018import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
019import org.fcrepo.kernel.api.utils.CacheEntry;
020import org.fcrepo.kernel.api.utils.ContentDigest;
021import org.fcrepo.kernel.api.utils.FixityResult;
022
023import org.slf4j.Logger;
024
025import javax.jcr.RepositoryException;
026
027import java.io.IOException;
028import java.net.URI;
029import java.security.MessageDigest;
030import java.security.NoSuchAlgorithmException;
031import java.util.Collection;
032
033import static java.util.Arrays.asList;
034import static org.slf4j.LoggerFactory.getLogger;
035
036/**
037 * Cache entry that wraps a binary stream and provides
038 * fixity methods against it
039 *
040 * @author fasseg
041 */
042public abstract class BasicCacheEntry implements CacheEntry {
043
044    public static final int DEV_NULL_BUFFER_SIZE = 4096;
045
046    private static final byte[] devNull = new byte[DEV_NULL_BUFFER_SIZE];
047
048    private static final Logger LOGGER = getLogger(BasicCacheEntry.class);
049
050    /**
051     * Calculate the fixity of a CacheEntry by piping it through
052     * a simple fixity-calculating InputStream
053     *
054     * @param digest the digest
055     * @return the fixity of this cache entry
056     * @throws RepositoryException if repository exception occurred
057     */
058    @Override
059    public Collection<FixityResult> checkFixity(final String digest)
060        throws RepositoryException {
061
062        try (FixityInputStream fixityInputStream = new FixityInputStream(this.getInputStream(),
063                MessageDigest.getInstance(digest))) {
064            // exhaust our source
065            while (fixityInputStream.read(devNull) != -1) { }
066
067            final URI calculatedChecksum = ContentDigest.asURI(digest,
068                                                                  fixityInputStream.getMessageDigest().digest());
069            final FixityResult result =
070                new FixityResultImpl(this,
071                                    fixityInputStream.getByteCount(),
072                                    calculatedChecksum);
073
074            LOGGER.debug("Got {}", result.toString());
075
076            return asList(result);
077        } catch (final IOException e) {
078            LOGGER.debug("Got error closing input stream: {}", e);
079            throw new RepositoryRuntimeException(e);
080        } catch (final NoSuchAlgorithmException e1) {
081            throw new RepositoryRuntimeException(e1);
082        }
083
084    }
085}