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.Interceptor;
025import ca.uhn.fhir.interceptor.api.Pointcut;
026import ca.uhn.fhir.rest.api.server.RequestDetails;
027import ca.uhn.fhir.validation.ValidationResult;
028import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
029import org.hl7.fhir.instance.model.api.IBaseResource;
030
031import javax.annotation.Nonnull;
032import javax.annotation.Nullable;
033import java.util.ArrayList;
034import java.util.List;
035
036@Interceptor
037public class ValidationResultEnrichingInterceptor {
038
039        /**
040         * A {@link RequestDetails#getUserData() user data} entry will be created with this
041         * key which contains the {@link ValidationResult} from validating the request.
042         */
043        public static final String REQUEST_VALIDATION_RESULT = ValidationResultEnrichingInterceptor.class.getName() + "_REQUEST_VALIDATION_RESULT";
044
045        @SuppressWarnings("unchecked")
046        @Hook(Pointcut.SERVER_OUTGOING_RESPONSE)
047        public boolean addValidationResultsToOperationOutcome(RequestDetails theRequestDetails, IBaseResource theResponseObject) {
048                if (theResponseObject instanceof IBaseOperationOutcome) {
049                        IBaseOperationOutcome oo = (IBaseOperationOutcome) theResponseObject;
050
051                        if (theRequestDetails != null) {
052                                List<ValidationResult> validationResult = (List<ValidationResult>) theRequestDetails.getUserData().remove(REQUEST_VALIDATION_RESULT);
053                                if (validationResult != null) {
054                                        for (ValidationResult next : validationResult) {
055                                                next.populateOperationOutcome(oo);
056                                        }
057                                }
058                        }
059
060                }
061
062                return true;
063        }
064
065
066        @SuppressWarnings("unchecked")
067        public static void addValidationResultToRequestDetails(@Nullable RequestDetails theRequestDetails, @Nonnull ValidationResult theValidationResult) {
068                if (theRequestDetails != null) {
069                        List<ValidationResult> results = (List<ValidationResult>) theRequestDetails.getUserData().computeIfAbsent(REQUEST_VALIDATION_RESULT, t -> new ArrayList<>(2));
070                        results.add(theValidationResult);
071                }
072        }
073}