#4078 Fix BinaryAccessProvider empty stream check (#4170)

* Fix #4078 by using theRequestDetails.loadRequestContents() to check if the input stream is empty or not

* Remove unused imports
This commit is contained in:
Tue Toft Nørgård 2022-10-21 22:08:21 +02:00 committed by GitHub
parent b04f082991
commit 1dba9392ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 45 deletions

View File

@ -26,12 +26,11 @@ import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -39,6 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@ -247,43 +247,17 @@ public class BinaryAccessProviderTest {
DaoMethodOutcome daoOutcome = new DaoMethodOutcome();
daoOutcome.setResource(docRef);
ServletInputStream sis = new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() throws IOException {
return 0;
}
@Override
public int available() throws IOException {
return 15;
}
};
StoredDetails sd = spy(StoredDetails.class);
sd.setBlobId("123");
sd.setBytes(15);
when(myDaoRegistry.getResourceDao(eq("DocumentReference"))).thenReturn(myResourceDao);
when(myResourceDao.read(any(), any(), anyBoolean())).thenReturn(docRef);
when(myResourceDao.update(docRef, myRequestDetails)).thenReturn(daoOutcome);
when(theServletRequest.getContentType()).thenReturn("Integer");
when(theServletRequest.getContentLength()).thenReturn(15);
when(myBinaryStorageSvc.shouldStoreBlob(15, docRef.getIdElement(), "Integer")).thenReturn(true);
when(theServletRequest.getInputStream()).thenReturn(sis);
myRequestDetails.setServletRequest(theServletRequest);
when(myBinaryStorageSvc.storeBlob(docRef.getIdElement(), null, "Integer", myRequestDetails.getInputStream())).thenReturn(sd);
when(myBinaryStorageSvc.storeBlob(eq(docRef.getIdElement()), isNull(), eq("Integer"), any(InputStream.class))).thenReturn(sd);
myRequestDetails.setRequestContents(SOME_BYTES);
try {

View File

@ -19,6 +19,7 @@ package ca.uhn.fhir.jpa.binary.provider;
* limitations under the License.
* #L%
*/
import ca.uhn.fhir.i18n.Msg;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.context.FhirContext;
@ -59,8 +60,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import static ca.uhn.fhir.util.UrlUtil.sanitizeUrlPart;
@ -102,7 +103,7 @@ public class BinaryAccessProvider {
Optional<String> attachmentId = target.getAttachmentId();
//for unit test only
if (addTargetAttachmentIdForTest){
if (addTargetAttachmentIdForTest) {
attachmentId = Optional.of("1");
}
@ -186,24 +187,22 @@ public class BinaryAccessProvider {
ourLog.trace("Request specified content length: {}", size);
String blobId = null;
byte[] bytes = theRequestDetails.loadRequestContents();
if (size > 0) {
if (myBinaryStorageSvc != null) {
InputStream inputStream = theRequestDetails.getInputStream();
if (inputStream.available() == 0 ) {
throw new IllegalStateException(Msg.code(2073) + "Input stream is empty! Ensure that you are uploading data, and if so, ensure that no interceptors are in use that may be consuming the input stream");
}
if (myBinaryStorageSvc.shouldStoreBlob(size, theResourceId, requestContentType)) {
StoredDetails storedDetails = myBinaryStorageSvc.storeBlob(theResourceId, null, requestContentType, inputStream);
size = storedDetails.getBytes();
blobId = storedDetails.getBlobId();
Validate.notBlank(blobId, "BinaryStorageSvc returned a null blob ID"); // should not happen
}
if (size > 0 && myBinaryStorageSvc != null) {
if (bytes == null || bytes.length == 0) {
throw new IllegalStateException(Msg.code(2073) + "Input stream is empty! Ensure that you are uploading data, and if so, ensure that no interceptors are in use that may be consuming the input stream");
}
if (myBinaryStorageSvc.shouldStoreBlob(size, theResourceId, requestContentType)) {
StoredDetails storedDetails = myBinaryStorageSvc.storeBlob(theResourceId, null, requestContentType, new ByteArrayInputStream(bytes));
size = storedDetails.getBytes();
blobId = storedDetails.getBlobId();
Validate.notBlank(blobId, "BinaryStorageSvc returned a null blob ID"); // should not happen
Validate.isTrue(size == theServletRequest.getContentLength(), "Unexpected stored size"); // Sanity check
}
}
if (blobId == null) {
byte[] bytes = theRequestDetails.loadRequestContents();
size = bytes.length;
target.setData(bytes);
} else {