HAPI FHIR Interceptor === 主要參考資料:[Interceptors: Overview](https://hapifhir.io/hapi-fhir/docs/interceptors/interceptors.html) Interceptor概述 --- * HAPI FHIR擴充(客製化)的一個主要方法,當思考「我如何讓 HAPI FHIR 執行 X」時,答案通常是建立Interceptor。 * Interceptor中文直譯為"攔截器",顧名思義,就是在FHIR 伺服器處理應用程式需求的pipeline過程中攔截相關訊息並提供客製化機制,達到某種特殊用途的效果。例如可針對特定IP攔截或放行、紀錄log、增加驗證機制等。 * Interceptor名詞介紹: * Interceptor:Java Class,擁有一個以上的Hook Method,可以使用 maker annotation **@interceptor**(非必要條件)。 * Hook:單一的Interceptor的method,當HAPI FHIR特定情況發生時所對應的回應,必須使用 **@Hook** * Pointcut:HAPI FHIR處理pipeline中的一個攔截點, 每一個Hook Method必須宣告所對應的攔截點。 * Hook Params:每一個Pointcut所定義之參數列表。可傳送給該Pointcut所對應之Hook Method。 Pointcut說明 --- Pointcut可區分成三個類型:Client、Plain Server與JPA Server。使用者可依據實際需要,選擇適當的Piontcut,開發對應的Hook Method已達到預期的效果。 * Client Pointcuts ![interceptors-client-pipeline](https://hackmd.io/_uploads/rkpevSaPJg.svg) * Plain Server Pointcuts * ![interceptors-server-pipeline](https://hackmd.io/_uploads/HJ7LPravJg.svg) * JPA Server Pointcut ![interceptors-server-jpa-pipeline](https://hackmd.io/_uploads/S1sse4Rv1e.svg) Interceptor客製流程 -- 1. 1 建立Interceptor class 2. Class至少包含一個標記為@Hook的method 3. Hook method必須對應到某一個pointcut 4. Hook method必須宣告為public 以下是一個簡單的例子,使用SERVER_INCOMING_REQUEST_PRE_HANDLED紀錄呼叫端的Url與SERVER_OUTGOING_RESPONSE紀錄response結果。 ``` package tw.com.tmhtc.fhir; import ca.uhn.fhir.interceptor.api.Hook; import ca.uhn.fhir.interceptor.api.Interceptor; import ca.uhn.fhir.interceptor.api.Pointcut; import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.api.server.ResponseDetails; @Interceptor public class SimpleInterceptor { @Hook(Pointcut.SERVER_INCOMING_REQUEST_PRE_HANDLED) public void incomingRequestPreHandled(RequestDetails theRequestDetails) { System.out.println("Request was handled: " + theRequestDetails.getCompleteUrl()); } @Hook(Pointcut.SERVER_OUTGOING_RESPONSE) public void outgoingResponse(ResponseDetails theResponseDetails) { System.out.println("Response was sent: " + theResponseDetails.getResponseCode()); } } ```