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