001package ca.uhn.fhir.jpa.batch.job;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
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.context.FhirContext;
024import ca.uhn.fhir.interceptor.model.RequestPartitionId;
025import ca.uhn.fhir.jpa.batch.job.model.PartitionedUrl;
026import ca.uhn.fhir.jpa.batch.job.model.RequestListJson;
027import ca.uhn.fhir.jpa.partition.IRequestPartitionHelperSvc;
028import ca.uhn.fhir.jpa.searchparam.MatchUrlService;
029import ca.uhn.fhir.jpa.searchparam.ResourceSearch;
030import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
031import ca.uhn.fhir.rest.api.server.RequestDetails;
032import org.springframework.beans.factory.annotation.Autowired;
033
034import java.util.ArrayList;
035import java.util.List;
036import java.util.Set;
037
038public class PartitionedUrlValidator {
039        @Autowired
040        MatchUrlService myMatchUrlService;
041        @Autowired
042        IRequestPartitionHelperSvc myRequestPartitionHelperSvc;
043        @Autowired
044        FhirContext myFhirContext;
045
046        public PartitionedUrlValidator() {
047        }
048
049        /**
050         * This method will throw an exception if the user is not allowed to access the requested resource type on the partition determined by the request
051         */
052
053        public RequestListJson buildRequestListJson(RequestDetails theRequest, List<String> theUrlsToProcess) {
054                List<PartitionedUrl> partitionedUrls = new ArrayList<>();
055                for (String url : theUrlsToProcess) {
056                        ResourceSearch resourceSearch = myMatchUrlService.getResourceSearch(url);
057                        RequestPartitionId requestPartitionId = myRequestPartitionHelperSvc.determineReadPartitionForRequestForSearchType(theRequest, resourceSearch.getResourceName(), resourceSearch.getSearchParameterMap(), null);
058                        partitionedUrls.add(new PartitionedUrl(url, requestPartitionId));
059                }
060                RequestListJson retval = new RequestListJson();
061                retval.setPartitionedUrls(partitionedUrls);
062                return retval;
063        }
064
065        public RequestPartitionId requestPartitionIdFromRequest(RequestDetails theRequest) {
066                Set<String> allResourceNames = myFhirContext.getResourceTypes();
067                SearchParameterMap map = SearchParameterMap.newSynchronous();
068                // Verify that the user has access to every resource type on the server:
069                for (String resourceName : allResourceNames) {
070                        myRequestPartitionHelperSvc.determineReadPartitionForRequestForSearchType(theRequest, resourceName, map, null);
071                }
072                // Then return the partition for the Patient resource type.  Note Patient was an arbitrary choice here.
073                return myRequestPartitionHelperSvc.determineReadPartitionForRequestForSearchType(theRequest, "Patient", map, null);
074        }
075}