001package ca.uhn.fhir.rest.client.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Client Framework
006 * %%
007 * Copyright (C) 2014 - 2020 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 */
022
023import ca.uhn.fhir.context.ConfigurationException;
024import ca.uhn.fhir.context.FhirContext;
025import ca.uhn.fhir.model.api.annotation.Description;
026import ca.uhn.fhir.model.valueset.BundleTypeEnum;
027import ca.uhn.fhir.rest.annotation.Search;
028import ca.uhn.fhir.rest.api.Constants;
029import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
030import ca.uhn.fhir.rest.api.SearchStyleEnum;
031import ca.uhn.fhir.rest.client.api.UrlSourceEnum;
032import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
033import ca.uhn.fhir.rest.param.ParameterUtil;
034import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
035import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
036import org.apache.commons.lang3.StringUtils;
037import org.hl7.fhir.instance.model.api.IBaseResource;
038import org.hl7.fhir.instance.model.api.IIdType;
039
040import java.lang.reflect.Method;
041import java.util.Collections;
042import java.util.LinkedHashMap;
043import java.util.List;
044import java.util.Map;
045import java.util.Map.Entry;
046
047import static org.apache.commons.lang3.StringUtils.isBlank;
048import static org.apache.commons.lang3.StringUtils.isNotBlank;
049
050public class SearchMethodBinding extends BaseResourceReturningMethodBinding {
051        private String myCompartmentName;
052        private String myDescription;
053        private Integer myIdParamIndex;
054        private String myQueryName;
055
056        public SearchMethodBinding(Class<? extends IBaseResource> theReturnResourceType, Method theMethod, FhirContext theContext, Object theProvider) {
057                super(theReturnResourceType, theMethod, theContext, theProvider);
058                Search search = theMethod.getAnnotation(Search.class);
059                this.myQueryName = StringUtils.defaultIfBlank(search.queryName(), null);
060                this.myCompartmentName = StringUtils.defaultIfBlank(search.compartmentName(), null);
061                this.myIdParamIndex = ParameterUtil.findIdParameterIndex(theMethod, getContext());
062
063                Description desc = theMethod.getAnnotation(Description.class);
064                if (desc != null) {
065                        if (isNotBlank(desc.formalDefinition())) {
066                                myDescription = StringUtils.defaultIfBlank(desc.formalDefinition(), null);
067                        } else {
068                                myDescription = StringUtils.defaultIfBlank(desc.shortDefinition(), null);
069                        }
070                }
071
072                /*
073                 * Check for parameter combinations and names that are invalid
074                 */
075                List<IParameter> parameters = getParameters();
076                // List<SearchParameter> searchParameters = new ArrayList<SearchParameter>();
077                for (int i = 0; i < parameters.size(); i++) {
078                        IParameter next = parameters.get(i);
079                        if (!(next instanceof SearchParameter)) {
080                                continue;
081                        }
082
083                        SearchParameter sp = (SearchParameter) next;
084                        if (sp.getName().startsWith("_")) {
085                                if (ALLOWED_PARAMS.contains(sp.getName())) {
086                                        String msg = getContext().getLocalizer().getMessage(getClass().getName() + ".invalidSpecialParamName", theMethod.getName(), theMethod.getDeclaringClass().getSimpleName(),
087                                                        sp.getName());
088                                        throw new ConfigurationException(msg);
089                                }
090                        }
091
092                        // searchParameters.add(sp);
093                }
094                // for (int i = 0; i < searchParameters.size(); i++) {
095                // SearchParameter next = searchParameters.get(i);
096                // // next.
097                // }
098
099                /*
100                 * Only compartment searching methods may have an ID parameter
101                 */
102                if (isBlank(myCompartmentName) && myIdParamIndex != null) {
103                        String msg = theContext.getLocalizer().getMessage(getClass().getName() + ".idWithoutCompartment", theMethod.getName(), theMethod.getDeclaringClass());
104                        throw new ConfigurationException(msg);
105                }
106
107        }
108
109        public String getDescription() {
110                return myDescription;
111        }
112
113        @Override
114        protected BundleTypeEnum getResponseBundleType() {
115                return BundleTypeEnum.SEARCHSET;
116        }
117
118        @Override
119        public RestOperationTypeEnum getRestOperationType() {
120                return RestOperationTypeEnum.SEARCH_TYPE;
121        }
122
123        @Override
124        public ReturnTypeEnum getReturnType() {
125                        return ReturnTypeEnum.BUNDLE;
126        }
127
128
129        @Override
130        public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException {
131                assert (myQueryName == null || ((theArgs != null ? theArgs.length : 0) == getParameters().size())) : "Wrong number of arguments: " + (theArgs != null ? theArgs.length : "null");
132
133                Map<String, List<String>> queryStringArgs = new LinkedHashMap<String, List<String>>();
134
135                if (myQueryName != null) {
136                        queryStringArgs.put(Constants.PARAM_QUERY, Collections.singletonList(myQueryName));
137                }
138
139                IIdType id = (IIdType) (myIdParamIndex != null ? theArgs[myIdParamIndex] : null);
140
141                String resourceName = getResourceName();
142                if (theArgs != null) {
143                        for (int idx = 0; idx < theArgs.length; idx++) {
144                                IParameter nextParam = getParameters().get(idx);
145                                nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], queryStringArgs, null);
146                        }
147                }
148
149                BaseHttpClientInvocation retVal = createSearchInvocation(getContext(), resourceName, queryStringArgs, id, myCompartmentName, null);
150
151                return retVal;
152        }
153
154
155        @Override
156        protected boolean isAddContentLocationHeader() {
157                return false;
158        }
159
160
161        @Override
162        public String toString() {
163                return getMethod().toString();
164        }
165
166        public static BaseHttpClientInvocation createSearchInvocation(FhirContext theContext, String theSearchUrl, UrlSourceEnum theUrlSource, Map<String, List<String>> theParams) {
167                return new HttpGetClientInvocation(theContext, theParams, theUrlSource, theSearchUrl);
168        }
169
170
171        public static BaseHttpClientInvocation createSearchInvocation(FhirContext theContext, String theResourceName, Map<String, List<String>> theParameters, IIdType theId, String theCompartmentName,
172                        SearchStyleEnum theSearchStyle) {
173                SearchStyleEnum searchStyle = theSearchStyle;
174                if (searchStyle == null) {
175                        int length = 0;
176                        for (Entry<String, List<String>> nextEntry : theParameters.entrySet()) {
177                                length += nextEntry.getKey().length();
178                                for (String next : nextEntry.getValue()) {
179                                        length += next.length();
180                                }
181                        }
182
183                        if (length < 5000) {
184                                searchStyle = SearchStyleEnum.GET;
185                        } else {
186                                searchStyle = SearchStyleEnum.POST;
187                        }
188                }
189
190                BaseHttpClientInvocation invocation;
191
192                boolean compartmentSearch = false;
193                if (theCompartmentName != null) {
194                        if (theId == null || !theId.hasIdPart()) {
195                                String msg = theContext.getLocalizer().getMessage(SearchMethodBinding.class.getName() + ".idNullForCompartmentSearch");
196                                throw new InvalidRequestException(msg);
197                        }
198                        compartmentSearch = true;
199                }
200
201                /*
202                 * Are we doing a get (GET [base]/Patient?name=foo) or a get with search (GET [base]/Patient/_search?name=foo) or a post (POST [base]/Patient with parameters in the POST body)
203                 */
204                switch (searchStyle) {
205                case GET:
206                default:
207                        if (compartmentSearch) {
208                                invocation = new HttpGetClientInvocation(theContext, theParameters, theResourceName, theId.getIdPart(), theCompartmentName);
209                        } else {
210                                invocation = new HttpGetClientInvocation(theContext, theParameters, theResourceName);
211                        }
212                        break;
213                case GET_WITH_SEARCH:
214                        if (compartmentSearch) {
215                                invocation = new HttpGetClientInvocation(theContext, theParameters, theResourceName, theId.getIdPart(), theCompartmentName, Constants.PARAM_SEARCH);
216                        } else {
217                                invocation = new HttpGetClientInvocation(theContext, theParameters, theResourceName, Constants.PARAM_SEARCH);
218                        }
219                        break;
220                case POST:
221                        if (compartmentSearch) {
222                                invocation = new HttpPostClientInvocation(theContext, theParameters, theResourceName, theId.getIdPart(), theCompartmentName, Constants.PARAM_SEARCH);
223                        } else {
224                                invocation = new HttpPostClientInvocation(theContext, theParameters, theResourceName, Constants.PARAM_SEARCH);
225                        }
226                }
227
228                return invocation;
229        }
230
231}