Fix limiting on size

This commit is contained in:
James Agnew 2019-07-24 13:11:05 -04:00
parent bff39e4139
commit b9dbd69e5d
5 changed files with 26 additions and 12 deletions

View File

@ -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;
}
};
}

View File

@ -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) {

View File

@ -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<BinaryStorageEntity> entityOpt = myBinaryStorageEntityDao.findByIdAndResourceId(theBlobId, theResourceId.toUnqualifiedVersionless().getValue());
if (entityOpt.isPresent() == false) {

View File

@ -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;

View File

@ -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;
}