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:
33
src/test/java/some/custom/pkg1/CustomOperationBean.java
Normal file
33
src/test/java/some/custom/pkg1/CustomOperationBean.java
Normal 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();
|
||||
}
|
||||
}
|
||||
28
src/test/java/some/custom/pkg1/CustomOperationPojo.java
Normal file
28
src/test/java/some/custom/pkg1/CustomOperationPojo.java
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user