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 ca.uhn.fhir.context.FhirContext;
27  import ca.uhn.fhir.model.api.IQueryParameterOr;
28  import ca.uhn.fhir.model.api.IQueryParameterType;
29  import ca.uhn.fhir.rest.api.QualifiedParamList;
30  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
31  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
32  
33  public final class QueryParameterOrBinder extends BaseBinder<IQueryParameterOr<?>> implements IParamBinder<IQueryParameterOr<?>> {
34  
35  	public QueryParameterOrBinder(Class<? extends IQueryParameterOr<?>> theType, List<Class<? extends IQueryParameterType>> theCompositeTypes) {
36  		super(theType, theCompositeTypes);
37  	}
38  
39  	@SuppressWarnings("unchecked")
40  	@Override
41  	public List<IQueryParameterOr<?>> encode(FhirContext theContext, IQueryParameterOr<?> theValue) throws InternalErrorException {
42  		IQueryParameterOr<?> retVal = (theValue);
43  		List<?> retVal2 = Collections.singletonList((IQueryParameterOr<?>)retVal);
44  		return (List<IQueryParameterOr<?>>) retVal2;
45  	}
46  
47  	@Override
48  	public IQueryParameterOr<?> parse(FhirContext theContext, String theParamName, List<QualifiedParamList> theString) throws InternalErrorException, InvalidRequestException {
49  		IQueryParameterOr<?> dt;
50  		try {
51  			dt = newInstance();
52  			if (theString.size() == 0 || theString.get(0).size() == 0) {
53  				return dt;
54  			}
55  			if (theString.size() > 1) {
56  				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.");
57  			}
58  			
59  			dt.setValuesAsQueryTokens(theContext, theParamName, theString.get(0));
60  		} catch (SecurityException e) {
61  			throw new InternalErrorException(e);
62  		}
63  		return dt;
64  	}
65  }