001/*- 002 * #%L 003 * HAPI FHIR - Server Framework 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.rest.server.interceptor.validation.address.impl; 021 022import ca.uhn.fhir.context.FhirContext; 023import ca.uhn.fhir.i18n.Msg; 024import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationException; 025import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationResult; 026import ca.uhn.fhir.rest.server.interceptor.validation.address.IAddressValidator; 027import com.fasterxml.jackson.databind.JsonNode; 028import com.fasterxml.jackson.databind.ObjectMapper; 029import org.hl7.fhir.instance.model.api.IBase; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032import org.springframework.http.ResponseEntity; 033import org.springframework.web.client.RestTemplate; 034 035import java.util.Properties; 036 037public abstract class BaseRestfulValidator implements IAddressValidator { 038 039 public static final String PROPERTY_SERVICE_KEY = "service.key"; 040 public static final String PROPERTY_SERVICE_ENDPOINT = "service.endpoint"; 041 042 private static final Logger ourLog = LoggerFactory.getLogger(BaseRestfulValidator.class); 043 044 private Properties myProperties; 045 046 protected abstract AddressValidationResult getValidationResult( 047 AddressValidationResult theResult, JsonNode response, FhirContext theFhirContext) throws Exception; 048 049 protected abstract ResponseEntity<String> getResponseEntity(IBase theAddress, FhirContext theFhirContext) 050 throws Exception; 051 052 protected RestTemplate newTemplate() { 053 return new RestTemplate(); 054 } 055 056 public BaseRestfulValidator(Properties theProperties) { 057 myProperties = theProperties; 058 } 059 060 @Override 061 public AddressValidationResult isValid(IBase theAddress, FhirContext theFhirContext) 062 throws AddressValidationException { 063 ResponseEntity<String> entity; 064 try { 065 entity = getResponseEntity(theAddress, theFhirContext); 066 } catch (Exception e) { 067 throw new AddressValidationException( 068 Msg.code(345) + "Unable to complete address validation web-service call", e); 069 } 070 071 if (isError(entity)) { 072 throw new AddressValidationException( 073 Msg.code(346) + String.format("Service returned an error code %s", entity.getStatusCode())); 074 } 075 076 String responseBody = entity.getBody(); 077 ourLog.debug("Validation service returned {}", responseBody); 078 079 AddressValidationResult retVal = new AddressValidationResult(); 080 retVal.setRawResponse(responseBody); 081 082 try { 083 JsonNode response = new ObjectMapper().readTree(responseBody); 084 ourLog.debug("Parsed address validator response {}", response); 085 return getValidationResult(retVal, response, theFhirContext); 086 } catch (Exception e) { 087 throw new AddressValidationException(Msg.code(347) + "Unable to validate the address", e); 088 } 089 } 090 091 protected boolean isError(ResponseEntity<String> entity) { 092 return entity.getStatusCode().isError(); 093 } 094 095 public Properties getProperties() { 096 return myProperties; 097 } 098 099 public void setProperties(Properties theProperties) { 100 myProperties = theProperties; 101 } 102 103 protected String getApiKey() { 104 return getProperties().getProperty(PROPERTY_SERVICE_KEY); 105 } 106 107 protected String getApiEndpoint() { 108 return getProperties().getProperty(PROPERTY_SERVICE_ENDPOINT); 109 } 110}