respect status code set in providers

This commit is contained in:
Emre Dincturk 2024-11-28 10:24:47 -05:00
parent 3e417ad0d1
commit 7fc2e461bf
2 changed files with 17 additions and 15 deletions

View File

@ -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<T extends IBaseResource> 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<T extends IBaseResource> ex
@OperationParam(name = ProviderConstants.OPERATION_MERGE_PREVIEW, typeName = "boolean", max = 1)
IPrimitiveType<Boolean> 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<T extends IBaseResource> 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);
}

View File

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