Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update DatastreamService#getFixity to support InMemory and External
Binary values Previously, getFixity only supported binary values that were persisted into a CacheStore, ignoring projected and in-memory values. This patch adds support for InMemory and External values by running their InputStream through our fixity checks (and assumes these values can't be repaired, as some configurations of cache stores can).
- Loading branch information
Showing
14 changed files
with
459 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
fcrepo-kernel/src/main/java/org/fcrepo/kernel/utils/BasicCacheEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* Copyright 2013 DuraSpace, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.fcrepo.kernel.utils; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import javax.jcr.RepositoryException; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
import static com.google.common.base.Throwables.propagate; | ||
import static org.fcrepo.kernel.utils.FixityResult.FixityState.BAD_CHECKSUM; | ||
import static org.fcrepo.kernel.utils.FixityResult.FixityState.BAD_SIZE; | ||
import static org.fcrepo.kernel.utils.FixityResult.FixityState.MISSING_STORED_FIXITY; | ||
import static org.fcrepo.kernel.utils.FixityResult.FixityState.SUCCESS; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
abstract public class BasicCacheEntry implements CacheEntry { | ||
|
||
private static final Logger LOGGER = getLogger(BasicCacheEntry.class); | ||
|
||
/** | ||
* Calculate the fixity of a CacheEntry by piping it through | ||
* a simple fixity-calculating InputStream | ||
* | ||
* @param checksum the checksum previously generated for the entry | ||
* @param size the size of the entry | ||
* @return | ||
* @throws RepositoryException | ||
*/ | ||
@Override | ||
public FixityResult checkFixity(final URI checksum, final long size) | ||
throws RepositoryException { | ||
|
||
final FixityInputStream fixityInputStream; | ||
|
||
final String digest = ContentDigest.getAlgorithm(checksum); | ||
try { | ||
fixityInputStream = new FixityInputStream(this.getInputStream(), | ||
MessageDigest.getInstance(digest)); | ||
} catch (NoSuchAlgorithmException e) { | ||
LOGGER.warn("Could not create MessageDigest: {}", e); | ||
throw propagate(e); | ||
} | ||
|
||
try { | ||
|
||
while (fixityInputStream.read() != -1) { | ||
// noop; we're just reading the stream for the checksum and size | ||
} | ||
|
||
final URI calculatedChecksum = ContentDigest.asURI(digest, | ||
fixityInputStream.getMessageDigest().digest()); | ||
final FixityResult result = | ||
new FixityResult(this, | ||
fixityInputStream.getByteCount(), | ||
calculatedChecksum); | ||
|
||
if (checksum.equals(ContentDigest.missingChecksum()) || size == -1L) { | ||
result.status.add(MISSING_STORED_FIXITY); | ||
} | ||
|
||
if (!result.matches(checksum)) { | ||
result.status.add(BAD_CHECKSUM); | ||
} | ||
|
||
if (!result.matches(size)) { | ||
result.status.add(BAD_SIZE); | ||
} | ||
|
||
if (result.matches(size, checksum)) { | ||
result.status.add(SUCCESS); | ||
} | ||
|
||
LOGGER.debug("Got {}", result.toString()); | ||
|
||
return result; | ||
} catch (final IOException e) { | ||
throw propagate(e); | ||
} finally { | ||
try { | ||
fixityInputStream.close(); | ||
} catch (IOException e) { | ||
LOGGER.debug("Got error closing input stream: {}", e); | ||
} | ||
} | ||
|
||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
fcrepo-kernel/src/main/java/org/fcrepo/kernel/utils/BinaryCacheEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Copyright 2013 DuraSpace, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.fcrepo.kernel.utils; | ||
|
||
import java.io.InputStream; | ||
|
||
import javax.jcr.Binary; | ||
import javax.jcr.RepositoryException; | ||
|
||
/** | ||
* A {@link CacheEntry} for simple Binary objects | ||
* @author frank asseg | ||
* | ||
*/ | ||
public class BinaryCacheEntry extends BasicCacheEntry { | ||
|
||
protected final Binary binary; | ||
protected final String externalUri; | ||
|
||
/** | ||
* Create a new BinaryCacheEntry | ||
* @param binary | ||
*/ | ||
public BinaryCacheEntry(final Binary binary, final String externalUri) { | ||
super(); | ||
this.binary = binary; | ||
this.externalUri = externalUri; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.fcrepo.kernel.utils.CacheEntry#getInputStream() | ||
*/ | ||
@Override | ||
public InputStream getInputStream() throws RepositoryException { | ||
return this.binary.getStream(); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.fcrepo.kernel.utils.CacheEntry#getExternalIdentifier() | ||
*/ | ||
@Override | ||
public String getExternalIdentifier() { | ||
return this.externalUri; | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
fcrepo-kernel/src/main/java/org/fcrepo/kernel/utils/CacheEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright 2013 DuraSpace, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.fcrepo.kernel.utils; | ||
|
||
import java.io.InputStream; | ||
import java.net.URI; | ||
|
||
import javax.jcr.RepositoryException; | ||
|
||
/** | ||
* A CacheEntry abstraction for the various possible types of entries | ||
* @author fasseg | ||
* | ||
*/ | ||
public interface CacheEntry { | ||
|
||
/** | ||
* Check the fixity of a {@link CacheEntry} | ||
* @param checksum the checksum previously generated for the entry | ||
* @param size the size of the entry | ||
* @return a {@link FixityResult} containing the relevant data | ||
* @throws RepositoryException | ||
*/ | ||
public FixityResult checkFixity(URI checksum, long size) | ||
throws RepositoryException; | ||
|
||
/** | ||
* Get a raw input stream from the underlying store | ||
* @return the content for this entry | ||
* @throws RepositoryException | ||
*/ | ||
public abstract InputStream getInputStream() throws RepositoryException; | ||
|
||
/** | ||
* Generate a human-readable identifier for the location of this entry | ||
* | ||
* @return | ||
* @throws RepositoryException | ||
*/ | ||
public abstract String getExternalIdentifier(); | ||
} |
Oops, something went wrong.