package ca.uhn.fhir.rest.server.interceptor;

import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.Interceptor;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.validation.ValidationResult;
import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
import org.hl7.fhir.instance.model.api.IBaseResource;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

@Interceptor
public class ValidationResultEnrichingInterceptor {

	/**
	 * A {@link RequestDetails#getUserData() user data} entry will be created with this
	 * key which contains the {@link ValidationResult} from validating the request.
	 */
	public static final String REQUEST_VALIDATION_RESULT = ValidationResultEnrichingInterceptor.class.getName() + "_REQUEST_VALIDATION_RESULT";

	@SuppressWarnings("unchecked")
	@Hook(Pointcut.SERVER_OUTGOING_RESPONSE)
	public boolean addValidationResultsToOperationOutcome(RequestDetails theRequestDetails, IBaseResource theResponseObject) {
		if (theResponseObject instanceof IBaseOperationOutcome) {
			IBaseOperationOutcome oo = (IBaseOperationOutcome) theResponseObject;

			if (theRequestDetails != null) {
				List<ValidationResult> validationResult = (List<ValidationResult>) theRequestDetails.getUserData().remove(REQUEST_VALIDATION_RESULT);
				if (validationResult != null) {
					for (ValidationResult next : validationResult) {
						next.populateOperationOutcome(oo);
					}
				}
			}

		}

		return true;
	}


	@SuppressWarnings("unchecked")
	public static void addValidationResultToRequestDetails(@Nullable RequestDetails theRequestDetails, @Nonnull ValidationResult theValidationResult) {
		if (theRequestDetails != null) {
			List<ValidationResult> results = (List<ValidationResult>) theRequestDetails.getUserData().computeIfAbsent(REQUEST_VALIDATION_RESULT, t -> new ArrayList<>(2));
			results.add(theValidationResult);
		}
	}
}

@@ -39,16 +39,684 @@
 eptor;%0A%0A
+/*-%0A * #%25L%0A * HAPI FHIR - Server Framework%0A * %25%25%0A * Copyright (C) 2014 - 2021 Smile CDR, Inc.%0A * %25%25%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A * #L%25%0A */%0A%0A
 import c

Add license header