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