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.i18n.Msg;
024import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
025import ca.uhn.fhir.jpa.batch.config.BatchConstants;
026import ca.uhn.fhir.jpa.batch.job.model.PartitionedUrl;
027import ca.uhn.fhir.jpa.batch.job.model.RequestListJson;
028import ca.uhn.fhir.jpa.searchparam.MatchUrlService;
029import ca.uhn.fhir.jpa.searchparam.ResourceSearch;
030import org.springframework.batch.core.JobParameters;
031import org.springframework.batch.core.JobParametersInvalidException;
032import org.springframework.batch.core.JobParametersValidator;
033
034/**
035 * This class will prevent a job from running any of the provided URLs are not valid on this server.
036 */
037public class MultiUrlJobParameterValidator implements JobParametersValidator {
038        public static String JOB_PARAM_OPERATION_NAME = "operation-name";
039        private final MatchUrlService myMatchUrlService;
040        private final DaoRegistry myDaoRegistry;
041
042        public MultiUrlJobParameterValidator(MatchUrlService theMatchUrlService, DaoRegistry theDaoRegistry) {
043                myMatchUrlService = theMatchUrlService;
044                myDaoRegistry = theDaoRegistry;
045        }
046
047        @Override
048        public void validate(JobParameters theJobParameters) throws JobParametersInvalidException {
049                if (theJobParameters == null) {
050                        throw new JobParametersInvalidException(Msg.code(1280) + "This job requires Parameters: [urlList]");
051                }
052
053                RequestListJson requestListJson = RequestListJson.fromJson(theJobParameters.getString(BatchConstants.JOB_PARAM_REQUEST_LIST));
054                for (PartitionedUrl partitionedUrl : requestListJson.getPartitionedUrls()) {
055                        String url = partitionedUrl.getUrl();
056                        try {
057                                ResourceSearch resourceSearch = myMatchUrlService.getResourceSearch(url, partitionedUrl.getRequestPartitionId());
058                                String resourceName = resourceSearch.getResourceName();
059                                if (!myDaoRegistry.isResourceTypeSupported(resourceName)) {
060                                        throw new JobParametersInvalidException(Msg.code(1281) + "The resource type " + resourceName + " is not supported on this server.");
061                                }
062                        } catch (UnsupportedOperationException e) {
063                                throw new JobParametersInvalidException(Msg.code(1282) + "Failed to parse " + theJobParameters.getString(JOB_PARAM_OPERATION_NAME) + " " + BatchConstants.JOB_PARAM_REQUEST_LIST + " item " + url + ": " + e.getMessage());
064                        }
065                }
066        }
067}