diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BaseBinaryStorageSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BaseBinaryStorageSvcImpl.java index b73d8660c3f..86646d46a97 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BaseBinaryStorageSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BaseBinaryStorageSvcImpl.java @@ -20,11 +20,11 @@ package ca.uhn.fhir.jpa.binstore; * #L% */ +import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; import com.google.common.hash.HashingInputStream; -import com.google.common.io.ByteStreams; -import com.google.common.io.CountingInputStream; +import org.apache.commons.io.input.CountingInputStream; import org.apache.commons.lang3.Validate; import org.hl7.fhir.instance.model.api.IIdType; @@ -85,11 +85,18 @@ abstract class BaseBinaryStorageSvcImpl implements IBinaryStorageSvc { return new HashingInputStream(hash, theInputStream); } - @SuppressWarnings("UnstableApiUsage") @Nonnull CountingInputStream createCountingInputStream(InputStream theInputStream) { - InputStream stream = ByteStreams.limit(theInputStream, myMaximumBinarySize); - return new CountingInputStream(stream); + return new CountingInputStream(theInputStream){ + @Override + public int getCount() { + int retVal = super.getCount(); + if (retVal > myMaximumBinarySize) { + throw new InternalErrorException("Binary size exceeds maximum: " + myMaximumBinarySize); + } + return retVal; + } + }; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BinaryAccessProvider.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BinaryAccessProvider.java index 30be8a7a9f4..1276e3010a1 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BinaryAccessProvider.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BinaryAccessProvider.java @@ -41,6 +41,8 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.hl7.fhir.instance.model.api.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.Nonnull; @@ -59,6 +61,7 @@ import static org.apache.commons.lang3.StringUtils.isBlank; */ public class BinaryAccessProvider { + private static final Logger ourLog = LoggerFactory.getLogger(BinaryAccessProvider.class); @Autowired private FhirContext myCtx; @Autowired @@ -169,6 +172,8 @@ public class BinaryAccessProvider { } long size = theServletRequest.getContentLength(); + ourLog.info("**** HAVE CONTENT LENGTH: {}", size); + String blobId = null; if (size > 0) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/DatabaseBlobBinaryStorageSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/DatabaseBlobBinaryStorageSvcImpl.java index a7e75c09bb5..13bab2f1699 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/DatabaseBlobBinaryStorageSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/DatabaseBlobBinaryStorageSvcImpl.java @@ -23,8 +23,8 @@ package ca.uhn.fhir.jpa.binstore; import ca.uhn.fhir.jpa.dao.data.IBinaryStorageEntityDao; import ca.uhn.fhir.jpa.model.entity.BinaryStorageEntity; import com.google.common.hash.HashingInputStream; -import com.google.common.io.CountingInputStream; import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.CountingInputStream; import org.hibernate.LobHelper; import org.hibernate.Session; import org.hl7.fhir.instance.model.api.IIdType; @@ -57,7 +57,7 @@ public class DatabaseBlobBinaryStorageSvcImpl extends BaseBinaryStorageSvcImpl { @Override @Transactional(Transactional.TxType.SUPPORTS) - public StoredDetails storeBlob(IIdType theResourceId, String theContentType, InputStream theInputStream) throws IOException { + public StoredDetails storeBlob(IIdType theResourceId, String theContentType, InputStream theInputStream) { Date publishedDate = new Date(); HashingInputStream hashingInputStream = createHashingInputStream(theInputStream); @@ -103,7 +103,7 @@ public class DatabaseBlobBinaryStorageSvcImpl extends BaseBinaryStorageSvcImpl { } @Override - public StoredDetails fetchBlobDetails(IIdType theResourceId, String theBlobId) throws IOException { + public StoredDetails fetchBlobDetails(IIdType theResourceId, String theBlobId) { Optional entityOpt = myBinaryStorageEntityDao.findByIdAndResourceId(theBlobId, theResourceId.toUnqualifiedVersionless().getValue()); if (entityOpt.isPresent() == false) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/FilesystemBinaryStorageSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/FilesystemBinaryStorageSvcImpl.java index d59380fae9e..bbdbfe29ac4 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/FilesystemBinaryStorageSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/FilesystemBinaryStorageSvcImpl.java @@ -26,9 +26,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.google.common.base.Charsets; import com.google.common.hash.HashingInputStream; -import com.google.common.io.CountingInputStream; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.CountingInputStream; import org.apache.commons.lang3.Validate; import org.hl7.fhir.instance.model.api.IIdType; import org.slf4j.Logger; diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/MemoryBinaryStorageSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/MemoryBinaryStorageSvcImpl.java index 389e23c590c..fa7f8ef73c5 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/MemoryBinaryStorageSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/MemoryBinaryStorageSvcImpl.java @@ -22,6 +22,7 @@ package ca.uhn.fhir.jpa.binstore; import com.google.common.hash.HashingInputStream; import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.CountingInputStream; import org.hl7.fhir.instance.model.api.IIdType; import java.io.IOException; @@ -52,12 +53,13 @@ public class MemoryBinaryStorageSvcImpl extends BaseBinaryStorageSvcImpl impleme String id = newRandomId(); String key = toKey(theResourceId, id); - HashingInputStream is = createHashingInputStream(theInputStream); + HashingInputStream hashingIs = createHashingInputStream(theInputStream); + CountingInputStream countingIs = createCountingInputStream(hashingIs); - byte[] bytes = IOUtils.toByteArray(is); + byte[] bytes = IOUtils.toByteArray(countingIs); theInputStream.close(); myDataMap.put(key, bytes); - StoredDetails storedDetails = new StoredDetails(id, bytes.length, theContentType, is, new Date()); + StoredDetails storedDetails = new StoredDetails(id, countingIs.getCount(), theContentType, hashingIs, new Date()); myDetailsMap.put(key, storedDetails); return storedDetails; }