001package ca.uhn.fhir.rest.server.interceptor;
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.IPreResourceShowDetails;
027import ca.uhn.fhir.rest.api.server.RequestDetails;
028import ca.uhn.fhir.rest.api.server.SimplePreResourceShowDetails;
029import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
030import ca.uhn.fhir.rest.server.util.CompositeInterceptorBroadcaster;
031import org.hl7.fhir.instance.model.api.IBaseResource;
032
033import javax.annotation.CheckReturnValue;
034import java.util.List;
035import java.util.Objects;
036
037public class ServerInterceptorUtil {
038
039        private ServerInterceptorUtil() {
040                super();
041        }
042
043        /**
044         * Fires {@link Pointcut#STORAGE_PRESHOW_RESOURCES} interceptor hook, and potentially remove resources
045         * from the resource list
046         */
047        @CheckReturnValue
048        public static List<IBaseResource> fireStoragePreshowResource(List<IBaseResource> theResources, RequestDetails theRequest, IInterceptorBroadcaster theInterceptorBroadcaster) {
049                List<IBaseResource> retVal = theResources;
050                retVal.removeIf(Objects::isNull);
051
052                // Interceptor call: STORAGE_PRESHOW_RESOURCE
053                // This can be used to remove results from the search result details before
054                // the user has a chance to know that they were in the results
055                if (retVal.size() > 0) {
056                        SimplePreResourceShowDetails accessDetails = new SimplePreResourceShowDetails(retVal);
057                        HookParams params = new HookParams()
058                                .add(IPreResourceShowDetails.class, accessDetails)
059                                .add(RequestDetails.class, theRequest)
060                                .addIfMatchesType(ServletRequestDetails.class, theRequest);
061                        CompositeInterceptorBroadcaster.doCallHooks(theInterceptorBroadcaster, theRequest, Pointcut.STORAGE_PRESHOW_RESOURCES, params);
062
063                        retVal = accessDetails.toList();
064                        retVal.removeIf(Objects::isNull);
065                }
066
067                return retVal;
068        }
069
070}