001package ca.uhn.fhir.jpa.validation; 002 003/*- 004 * #%L 005 * HAPI FHIR Storage api 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.context.FhirContext; 025import ca.uhn.fhir.context.support.IValidationSupport; 026import ca.uhn.fhir.jpa.api.dao.DaoRegistry; 027import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; 028import ca.uhn.fhir.rest.api.server.RequestDetails; 029import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; 030import org.hl7.fhir.common.hapi.validation.validator.VersionSpecificWorkerContextWrapper; 031import org.hl7.fhir.exceptions.DefinitionException; 032import org.hl7.fhir.exceptions.FHIRException; 033import org.hl7.fhir.exceptions.FHIRFormatError; 034import org.hl7.fhir.instance.model.api.IBaseResource; 035import org.hl7.fhir.r4.model.IdType; 036import org.hl7.fhir.r5.elementmodel.Element; 037import org.hl7.fhir.r5.elementmodel.JsonParser; 038import org.hl7.fhir.r5.model.CanonicalResource; 039import org.hl7.fhir.r5.utils.validation.IResourceValidator; 040import org.hl7.fhir.r5.utils.validation.IValidatorResourceFetcher; 041import org.slf4j.Logger; 042import org.slf4j.LoggerFactory; 043import org.springframework.beans.factory.annotation.Autowired; 044 045import javax.annotation.PostConstruct; 046import java.io.IOException; 047import java.net.MalformedURLException; 048import java.net.URISyntaxException; 049import java.util.Locale; 050 051public class ValidatorResourceFetcher implements IValidatorResourceFetcher { 052 053 private static final Logger ourLog = LoggerFactory.getLogger(ValidatorResourceFetcher.class); 054 055 @Autowired 056 private DaoRegistry myDaoRegistry; 057 @Autowired 058 private FhirContext myFhirContext; 059 @Autowired 060 private IValidationSupport myValidationSupport; 061 private VersionSpecificWorkerContextWrapper myVersionSpecificContextWrapper; 062 063 @PostConstruct 064 public void start() { 065 myVersionSpecificContextWrapper = VersionSpecificWorkerContextWrapper.newVersionSpecificWorkerContextWrapper(myValidationSupport); 066 } 067 068 @Override 069 public Element fetch(IResourceValidator iResourceValidator, Object appContext, String theUrl) throws FHIRFormatError, DefinitionException, FHIRException, IOException { 070 IdType id = new IdType(theUrl); 071 String resourceType = id.getResourceType(); 072 IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(resourceType); 073 IBaseResource target; 074 try { 075 target = dao.read(id, (RequestDetails) appContext); 076 } catch (ResourceNotFoundException e) { 077 ourLog.info("Failed to resolve local reference: {}", theUrl); 078 return null; 079 } 080 081 try { 082 return new JsonParser(myVersionSpecificContextWrapper).parse(myFhirContext.newJsonParser().encodeResourceToString(target), resourceType); 083 } catch (Exception e) { 084 throw new FHIRException(Msg.code(576) + e); 085 } 086 } 087 088 @Override 089 public boolean resolveURL(IResourceValidator iResourceValidator, Object o, String s, String s1, String s2) throws IOException, FHIRException { 090 return true; 091 } 092 093 @Override 094 public byte[] fetchRaw(IResourceValidator iResourceValidator, String s) throws MalformedURLException, IOException { 095 throw new UnsupportedOperationException(Msg.code(577)); 096 } 097 098 @Override 099 public IValidatorResourceFetcher setLocale(Locale locale) { 100 // ignore 101 return this; 102 } 103 104 @Override 105 public CanonicalResource fetchCanonicalResource(IResourceValidator iResourceValidator, String s) throws URISyntaxException { 106 return null; 107 } 108 109 @Override 110 public boolean fetchesCanonicalResource(IResourceValidator iResourceValidator, String s) { 111 return false; 112 } 113}