001package ca.uhn.fhir.rest.param;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2023 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.context.FhirContext;
024import ca.uhn.fhir.i18n.Msg;
025import ca.uhn.fhir.model.api.IQueryParameterType;
026import ca.uhn.fhir.rest.api.Constants;
027import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
028
029import static org.apache.commons.lang3.StringUtils.isNotBlank;
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) {
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" value 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.code(1935) + msg);
098                                }
099                        }
100
101                        myMissing = null;
102                        doSetValueAsQueryToken(theContext, theParamName, theQualifier, theValue);
103                }
104        }
105
106}