View Javadoc
1   package ca.uhn.fhir.rest.param;
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  import static org.apache.commons.lang3.StringUtils.isBlank;
23  
24  import java.util.List;
25  
26  import org.apache.commons.lang3.Validate;
27  
28  import ca.uhn.fhir.context.ConfigurationException;
29  import ca.uhn.fhir.context.FhirContext;
30  import ca.uhn.fhir.model.api.IQueryParameterType;
31  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
32  
33  public class CompositeParam<A extends IQueryParameterType, B extends IQueryParameterType> extends BaseParam implements IQueryParameterType {
34  
35  	private A myLeftType;
36  	private B myRightType;
37  
38  	public CompositeParam(A theLeftInstance, B theRightInstance) {
39  		myLeftType = theLeftInstance;
40  		myRightType = theRightInstance;
41  	}
42  
43  	public CompositeParam(Class<A> theLeftType, Class<B> theRightType) {
44  		Validate.notNull(theLeftType);
45  		Validate.notNull(theRightType);
46  		try {
47  			myLeftType = theLeftType.newInstance();
48  		} catch (InstantiationException e) {
49  			throw new ConfigurationException("Failed to instantiate type: " + myLeftType, e);
50  		} catch (IllegalAccessException e) {
51  			throw new ConfigurationException("Failed to instantiate type: " + myLeftType, e);
52  		}
53  		try {
54  			myRightType = theRightType.newInstance();
55  		} catch (InstantiationException e) {
56  			throw new ConfigurationException("Failed to instantiate type: " + myRightType, e);
57  		} catch (IllegalAccessException e) {
58  			throw new ConfigurationException("Failed to instantiate type: " + myRightType, e);
59  		}
60  	}
61  
62  	@Override
63  	String doGetQueryParameterQualifier() {
64  		return null;
65  	}
66  
67  	@Override
68  	String doGetValueAsQueryToken(FhirContext theContext) {
69  		StringBuilder b = new StringBuilder();
70  		if (myLeftType != null) {
71  			b.append(myLeftType.getValueAsQueryToken(theContext));
72  		}
73  		b.append('$');
74  		if (myRightType != null) {
75  			b.append(myRightType.getValueAsQueryToken(theContext));
76  		}
77  		return b.toString();
78  	}
79  
80  	@Override
81  	void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
82  		if (isBlank(theValue)) {
83  			myLeftType.setValueAsQueryToken(theContext, theParamName, theQualifier, "");
84  			myRightType.setValueAsQueryToken(theContext, theParamName, theQualifier, "");
85  		} else {
86  			List<String> parts = ParameterUtil.splitParameterString(theValue, '$', false);
87  			if (parts.size() > 2) {
88  				throw new InvalidRequestException("Invalid value for composite parameter (only one '$' is valid for this parameter, others must be escaped). Value was: " + theValue);
89  			}
90  			myLeftType.setValueAsQueryToken(theContext, theParamName, theQualifier, parts.get(0));
91  			if (parts.size() > 1) {
92  				myRightType.setValueAsQueryToken(theContext, theParamName, theQualifier, parts.get(1));
93  			}
94  		}
95  	}
96  
97  	/**
98  	 * @return Returns the left value for this parameter (the first of two parameters in this composite)
99  	 */
100 	public A getLeftValue() {
101 		return myLeftType;
102 	}
103 
104 	/**
105 	 * @return Returns the right value for this parameter (the second of two parameters in this composite)
106 	 */
107 	public B getRightValue() {
108 		return myRightType;
109 	}
110 
111 }