mirror of https://github.com/apache/jclouds.git
Check Content-Length in local blobstore putBlob
This commit is contained in:
parent
e50bd79f01
commit
de333e8b3c
|
@ -462,7 +462,12 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
|||
try {
|
||||
Files.createParentDirs(tmpFile);
|
||||
his = new HashingInputStream(Hashing.md5(), payload.openStream());
|
||||
Files.asByteSink(tmpFile).writeFrom(his);
|
||||
long actualSize = Files.asByteSink(tmpFile).writeFrom(his);
|
||||
Long expectedSize = blob.getMetadata().getContentMetadata().getContentLength();
|
||||
if (expectedSize != null && actualSize != expectedSize) {
|
||||
throw new IOException("Content-Length mismatch, actual: " + actualSize +
|
||||
" expected: " + expectedSize);
|
||||
}
|
||||
HashCode actualHashCode = his.hash();
|
||||
HashCode expectedHashCode = payload.getContentMetadata().getContentMD5AsHashCode();
|
||||
if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) {
|
||||
|
|
|
@ -34,6 +34,7 @@ import java.util.concurrent.TimeUnit;
|
|||
|
||||
import javax.inject.Provider;
|
||||
|
||||
import org.assertj.core.api.Fail;
|
||||
import org.jclouds.blobstore.domain.Blob;
|
||||
import org.jclouds.blobstore.domain.BlobBuilder;
|
||||
import org.jclouds.blobstore.domain.ContainerAccess;
|
||||
|
@ -667,6 +668,21 @@ public class FilesystemStorageStrategyImplTest {
|
|||
assertFalse(blob.getMetadata().getUserMetadata().containsKey("key1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutIncorrectContentLength() throws Exception {
|
||||
Blob blob = new BlobBuilderImpl()
|
||||
.name("key")
|
||||
.payload(randomByteSource().slice(0, 1024))
|
||||
.contentLength(512)
|
||||
.build();
|
||||
try {
|
||||
storageStrategy.putBlob(CONTAINER_NAME, blob);
|
||||
Fail.failBecauseExceptionWasNotThrown(IOException.class);
|
||||
} catch (IOException ioe) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------- Private methods
|
||||
|
||||
/**
|
||||
|
|
|
@ -165,6 +165,12 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
|
|||
HashingInputStream input = new HashingInputStream(Hashing.md5(), blob.getPayload().openStream());
|
||||
try {
|
||||
payload = ByteStreams.toByteArray(input);
|
||||
long actualSize = payload.length;
|
||||
Long expectedSize = blob.getMetadata().getContentMetadata().getContentLength();
|
||||
if (expectedSize != null && actualSize != expectedSize) {
|
||||
throw new IOException("Content-Length mismatch, actual: " + actualSize +
|
||||
" expected: " + expectedSize);
|
||||
}
|
||||
actualHashCode = input.hash();
|
||||
HashCode expectedHashCode = blob.getPayload().getContentMetadata().getContentMD5AsHashCode();
|
||||
if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) {
|
||||
|
|
Loading…
Reference in New Issue