001/*-
002 * #%L
003 * HAPI FHIR JPA - Search Parameters
004 * %%
005 * Copyright (C) 2014 - 2024 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.searchparam.matcher;
021
022import ca.uhn.fhir.jpa.searchparam.MatchUrlService;
023import ca.uhn.fhir.rest.server.interceptor.auth.IAuthorizationSearchParamMatcher;
024import org.hl7.fhir.instance.model.api.IBaseResource;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * Adapter from {@link SearchParamMatcher} to our authorization version.
030 */
031public class AuthorizationSearchParamMatcher implements IAuthorizationSearchParamMatcher {
032        private static final Logger ourLog = LoggerFactory.getLogger(AuthorizationSearchParamMatcher.class);
033        private final SearchParamMatcher mySearchParamMatcher;
034
035        public AuthorizationSearchParamMatcher(SearchParamMatcher mySearchParamMatcher) {
036                this.mySearchParamMatcher = mySearchParamMatcher;
037        }
038
039        @Override
040        public MatchResult match(String theQueryParameters, IBaseResource theResource) {
041                try {
042                        InMemoryMatchResult inMemoryMatchResult = mySearchParamMatcher.match(theQueryParameters, theResource, null);
043                        if (!inMemoryMatchResult.supported()) {
044                                return MatchResult.buildUnsupported(inMemoryMatchResult.getUnsupportedReason());
045                        }
046                        if (inMemoryMatchResult.matched()) {
047                                return MatchResult.buildMatched();
048                        } else {
049                                return MatchResult.buildUnmatched();
050                        }
051                } catch (MatchUrlService.UnrecognizedSearchParameterException e) {
052                        // The matcher treats a bad expression as InvalidRequestException because
053                        // it assumes it is during SearchParameter storage.
054                        // Instead, we adapt this to UNSUPPORTED during authorization.
055                        // We may be applying to all types, and this filter won't match.
056                        ourLog.info("Unsupported filter {} applied to resource: {}", theQueryParameters, e.getMessage());
057                        return MatchResult.buildUnsupported(e.getMessage());
058                }
059        }
060}