001package ca.uhn.fhir.rest.server.interceptor; 002 003/* 004 * #%L 005 * HAPI FHIR - Server Framework 006 * %% 007 * Copyright (C) 2014 - 2019 University Health Network 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 java.util.HashSet; 024import java.util.Set; 025 026import ca.uhn.fhir.interceptor.api.Hook; 027import ca.uhn.fhir.interceptor.api.Pointcut; 028import org.apache.commons.lang3.Validate; 029import org.hl7.fhir.instance.model.api.IBaseResource; 030 031import ca.uhn.fhir.rest.api.RestOperationTypeEnum; 032import ca.uhn.fhir.rest.api.server.RequestDetails; 033import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; 034import ca.uhn.fhir.validation.FhirValidator; 035import ca.uhn.fhir.validation.ResultSeverityEnum; 036import ca.uhn.fhir.validation.ValidationResult; 037 038/** 039 * This interceptor intercepts each outgoing response and if it contains a FHIR resource, validates that resource. The interceptor may be configured to run any validator modules, and will then add 040 * headers to the response or fail the request with an {@link UnprocessableEntityException HTTP 422 Unprocessable Entity}. 041 */ 042public class ResponseValidatingInterceptor extends BaseValidatingInterceptor<IBaseResource> { 043 044 /** 045 * X-HAPI-Request-Validation 046 */ 047 public static final String DEFAULT_RESPONSE_HEADER_NAME = "X-FHIR-Response-Validation"; 048 049 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResponseValidatingInterceptor.class); 050 051 private Set<RestOperationTypeEnum> myExcludeOperationTypes; 052 053 /** 054 * Do not validate the following operations. A common use for this is to exclude {@link RestOperationTypeEnum#METADATA} so that this operation will execute as quickly as possible. 055 */ 056 public void addExcludeOperationType(RestOperationTypeEnum theOperationType) { 057 Validate.notNull(theOperationType, "theOperationType must not be null"); 058 if (myExcludeOperationTypes == null) { 059 myExcludeOperationTypes = new HashSet<>(); 060 } 061 myExcludeOperationTypes.add(theOperationType); 062 } 063 064 @Override 065 ValidationResult doValidate(FhirValidator theValidator, IBaseResource theRequest) { 066 return theValidator.validateWithResult(theRequest); 067 } 068 069 @Hook(Pointcut.SERVER_OUTGOING_RESPONSE) 070 public boolean outgoingResponse(RequestDetails theRequestDetails, IBaseResource theResponseObject) { 071 RestOperationTypeEnum operationType = theRequestDetails.getRestOperationType(); 072 if (operationType != null && myExcludeOperationTypes != null && myExcludeOperationTypes.contains(operationType)) { 073 ourLog.trace("Operation type {} is excluded from validation", operationType); 074 return true; 075 } 076 077 validate(theResponseObject, theRequestDetails); 078 079 return true; 080 } 081 082 @Override 083 String provideDefaultResponseHeaderName() { 084 return DEFAULT_RESPONSE_HEADER_NAME; 085 } 086 087 /** 088 * Sets the name of the response header to add validation failures to 089 * 090 * @see #DEFAULT_RESPONSE_HEADER_NAME 091 * @see #setAddResponseHeaderOnSeverity(ResultSeverityEnum) 092 */ 093 @Override 094 public void setResponseHeaderName(String theResponseHeaderName) { 095 super.setResponseHeaderName(theResponseHeaderName); 096 } 097 098}