Understanding how to use Interceptors

477 views
Skip to first unread message

Jordan Force

unread,
Aug 5, 2022, 2:16:23 PM8/5/22
to HAPI FHIR
I'm just starting out learning to use HAPI. I've cloned the JPA server starter (https://github.com/hapifhir/hapi-fhir-jpaserver-starter) repo, and switched to one of the release commits. I'm able to build and run the docker container, and visit the webpage at localhost:8080. I'm able to run the eclipse debugger and set breakpoints.

I'm trying to add authentication, and as a first step, I'm trying to set up a very simple interceptor:

@Interceptor
public class BasicSecurityInterceptor extends InterceptorAdapter{
      @Hook(Pointcut.SERVER_INCOMING_REQUEST_POST_PROCESSED)
      public boolean incomingRequestPostProcessed(RequestDetails theRequestDetails, HttpServletRequest theRequest, HttpServletResponse theResponse) {
           System.out.println("Hello");
           return true;
}
}

I placed this code into a BasicSecurityInterceptor.java file inside the ca.uhn.fhir.jpa.starter package, re-built and re-ran the docker container. I confirmed that BasicSecurityInterceptor.class is placed in the app at the expected location. Then, I set a breakpoint at the first line of incomingRequestPostProcessed, and then send a POST query to localhost:8080/fhir/Patient. I'm expecting the breakpoint to break, but it never does. What am I doing wrong? Is this the wrong procedure for hooking an interceptor to HAPI?

Thanks,

Jordan

Kevin Dougan

unread,
Aug 8, 2022, 2:14:02 PM8/8/22
to HAPI FHIR
Hi Jordan,

There are a number of Interceptor examples, including downloadable source code, on this page:

You can see that all of the examples call a method to register themselves:
registerInterceptor(interceptor);

It sounds like you will need to "register your Interceptor" so the server is made aware of it and knows to call it.

Hope this helps!
Kevin

Jordan Force

unread,
Aug 12, 2022, 11:05:37 AM8/12/22
to HAPI FHIR
Thanks, that helps a lot! I'll give it a try.

Dinesh Chanchalani

unread,
Sep 28, 2023, 3:18:59 PM9/28/23
to HAPI FHIR
Hi Kevin,

I am facing a similar issue. I did register the interceptor in a MyRestfulServer class within the initilize method.

But, what I face is pretty strange. The interceptor executes when the URL is localhost:8080/fhir/metadata but does not for localhost:8080/fhir/Patient or for  localhost:8080/fhir/Patient/mypatientId

In fact when I register the Interceptor I get a 404 response on the Patient resource GET requests but a 200 on the metadata. If I do not register the interceptor, I get a 200 response on both Patient and metadata

Here is my code (without the import statements):


package ca.uhn.fhir.jpa.starter;

@Interceptor
public class BasicSecurityInterceptor extends InterceptorAdapter {

@Hook(Pointcut.SERVER_INCOMING_REQUEST_POST_PROCESSED)
public boolean incomingRequestPostProcessed(
RequestDetails theRequestDetails, HttpServletRequest theRequest, HttpServletResponse theResponse)
throws AuthenticationException {

System.out.println("Hello");
return true;
}

}

package ca.uhn.fhir.jpa.starter;

@WebServlet(
urlPatterns = {"/fhir/*"},
displayName = "FHIR Server")
public class MyRestfulServer extends RestfulServer {

@Override
protected void initialize() throws ServletException {

System.out.println("first line in initialize");
super.initialize();

// create FhirContext for the appropriate version
setFhirContext(FhirContext.forR4());

registerInterceptor(new BasicSecurityInterceptor());

System.out.println("last line in initialize");

}
}


Dinesh Chanchalani

unread,
Sep 28, 2023, 6:58:02 PM9/28/23
to HAPI FHIR
I guess I will need to register resource providers for the interceptor on work on a resource?

Added this code before the registerInterceptor line

List<IResourceProvider> resourceProviders = new ArrayList<>();
resourceProviders.add(new PatientResourceProvider());
setResourceProviders(resourceProviders);

and for a null pointer exception

2023-09-28 15:03:21.037 [http-nio-8080-exec-1] ERROR c.u.f.r.s.i.ExceptionHandlingInterceptor [ExceptionHandlingInterceptor.java:175] Failure during REST processing
ca.uhn.fhir.rest.server.exceptions.InternalErrorException: HAPI-0389: Failed to call access method: java.lang.NullPointerException: Cannot invoke "ca.uhn.fhir.jpa.api.dao.IFhirResourceDao.translateRawParameters(java.util.Map, ca.uhn.fhir.jpa.searchparam.SearchParameterMap)" because the return value of "ca.uhn.fhir.jpa.rp.r4.PatientResourceProvider.getDao()" is null
        at ca.uhn.fhir.rest.server.method.BaseMethodBinding.invokeServerMethod(BaseMethodBinding.java:269)
        at ca.uhn.fhir.rest.server.method.SearchMethodBinding.invokeServer(SearchMethodBinding.java:293)
        at ca.uhn.fhir.rest.server.method.SearchMethodBinding.invokeServer(SearchMethodBinding.java:54)
        at ca.uhn.fhir.rest.server.method.BaseResourceReturningMethodBinding.doInvokeServer(BaseResourceReturningMethodBinding.java:324)
        at ca.uhn.fhir.rest.server.method.BaseResourceReturningMethodBinding.invokeServer(BaseResourceReturningMethodBinding.java:429)
        at ca.uhn.fhir.rest.server.RestfulServer.handleRequest(RestfulServer.java:1170)
        at ca.uhn.fhir.rest.server.RestfulServer.doGet(RestfulServer.java:416)
        at ca.uhn.fhir.rest.server.RestfulServer.service(RestfulServer.java:1869)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:209)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)
        at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)
        at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:126)
        at org.springframework.boot.web.servlet.support.ErrorPageFilter.access$000(ErrorPageFilter.java:64)
        at org.springframework.boot.web.servlet.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:101)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
        at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:119)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:481)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:130)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:673)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:390)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:926)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1791)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
        at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
        at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.reflect.InvocationTargetException: null
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at ca.uhn.fhir.rest.server.method.BaseMethodBinding.invokeServerMethod(BaseMethodBinding.java:261)
        ... 45 common frames omitted
Caused by: java.lang.NullPointerException: Cannot invoke "ca.uhn.fhir.jpa.api.dao.IFhirResourceDao.translateRawParameters(java.util.Map, ca.uhn.fhir.jpa.searchparam.SearchParameterMap)" because the return value of "ca.uhn.fhir.jpa.rp.r4.PatientResourceProvider.getDao()" is null
        at ca.uhn.fhir.jpa.rp.r4.PatientResourceProvider.search(PatientResourceProvider.java:262)
Reply all
Reply to author
Forward
0 new messages