001package ca.uhn.fhir.rest.param; 002 003import static org.apache.commons.lang3.StringUtils.isNotBlank; 004 005import ca.uhn.fhir.context.FhirContext; 006 007/* 008 * #%L 009 * HAPI FHIR - Core Library 010 * %% 011 * Copyright (C) 2014 - 2021 Smile CDR, Inc. 012 * %% 013 * Licensed under the Apache License, Version 2.0 (the "License"); 014 * you may not use this file except in compliance with the License. 015 * You may obtain a copy of the License at 016 * 017 * http://www.apache.org/licenses/LICENSE-2.0 018 * 019 * Unless required by applicable law or agreed to in writing, software 020 * distributed under the License is distributed on an "AS IS" BASIS, 021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 022 * See the License for the specific language governing permissions and 023 * limitations under the License. 024 * #L% 025 */ 026 027import ca.uhn.fhir.model.api.IQueryParameterType; 028import ca.uhn.fhir.rest.api.Constants; 029import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 030 031/** 032 * Base class for RESTful operation parameter types 033 */ 034public abstract class BaseParam implements IQueryParameterType { 035 036 private Boolean myMissing; 037 038 abstract String doGetQueryParameterQualifier(); 039 040 abstract String doGetValueAsQueryToken(FhirContext theContext); 041 042 abstract void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue); 043 044 /** 045 * If set to non-null value, indicates that this parameter has been populated with a "[name]:missing=true" or "[name]:missing=false" vale instead of a normal value 046 */ 047 @Override 048 public Boolean getMissing() { 049 return myMissing; 050 } 051 052 @Override 053 public final String getQueryParameterQualifier() { 054 if (myMissing != null && myMissing.booleanValue()) { 055 return Constants.PARAMQUALIFIER_MISSING; 056 } 057 return doGetQueryParameterQualifier(); 058 } 059 060 @Override 061 public final String getValueAsQueryToken(FhirContext theContext) { 062 if (myMissing != null) { 063 return myMissing ? Constants.PARAMQUALIFIER_MISSING_TRUE : Constants.PARAMQUALIFIER_MISSING_FALSE; 064 } 065 return doGetValueAsQueryToken(theContext); 066 } 067 068 /** 069 * Does this parameter type support chained parameters (only reference should return <code>true</code> for this) 070 */ 071 protected boolean isSupportsChain() { 072 return false; 073 } 074 075 /** 076 * If set to non-null value, indicates that this parameter has been populated 077 * with a "[name]:missing=true" or "[name]:missing=false" vale instead of a 078 * normal value 079 * 080 * @return Returns a reference to <code>this</code> for easier method chaining 081 */ 082 @Override 083 public BaseParam setMissing(Boolean theMissing) { 084 myMissing = theMissing; 085 return this; 086 } 087 088 @Override 089 public final void setValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) { 090 if (Constants.PARAMQUALIFIER_MISSING.equals(theQualifier)) { 091 myMissing = "true".equals(theValue); 092 doSetValueAsQueryToken(theContext, theParamName, null, null); 093 } else { 094 if (isNotBlank(theQualifier) && theQualifier.charAt(0) == '.') { 095 if (!isSupportsChain()) { 096 String msg = theContext.getLocalizer().getMessage(BaseParam.class, "chainNotSupported", theParamName + theQualifier, theQualifier); 097 throw new InvalidRequestException(msg); 098 } 099 } 100 101 myMissing = null; 102 doSetValueAsQueryToken(theContext, theParamName, theQualifier, theValue); 103 } 104 } 105 106}