Fix limiting on size
This commit is contained in:
parent
bff39e4139
commit
b9dbd69e5d
|
@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue