001package ca.uhn.fhir.rest.param; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2019 University Health Network 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.isBlank; 023 024import java.util.List; 025 026import org.apache.commons.lang3.Validate; 027 028import ca.uhn.fhir.context.ConfigurationException; 029import ca.uhn.fhir.context.FhirContext; 030import ca.uhn.fhir.model.api.IQueryParameterType; 031import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 032 033public class CompositeParam<A extends IQueryParameterType, B extends IQueryParameterType> extends BaseParam implements IQueryParameterType { 034 035 private A myLeftType; 036 private B myRightType; 037 038 public CompositeParam(A theLeftInstance, B theRightInstance) { 039 myLeftType = theLeftInstance; 040 myRightType = theRightInstance; 041 } 042 043 public CompositeParam(Class<A> theLeftType, Class<B> theRightType) { 044 Validate.notNull(theLeftType); 045 Validate.notNull(theRightType); 046 try { 047 myLeftType = theLeftType.newInstance(); 048 } catch (InstantiationException e) { 049 throw new ConfigurationException("Failed to instantiate type: " + myLeftType, e); 050 } catch (IllegalAccessException e) { 051 throw new ConfigurationException("Failed to instantiate type: " + myLeftType, e); 052 } 053 try { 054 myRightType = theRightType.newInstance(); 055 } catch (InstantiationException e) { 056 throw new ConfigurationException("Failed to instantiate type: " + myRightType, e); 057 } catch (IllegalAccessException e) { 058 throw new ConfigurationException("Failed to instantiate type: " + myRightType, e); 059 } 060 } 061 062 @Override 063 String doGetQueryParameterQualifier() { 064 return null; 065 } 066 067 @Override 068 String doGetValueAsQueryToken(FhirContext theContext) { 069 StringBuilder b = new StringBuilder(); 070 if (myLeftType != null) { 071 b.append(myLeftType.getValueAsQueryToken(theContext)); 072 } 073 b.append('$'); 074 if (myRightType != null) { 075 b.append(myRightType.getValueAsQueryToken(theContext)); 076 } 077 return b.toString(); 078 } 079 080 @Override 081 void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) { 082 if (isBlank(theValue)) { 083 myLeftType.setValueAsQueryToken(theContext, theParamName, theQualifier, ""); 084 myRightType.setValueAsQueryToken(theContext, theParamName, theQualifier, ""); 085 } else { 086 List<String> parts = ParameterUtil.splitParameterString(theValue, '$', false); 087 if (parts.size() > 2) { 088 throw new InvalidRequestException("Invalid value for composite parameter (only one '$' is valid for this parameter, others must be escaped). Value was: " + theValue); 089 } 090 myLeftType.setValueAsQueryToken(theContext, theParamName, theQualifier, parts.get(0)); 091 if (parts.size() > 1) { 092 myRightType.setValueAsQueryToken(theContext, theParamName, theQualifier, parts.get(1)); 093 } 094 } 095 } 096 097 /** 098 * @return Returns the left value for this parameter (the first of two parameters in this composite) 099 */ 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}