From 7fc2e461bf4664bb56e87d0f1b9964780c95825c Mon Sep 17 00:00:00 2001 From: Emre Dincturk Date: Thu, 28 Nov 2024 10:24:47 -0500 Subject: [PATCH] respect status code set in providers --- .../BaseJpaResourceProviderPatient.java | 17 ++++------------- .../BaseResourceReturningMethodBinding.java | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/provider/BaseJpaResourceProviderPatient.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/provider/BaseJpaResourceProviderPatient.java index f045292bcef..df86efa8b6f 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/provider/BaseJpaResourceProviderPatient.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/provider/BaseJpaResourceProviderPatient.java @@ -57,7 +57,6 @@ import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.Identifier; -import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -262,7 +261,7 @@ public abstract class BaseJpaResourceProviderPatient ex @Operation( name = ProviderConstants.OPERATION_MERGE, canonicalUrl = "http://hl7.org/fhir/OperationDefinition/Patient-merge") - public void patientMerge( + public IBaseParameters patientMerge( HttpServletRequest theServletRequest, HttpServletResponse theServletResponse, ServletRequestDetails theRequestDetails, @@ -277,8 +276,7 @@ public abstract class BaseJpaResourceProviderPatient ex @OperationParam(name = ProviderConstants.OPERATION_MERGE_PREVIEW, typeName = "boolean", max = 1) IPrimitiveType thePreview, @OperationParam(name = ProviderConstants.OPERATION_MERGE_RESULT_PATIENT, max = 1) - IBaseResource theResultPatient) - throws IOException { + IBaseResource theResultPatient) { startRequest(theServletRequest); try { @@ -302,15 +300,8 @@ public abstract class BaseJpaResourceProviderPatient ex ParametersUtil.addParameterToParameters(fhirContext, retVal, "outcome", mergeOutcome.getOperationOutcome()); theServletResponse.setStatus(mergeOutcome.getHttpStatusCode()); - // TODO Emre: we are writing the response to directly, otherwise the response status we set above is - // ignored. CDA Import operation does it this way too, but what if the client requests xml response? - // there needs to be a better way to do this - theServletResponse.setContentType(Constants.CT_JSON); - fhirContext - .newJsonParser() - .setPrettyPrint(true) - .encodeResourceToWriter(retVal, theServletResponse.getWriter()); - theServletResponse.getWriter().close(); + + return retVal; } finally { endRequest(theServletRequest); } diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/BaseResourceReturningMethodBinding.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/BaseResourceReturningMethodBinding.java index 0042aadcbdd..b7df1fac230 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/BaseResourceReturningMethodBinding.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/BaseResourceReturningMethodBinding.java @@ -277,16 +277,27 @@ public abstract class BaseResourceReturningMethodBinding extends BaseMethodBindi When we write directly to an HttpServletResponse, the invocation returns null. However, we still want to invoke the SERVER_OUTGOING_RESPONSE pointcut. */ + + // if the response status code is set by the method, respect it. Otherwise, use the default 200. + int responseCode = Constants.STATUS_HTTP_200_OK; + if (theRequest instanceof ServletRequestDetails) { + HttpServletResponse servletResponse = ((ServletRequestDetails) theRequest).getServletResponse(); + if (servletResponse != null && servletResponse.getStatus() > 0) { + responseCode = servletResponse.getStatus(); + } + } + if (response == null) { ResponseDetails responseDetails = new ResponseDetails(); - responseDetails.setResponseCode(Constants.STATUS_HTTP_200_OK); + responseDetails.setResponseCode(responseCode); callOutgoingResponseHook(theRequest, responseDetails); return null; } else { Set summaryMode = RestfulServerUtils.determineSummaryMode(theRequest); ResponseDetails responseDetails = new ResponseDetails(); responseDetails.setResponseResource(response); - responseDetails.setResponseCode(Constants.STATUS_HTTP_200_OK); + responseDetails.setResponseCode(responseCode); + if (!callOutgoingResponseHook(theRequest, responseDetails)) { return null; }