001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.interceptor;
021
022import ca.uhn.fhir.i18n.Msg;
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.jpa.model.IProvenanceAgentsPointcutParameter;
027import ca.uhn.fhir.jpa.model.ProvenanceAgentsPointcutParameter;
028import ca.uhn.fhir.model.api.IProvenanceAgent;
029import ca.uhn.fhir.rest.api.server.RequestDetails;
030import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
031import ca.uhn.fhir.rest.server.util.CompositeInterceptorBroadcaster;
032
033import java.util.Collections;
034import java.util.List;
035
036/**
037 *  Utility class for PROVENANCE_AGENTS pointcut.
038 */
039public class ProvenanceAgentsPointcutUtil {
040
041        private ProvenanceAgentsPointcutUtil() {}
042
043        public static List<IProvenanceAgent> ifHasCallHooks(
044                        RequestDetails theRequestDetails, IInterceptorBroadcaster theInterceptorBroadcaster) {
045                IInterceptorBroadcaster compositeBroadcaster =
046                                CompositeInterceptorBroadcaster.newCompositeBroadcaster(theInterceptorBroadcaster, theRequestDetails);
047
048                if (compositeBroadcaster.hasHooks(Pointcut.PROVENANCE_AGENTS)) {
049
050                        ProvenanceAgentsPointcutParameter agentParam = new ProvenanceAgentsPointcutParameter();
051                        agentParam.setRequestDetails(theRequestDetails);
052
053                        HookParams hookParams = new HookParams().add(IProvenanceAgentsPointcutParameter.class, agentParam);
054                        compositeBroadcaster.callHooks(Pointcut.PROVENANCE_AGENTS, hookParams);
055
056                        List<IProvenanceAgent> agents = agentParam.getProvenanceAgents();
057                        if (agents.isEmpty()) {
058                                throw new InternalErrorException(Msg.code(2723)
059                                                + "No Provenance Agent was provided by any interceptor for Pointcut.PROVENANCE_AGENTS");
060                        }
061                        return agents;
062                }
063                return Collections.emptyList();
064        }
065}