001/*
002 * #%L
003 * HAPI FHIR - Core Library
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.param;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.model.api.IQueryParameterType;
024import ca.uhn.fhir.model.primitive.StringDt;
025import ca.uhn.fhir.model.primitive.UriDt;
026import org.apache.commons.lang3.builder.ToStringBuilder;
027import org.apache.commons.lang3.builder.ToStringStyle;
028
029import static org.apache.commons.lang3.StringUtils.defaultString;
030
031public class UriParam extends BaseParam implements IQueryParameterType {
032
033        private UriParamQualifierEnum myQualifier;
034        private String myValue;
035
036        /**
037         * Constructor
038         */
039        public UriParam() {
040                super();
041        }
042
043        public UriParam(String theValue) {
044                setValue(theValue);
045        }
046
047        @Override
048        String doGetQueryParameterQualifier() {
049                return myQualifier != null ? myQualifier.getValue() : null;
050        }
051
052        @Override
053        String doGetValueAsQueryToken() {
054                return ParameterUtil.escape(myValue);
055        }
056
057        @Override
058        void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
059                myQualifier = UriParamQualifierEnum.forValue(theQualifier);
060                myValue = ParameterUtil.unescape(theValue);
061        }
062
063        /**
064         * Gets the qualifier for this param (may be <code>null</code> and generally will be)
065         */
066        public UriParamQualifierEnum getQualifier() {
067                return myQualifier;
068        }
069
070        public String getValue() {
071                return myValue;
072        }
073
074        public StringDt getValueAsStringDt() {
075                return new StringDt(myValue);
076        }
077
078        public UriDt getValueAsUriDt() {
079                return new UriDt(myValue);
080        }
081
082        public String getValueNotNull() {
083                return defaultString(myValue);
084        }
085
086        /**
087         * Sets the qualifier for this param (may be <code>null</code> and generally will be)
088         *
089         * @return Returns a reference to <code>this</code> for easy method chanining
090         */
091        public UriParam setQualifier(UriParamQualifierEnum theQualifier) {
092                myQualifier = theQualifier;
093                return this;
094        }
095
096        /**
097         * Sets the value for this param
098         *
099         * @return Returns a reference to <code>this</code> for easy method chanining
100         */
101        public UriParam setValue(String theValue) {
102                myValue = theValue;
103                return this;
104        }
105
106        @Override
107        public String toString() {
108                ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
109                builder.append("value", getValue());
110                return builder.toString();
111        }
112}