
001/*- 002 * #%L 003 * HAPI FHIR - Server Framework 004 * %% 005 * Copyright (C) 2014 - 2025 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.rest.server.interceptor.validation; 021 022import ca.uhn.fhir.i18n.Msg; 023import ca.uhn.fhir.model.api.IModelJson; 024import ca.uhn.fhir.validation.ResultSeverityEnum; 025import com.fasterxml.jackson.annotation.JsonCreator; 026import com.fasterxml.jackson.annotation.JsonProperty; 027import org.apache.commons.collections4.CollectionUtils; 028 029import java.security.InvalidParameterException; 030import java.util.Collection; 031import java.util.regex.Pattern; 032 033import static org.apache.commons.lang3.StringUtils.isBlank; 034import static org.apache.commons.lang3.StringUtils.isNotBlank; 035 036/** 037 * Specification of ValidationResult matching parameters and severity to be applied in case of match 038 */ 039public class ValidationPostProcessingRuleJson implements IModelJson { 040 041 @JsonCreator 042 public ValidationPostProcessingRuleJson( 043 @JsonProperty("msgId") String theMsgId, 044 @JsonProperty("msgIdRegex") String themsgIdRegex, 045 @JsonProperty(value = "oldSeverities") Collection<ResultSeverityEnum> theOldSeverities, 046 @JsonProperty("messageFragments") Collection<String> theExtraMessageFragments, 047 @JsonProperty(value = "newSeverity") ResultSeverityEnum theNewSeverity) { 048 049 validateParams(theMsgId, themsgIdRegex, theNewSeverity); 050 051 myMsgId = theMsgId; 052 if (isNotBlank(themsgIdRegex)) { 053 myMsgIdRegexPattern = Pattern.compile(themsgIdRegex, Pattern.CASE_INSENSITIVE); 054 } 055 myOldSeverities = CollectionUtils.emptyIfNull(theOldSeverities); 056 myNewSeverity = theNewSeverity; 057 myMessageFragments = CollectionUtils.emptyIfNull(theExtraMessageFragments); 058 } 059 060 private void validateParams(String theMsgId, String themsgIdRegex, ResultSeverityEnum theNewSeverity) { 061 062 if (isBlank(theMsgId) && isBlank(themsgIdRegex)) { 063 throw new InvalidParameterException(Msg.code(2705) + "One of 'msgId' and 'msgIdRegex' must be present"); 064 } 065 066 if (isNotBlank(theMsgId) && isNotBlank(themsgIdRegex)) { 067 throw new InvalidParameterException( 068 Msg.code(2706) + "Only one of 'msgId' and 'msgIdRegex' must be present"); 069 } 070 071 if (theNewSeverity == null) { 072 throw new InvalidParameterException(Msg.code(2707) + "Parameter 'newSeverity' must be present"); 073 } 074 } 075 076 @JsonProperty("msgId") 077 private String myMsgId; 078 079 private Pattern myMsgIdRegexPattern; 080 081 @JsonProperty(value = "oldSeverities", required = true) 082 private Collection<ResultSeverityEnum> myOldSeverities; 083 084 @JsonProperty("messageFragments") 085 private Collection<String> myMessageFragments; 086 087 @JsonProperty(value = "newSeverity", required = true) 088 private ResultSeverityEnum myNewSeverity; 089 090 public String getMsgId() { 091 return myMsgId; 092 } 093 094 public ResultSeverityEnum getNewSeverity() { 095 return myNewSeverity; 096 } 097 098 public Collection<ResultSeverityEnum> getOldSeverities() { 099 return myOldSeverities; 100 } 101 102 public Collection<String> getMessageFragments() { 103 return myMessageFragments; 104 } 105 106 public Pattern getMsgIdRegexPattern() { 107 return myMsgIdRegexPattern; 108 } 109}