Allow custom operations/providers in addition to interceptors (#654)

* Allow custom operations/providers to be added in the same way custom interceptors are currently loaded

* Add new property to documentation and default yaml file

* Add test for custom operation
This commit is contained in:
David Conlan
2024-03-04 02:39:03 +10:00
committed by GitHub
parent 3ea85a05aa
commit 4226648867
7 changed files with 193 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
package some.custom.pkg1;
import ca.uhn.fhir.rest.annotation.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* Code taken from hapi documentation on how to implement an operation which handles its own request/response
* <a href="https://hapifhir.io/hapi-fhir/docs/server_plain/rest_operations_operations.html#manually-handing-requestresponse">...</a>
*/
@Component
public class CustomOperationBean {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(CustomOperationBean.class);
@Operation(name = "$springBeanOperation", manualResponse = true, manualRequest = true)
public void springBeanOperation(HttpServletRequest theServletRequest, HttpServletResponse theServletResponse)
throws IOException {
String contentType = theServletRequest.getContentType();
byte[] bytes = IOUtils.toByteArray(theServletRequest.getInputStream());
ourLog.info("Received call with content type {} and {} bytes", contentType, bytes.length);
theServletResponse.setContentType("text/plain");
theServletResponse.getWriter().write("springBean");
theServletResponse.getWriter().close();
}
}

View File

@@ -0,0 +1,28 @@
package some.custom.pkg1;
import ca.uhn.fhir.rest.annotation.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class CustomOperationPojo {
private final Logger LOGGER = LoggerFactory.getLogger(CustomOperationPojo.class);
@Operation(name = "$pojoOperation", manualResponse = true, manualRequest = true)
public void $pojoOperation(HttpServletRequest theServletRequest, HttpServletResponse theServletResponse)
throws IOException {
String contentType = theServletRequest.getContentType();
byte[] bytes = IOUtils.toByteArray(theServletRequest.getInputStream());
LOGGER.info("Received call with content type {} and {} bytes", contentType, bytes.length);
theServletResponse.setContentType("text/plain");
theServletResponse.getWriter().write("pojo");
theServletResponse.getWriter().close();
}
}