001package ca.uhn.fhir.rest.server.interceptor; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2016 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 org.apache.commons.lang3.Validate; 027import org.hl7.fhir.instance.model.api.IBaseResource; 028 029import ca.uhn.fhir.rest.api.RestOperationTypeEnum; 030import ca.uhn.fhir.rest.method.RequestDetails; 031import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; 032import ca.uhn.fhir.validation.FhirValidator; 033import ca.uhn.fhir.validation.ResultSeverityEnum; 034import ca.uhn.fhir.validation.ValidationResult; 035 036/** 037 * 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 038 * headers to the response or fail the request with an {@link UnprocessableEntityException HTTP 422 Unprocessable Entity}. 039 */ 040public class ResponseValidatingInterceptor extends BaseValidatingInterceptor<IBaseResource> { 041 042 /** 043 * X-HAPI-Request-Validation 044 */ 045 public static final String DEFAULT_RESPONSE_HEADER_NAME = "X-FHIR-Response-Validation"; 046 047 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResponseValidatingInterceptor.class); 048 049 private Set<RestOperationTypeEnum> myExcludeOperationTypes; 050 051 /** 052 * 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. 053 */ 054 public void addExcludeOperationType(RestOperationTypeEnum theOperationType) { 055 Validate.notNull(theOperationType, "theOperationType must not be null"); 056 if (myExcludeOperationTypes == null) { 057 myExcludeOperationTypes = new HashSet<RestOperationTypeEnum>(); 058 } 059 myExcludeOperationTypes.add(theOperationType); 060 } 061 062 @Override 063 ValidationResult doValidate(FhirValidator theValidator, IBaseResource theRequest) { 064 return theValidator.validateWithResult(theRequest); 065 } 066 067 @Override 068 public boolean outgoingResponse(RequestDetails theRequestDetails, IBaseResource theResponseObject) { 069 RestOperationTypeEnum operationType = theRequestDetails.getRestOperationType(); 070 if (operationType != null && myExcludeOperationTypes != null && myExcludeOperationTypes.contains(operationType)) { 071 ourLog.trace("Operation type {} is excluded from validation", operationType); 072 return true; 073 } 074 075 validate(theResponseObject, theRequestDetails); 076 077 return true; 078 } 079 080 @Override 081 String provideDefaultResponseHeaderName() { 082 return DEFAULT_RESPONSE_HEADER_NAME; 083 } 084 085 /** 086 * Sets the name of the response header to add validation failures to 087 * 088 * @see #DEFAULT_RESPONSE_HEADER_NAME 089 * @see #setAddResponseHeaderOnSeverity(ResultSeverityEnum) 090 */ 091 @Override 092 public void setResponseHeaderName(String theResponseHeaderName) { 093 super.setResponseHeaderName(theResponseHeaderName); 094 } 095 096}