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