diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BinaryStorageInterceptor.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BinaryStorageInterceptor.java index f5e4e69cade..9a69570f47f 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BinaryStorageInterceptor.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/binstore/BinaryStorageInterceptor.java @@ -119,6 +119,10 @@ public class BinaryStorageInterceptor { } private void extractLargeBinaries(ServletRequestDetails theRequestDetails, IBaseResource theResource, Pointcut thePoincut) throws IOException { + if (theRequestDetails == null) { + // RequestDetails will only be null for internal HAPI events. If externalization is required for them it will need to be done in a different way. + return; + } IIdType resourceId = theResource.getIdElement(); if (!resourceId.hasResourceType() && resourceId.hasIdPart()) { String resourceType = myCtx.getResourceDefinition(theResource).getName(); @@ -145,7 +149,7 @@ public class BinaryStorageInterceptor { newBlobId = myBinaryStorageSvc.newBlobId(); List deferredBinaryTargets = getOrCreateDeferredBinaryStorageMap(theRequestDetails); DeferredBinaryTarget newDeferredBinaryTarget = new DeferredBinaryTarget(newBlobId, nextTarget, data); - deferredBinaryTargets.add(newDeferredBinaryTarget); + deferredBinaryTargets.add(newDeferredBinaryTarget); } myBinaryAccessProvider.replaceDataWithExtension(nextTarget, newBlobId); @@ -159,9 +163,9 @@ public class BinaryStorageInterceptor { @SuppressWarnings("unchecked") private List getOrCreateDeferredBinaryStorageMap(ServletRequestDetails theRequestDetails) { List deferredBinaryTargets = (List) theRequestDetails.getUserData().get(getDeferredListKey()); - if (deferredBinaryTargets == null) { - deferredBinaryTargets = new ArrayList<>(); - theRequestDetails.getUserData().put(getDeferredListKey(), deferredBinaryTargets); + if (deferredBinaryTargets == null) { + deferredBinaryTargets = new ArrayList<>(); + theRequestDetails.getUserData().put(getDeferredListKey(), deferredBinaryTargets); } return deferredBinaryTargets; } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BinaryStorageInterceptorR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BinaryStorageInterceptorR4Test.java index 8fe8fe3e0c1..6a14053e267 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BinaryStorageInterceptorR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BinaryStorageInterceptorR4Test.java @@ -75,6 +75,28 @@ public class BinaryStorageInterceptorR4Test extends BaseResourceProviderR4Test { } + + @Test + public void testCreateAndRetrieveBinary_ServerAssignedId_ExternalizedBinary_NullServletRequest() { + + // Create a resource with a big enough binary + Binary binary = new Binary(); + binary.setContentType("application/octet-stream"); + binary.setData(SOME_BYTES); + DaoMethodOutcome outcome = myBinaryDao.create(binary); + + // Make sure it was externalized + IIdType id = outcome.getId().toUnqualifiedVersionless(); + String encoded = myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(outcome.getResource()); + ourLog.info("Encoded: {}", encoded); + assertThat(encoded, not(containsString(JpaConstants.EXT_EXTERNALIZED_BINARY_ID))); + assertThat(encoded, containsString("\"data\"")); + + Binary output = myBinaryDao.read(id); + assertEquals("application/octet-stream", output.getContentType()); + assertArrayEquals(SOME_BYTES, output.getData()); + } + @Test public void testCreateAndRetrieveBinary_ServerAssignedId_NonExternalizedBinary() {