001package ca.uhn.fhir.rest.server.util; 002 003/*- 004 * #%L 005 * HAPI FHIR - Server Framework 006 * %% 007 * Copyright (C) 2014 - 2022 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import ca.uhn.fhir.interceptor.api.HookParams; 024import ca.uhn.fhir.interceptor.api.IInterceptorBroadcaster; 025import ca.uhn.fhir.interceptor.api.Pointcut; 026import ca.uhn.fhir.rest.api.server.RequestDetails; 027 028import javax.annotation.Nullable; 029 030public class CompositeInterceptorBroadcaster { 031 032 /** 033 * Non instantiable 034 */ 035 private CompositeInterceptorBroadcaster() { 036 // nothing 037 } 038 039 /** 040 * Broadcast hooks to both the interceptor service associated with the request, as well 041 * as the one associated with the JPA module. 042 */ 043 public static boolean doCallHooks(IInterceptorBroadcaster theInterceptorBroadcaster, @Nullable RequestDetails theRequestDetails, Pointcut thePointcut, HookParams theParams) { 044 return newCompositeBroadcaster(theInterceptorBroadcaster, theRequestDetails).callHooks(thePointcut, theParams); 045 } 046 047 /** 048 * Broadcast hooks to both the interceptor service associated with the request, as well 049 * as the one associated with the JPA module. 050 */ 051 public static Object doCallHooksAndReturnObject(IInterceptorBroadcaster theInterceptorBroadcaster, RequestDetails theRequestDetails, Pointcut thePointcut, HookParams theParams) { 052 return newCompositeBroadcaster(theInterceptorBroadcaster, theRequestDetails).callHooksAndReturnObject(thePointcut, theParams); 053 } 054 055 public static boolean hasHooks(Pointcut thePointcut, IInterceptorBroadcaster theInterceptorBroadcaster, RequestDetails theRequestDetails) { 056 return newCompositeBroadcaster(theInterceptorBroadcaster, theRequestDetails).hasHooks(thePointcut); 057 } 058 059 /** 060 * @since 5.5.0 061 */ 062 public static IInterceptorBroadcaster newCompositeBroadcaster(IInterceptorBroadcaster theInterceptorBroadcaster, RequestDetails theRequestDetails) { 063 return new IInterceptorBroadcaster() { 064 @Override 065 public boolean callHooks(Pointcut thePointcut, HookParams theParams) { 066 boolean retVal = true; 067 if (theInterceptorBroadcaster != null) { 068 retVal = theInterceptorBroadcaster.callHooks(thePointcut, theParams); 069 } 070 if (theRequestDetails != null && theRequestDetails.getInterceptorBroadcaster() != null && retVal) { 071 IInterceptorBroadcaster interceptorBroadcaster = theRequestDetails.getInterceptorBroadcaster(); 072 interceptorBroadcaster.callHooks(thePointcut, theParams); 073 } 074 return retVal; 075 } 076 077 @Override 078 public Object callHooksAndReturnObject(Pointcut thePointcut, HookParams theParams) { 079 Object retVal = true; 080 if (theInterceptorBroadcaster != null) { 081 retVal = theInterceptorBroadcaster.callHooksAndReturnObject(thePointcut, theParams); 082 } 083 if (theRequestDetails != null && theRequestDetails.getInterceptorBroadcaster() != null && retVal == null) { 084 IInterceptorBroadcaster interceptorBroadcaster = theRequestDetails.getInterceptorBroadcaster(); 085 retVal = interceptorBroadcaster.callHooksAndReturnObject(thePointcut, theParams); 086 } 087 return retVal; 088 } 089 090 @Override 091 public boolean hasHooks(Pointcut thePointcut) { 092 if (theInterceptorBroadcaster != null && theInterceptorBroadcaster.hasHooks(thePointcut)) { 093 return true; 094 } 095 return theRequestDetails != null && 096 theRequestDetails.getInterceptorBroadcaster() != null && 097 theRequestDetails.getInterceptorBroadcaster().hasHooks(thePointcut); 098 } 099 }; 100 } 101}