View Javadoc
1   package ca.uhn.fhir.rest.param.binder;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.commons.lang3.StringUtils;
27  
28  import ca.uhn.fhir.context.FhirContext;
29  import ca.uhn.fhir.model.api.IQueryParameterOr;
30  import ca.uhn.fhir.model.api.IQueryParameterType;
31  import ca.uhn.fhir.rest.api.QualifiedParamList;
32  import ca.uhn.fhir.rest.param.ParameterUtil;
33  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
34  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
35  
36  public final class QueryParameterTypeBinder extends BaseBinder<IQueryParameterType> implements IParamBinder<IQueryParameterType> {
37  
38  	public QueryParameterTypeBinder(Class<? extends IQueryParameterType> theType, List<Class<? extends IQueryParameterType>> theCompositeTypes) {
39  		super(theType, theCompositeTypes);
40  	}
41  
42  	@SuppressWarnings("unchecked")
43  	@Override
44  	public List<IQueryParameterOr<?>> encode(FhirContext theContext, IQueryParameterType theValue) throws InternalErrorException {
45  		IQueryParameterType param = theValue;
46  		List<?> retVal = Collections.singletonList(ParameterUtil.singleton(param, null));
47  		return (List<IQueryParameterOr<?>>) retVal;
48  	}
49  
50  	@Override
51  	public IQueryParameterType parse(FhirContext theContext, String theParamName, List<QualifiedParamList> theParams) throws InternalErrorException, InvalidRequestException {
52  		String value = theParams.get(0).get(0);
53  		if (StringUtils.isBlank(value)) {
54  			return null;
55  		}
56  		
57  		IQueryParameterType dt = super.newInstance();
58  
59  		if (theParams.size() == 0 || theParams.get(0).size() == 0) {
60  			return dt;
61  		}
62  		if (theParams.size() > 1 || theParams.get(0).size() > 1) {
63  			throw new InvalidRequestException("Multiple values detected for non-repeatable parameter '" + theParamName + "'. This server is not configured to allow multiple (AND/OR) values for this param.");
64  		}
65  
66  		dt.setValueAsQueryToken(theContext, theParamName, theParams.get(0).getQualifier(), value);
67  		return dt;
68  	}
69  
70  }