001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2024 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.binder;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.model.api.IQueryParameterOr;
025import ca.uhn.fhir.model.api.IQueryParameterType;
026import ca.uhn.fhir.rest.api.QualifiedParamList;
027import ca.uhn.fhir.rest.param.ParameterUtil;
028import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
029import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
030import org.apache.commons.lang3.StringUtils;
031
032import java.util.Collections;
033import java.util.List;
034
035public final class QueryParameterTypeBinder extends BaseBinder<IQueryParameterType>
036                implements IParamBinder<IQueryParameterType> {
037
038        public QueryParameterTypeBinder(
039                        Class<? extends IQueryParameterType> theType,
040                        List<Class<? extends IQueryParameterType>> theCompositeTypes) {
041                super(theType, theCompositeTypes);
042        }
043
044        @SuppressWarnings("unchecked")
045        @Override
046        public List<IQueryParameterOr<?>> encode(FhirContext theContext, IQueryParameterType theValue)
047                        throws InternalErrorException {
048                IQueryParameterType param = theValue;
049                List<?> retVal = Collections.singletonList(ParameterUtil.singleton(param, null));
050                return (List<IQueryParameterOr<?>>) retVal;
051        }
052
053        @Override
054        public IQueryParameterType parse(FhirContext theContext, String theParamName, List<QualifiedParamList> theParams)
055                        throws InternalErrorException, InvalidRequestException {
056                String value = theParams.get(0).get(0);
057                if (StringUtils.isBlank(value)) {
058                        return null;
059                }
060
061                IQueryParameterType dt = super.newInstance();
062
063                if (theParams.size() == 0 || theParams.get(0).size() == 0) {
064                        return dt;
065                }
066                if (theParams.size() > 1 || theParams.get(0).size() > 1) {
067                        throw new InvalidRequestException(
068                                        Msg.code(1962) + "Multiple values detected for non-repeatable parameter '" + theParamName
069                                                        + "'. This server is not configured to allow multiple (AND/OR) values for this param.");
070                }
071
072                dt.setValueAsQueryToken(theContext, theParamName, theParams.get(0).getQualifier(), value);
073                return dt;
074        }
075}