001package ca.uhn.fhir.jpa.interceptor;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2023 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.Hook;
024import ca.uhn.fhir.interceptor.api.Interceptor;
025import ca.uhn.fhir.interceptor.api.Pointcut;
026import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
027import ca.uhn.fhir.rest.api.server.RequestDetails;
028import org.apache.commons.lang3.Validate;
029
030/**
031 * This interceptor for the HAPI FHIR JPA server forces all queries to
032 * be performed as offset queries. This means that the query cache will
033 * not be used and searches will never result in any writes to the
034 * database.
035 */
036@Interceptor
037public class ForceOffsetSearchModeInterceptor {
038
039        /**
040         * Default value for {@link #setDefaultCount(Integer)}
041         */
042        public static final int DEFAULT_DEFAULT_COUNT = 100;
043
044        private Integer myDefaultCount = DEFAULT_DEFAULT_COUNT;
045
046        public void setDefaultCount(Integer theDefaultCount) {
047                Validate.notNull(theDefaultCount, "theDefaultCount must not be null");
048                myDefaultCount = theDefaultCount;
049        }
050
051        @Hook(Pointcut.STORAGE_PRESEARCH_REGISTERED)
052        public void storagePreSearchRegistered(SearchParameterMap theMap, RequestDetails theRequestDetails) {
053
054                // If the params indicate a synchronous search, it doesn't make
055                // sense to inject any offset processing since the search
056                // will be handled synchronously anyhow
057                if (theMap.isLoadSynchronous()) {
058                        return;
059                }
060
061                if (theMap.getOffset() == null) {
062                        theMap.setOffset(0);
063                }
064                if (theMap.getCount() == null) {
065                        theMap.setCount(myDefaultCount);
066                }
067        }
068
069
070}